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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
/**
* @file lluuidhashmap.h
* @brief A uuid based hash map.
*
* $LicenseInfo:firstyear=2003&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#ifndef LL_LLUUIDHASHMAP_H
#define LL_LLUUIDHASHMAP_H
#include "stdtypes.h"
#include "llerror.h"
#include "lluuid.h"
// UUID hash map
/*
LLUUIDHashMap<uuid_pair, 32> foo(test_equals);
LLUUIDHashMapIter<uuid_pair, 32> bar(&foo);
LLDynamicArray<LLUUID> source_ids;
const S32 COUNT = 100000;
S32 q;
for (q = 0; q < COUNT; q++)
{
llinfos << "Creating" << llendl;
LLUUID id;
id.generate();
//llinfos << q << ":" << id << llendl;
uuid_pair pair;
pair.mUUID = id;
pair.mValue = q;
foo.set(id, pair);
source_ids.put(id);
//ms_sleep(1);
}
uuid_pair cur;
llinfos << "Iterating" << llendl;
for (cur = bar.first(); !bar.done(); cur = bar.next())
{
if (source_ids[cur.mValue] != cur.mUUID)
{
llerrs << "Incorrect value iterated!" << llendl;
}
//llinfos << cur.mValue << ":" << cur.mUUID << llendl;
//ms_sleep(1);
}
llinfos << "Finding" << llendl;
for (q = 0; q < COUNT; q++)
{
cur = foo.get(source_ids[q]);
if (source_ids[cur.mValue] != cur.mUUID)
{
llerrs << "Incorrect value found!" << llendl;
}
//llinfos << res.mValue << ":" << res.mUUID << llendl;
//ms_sleep(1);
}
llinfos << "Removing" << llendl;
for (q = 0; q < COUNT/2; q++)
{
if (!foo.remove(source_ids[q]))
{
llerrs << "Remove failed!" << llendl;
}
//ms_sleep(1);
}
llinfos << "Iterating" << llendl;
for (cur = bar.first(); !bar.done(); cur = bar.next())
{
if (source_ids[cur.mValue] != cur.mUUID)
{
llerrs << "Incorrect value found!" << llendl;
}
//llinfos << cur.mValue << ":" << cur.mUUID << llendl;
//ms_sleep(1);
}
llinfos << "Done with UUID map test" << llendl;
return 0;
*/
//
// LLUUIDHashNode
//
template <class DATA, int SIZE>
class LLUUIDHashNode
{
public:
LLUUIDHashNode();
public:
S32 mCount;
U8 mKey[SIZE];
DATA mData[SIZE];
LLUUIDHashNode<DATA, SIZE> *mNextNodep;
};
//
// LLUUIDHashNode implementation
//
template <class DATA, int SIZE>
LLUUIDHashNode<DATA, SIZE>::LLUUIDHashNode()
{
mCount = 0;
mNextNodep = NULL;
}
template <class DATA_TYPE, int SIZE>
class LLUUIDHashMap
{
public:
// basic constructor including sorter
LLUUIDHashMap(BOOL (*equals)(const LLUUID &uuid, const DATA_TYPE &data),
const DATA_TYPE &null_data);
~LLUUIDHashMap();
inline DATA_TYPE &get(const LLUUID &uuid);
inline BOOL check(const LLUUID &uuid) const;
inline DATA_TYPE &set(const LLUUID &uuid, const DATA_TYPE &type);
inline BOOL remove(const LLUUID &uuid);
void removeAll();
inline S32 getLength() const; // Warning, NOT O(1!)
public:
BOOL (*mEquals)(const LLUUID &uuid, const DATA_TYPE &data);
LLUUIDHashNode<DATA_TYPE, SIZE> mNodes[256];
S32 mIterCount;
protected:
DATA_TYPE mNull;
};
//
// LLUUIDHashMap implementation
//
template <class DATA_TYPE, int SIZE>
LLUUIDHashMap<DATA_TYPE, SIZE>::LLUUIDHashMap(BOOL (*equals)(const LLUUID &uuid, const DATA_TYPE &data),
const DATA_TYPE &null_data)
: mEquals(equals),
mIterCount(0),
mNull(null_data)
{ }
template <class DATA_TYPE, int SIZE>
LLUUIDHashMap<DATA_TYPE, SIZE>::~LLUUIDHashMap()
{
removeAll();
}
template <class DATA_TYPE, int SIZE>
void LLUUIDHashMap<DATA_TYPE, SIZE>::removeAll()
{
S32 bin;
for (bin = 0; bin < 256; bin++)
{
LLUUIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[bin];
BOOL first = TRUE;
while (nodep)
{
S32 i;
const S32 count = nodep->mCount;
// Iterate through all members of this node
for (i = 0; i < count; i++)
{
nodep->mData[i] = mNull;
}
nodep->mCount = 0;
// Done with all objects in this node, go to the next.
LLUUIDHashNode<DATA_TYPE, SIZE>* curp = nodep;
nodep = nodep->mNextNodep;
// Delete the node if it's not the first node
if (first)
{
first = FALSE;
curp->mNextNodep = NULL;
}
else
{
delete curp;
}
}
}
}
template <class DATA_TYPE, int SIZE>
inline S32 LLUUIDHashMap<DATA_TYPE, SIZE>::getLength() const
{
S32 count = 0;
S32 bin;
for (bin = 0; bin < 256; bin++)
{
LLUUIDHashNode<DATA_TYPE, SIZE>* nodep = (LLUUIDHashNode<DATA_TYPE, SIZE>*) &mNodes[bin];
while (nodep)
{
count += nodep->mCount;
nodep = nodep->mNextNodep;
}
}
return count;
}
template <class DATA_TYPE, int SIZE>
inline DATA_TYPE &LLUUIDHashMap<DATA_TYPE, SIZE>::get(const LLUUID &uuid)
{
LLUUIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[uuid.mData[0]];
// Grab the second byte of the UUID, which is the key for the node data
const S32 second_byte = uuid.mData[1];
while (nodep)
{
S32 i;
const S32 count = nodep->mCount;
// Iterate through all members of this node
for (i = 0; i < count; i++)
{
if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i]))
{
// The second byte matched, and our equality test passed.
// We found it.
return nodep->mData[i];
}
}
// Done with all objects in this node, go to the next.
nodep = nodep->mNextNodep;
}
return mNull;
}
template <class DATA_TYPE, int SIZE>
inline BOOL LLUUIDHashMap<DATA_TYPE, SIZE>::check(const LLUUID &uuid) const
{
const LLUUIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[uuid.mData[0]];
// Grab the second byte of the UUID, which is the key for the node data
const S32 second_byte = uuid.mData[1];
while (nodep)
{
S32 i;
const S32 count = nodep->mCount;
// Iterate through all members of this node
for (i = 0; i < count; i++)
{
if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i]))
{
// The second byte matched, and our equality test passed.
// We found it.
return TRUE;
}
}
// Done with all objects in this node, go to the next.
nodep = nodep->mNextNodep;
}
// Didn't find anything
return FALSE;
}
template <class DATA_TYPE, int SIZE>
inline DATA_TYPE &LLUUIDHashMap<DATA_TYPE, SIZE>::set(const LLUUID &uuid, const DATA_TYPE &data)
{
// Set is just like a normal find, except that if we find a match
// we replace it with the input value.
// If we don't find a match, we append to the end of the list.
LLUUIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[uuid.mData[0]];
const S32 second_byte = uuid.mData[1];
while (1)
{
const S32 count = nodep->mCount;
S32 i;
for (i = 0; i < count; i++)
{
if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i]))
{
// We found a match for this key, replace the data with
// the incoming data.
nodep->mData[i] = data;
return nodep->mData[i];
}
}
if (!nodep->mNextNodep)
{
// We've iterated through all of the keys without finding a match
if (i < SIZE)
{
// There's still some space on this node, append
// the key and data to it.
nodep->mKey[i] = second_byte;
nodep->mData[i] = data;
nodep->mCount++;
return nodep->mData[i];
}
else
{
// This node is full, append a new node to the end.
nodep->mNextNodep = new LLUUIDHashNode<DATA_TYPE, SIZE>;
nodep->mNextNodep->mKey[0] = second_byte;
nodep->mNextNodep->mData[0] = data;
nodep->mNextNodep->mCount = 1;
return nodep->mNextNodep->mData[0];
}
}
// No match on this node, go to the next
nodep = nodep->mNextNodep;
}
}
template <class DATA_TYPE, int SIZE>
inline BOOL LLUUIDHashMap<DATA_TYPE, SIZE>::remove(const LLUUID &uuid)
{
if (mIterCount)
{
// We don't allow remove when we're iterating, it's bad karma!
llerrs << "Attempted remove while an outstanding iterator in LLUUIDHashMap!" << llendl;
}
// Remove is the trickiest operation.
// What we want to do is swap the last element of the last
// node if we find the one that we want to remove, but we have
// to deal with deleting the node from the tail if it's empty, but
// NOT if it's the only node left.
LLUUIDHashNode<DATA_TYPE, SIZE> *nodep = &mNodes[uuid.mData[0]];
// Not empty, we need to search through the nodes
const S32 second_byte = uuid.mData[1];
// A modification of the standard search algorithm.
while (nodep)
{
const S32 count = nodep->mCount;
S32 i;
for (i = 0; i < count; i++)
{
if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i]))
{
// We found the node that we want to remove.
// Find the last (and next-to-last) node, and the index of the last
// element. We could conceviably start from the node we're on,
// but that makes it more complicated, this is easier.
LLUUIDHashNode<DATA_TYPE, SIZE> *prevp = &mNodes[uuid.mData[0]];
LLUUIDHashNode<DATA_TYPE, SIZE> *lastp = prevp;
// Find the last and next-to-last
while (lastp->mNextNodep)
{
prevp = lastp;
lastp = lastp->mNextNodep;
}
// First, swap in the last to the current location.
nodep->mKey[i] = lastp->mKey[lastp->mCount - 1];
nodep->mData[i] = lastp->mData[lastp->mCount - 1];
// Now, we delete the entry
lastp->mCount--;
lastp->mData[lastp->mCount] = mNull;
if (!lastp->mCount)
{
// We deleted the last element!
if (lastp != &mNodes[uuid.mData[0]])
{
// Only blitz the node if it's not the head
// Set the previous node to point to NULL, then
// blitz the empty last node
prevp->mNextNodep = NULL;
delete lastp;
}
}
return TRUE;
}
}
// Iterate to the next node, we've scanned all the entries in this one.
nodep = nodep->mNextNodep;
}
return FALSE;
}
//
// LLUUIDHashMapIter
//
template <class DATA_TYPE, int SIZE>
class LLUUIDHashMapIter
{
public:
LLUUIDHashMapIter(LLUUIDHashMap<DATA_TYPE, SIZE> *hash_mapp);
~LLUUIDHashMapIter();
inline void reset();
inline void first();
inline void next();
inline BOOL done() const;
DATA_TYPE& operator*() const
{
return mCurHashNodep->mData[mCurHashNodeKey];
}
DATA_TYPE* operator->() const
{
return &(operator*());
}
protected:
LLUUIDHashMap<DATA_TYPE, SIZE> *mHashMapp;
LLUUIDHashNode<DATA_TYPE, SIZE> *mCurHashNodep;
S32 mCurHashMapNodeNum;
S32 mCurHashNodeKey;
DATA_TYPE mNull;
};
//
// LLUUIDHashMapIter Implementation
//
template <class DATA_TYPE, int SIZE>
LLUUIDHashMapIter<DATA_TYPE, SIZE>::LLUUIDHashMapIter(LLUUIDHashMap<DATA_TYPE, SIZE> *hash_mapp)
{
mHashMapp = hash_mapp;
mCurHashNodep = NULL;
mCurHashMapNodeNum = 0;
mCurHashNodeKey = 0;
}
template <class DATA_TYPE, int SIZE>
LLUUIDHashMapIter<DATA_TYPE, SIZE>::~LLUUIDHashMapIter()
{
reset();
}
template <class DATA_TYPE, int SIZE>
inline void LLUUIDHashMapIter<DATA_TYPE, SIZE>::reset()
{
if (mCurHashNodep)
{
// We're partway through an iteration, we can clean up now
mHashMapp->mIterCount--;
mCurHashNodep = NULL;
}
}
template <class DATA_TYPE, int SIZE>
inline void LLUUIDHashMapIter<DATA_TYPE, SIZE>::first()
{
// Iterate through until we find the first non-empty node;
S32 i;
for (i = 0; i < 256; i++)
{
if (mHashMapp->mNodes[i].mCount)
{
if (!mCurHashNodep)
{
// Increment, since it's no longer safe for us to do a remove
mHashMapp->mIterCount++;
}
mCurHashNodep = &mHashMapp->mNodes[i];
mCurHashMapNodeNum = i;
mCurHashNodeKey = 0;
//return mCurHashNodep->mData[0];
return;
}
}
// Completely empty!
mCurHashNodep = NULL;
//return mNull;
return;
}
template <class DATA_TYPE, int SIZE>
inline BOOL LLUUIDHashMapIter<DATA_TYPE, SIZE>::done() const
{
return mCurHashNodep ? FALSE : TRUE;
}
template <class DATA_TYPE, int SIZE>
inline void LLUUIDHashMapIter<DATA_TYPE, SIZE>::next()
{
// No current entry, this iterator is done
if (!mCurHashNodep)
{
//return mNull;
return;
}
// Go to the next element
mCurHashNodeKey++;
if (mCurHashNodeKey < mCurHashNodep->mCount)
{
// We're not done with this node, return the current element
//return mCurHashNodep->mData[mCurHashNodeKey];
return;
}
// Done with this node, move to the next
mCurHashNodep = mCurHashNodep->mNextNodep;
if (mCurHashNodep)
{
// Return the first element
mCurHashNodeKey = 0;
//return mCurHashNodep->mData[0];
return;
}
// Find the next non-empty node (keyed on the first byte)
mCurHashMapNodeNum++;
S32 i;
for (i = mCurHashMapNodeNum; i < 256; i++)
{
if (mHashMapp->mNodes[i].mCount)
{
// We found one that wasn't empty
mCurHashNodep = &mHashMapp->mNodes[i];
mCurHashMapNodeNum = i;
mCurHashNodeKey = 0;
//return mCurHashNodep->mData[0];
return;
}
}
// OK, we're done, nothing else to iterate
mCurHashNodep = NULL;
mHashMapp->mIterCount--; // Decrement since we're safe to do removes now
//return mNull;
}
#endif // LL_LLUUIDHASHMAP_H
|