15 #include "../core/math_func.hpp" 17 template <
class Titem_>
20 typedef typename Titem_::Key Key;
33 inline const Titem_ *
Find(
const Key &key)
const 35 for (
const Titem_ *pItem = m_pFirst; pItem != NULL; pItem = pItem->GetHashNext()) {
36 if (pItem->GetKey() == key) {
45 inline Titem_ *
Find(
const Key &key)
47 for (Titem_ *pItem = m_pFirst; pItem != NULL; pItem = pItem->GetHashNext()) {
48 if (pItem->GetKey() == key) {
59 assert(new_item.GetHashNext() == NULL);
60 new_item.SetHashNext(m_pFirst);
65 inline bool Detach(Titem_ &item_to_remove)
67 if (m_pFirst == &item_to_remove) {
68 m_pFirst = item_to_remove.GetHashNext();
69 item_to_remove.SetHashNext(NULL);
72 Titem_ *pItem = m_pFirst;
77 Titem_ *pNextItem = pItem->GetHashNext();
78 if (pNextItem == &item_to_remove)
break;
81 pItem->SetHashNext(item_to_remove.GetHashNext());
82 item_to_remove.SetHashNext(NULL);
87 inline Titem_ *
Detach(
const Key &key)
90 if (m_pFirst == NULL) {
94 if (m_pFirst->GetKey() == key) {
95 Titem_ &ret_item = *m_pFirst;
96 m_pFirst = m_pFirst->GetHashNext();
97 ret_item.SetHashNext(NULL);
101 Titem_ *pPrev = m_pFirst;
102 for (Titem_ *pItem = m_pFirst->GetHashNext(); pItem != NULL; pPrev = pItem, pItem = pItem->GetHashNext()) {
103 if (pItem->GetKey() == key) {
105 pPrev->SetHashNext(pItem->GetHashNext());
106 pItem->SetHashNext(NULL);
136 template <
class Titem_,
int Thash_bits_>
139 typedef Titem_ Titem;
140 typedef typename Titem_::Key Tkey;
141 static const int Thash_bits = Thash_bits_;
142 static const int Tcapacity = 1 << Thash_bits;
151 Slot m_slots[Tcapacity];
164 int32 hash = key.CalcHash();
165 if ((8 * Thash_bits) < 32) hash ^= hash >> (
min(8 * Thash_bits, 31));
166 if ((4 * Thash_bits) < 32) hash ^= hash >> (
min(4 * Thash_bits, 31));
167 if ((2 * Thash_bits) < 32) hash ^= hash >> (
min(2 * Thash_bits, 31));
168 if ((1 * Thash_bits) < 32) hash ^= hash >> (
min(1 * Thash_bits, 31));
169 hash &= (1 << Thash_bits) - 1;
176 return CalcHash(item.GetKey());
189 for (
int i = 0; i < Tcapacity; i++) m_slots[i].
Clear();
193 const Titem_ *
Find(
const Tkey &key)
const 195 int hash = CalcHash(key);
196 const Slot &slot = m_slots[hash];
197 const Titem_ *item = slot.
Find(key);
204 int hash = CalcHash(key);
205 Slot &slot = m_slots[hash];
206 Titem_ *item = slot.
Find(key);
213 int hash = CalcHash(key);
214 Slot &slot = m_slots[hash];
215 Titem_ *item = slot.
Detach(key);
223 Titem_&
Pop(
const Tkey &key)
225 Titem_ *item = TryPop(key);
226 assert(item != NULL);
233 const Tkey &key = item.GetKey();
234 int hash = CalcHash(key);
235 Slot &slot = m_slots[hash];
236 bool ret = slot.
Detach(item);
246 bool ret = TryPop(item);
253 int hash = CalcHash(new_item);
254 Slot &slot = m_slots[hash];
255 assert(slot.
Find(new_item.GetKey()) == NULL);
static int CalcHash(const Tkey &key)
static helper - return hash for the given key modulo number of slots
class CHashTableT<Titem, Thash_bits> - simple hash table of pointers allocated elsewhere.
void Push(Titem_ &new_item)
add one item - copy it from the given item
CHashTableSlotT< Titem_ > Slot
each slot contains pointer to the first item in the list, Titem contains pointer to the next item - G...
Titem_ & Pop(const Tkey &key)
non-const item search & removal
const Titem_ * Find(const Key &key) const
hash table slot helper - linear search for item with given key through the given blob - const version...
bool TryPop(Titem_ &item)
non-const item search & optional removal (if found)
Titem_ * Find(const Key &key)
hash table slot helper - linear search for item with given key through the given blob - non-const ver...
const Titem_ * Find(const Tkey &key) const
const item search
bool Detach(Titem_ &item_to_remove)
hash table slot helper - remove item from a slot
void Clear()
simple clear - forget all items - used by CSegmentCostCacheT.Flush()
Titem_ * Detach(const Key &key)
hash table slot helper - remove and return item from a slot
static T min(const T a, const T b)
Returns the minimum of two values.
void Pop(Titem_ &item)
non-const item search & removal
Titem_ * TryPop(const Tkey &key)
non-const item search & optional removal (if found)
int Count() const
item count
void Clear()
hash table slot helper - clears the slot by simple forgetting its items
void Attach(Titem_ &new_item)
hash table slot helper - add new item to the slot
static int CalcHash(const Titem_ &item)
static helper - return hash for the given item modulo number of slots
Titem_ * Find(const Tkey &key)
non-const item search