forked from HeliumProject/Foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Name.inl
327 lines (287 loc) · 11.1 KB
/
Name.inl
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
/// Constructor.
///
/// When using the default constructor, the internal name entry pointer is left in an uninitialized state and must
/// be manually initialized using Set() or some other assignment before being used.
template< typename TableType >
Helium::NameBase< TableType >::NameBase()
{
}
/// Constructor.
///
/// The name entry string will be initialized to null.
template< typename TableType >
Helium::NameBase< TableType >::NameBase( ENullName )
: m_pEntry( NULL )
{
}
/// Constructor.
///
/// @param[in] pString String to which the contents of this name should be initialized.
template< typename TableType >
Helium::NameBase< TableType >::NameBase( const CharType* pString )
{
Set( pString );
}
/// Constructor.
///
/// @param[in] rString String to which the contents of this name should be initialized.
template< typename TableType >
Helium::NameBase< TableType >::NameBase( const StringBase< CharType >& rString )
{
Set( rString );
}
/// Get the string contents for this name.
///
/// @return Null-terminated name string. Even if this entry is empty, this will never return a null pointer.
template< typename TableType >
const typename Helium::NameBase< TableType >::CharType* Helium::NameBase< TableType >::Get() const
{
return( m_pEntry ? m_pEntry : TableType::sm_emptyString );
}
/// Get the string contents for this name.
///
/// @return Null-terminated name string if set to a name other than NULL_NAME, null pointer if the name is set to
/// NULL_NAME.
template< typename TableType >
const typename Helium::NameBase< TableType >::CharType* Helium::NameBase< TableType >::GetDirect() const
{
return m_pEntry;
}
/// Set this name.
///
/// @param[in] pString String to which this name should be set (can be null).
///
/// @see Get()
template< typename TableType >
void Helium::NameBase< TableType >::Set( const CharType* pString )
{
// Check for empty strings first.
if( !pString || pString[ 0 ] == TXT( '\0' ) )
{
m_pEntry = NULL;
return;
}
// Lazily initialize the hash table. Note that this is not inherently thread-safe, but there should always be
// at least one name created before any sub-threads are spawned.
if( !TableType::sm_pNameMemoryHeap )
{
TableType::sm_pNameMemoryHeap = new StackMemoryHeap<>( STACK_HEAP_BLOCK_SIZE );
HELIUM_ASSERT( TableType::sm_pNameMemoryHeap );
HELIUM_ASSERT( !TableType::sm_pTable );
TableType::sm_pTable = new TableBucket [ TABLE_BUCKET_COUNT ];
HELIUM_ASSERT( TableType::sm_pTable );
}
HELIUM_ASSERT( TableType::sm_pTable );
// Compute the string's hash table index and retrieve the corresponding bucket.
uint32_t bucketIndex = StringHash( pString ) % TABLE_BUCKET_COUNT;
TableBucket& rBucket = TableType::sm_pTable[ bucketIndex ];
// Locate the string in the table. If it does not exist, add it.
size_t entryCount = 0;
m_pEntry = rBucket.Find( pString, entryCount );
if( !m_pEntry )
{
m_pEntry = rBucket.Add( pString, entryCount );
HELIUM_ASSERT( m_pEntry );
}
}
/// Set this name.
///
/// @param[in] rString String to which this name should be set.
///
/// @see Get()
template< typename TableType >
void Helium::NameBase< TableType >::Set( const StringBase< CharType >& rString )
{
Set( rString.GetData() );
}
/// Get whether this name is empty (null).
///
/// @return True if this name is empty, false if not.
template< typename TableType >
bool Helium::NameBase< TableType >::IsEmpty() const
{
return( m_pEntry == NULL );
}
/// Clear out the contents of this name (set it to null).
template< typename TableType >
void Helium::NameBase< TableType >::Clear()
{
m_pEntry = NULL;
}
/// Get the string contents for this name.
///
/// @return Null-terminated name string. Even if this entry is empty, this will never return a null pointer.
template< typename TableType >
const typename Helium::NameBase< TableType >::CharType* Helium::NameBase< TableType >::operator*() const
{
return Get();
}
/// Less-than comparison operator.
///
/// Note that only the name table entry pointers are compared. The result of the comparison does not imply any
/// alphabetical sorting, nor can it be ensured to remain consistent across multiple executions of an application.
///
/// @param[in] rName Name with which to compare.
///
/// @return True if the name table entry for this name precedes that of the given name, false if not.
template< typename TableType >
bool Helium::NameBase< TableType >::operator<( const NameBase& rName ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself in our comparisons.
return ( m_pEntry < rName.m_pEntry );
}
/// Greater-than comparison operator.
///
/// Note that only the name table entry pointers are compared. The result of the comparison does not imply any
/// alphabetical sorting, nor can it be ensured to remain consistent across multiple executions of an application.
///
/// @param[in] rName Name with which to compare.
///
/// @return True if the name table entry for this name succeeds that of the given name, false if not.
template< typename TableType >
bool Helium::NameBase< TableType >::operator>( const NameBase& rName ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself in our comparisons.
return ( m_pEntry > rName.m_pEntry );
}
/// Less-or-equal comparison operator.
///
/// Note that only the name table entry pointers are compared. The result of the comparison does not imply any
/// alphabetical sorting, nor can it be ensured to remain consistent across multiple executions of an application.
///
/// @param[in] rName Name with which to compare.
///
/// @return True if the name table entry for this name precedes or is the same as that of the given name, false if not.
template< typename TableType >
bool Helium::NameBase< TableType >::operator<=( const NameBase& rName ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself in our comparisons.
return ( m_pEntry <= rName.m_pEntry );
}
/// Greater-or-equal comparison operator.
///
/// Note that only the name table entry pointers are compared. The result of the comparison does not imply any
/// alphabetical sorting, nor can it be ensured to remain consistent across multiple executions of an application.
///
/// @param[in] rName Name with which to compare.
///
/// @return True if the name table entry for this name succeeds or is the same as that of the given name, false if not.
template< typename TableType >
bool Helium::NameBase< TableType >::operator>=( const NameBase& rName ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself in our comparisons.
return ( m_pEntry >= rName.m_pEntry );
}
/// Equality comparison operator.
///
/// @param[in] rName Name with which to compare.
///
/// @return True if this name and the given name match, false if not.
template< typename TableType >
bool Helium::NameBase< TableType >::operator==( const NameBase& rName ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself in our comparisons.
return ( m_pEntry == rName.m_pEntry );
}
/// Inequality comparison operator.
///
/// @param[in] rName Name with which to compare.
///
/// @return True if this name and the given name do not match, false if they do match.
template< typename TableType >
bool Helium::NameBase< TableType >::operator!=( const NameBase& rName ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself in our comparisons.
return ( m_pEntry != rName.m_pEntry );
}
/// Release the name table and free all allocated memory.
///
/// This should only be called immediately prior to application exit.
template< typename TableType >
void Helium::NameBase< TableType >::Shutdown()
{
HELIUM_TRACE( TraceLevels::Info, TXT( "Shutting down Name table.\n" ) );
delete [] TableType::sm_pTable;
TableType::sm_pTable = NULL;
delete TableType::sm_pNameMemoryHeap;
TableType::sm_pNameMemoryHeap = NULL;
HELIUM_TRACE( TraceLevels::Info, TXT( "Name table shutdown complete.\n" ) );
}
/// Find an existing string in this table.
///
/// @param[in] pString String to find.
/// @param[out] rEntryCount Number of entries in this bucket when the search was performed.
///
/// @return Entry string if found, null if not found.
///
/// @see Add()
template< typename TableType >
const typename Helium::NameBase< TableType >::CharType* Helium::NameBase< TableType >::TableBucket::Find(
const CharType* pString,
size_t& rEntryCount )
{
HELIUM_ASSERT( pString );
ScopeReadLock readLock( m_lock );
const CharType** ppEntries = m_entries.GetData();
size_t entryCount = m_entries.GetSize();
HELIUM_ASSERT( ppEntries || entryCount == 0 );
rEntryCount = entryCount;
for( size_t entryIndex = 0; entryIndex < entryCount; ++entryIndex )
{
const CharType* pEntry = ppEntries[ entryIndex ];
if( CompareString( pEntry, pString ) == 0 )
{
return pEntry;
}
}
return NULL;
}
/// Add a string to this table if it does not already exist.
///
/// @param[in] pString String to locate or add.
/// @param[in] previousEntryCount Number of entries already checked during a previous Find() call (existing entries
/// are not expected to change). In other words, the entry index from which to start
/// checking for any additional string entries that may have been added since the
/// previous Find() call.
///
/// @return Pointer to the string table entry for the string.
///
/// @see Find()
template< typename TableType >
const typename Helium::NameBase< TableType >::CharType* Helium::NameBase< TableType >::TableBucket::Add(
const CharType* pString,
size_t previousEntryCount )
{
HELIUM_ASSERT( pString );
ScopeWriteLock writeLock( m_lock );
const CharType** ppEntries = m_entries.GetData();
size_t entryCount = m_entries.GetSize();
HELIUM_ASSERT( ppEntries || entryCount == 0 );
HELIUM_ASSERT( previousEntryCount <= entryCount );
for( size_t entryIndex = previousEntryCount; entryIndex < entryCount; ++entryIndex )
{
const CharType* pEntry = ppEntries[ entryIndex ];
if( CompareString( pEntry, pString ) == 0 )
{
return pEntry;
}
}
size_t newEntryAllocSize = sizeof( CharType ) * ( StringLength( pString ) + 1 );
HELIUM_ASSERT( TableType::sm_pNameMemoryHeap );
CharType* pEntry = static_cast< CharType* >( TableType::sm_pNameMemoryHeap->Allocate( newEntryAllocSize ) );
HELIUM_ASSERT( pEntry );
MemoryCopy( pEntry, pString, newEntryAllocSize );
m_entries.Push( pEntry );
return pEntry;
}
/// Default Name hash.
///
/// @param[in] rKey Key for which to compute a hash value.
///
/// @return Hash value.
template< typename TableType >
size_t Helium::Hash< Helium::NameBase< TableType > >::operator()( const NameBase< TableType >& rKey ) const
{
// Each name entry has a unique name pointer, so we can use the pointer itself as our hash.
return static_cast< size_t >( reinterpret_cast< uintptr_t >( rKey.GetDirect() ) );
}