16 typedef typename TItem::Key Key;
18 TItem *first_item =
nullptr;
23 this->first_item =
nullptr;
27 inline const TItem *
Find(
const Key &key)
const
29 for (
const TItem *item = this->first_item; item !=
nullptr; item = item->GetHashNext()) {
30 if (item->GetKey() == key) {
39 inline TItem *
Find(
const Key &key)
41 for (TItem *item = this->first_item; item !=
nullptr; item = item->GetHashNext()) {
42 if (item->GetKey() == key) {
53 assert(new_item.GetHashNext() ==
nullptr);
54 new_item.SetHashNext(this->first_item);
55 this->first_item = &new_item;
59 inline bool Detach(TItem &item_to_remove)
61 if (this->first_item == &item_to_remove) {
62 this->first_item = item_to_remove.GetHashNext();
63 item_to_remove.SetHashNext(
nullptr);
66 TItem *item = this->first_item;
68 if (item ==
nullptr) {
71 TItem *next_item = item->GetHashNext();
72 if (next_item == &item_to_remove)
break;
75 item->SetHashNext(item_to_remove.GetHashNext());
76 item_to_remove.SetHashNext(
nullptr);
84 if (this->first_item ==
nullptr) {
88 if (this->first_item->GetKey() == key) {
89 TItem &ret_item = *this->first_item;
90 this->first_item = this->first_item->GetHashNext();
91 ret_item.SetHashNext(
nullptr);
95 TItem *previous_item = this->first_item;
96 for (TItem *item = this->first_item->GetHashNext(); item !=
nullptr; previous_item = item, item = item->GetHashNext()) {
97 if (item->GetKey() == key) {
99 previous_item->SetHashNext(item->GetHashNext());
100 item->SetHashNext(
nullptr);
130template <
class Titem,
int Thash_bits_>
133 typedef typename Titem::Key Tkey;
134 static constexpr int HASH_BITS = Thash_bits_;
135 static constexpr int CAPACITY = 1 << HASH_BITS;
144 Slot slots[CAPACITY];
145 int number_of_items = 0;
150 uint32_t hash = key.CalcHash();
151 hash -= (hash >> 17);
153 hash &= (1 << HASH_BITS) - 1;
167 return this->number_of_items;
173 for (
int i = 0; i < CAPACITY; i++) this->slots[i].
Clear();
174 this->number_of_items = 0;
178 const Titem *
Find(
const Tkey &key)
const
181 const Slot &slot = this->slots[hash];
182 const Titem *item = slot.
Find(key);
190 Slot &slot = this->slots[hash];
191 Titem *item = slot.
Find(key);
199 Slot &slot = this->slots[hash];
200 Titem *item = slot.
Detach(key);
201 if (item !=
nullptr) {
202 this->number_of_items--;
208 Titem &
Pop(
const Tkey &key)
210 Titem *item =
TryPop(key);
211 assert(item !=
nullptr);
218 const Tkey &key = item.GetKey();
220 Slot &slot = this->slots[hash];
221 bool ret = slot.
Detach(item);
223 this->number_of_items--;
231 [[maybe_unused]]
bool ret =
TryPop(item);
239 Slot &slot = this->slots[hash];
240 assert(slot.
Find(new_item.GetKey()) ==
nullptr);
242 this->number_of_items++;
class HashTable<Titem, HASH_BITS> - simple hash table of pointers allocated elsewhere.
Titem * TryPop(const Tkey &key)
non-const item search & optional removal (if found)
void Push(Titem &new_item)
add one item - copy it from the given item
const Titem * Find(const Tkey &key) const
const item search
Titem * Find(const Tkey &key)
non-const item search
int Count() const
item count
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Titem & Pop(const Tkey &key)
non-const item search & removal
static int CalcHash(const Titem &item)
static helper - return hash for the given item modulo number of slots
HashTableSlot< Titem > Slot
each slot contains pointer to the first item in the list, Titem contains pointer to the next item - G...
bool TryPop(Titem &item)
non-const item search & optional removal (if found)
static int CalcHash(const Tkey &key)
static helper - return hash for the given key modulo number of slots
void Pop(Titem &item)
non-const item search & removal
TItem * Find(const Key &key)
hash table slot helper - linear search for item with given key through the given blob - non-const ver...
bool Detach(TItem &item_to_remove)
hash table slot helper - remove item from a slot
const TItem * Find(const Key &key) const
hash table slot helper - linear search for item with given key through the given blob - const version
void Attach(TItem &new_item)
hash table slot helper - add new item to the slot
TItem * Detach(const Key &key)
hash table slot helper - remove and return item from a slot
void Clear()
hash table slot helper - clears the slot by simple forgetting its items