-
Notifications
You must be signed in to change notification settings - Fork 5
/
ON_SimpleMap.h
284 lines (225 loc) · 5.66 KB
/
ON_SimpleMap.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
#pragma once
#ifndef LBPRHLIB
#error RHLIB header included in non-Rhino compile
#endif
template<class KEY, unsigned int HashTableSize>
__forceinline unsigned int OSM_HashIndex(const KEY& key)
{
return ON_CRC16(0, sizeof(KEY), &key) % HashTableSize;
}
template <class KEY, class VALUE, unsigned int HashTableSize = 256>
class ON_SimpleMap
{
private:
struct HashElement
{
HashElement* m_next;
KEY m_key;
VALUE m_value;
};
public:
ON_SimpleMap()
: m_block_list(NULL),
m_element_list(NULL),
m_iCount(0)
{
memset(m_table, 0, sizeof(m_table));
}
~ON_SimpleMap() { Destroy(); }
VALUE* Lookup(const KEY&) const;
bool Lookup(const KEY&, VALUE&) const;
void Destroy(void);
bool Remove(const KEY&); // **************** Do not call Remove() during iteration of the map.
bool SetAt(const KEY&, const VALUE&);
bool IsEmpty(void) const;
UINT_PTR Count(void) const;
class Iterator
{
public:
Iterator(HashElement* const * t) : table(t), index(0), element(NULL) {}
VALUE* Next(void);
bool Next(VALUE&);
bool Next(KEY&, VALUE&);
void Reset(void) { index = 0; element=NULL; }
protected:
HashElement* const * table;
HashElement* element;
INT_PTR index;
};
// **************** Do not call Remove() during iteration of the map.
Iterator GetIterator(void) const { return Iterator(m_table); }
private:
__forceinline int HashIndex(const KEY& key) const { return OSM_HashIndex<KEY, HashTableSize>(key); }
struct HashElementBlock
{
HashElementBlock* m_next;
HashElement m_memory_block[1024];
};
HashElement* m_table[HashTableSize];
HashElement* m_element_list; // unused elements
HashElementBlock* m_block_list; // unused blocks
UINT_PTR m_iCount;
VALUE GetElementObject(const HashElement*) const;
private:
// no implementation
ON_SimpleMap(const ON_SimpleMap&);
ON_SimpleMap& operator=(const ON_SimpleMap&);
};
template <class KEY, class VALUE, unsigned int HashTableSize>
void ON_SimpleMap<KEY, VALUE, HashTableSize>::Destroy(void)
{
HashElementBlock* blk = m_block_list;
while (NULL != blk)
{
void* p = blk;
blk = blk->m_next;
onfree(p);
}
m_element_list = NULL;
m_block_list = NULL;
memset(m_table, 0, sizeof(m_table));
m_iCount = 0;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
bool ON_SimpleMap<KEY, VALUE, HashTableSize>::Lookup(const KEY& key, VALUE& val) const
{
VALUE* p = Lookup(key);
if (NULL == p)
return false;
val = *p;
return true;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
VALUE* ON_SimpleMap<KEY, VALUE, HashTableSize>::Lookup(const KEY& key) const
{
const int hash_index = HashIndex(key);
HashElement* elem = m_table[hash_index];
while (NULL != elem)
{
if (key == elem->m_key)
return &elem->m_value;
elem = elem->m_next;
}
return NULL;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
bool ON_SimpleMap<KEY, VALUE, HashTableSize>::Remove(const KEY& key)
{
const int hash_index = HashIndex(key);
HashElement* eprev = NULL;
HashElement* elem = m_table[hash_index];
while (NULL != elem)
{
if (key == elem->m_key)
{
if (NULL != eprev)
{
eprev->m_next = elem->m_next;
}
else
{
m_table[hash_index] = elem->m_next;
}
elem->m_next = m_element_list;
m_element_list = elem;
m_iCount--;
return true;
}
eprev = elem;
elem = elem->m_next;
}
return false;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
bool ON_SimpleMap<KEY, VALUE, HashTableSize>::SetAt(const KEY& key, const VALUE& value)
{
VALUE* p = Lookup(key);
if (NULL != p)
{
*p = value;
return true;
}
if (NULL == m_element_list)
{
HashElementBlock* pBlock = (HashElementBlock*)onmalloc(sizeof(HashElementBlock));
memset(pBlock, 0, sizeof(HashElementBlock));
for (int i = 0; i < 1023; i++)
{
pBlock->m_memory_block[i].m_next = &pBlock->m_memory_block[i+1];
}
pBlock->m_next = m_block_list;
m_block_list = pBlock;
m_element_list = pBlock->m_memory_block;
}
const int hash_index = HashIndex(key);
HashElement* elem = m_element_list;
m_element_list = m_element_list->m_next;
elem->m_key = key;
elem->m_value = value;
elem->m_next = m_table[hash_index];
m_table[hash_index] = elem;
m_iCount++;
return true;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
UINT_PTR ON_SimpleMap<KEY, VALUE, HashTableSize>::Count(void) const
{
return m_iCount;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
bool ON_SimpleMap<KEY, VALUE, HashTableSize>::IsEmpty(void) const
{
return 0 == m_iCount;
/*for (int i = 0; i < HashTableSize; i++)
{
if (NULL != m_table[i])
return false;
}
return true;*/
}
template <class KEY, class VALUE, unsigned int HashTableSize>
bool ON_SimpleMap<KEY, VALUE, HashTableSize>::Iterator::Next(VALUE& value)
{
VALUE* p = Next();
if(NULL == p) return false;
value = *p;
return true;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
VALUE* ON_SimpleMap<KEY, VALUE, HashTableSize>::Iterator::Next(void)
{
if (NULL != element)
{
element = element->m_next;
}
while (NULL == element)
{
if (index == HashTableSize)
return NULL;
element = table[index++];
}
return &element->m_value;
}
template <class KEY, class VALUE, unsigned int HashTableSize>
bool ON_SimpleMap<KEY, VALUE, HashTableSize>::Iterator::Next(KEY& key, VALUE& value)
{
if (NULL != element)
{
element = element->m_next;
}
while (NULL == element)
{
if (index == HashTableSize)
return false;
element = table[index++];
}
if(NULL == element)
return false;
value = element->m_value;
key = element->m_key;
return true;
}
template <class VALUE>
class ON_SimpleUuidMap : public ON_SimpleMap<UUID, VALUE, 256>
{
};