From 0217ea0fb82b9170bb6efce734f1965ff2b181e7 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 18 Jan 2015 12:12:33 +0100 Subject: [PATCH] Added hashlib::idict<> --- CodingReadme | 14 +++++++++++ kernel/hashlib.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++-- kernel/yosys.h | 1 + 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/CodingReadme b/CodingReadme index 92d54d283..78bc5a3ce 100644 --- a/CodingReadme +++ b/CodingReadme @@ -72,6 +72,20 @@ replacement for std::unordered_set. The main characteristics are: - dict and pool will have the same order of iteration across all compilers, standard libraries and architectures. +In addition to dict and pool there is also an idict that +creates a bijective map from K to the integers. For example: + + idict si; + log("%d\n", si("hello")); // will print 42 + log("%d\n", si("world")); // will print 43 + log("%d\n", si.at("world")); // will print 43 + log("%d\n", si.at("dummy")); // will throw exception + log("%s\n", si[42].c_str())); // will print hello + log("%s\n", si[43].c_str())); // will print world + log("%s\n", si[44].c_str())); // will throw exception + +It is not possible to remove elements from an idict. + 2. Standard STL data types In Yosys we use std::vector and std::string whenever applicable. When 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> +template> class dict; +template> class idict; +template> class pool; + +template class dict { struct entry_t @@ -485,9 +489,12 @@ public: const_iterator end() const { return const_iterator(nullptr, -1); } }; -template> +template class pool { + template friend class idict; + +protected: struct entry_t { K udata; @@ -783,6 +790,55 @@ public: const_iterator end() const { return const_iterator(nullptr, -1); } }; +template +class idict +{ + pool database; + +public: + typedef typename pool::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 { -- 2.30.2