diff options
author | Clifford Wolf <clifford@clifford.at> | 2015-01-18 12:12:33 +0100 |
---|---|---|
committer | Clifford Wolf <clifford@clifford.at> | 2015-01-18 12:12:33 +0100 |
commit | 0217ea0fb82b9170bb6efce734f1965ff2b181e7 (patch) | |
tree | fdc498f2d8c025569f27c9b466b86a775890eea8 /kernel | |
parent | 61192514e3e22e196e1df5aaaf98bb2ed6fd57df (diff) | |
download | yosys-0217ea0fb82b9170bb6efce734f1965ff2b181e7.tar.gz yosys-0217ea0fb82b9170bb6efce734f1965ff2b181e7.tar.bz2 yosys-0217ea0fb82b9170bb6efce734f1965ff2b181e7.zip |
Added hashlib::idict<>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/hashlib.h | 60 | ||||
-rw-r--r-- | kernel/yosys.h | 1 |
2 files changed, 59 insertions, 2 deletions
diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 72e9bc2ef..52b38a064 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -163,7 +163,11 @@ inline int hashtable_size(int min_size) throw std::length_error("hash table exceeded maximum size."); } -template<typename K, typename T, typename OPS = hash_ops<K>> +template<typename K, typename T, typename OPS = hash_ops<K>> class dict; +template<typename K, int offset = 0, typename OPS = hash_ops<K>> class idict; +template<typename K, typename OPS = hash_ops<K>> class pool; + +template<typename K, typename T, typename OPS> class dict { struct entry_t @@ -485,9 +489,12 @@ public: const_iterator end() const { return const_iterator(nullptr, -1); } }; -template<typename K, typename OPS = hash_ops<K>> +template<typename K, typename OPS> class pool { + template<typename, int, typename> friend class idict; + +protected: struct entry_t { K udata; @@ -783,6 +790,55 @@ public: const_iterator end() const { return const_iterator(nullptr, -1); } }; +template<typename K, int offset, typename OPS> +class idict +{ + pool<K, OPS> database; + +public: + typedef typename pool<K, OPS>::const_iterator const_iterator; + + int operator()(const K &key) + { + int hash = database.do_hash(key); + int i = database.do_lookup(key, hash); + if (i < 0) + i = database.do_insert(key, hash); + return i + offset; + } + + int at(const K &key) const + { + int hash = database.do_hash(key); + int i = database.do_lookup(key, hash); + if (i < 0) + throw std::out_of_range("idict::at()"); + return i + offset; + } + + int count(const K &key) const + { + int hash = database.do_hash(key); + int i = database.do_lookup(key, hash); + return i < 0 ? 0 : 1; + } + + void expect(const K &key, int i) + { + int j = (*this)(key); + if (i != j) + throw std::out_of_range("idict::expect()"); + } + + const K &operator[](int index) const + { + return database.entries.at(index - offset).udata; + } + + const_iterator begin() const { return database.begin(); } + const_iterator end() const { return database.end(); } +}; + } /* namespace hashlib */ #endif diff --git a/kernel/yosys.h b/kernel/yosys.h index d1e01b19c..a6f4cb665 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -148,6 +148,7 @@ using hashlib::hash_cstr_ops; using hashlib::hash_ptr_ops; using hashlib::hash_obj_ops; using hashlib::dict; +using hashlib::idict; using hashlib::pool; namespace RTLIL { |