aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorAlberto Gonzalez <boqwxp@airmail.cc>2020-04-24 08:37:16 +0000
committerAlberto Gonzalez <boqwxp@airmail.cc>2020-05-14 20:06:55 +0000
commit976edb7597692ac04111b3c51b13e18105c14f42 (patch)
tree0652fb1f029da498790aefca271d59b444234e00 /kernel
parent35b94d1f664928dcf8476a9a6f35e2bb7f647ee1 (diff)
downloadyosys-976edb7597692ac04111b3c51b13e18105c14f42.tar.gz
yosys-976edb7597692ac04111b3c51b13e18105c14f42.tar.bz2
yosys-976edb7597692ac04111b3c51b13e18105c14f42.zip
kernel: Ensure `dict` always hashes to the same value given the same contents.
Diffstat (limited to 'kernel')
-rw-r--r--kernel/hashlib.h9
1 files changed, 6 insertions, 3 deletions
diff --git a/kernel/hashlib.h b/kernel/hashlib.h
index 18114b6ad..1284f3f8d 100644
--- a/kernel/hashlib.h
+++ b/kernel/hashlib.h
@@ -207,6 +207,7 @@ class dict
entry_t() { }
entry_t(const std::pair<K, T> &udata, int next) : udata(udata), next(next) { }
entry_t(std::pair<K, T> &&udata, int next) : udata(std::move(udata)), next(next) { }
+ bool operator<(const entry_t &other) const { return udata.first < other.udata.first; }
};
std::vector<int> hashtable;
@@ -616,10 +617,12 @@ public:
}
unsigned int hash() const {
+ std::vector<entry_t> entries_(entries); //make a copy to preserve const-ness
+ std::sort(entries_.begin(), entries_.end());
unsigned int h = mkhash_init;
- for (auto &it : entries) {
- h = mkhash(h, hash_ops<K>::hash(it.udata.first));
- h = mkhash(h, hash_ops<T>::hash(it.udata.second));
+ for (unsigned int i = 0; i < entries_.size(); ++i) {
+ h = mkhash(h, hash_ops<K>::hash(entries_[i].udata.first));
+ h = mkhash(h, hash_ops<T>::hash(entries_[i].udata.second));
}
return h;
}