Codechange: Renamed CHashTableT to HashTable and corrected code style

This commit is contained in:
Koen Bussemaker 2024-10-21 11:05:25 +02:00 committed by Kuhnovic
parent 0e13a7d124
commit 0200bc3720
3 changed files with 76 additions and 88 deletions

View File

@ -12,97 +12,95 @@
#include "../core/math_func.hpp"
template <class Titem_>
struct CHashTableSlotT
template <class TItem>
struct HashTableSlot
{
typedef typename Titem_::Key Key; // make Titem_::Key a property of HashTable
typedef typename TItem::Key Key; // make Titem::Key a property of HashTable
Titem_ *m_pFirst;
inline CHashTableSlotT() : m_pFirst(nullptr) {}
TItem *first_item = nullptr;
/** hash table slot helper - clears the slot by simple forgetting its items */
inline void Clear()
{
m_pFirst = nullptr;
this->first_item = nullptr;
}
/** hash table slot helper - linear search for item with given key through the given blob - const version */
inline const Titem_ *Find(const Key &key) const
inline const TItem *Find(const Key &key) const
{
for (const Titem_ *pItem = m_pFirst; pItem != nullptr; pItem = pItem->GetHashNext()) {
if (pItem->GetKey() == key) {
for (const TItem *item = this->first_item; item != nullptr; item = item->GetHashNext()) {
if (item->GetKey() == key) {
/* we have found the item, return it */
return pItem;
return item;
}
}
return nullptr;
}
/** hash table slot helper - linear search for item with given key through the given blob - non-const version */
inline Titem_ *Find(const Key &key)
inline TItem *Find(const Key &key)
{
for (Titem_ *pItem = m_pFirst; pItem != nullptr; pItem = pItem->GetHashNext()) {
if (pItem->GetKey() == key) {
for (TItem *item = this->first_item; item != nullptr; item = item->GetHashNext()) {
if (item->GetKey() == key) {
/* we have found the item, return it */
return pItem;
return item;
}
}
return nullptr;
}
/** hash table slot helper - add new item to the slot */
inline void Attach(Titem_ &new_item)
inline void Attach(TItem &new_item)
{
assert(new_item.GetHashNext() == nullptr);
new_item.SetHashNext(m_pFirst);
m_pFirst = &new_item;
new_item.SetHashNext(this->first_item);
this->first_item = &new_item;
}
/** hash table slot helper - remove item from a slot */
inline bool Detach(Titem_ &item_to_remove)
inline bool Detach(TItem &item_to_remove)
{
if (m_pFirst == &item_to_remove) {
m_pFirst = item_to_remove.GetHashNext();
if (this->first_item == &item_to_remove) {
this->first_item = item_to_remove.GetHashNext();
item_to_remove.SetHashNext(nullptr);
return true;
}
Titem_ *pItem = m_pFirst;
TItem *item = this->first_item;
for (;;) {
if (pItem == nullptr) {
if (item == nullptr) {
return false;
}
Titem_ *pNextItem = pItem->GetHashNext();
if (pNextItem == &item_to_remove) break;
pItem = pNextItem;
TItem *next_item = item->GetHashNext();
if (next_item == &item_to_remove) break;
item = next_item;
}
pItem->SetHashNext(item_to_remove.GetHashNext());
item->SetHashNext(item_to_remove.GetHashNext());
item_to_remove.SetHashNext(nullptr);
return true;
}
/** hash table slot helper - remove and return item from a slot */
inline Titem_ *Detach(const Key &key)
inline TItem *Detach(const Key &key)
{
/* do we have any items? */
if (m_pFirst == nullptr) {
if (this->first_item == nullptr) {
return nullptr;
}
/* is it our first item? */
if (m_pFirst->GetKey() == key) {
Titem_ &ret_item = *m_pFirst;
m_pFirst = m_pFirst->GetHashNext();
if (this->first_item->GetKey() == key) {
TItem &ret_item = *this->first_item;
this->first_item = this->first_item->GetHashNext();
ret_item.SetHashNext(nullptr);
return &ret_item;
}
/* find it in the following items */
Titem_ *pPrev = m_pFirst;
for (Titem_ *pItem = m_pFirst->GetHashNext(); pItem != nullptr; pPrev = pItem, pItem = pItem->GetHashNext()) {
if (pItem->GetKey() == key) {
TItem *previous_item = this->first_item;
for (TItem *item = this->first_item->GetHashNext(); item != nullptr; previous_item = item, item = item->GetHashNext()) {
if (item->GetKey() == key) {
/* we have found the item, unlink and return it */
pPrev->SetHashNext(pItem->GetHashNext());
pItem->SetHashNext(nullptr);
return pItem;
previous_item->SetHashNext(item->GetHashNext());
item->SetHashNext(nullptr);
return item;
}
}
return nullptr;
@ -110,12 +108,12 @@ struct CHashTableSlotT
};
/**
* class CHashTableT<Titem, Thash_bits> - simple hash table
* class HashTable<Titem, HASH_BITS> - simple hash table
* of pointers allocated elsewhere.
*
* Supports: Add/Find/Remove of Titems.
*
* Your Titem must meet some extra requirements to be CHashTableT
* Your Titem must meet some extra requirements to be HashTable
* compliant:
* - its constructor/destructor (if any) must be public
* - if the copying of item requires an extra resource management,
@ -131,43 +129,35 @@ struct CHashTableSlotT
* - public 'equality' operator to compare the key with another one
* bool operator==(const Key &other) const;
*/
template <class Titem_, int Thash_bits_>
class CHashTableT {
template <class Titem, int Thash_bits_>
class HashTable {
public:
typedef Titem_ Titem; // make Titem_ visible from outside of class
typedef typename Titem_::Key Tkey; // make Titem_::Key a property of HashTable
static const int Thash_bits = Thash_bits_; // publish num of hash bits
static const int Tcapacity = 1 << Thash_bits; // and num of slots 2^bits
typedef typename Titem::Key Tkey; // make Titem::Key a property of HashTable
static constexpr int HASH_BITS = Thash_bits_; // publish num of hash bits
static constexpr int CAPACITY = 1 << HASH_BITS; // and num of slots 2^bits
protected:
/**
* each slot contains pointer to the first item in the list,
* Titem contains pointer to the next item - GetHashNext(), SetHashNext()
*/
typedef CHashTableSlotT<Titem_> Slot;
typedef HashTableSlot<Titem> Slot;
Slot m_slots[Tcapacity]; // here we store our data (array of blobs)
int m_num_items; // item counter
Slot slots[CAPACITY]; // here we store our data (array of blobs)
int number_of_items = 0; // item counter
public:
/* default constructor */
inline CHashTableT() : m_num_items(0)
{
}
protected:
/** static helper - return hash for the given key modulo number of slots */
inline static int CalcHash(const Tkey &key)
{
uint32_t hash = key.CalcHash();
hash -= (hash >> 17); // hash * 131071 / 131072
hash -= (hash >> 5); // * 31 / 32
hash &= (1 << Thash_bits) - 1; // modulo slots
hash &= (1 << HASH_BITS) - 1; // modulo slots
return hash;
}
/** static helper - return hash for the given item modulo number of slots */
inline static int CalcHash(const Titem_ &item)
inline static int CalcHash(const Titem &item)
{
return CalcHash(item.GetKey());
}
@ -176,82 +166,82 @@ public:
/** item count */
inline int Count() const
{
return m_num_items;
return this->number_of_items;
}
/** simple clear - forget all items - used by CSegmentCostCacheT.Flush() */
inline void Clear()
{
for (int i = 0; i < Tcapacity; i++) m_slots[i].Clear();
this->m_num_items = 0;
for (int i = 0; i < CAPACITY; i++) this->slots[i].Clear();
this->number_of_items = 0;
}
/** const item search */
const Titem_ *Find(const Tkey &key) const
const Titem *Find(const Tkey &key) const
{
int hash = CalcHash(key);
const Slot &slot = m_slots[hash];
const Titem_ *item = slot.Find(key);
const Slot &slot = this->slots[hash];
const Titem *item = slot.Find(key);
return item;
}
/** non-const item search */
Titem_ *Find(const Tkey &key)
Titem *Find(const Tkey &key)
{
int hash = CalcHash(key);
Slot &slot = m_slots[hash];
Titem_ *item = slot.Find(key);
Slot &slot = this->slots[hash];
Titem *item = slot.Find(key);
return item;
}
/** non-const item search & optional removal (if found) */
Titem_ *TryPop(const Tkey &key)
Titem *TryPop(const Tkey &key)
{
int hash = CalcHash(key);
Slot &slot = m_slots[hash];
Titem_ *item = slot.Detach(key);
Slot &slot = this->slots[hash];
Titem *item = slot.Detach(key);
if (item != nullptr) {
m_num_items--;
this->number_of_items--;
}
return item;
}
/** non-const item search & removal */
Titem_ &Pop(const Tkey &key)
Titem &Pop(const Tkey &key)
{
Titem_ *item = TryPop(key);
Titem *item = TryPop(key);
assert(item != nullptr);
return *item;
}
/** non-const item search & optional removal (if found) */
bool TryPop(Titem_ &item)
bool TryPop(Titem &item)
{
const Tkey &key = item.GetKey();
int hash = CalcHash(key);
Slot &slot = m_slots[hash];
Slot &slot = this->slots[hash];
bool ret = slot.Detach(item);
if (ret) {
m_num_items--;
this->number_of_items--;
}
return ret;
}
/** non-const item search & removal */
void Pop(Titem_ &item)
void Pop(Titem &item)
{
[[maybe_unused]] bool ret = TryPop(item);
assert(ret);
}
/** add one item - copy it from the given item */
void Push(Titem_ &new_item)
void Push(Titem &new_item)
{
int hash = CalcHash(new_item);
Slot &slot = m_slots[hash];
Slot &slot = this->slots[hash];
assert(slot.Find(new_item.GetKey()) == nullptr);
slot.Attach(new_item);
m_num_items++;
this->number_of_items++;
}
};

View File

@ -24,8 +24,8 @@ public:
typedef Titem_ Titem; ///< Make #Titem_ visible from outside of class.
typedef typename Titem_::Key Key; ///< Make Titem_::Key a property of this class.
using CItemArray = std::deque<Titem_>; ///< Type that we will use as item container.
typedef CHashTableT<Titem_, Thash_bits_open_ > COpenList; ///< How pointers to open nodes will be stored.
typedef CHashTableT<Titem_, Thash_bits_closed_> CClosedList; ///< How pointers to closed nodes will be stored.
typedef HashTable<Titem_, Thash_bits_open_ > COpenList; ///< How pointers to open nodes will be stored.
typedef HashTable<Titem_, Thash_bits_closed_> CClosedList; ///< How pointers to closed nodes will be stored.
typedef CBinaryHeapT<Titem_> CPriorityQueue; ///< How the priority queue will be managed.
protected:

View File

@ -67,14 +67,12 @@ struct CSegmentCostCacheBase
*/
template <class Tsegment>
struct CSegmentCostCacheT : public CSegmentCostCacheBase {
static const int C_HASH_BITS = 14;
static constexpr int HASH_BITS = 14;
typedef CHashTableT<Tsegment, C_HASH_BITS> HashTable;
using Heap = std::deque<Tsegment>;
typedef typename Tsegment::Key Key; ///< key to hash table
using Key = typename Tsegment::Key; ///< key to hash table
HashTable map;
Heap heap;
HashTable<Tsegment, HASH_BITS> map;
std::deque<Tsegment> heap;
inline CSegmentCostCacheT() {}