OpenBarnyard
 
Loading...
Searching...
No Matches
THashTable Class Reference

#include <THashTable.h>

Classes

struct  Bucket
 

Public Types

using t_ItemCompareFunc = TBOOL ( * )( const void* a_pMem1, const void* a_pMem2, TSIZE a_iSize )
 
using t_ItemHashFunc = TUINT32 ( * )( const void* a_pMem, TSIZE a_iSize, TUINT32 a_uiMaxValue )
 

Public Member Functions

 THashTable ()
 
 ~THashTable ()
 
TINTGetHashToBucketIds () const
 
TINT GetItemCount () const
 
void * GetItemArray () const
 
void DeleteBucketMemory ()
 
void Destroy ()
 
BucketFind (void *a_pData)
 
BucketInsert (void *a_pData)
 
BucketAppend (void *a_pData)
 
TBOOL Create (TINT a_iItemCountTotal, TINT a_iItemSize, TINT a_iBucketSize, TINT a_iHashNodeCount)
 
void SetItemCompareFunction (t_ItemCompareFunc a_HashCompareFunc)
 
void SetItemHashFunction (t_ItemHashFunc a_HashCompareFunc)
 

Static Public Attributes

static constexpr TINT INVALID_BUCKET_ID = -1
 

Detailed Description

Definition at line 5 of file THashTable.h.

Member Typedef Documentation

◆ t_ItemCompareFunc

using THashTable::t_ItemCompareFunc = TBOOL ( * )( const void* a_pMem1, const void* a_pMem2, TSIZE a_iSize )

Definition at line 16 of file THashTable.h.

◆ t_ItemHashFunc

using THashTable::t_ItemHashFunc = TUINT32 ( * )( const void* a_pMem, TSIZE a_iSize, TUINT32 a_uiMaxValue )

Definition at line 17 of file THashTable.h.

Constructor & Destructor Documentation

◆ THashTable()

THashTable::THashTable ( )

Definition at line 33 of file THashTable.cpp.

34{
35 m_iBucketSize = 0;
36 m_iItemSize = 0;
37 m_iItemCount = 0;
38 m_iItemCountTotal = 0;
39 m_iHashNodeCount = 0;
40 m_iHashNodeCountTotal = 0;
41 m_pHashToBucketId = TNULL;
42 m_pBuckets = TNULL;
43 m_pItems = TNULL;
44 m_ItemHashFunc = TNULL;
45 m_ItemCompareFunc = TNULL;
46}
#define TNULL
Definition Typedefs.h:23

◆ ~THashTable()

THashTable::~THashTable ( )

Definition at line 48 of file THashTable.cpp.

49{
50 Destroy();
51}
void Destroy()

Member Function Documentation

◆ Append()

THashTable::Bucket * THashTable::Append ( void * a_pData)

Definition at line 105 of file THashTable.cpp.

106{
107 TASSERT( 0 != m_iItemSize );
108
109 if ( m_pHashToBucketId != TNULL )
110 {
111 TASSERT( !( ( m_iHashNodeCount == m_iHashNodeCountTotal ) || ( m_iItemCount == m_iItemCountTotal ) ) );
112
113 if ( !( ( m_iHashNodeCount == m_iHashNodeCountTotal ) || ( m_iItemCount == m_iItemCountTotal ) ) )
114 {
115 TUINT32 uiHash = m_ItemHashFunc( a_pData, m_iItemSize, m_iBucketSize );
116
117 m_pBuckets[ m_iHashNodeCountTotal ].iNextBucketId = m_pHashToBucketId[ uiHash ];
118 m_pBuckets[ m_iHashNodeCountTotal ].iItemIndex = m_iItemCount;
119
120 TUtil::MemCopy( m_pItems + m_iItemCount * m_iItemSize, a_pData, m_iItemSize );
121
122 m_pHashToBucketId[ uiHash ] = m_iHashNodeCountTotal;
123
124 m_iItemCount++;
125 m_iHashNodeCountTotal++;
126
127 return &m_pBuckets[ m_iHashNodeCountTotal ];
128 }
129 }
130
131 return TNULL;
132}
#define TASSERT(X,...)
Definition Defines.h:138
uint32_t TUINT32
Definition Typedefs.h:13
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90

◆ Create()

TBOOL THashTable::Create ( TINT a_iItemCountTotal,
TINT a_iItemSize,
TINT a_iBucketSize,
TINT a_iHashNodeCount )

Definition at line 134 of file THashTable.cpp.

135{
136 if ( m_pHashToBucketId == TNULL )
137 {
138 if ( a_iHashNodeCount < a_iItemCountTotal )
139 {
140 a_iHashNodeCount = a_iItemCountTotal;
141 }
142
143 m_iBucketSize = a_iBucketSize;
144 m_iItemSize = a_iItemSize;
145 m_iItemCount = 0;
146 m_iItemCountTotal = a_iItemCountTotal;
147 m_iHashNodeCount = a_iHashNodeCount;
148 m_pHashToBucketId = new TINT[ m_iBucketSize ];
149 m_pBuckets = new Bucket[ m_iHashNodeCount ];
150 m_pItems = TSTATICCAST( TBYTE, TMemalign( m_iItemSize * m_iItemCountTotal, 16 ) );
151
152 SetItemHashFunction( DefaultItemHashFunc );
153 SetItemCompareFunction( DefaultItemCompareFunc );
154
155 for ( TINT i = 0; i < m_iBucketSize; i++ )
156 {
157 m_pHashToBucketId[ i ] = INVALID_BUCKET_ID;
158 }
159
160 return TTRUE;
161 }
162
163 return TFALSE;
164}
void * TMemalign(TSIZE a_uiAlignment, TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock)
Allocates aligned memory from a specific memory block.
Definition TMemory.cpp:1020
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
uint8_t TBYTE
Definition Typedefs.h:19
void SetItemHashFunction(t_ItemHashFunc a_HashCompareFunc)
Definition THashTable.h:36
void SetItemCompareFunction(t_ItemCompareFunc a_HashCompareFunc)
Definition THashTable.h:35
static constexpr TINT INVALID_BUCKET_ID
Definition THashTable.h:14

◆ DeleteBucketMemory()

void THashTable::DeleteBucketMemory ( )

Definition at line 53 of file THashTable.cpp.

54{
55 delete[] m_pHashToBucketId;
56 m_pHashToBucketId = TNULL;
57}

◆ Destroy()

void THashTable::Destroy ( )

Definition at line 59 of file THashTable.cpp.

60{
62
63 delete[] m_pBuckets;
64 m_pBuckets = TNULL;
65
66 TFree( m_pItems );
67 m_pItems = TNULL;
68}
void TFree(void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:1054
void DeleteBucketMemory()

◆ Find()

THashTable::Bucket * THashTable::Find ( void * a_pData)

Definition at line 81 of file THashTable.cpp.

82{
83 TASSERT( 0 != m_iItemSize );
84 TINT* pBuckets = GetHashToBucketIds();
85
86 if ( pBuckets != TNULL )
87 {
88 TUINT32 uiHash = m_ItemHashFunc( a_pData, m_iItemSize, m_iBucketSize );
89 TINT iBucket = pBuckets[ uiHash ];
90
91 while ( iBucket != INVALID_BUCKET_ID )
92 {
93 Bucket& item = m_pBuckets[ iBucket ];
94
95 if ( m_ItemCompareFunc( a_pData, m_pItems + m_iItemSize * item.iItemIndex, m_iItemSize ) )
96 return &item;
97
98 iBucket = item.iNextBucketId;
99 }
100 }
101
102 return TNULL;
103}
TINT * GetHashToBucketIds() const
Definition THashTable.h:23

◆ GetHashToBucketIds()

TINT * THashTable::GetHashToBucketIds ( ) const
inline

Definition at line 23 of file THashTable.h.

23{ return m_pHashToBucketId; }

◆ GetItemArray()

void * THashTable::GetItemArray ( ) const
inline

Definition at line 25 of file THashTable.h.

25{ return m_pItems; }

◆ GetItemCount()

TINT THashTable::GetItemCount ( ) const
inline

Definition at line 24 of file THashTable.h.

24{ return m_iItemCount; }

◆ Insert()

THashTable::Bucket * THashTable::Insert ( void * a_pData)

Definition at line 70 of file THashTable.cpp.

71{
72 TASSERT( 0 != m_iItemSize );
73
74 if ( TNULL == GetHashToBucketIds() )
75 return TNULL;
76
77 THashTable::Bucket* pFoundBucket = Find( a_pData );
78 return ( pFoundBucket ) ? pFoundBucket : Append( a_pData );
79}
Bucket * Find(void *a_pData)
Bucket * Append(void *a_pData)

◆ SetItemCompareFunction()

void THashTable::SetItemCompareFunction ( t_ItemCompareFunc a_HashCompareFunc)
inline

Definition at line 35 of file THashTable.h.

35{ m_ItemCompareFunc = a_HashCompareFunc; }

◆ SetItemHashFunction()

void THashTable::SetItemHashFunction ( t_ItemHashFunc a_HashCompareFunc)
inline

Definition at line 36 of file THashTable.h.

36{ m_ItemHashFunc = a_HashCompareFunc; }

Member Data Documentation

◆ INVALID_BUCKET_ID

TINT THashTable::INVALID_BUCKET_ID = -1
inlinestaticconstexpr

Definition at line 14 of file THashTable.h.


The documentation for this class was generated from the following files: