crlib: reduce hashmap memory usage.

build: fixed clang build.
This commit is contained in:
ds 2020-11-08 11:41:41 +03:00
commit d40aab2b40
3 changed files with 14 additions and 7 deletions

View file

@ -103,8 +103,15 @@ private:
return hash_ (key) % length;
}
void rehash () {
auto capacity = (capacity_ << 1);
void rehash (size_t targetCapacity) {
if (length_ + targetCapacity < capacity_) {
return;
}
auto capacity = capacity_ ? capacity_ : 3;
while (length_ + targetCapacity > capacity) {
capacity *= 2;
}
auto contents = cr::makeUnique <Entries> (capacity);
for (size_t i = 0; i < capacity_; ++i) {
@ -188,7 +195,7 @@ public:
for (size_t i = 0; i < capacity_; ++i) {
contents_[i].used = false;
}
rehash ();
rehash (0);
}
void foreach (Lambda <void (const K &, const V &)> callback) {
@ -201,8 +208,8 @@ public:
public:
V &operator [] (const K &key) {
if ((length_ << 1) > capacity_) {
rehash ();
if (length_ >= capacity_) {
rehash (length_ << 1);
}
auto result = put (key, contents_, capacity_);