forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutlhashdict.h
342 lines (289 loc) · 9.94 KB
/
utlhashdict.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#ifndef UTLHASHDICT_H
#define UTLHASHDICT_H
#if defined( _WIN32 )
#pragma once
#endif
#include "tier1/utlhash.h"
#include "tier1/generichash.h"
#include "mathlib/mathlib.h"
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive = true, bool bDupeStrings = true>
class CUtlHashDict
{
public:
// constructor, destructor
CUtlHashDict( int bucketCount = 16, int growCount = 0, int initCount = 0 );
~CUtlHashDict( );
// gets particular elements
T& Element( unsigned i );
const T& Element( unsigned i ) const;
T& operator[]( unsigned i );
const T& operator[]( unsigned i ) const;
// gets element names
char const *GetElementName( unsigned i ) const;
// Number of elements
int Count() const;
// Checks if a node is valid and in the tree
bool IsValidIndex( unsigned i ) const;
// Invalid index
static unsigned InvalidHandle();
// Insert method (inserts in order)
unsigned Insert( const char *pName, const T &element );
unsigned Insert( const char *pName );
// Find method
unsigned Find( const char *pName ) const;
// Remove methods
void RemoveAt( unsigned i );
void Remove( const char *pName );
void RemoveAll( );
// Purge memory
void Purge();
void PurgeAndDeleteElements(); // Call delete on each element.
// Iteration methods
unsigned First() const;
unsigned Next( unsigned i ) const;
protected:
struct Entry_t
{
const char *pszSymbol;
T value;
};
template <bool bCaseInsensitive>
class CCompare
{
public:
CCompare( int ignored ) {}
bool operator()( const Entry_t &entry1, const Entry_t &entry2 ) const
{
return !( ( bCaseInsensitive ) ? stricmp( entry1.pszSymbol, entry2.pszSymbol ) : strcmp( entry1.pszSymbol, entry2.pszSymbol ) );
}
};
template <bool bCaseInsensitive>
class CHash
{
public:
CHash( int ignored ) {}
unsigned operator()( const Entry_t &entry ) const
{
return !( ( bCaseInsensitive ) ? HashStringCaseless( entry.pszSymbol ) : HashString( entry.pszSymbol ) );
}
};
typedef CUtlHash<Entry_t, CCompare<bCaseInsensitive>, CHash<bCaseInsensitive> > CHashTable;
CHashTable m_Elements;
int m_nCount;
};
//-----------------------------------------------------------------------------
// constructor, destructor
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::CUtlHashDict( int bucketCount = 16, int growCount = 0, int initCount = 0 ) :
m_Elements( SmallestPowerOfTwoGreaterOrEqual(bucketCount), growCount, initCount )
{
Assert( SmallestPowerOfTwoGreaterOrEqual(bucketCount) <= 0xffff );
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::~CUtlHashDict()
{
Purge();
}
//-----------------------------------------------------------------------------
// gets particular elements
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline T& CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Element( unsigned i )
{
return m_Elements[i].value;
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline const T& CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Element( unsigned i ) const
{
return m_Elements[i].value;
}
//-----------------------------------------------------------------------------
// gets element names
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline char const *CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::GetElementName( unsigned i ) const
{
return m_Elements[i].pszSymbol;
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline T& CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::operator[]( unsigned i )
{
return m_Elements[i].value;
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline const T & CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::operator[]( unsigned i ) const
{
return m_Elements[i].value;
}
//-----------------------------------------------------------------------------
// Num elements
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline int CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Count() const
{
Assert( m_nCount == m_Elements.Count() );
return m_nCount;
}
//-----------------------------------------------------------------------------
// Checks if a node is valid and in the tree
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline bool CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::IsValidIndex( unsigned i ) const
{
return m_Elements.IsValidHandle(i);
}
//-----------------------------------------------------------------------------
// Invalid index
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
inline unsigned CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::InvalidHandle()
{
return CHashTable::InvalidHandle();
}
//-----------------------------------------------------------------------------
// Delete a node from the tree
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
void CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::RemoveAt(unsigned elem)
{
if ( bDupeStrings )
{
free( (void *)m_Elements[elem].pszSymbol );
}
m_Elements.Remove(elem);
m_nCount--;
}
//-----------------------------------------------------------------------------
// remove a node in the tree
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings> void CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Remove( const char *search )
{
unsigned node = Find( search );
if (node != InvalidHandle())
{
RemoveAt(node);
}
}
//-----------------------------------------------------------------------------
// Removes all nodes from the tree
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
void CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::RemoveAll()
{
if ( bDupeStrings )
{
typename UtlHashHandle_t index = m_Elements.GetFirstHandle();
while ( index != m_Elements.InvalidHandle() )
{
free( (void *)m_Elements[index].pszSymbol );
index = m_Elements.GetNextHandle( index );
}
}
m_Elements.RemoveAll();
m_nCount = 0;
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
void CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Purge()
{
if ( bDupeStrings )
{
typename UtlHashHandle_t index = m_Elements.GetFirstHandle();
while ( index != m_Elements.InvalidHandle() )
{
free( (void *)m_Elements[index].pszSymbol );
index = m_Elements.GetNextHandle( index );
}
}
m_Elements.Purge();
m_nCount = 0;
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
void CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::PurgeAndDeleteElements()
{
// Delete all the elements.
unsigned index = m_Elements.GetFirstHandle();
while ( index != m_Elements.InvalidHandle() )
{
if ( bDupeStrings )
{
free( (void *)m_Elements[index].pszSymbol );
}
delete m_Elements[index].value;
index = m_Elements.GetNextHandle( index );
}
m_Elements.RemoveAll();
m_nCount = 0;
}
//-----------------------------------------------------------------------------
// inserts a node into the tree
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
unsigned CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Insert( const char *pName, const T &element )
{
MEM_ALLOC_CREDIT_CLASS();
m_nCount++;
Entry_t entry =
{
(bDupeStrings) ? strdup( pName ) : pName,
element
};
bool bInserted;
unsigned result = m_Elements.Insert( entry, &bInserted );
if ( bDupeStrings && !bInserted )
{
free( (void *)entry.pszSymbol );
}
return result;
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
unsigned CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Insert( const char *pName )
{
MEM_ALLOC_CREDIT_CLASS();
m_nCount++;
Entry_t entry =
{
(bDupeStrings) ? strdup( pName ) : pName
};
bool bInserted;
unsigned result = m_Elements.Insert( entry, &bInserted );
if ( bDupeStrings && !bInserted )
{
free( (void *)entry.pszSymbol );
}
return result;
}
//-----------------------------------------------------------------------------
// finds a node in the tree
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
unsigned CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Find( const char *pName ) const
{
MEM_ALLOC_CREDIT_CLASS();
if ( pName )
return m_Elements.Find( *((Entry_t *)&pName) );
else
return InvalidHandle();
}
//-----------------------------------------------------------------------------
// Iteration methods
//-----------------------------------------------------------------------------
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
unsigned CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::First() const
{
return m_Elements.GetFirstHandle();
}
template <typename T, bool bCaseInsensitive, bool bDupeStrings>
unsigned CUtlHashDict<T, bCaseInsensitive, bDupeStrings>::Next( unsigned i ) const
{
return m_Elements.GetNextHandle(i);
}
#endif // UTLHASHDICT_H