Hashmap added Count()

This commit is contained in:
2026-03-02 04:02:46 +02:00
parent 07c9ed49c7
commit ffe6dd83ca

View File

@@ -30,6 +30,7 @@ public:
if (storage[keyHashPosition] == nullptr) { if (storage[keyHashPosition] == nullptr) {
storage[keyHashPosition] = new std::vector<hashStorage>; storage[keyHashPosition] = new std::vector<hashStorage>;
storage[keyHashPosition]->emplace_back(key, std::move(value), keyHash); storage[keyHashPosition]->emplace_back(key, std::move(value), keyHash);
++count;
return storage[keyHashPosition]->back().value; return storage[keyHashPosition]->back().value;
} }
@@ -39,6 +40,7 @@ public:
Has(key)); Has(key));
storage[keyHashPosition]->emplace_back(key, std::move(value), keyHash); storage[keyHashPosition]->emplace_back(key, std::move(value), keyHash);
++count;
return storage[keyHashPosition]->back().value; return storage[keyHashPosition]->back().value;
} }
@@ -54,6 +56,7 @@ public:
[keyHash, &key](const hashStorage &hash) { [keyHash, &key](const hashStorage &hash) {
return hash.hash == keyHash && hash.key == key; return hash.hash == keyHash && hash.key == key;
}); });
--count;
} }
bool Has(const Key &key) { bool Has(const Key &key) {
@@ -102,8 +105,11 @@ public:
entry->clear(); entry->clear();
delete entry; delete entry;
} }
count = 0;
} }
std::size_t Count() { return count; }
private: private:
struct hashStorage { struct hashStorage {
Key key; Key key;
@@ -113,6 +119,7 @@ private:
using bucket = std::vector<hashStorage>; using bucket = std::vector<hashStorage>;
std::vector<bucket *> storage; std::vector<bucket *> storage;
std::size_t count = 0;
}; };
} // namespace Tourmaline::Containers } // namespace Tourmaline::Containers
#endif #endif