aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/lllocalidhashmap.h
blob: e1d34459f8a85bcb37433d34528824aba02202c3 (plain)
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/** 
 * @file lllocalidhashmap.h
 * @brief Map specialized for dealing with local ids
 *
 * Copyright (c) 2003-2007, Linden Research, Inc.
 * 
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 */

#ifndef LL_LLLOCALIDHASHMAP_H
#define LL_LLLOCALIDHASHMAP_H

#include "stdtypes.h"
#include "llerror.h"

const S32 MAX_ITERS = 4;
// LocalID hash map

//
// LLLocalIDHashNode
//

template <class DATA, int SIZE>
class LLLocalIDHashNode
{
public:
	LLLocalIDHashNode();

public:
	S32 mCount;
	U32	mKey[SIZE];
	DATA mData[SIZE];
	LLLocalIDHashNode<DATA, SIZE> *mNextNodep;
};


//
// LLLocalIDHashNode implementation
//
template <class DATA, int SIZE>
LLLocalIDHashNode<DATA, SIZE>::LLLocalIDHashNode()
{
	mCount = 0;
	mNextNodep = NULL;
}

//
// LLLocalIDHashMapIter
//
template <class DATA_TYPE, int SIZE>
class LLLocalIDHashMap;

template <class DATA_TYPE, int SIZE>
class LLLocalIDHashMapIter
{
public:
	LLLocalIDHashMapIter(LLLocalIDHashMap<DATA_TYPE, SIZE> *hash_mapp);
	~LLLocalIDHashMapIter();

	void setMap(LLLocalIDHashMap<DATA_TYPE, SIZE> *hash_mapp);
	inline void first();
	inline void next();
	inline DATA_TYPE& current(); // *NOTE: Deprecate? Phoenix 2005-04-15
	inline BOOL done() const;
	inline S32  currentBin() const;
	inline void setBin(S32 bin);

	DATA_TYPE& operator*() const
	{
		return mCurHashNodep->mData[mCurHashNodeKey];
	}
	DATA_TYPE* operator->() const
	{
		return &(operator*());
	}

	LLLocalIDHashMap<DATA_TYPE, SIZE> *mHashMapp;
	LLLocalIDHashNode<DATA_TYPE, SIZE> *mCurHashNodep;

	S32 mCurHashMapNodeNum;
	S32 mCurHashNodeKey;

	DATA_TYPE mNull;

	S32 mIterID;
};



template <class DATA_TYPE, int SIZE>
class LLLocalIDHashMap
{
public:
	friend class LLLocalIDHashMapIter<DATA_TYPE, SIZE>;

	LLLocalIDHashMap(); // DO NOT use this unless you explicitly setNull, or the data type constructs a "null"
						// object by default
	// basic constructor including sorter
	LLLocalIDHashMap(const DATA_TYPE &null_data);
	// Hack, this should really be a const ref, but I'm not doing it that way because the sim
	// usually uses pointers.
	~LLLocalIDHashMap();

	inline DATA_TYPE &get(const U32 local_id);
	inline BOOL check(const U32 local_id) const;
	inline DATA_TYPE &set(const U32 local_id, const DATA_TYPE data);
	inline BOOL remove(const U32 local_id);
	void removeAll();

	void setNull(const DATA_TYPE data) { mNull = data; }

	inline S32 getLength() const; // Warning, NOT O(1!)

	void dumpIter();
	void dumpBin(U32 bin);

protected:
	// Only used by the iterator.
	void addIter(LLLocalIDHashMapIter<DATA_TYPE, SIZE> *iter);
	void removeIter(LLLocalIDHashMapIter<DATA_TYPE, SIZE> *iter);

	// Remove the item and shift all items afterward down the list,
	// fixing up iterators as we go.
	BOOL removeWithShift(const U32 local_id);

protected:
	LLLocalIDHashNode<DATA_TYPE, SIZE> mNodes[256];

	S32 mIterCount;
	LLLocalIDHashMapIter<DATA_TYPE, SIZE> *mIters[MAX_ITERS];

	DATA_TYPE mNull;
};


//
// LLLocalIDHashMap implementation
//

template <class DATA_TYPE, int SIZE>
LLLocalIDHashMap<DATA_TYPE, SIZE>::LLLocalIDHashMap()
:	mIterCount(0),
	mNull()
{
	S32 i;
	for (i = 0; i < MAX_ITERS; i++)
	{
		mIters[i] = NULL;
	}
}

template <class DATA_TYPE, int SIZE>
LLLocalIDHashMap<DATA_TYPE, SIZE>::LLLocalIDHashMap(const DATA_TYPE &null_data)
:	mIterCount(0),
	mNull(null_data)
{
	S32 i;
	for (i = 0; i < MAX_ITERS; i++)
	{
		mIters[i] = NULL;
	}
}

template <class DATA_TYPE, int SIZE>
LLLocalIDHashMap<DATA_TYPE, SIZE>::~LLLocalIDHashMap()
{
	S32 i;
	for (i = 0; i < MAX_ITERS; i++)
	{
		if (mIters[i])
		{
			mIters[i]->mHashMapp = NULL;
			mIterCount--;
		}
	}
	removeAll();
}

template <class DATA_TYPE, int SIZE>
void LLLocalIDHashMap<DATA_TYPE, SIZE>::removeAll()
{
	S32 bin;
	for (bin = 0; bin < 256; bin++)
	{
		LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[bin];

		BOOL first = TRUE;
		do // First node guaranteed to be there
		{
			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.

			LLLocalIDHashNode<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;
			}
		} while (nodep);
	}
}

template <class DATA_TYPE, int SIZE>
void LLLocalIDHashMap<DATA_TYPE, SIZE>::dumpIter()
{
	std::cout << "Hash map with " << mIterCount << " iterators" << std::endl;

	std::cout << "Hash Map Iterators:" << std::endl;
	S32 i;
	for (i = 0; i < MAX_ITERS; i++)
	{
		if (mIters[i])
		{
			llinfos << i << " " << mIters[i]->mCurHashNodep << " " << mIters[i]->mCurHashNodeKey << llendl;
		}
		else
		{
			llinfos << i << "null" << llendl;
		}
	}
}

template <class DATA_TYPE, int SIZE>
void LLLocalIDHashMap<DATA_TYPE, SIZE>::dumpBin(U32 bin)
{
	std::cout << "Dump bin " << bin << std::endl;

	LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[bin];
	S32 node = 0;
	do // First node guaranteed to be there.
	{
		std::cout << "Bin " << bin 
			<< " node " << node
			<< " count " << nodep->mCount
			<< " contains " << std::flush;

		S32 i;
		for (i = 0; i < nodep->mCount; i++)
		{
			std::cout << nodep->mData[i] << " " << std::flush;
		}

		std::cout << std::endl;

		nodep = nodep->mNextNodep;
		node++;
	} while (nodep);
}

template <class DATA_TYPE, int SIZE>
inline S32 LLLocalIDHashMap<DATA_TYPE, SIZE>::getLength() const
{
	S32 count = 0;
	S32 bin;
	for (bin = 0; bin < 256; bin++)
	{
		const LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[bin];
		while (nodep)
		{
			count += nodep->mCount;
			nodep = nodep->mNextNodep;
		}
	}
	return count;
}

template <class DATA_TYPE, int SIZE>
inline DATA_TYPE &LLLocalIDHashMap<DATA_TYPE, SIZE>::get(const U32 local_id)
{
	LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[local_id & 0xff];

	do // First node guaranteed to be there
	{
		S32 i;
		const S32 count = nodep->mCount;

		// Iterate through all members of this node
		for (i = 0; i < count; i++)
		{
			if (nodep->mKey[i] == local_id)
			{
				// We found it.
				return nodep->mData[i];
			}
		}

		// Done with all objects in this node, go to the next.
		nodep = nodep->mNextNodep;
	} while (nodep);

	return mNull;
}


template <class DATA_TYPE, int SIZE>
inline BOOL LLLocalIDHashMap<DATA_TYPE, SIZE>::check(const U32 local_id) const
{
	const LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[local_id & 0xff];

	do // First node guaranteed to be there
	{
		S32 i;
		const S32 count = nodep->mCount;

		// Iterate through all members of this node
		for (i = 0; i < count; i++)
		{
			if (nodep->mKey[i] == local_id)
			{
				// We found it.
				return TRUE;
			}
		}

		// Done with all objects in this node, go to the next.
		nodep = nodep->mNextNodep;
	} while (nodep);

	// Didn't find anything
	return FALSE;
}


template <class DATA_TYPE, int SIZE>
inline DATA_TYPE &LLLocalIDHashMap<DATA_TYPE, SIZE>::set(const U32 local_id, 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.

	LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[local_id & 0xff];

	while (1)
	{
		const S32 count = nodep->mCount;

		S32 i;
		for (i = 0; i < count; i++)
		{
			if (nodep->mKey[i] == local_id)
			{
				// 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] = local_id;
				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 LLLocalIDHashNode<DATA_TYPE, SIZE>;
				nodep->mNextNodep->mKey[0] = local_id;
				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 LLLocalIDHashMap<DATA_TYPE, SIZE>::remove(const U32 local_id)
{
	// 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.

	const S32 node_index = local_id & 0xff;

	LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[node_index];

	// A modification of the standard search algorithm.
	do // First node guaranteed to be there
	{
		const S32 count = nodep->mCount;

		S32 i;
		for (i = 0; i < count; i++)
		{
			if (nodep->mKey[i] == local_id)
			{
				// If we're removing the item currently pointed to by one
				// or more iterators, we can just swap in the last item
				// and back the iterator(s) up by one.
				// Otherwise, we need to do a slow and safe shift of all
				// items back to one position to fill the hole and fix up 
				// all iterators we find.
				BOOL need_shift = FALSE;
				S32 cur_iter;
				if (mIterCount)
				{
					for (cur_iter = 0; cur_iter < MAX_ITERS; cur_iter++)
					{
						if (mIters[cur_iter])
						{
							// We only care if the hash map node is on the one
							// that we're working on.  If it's before, we've already
							// traversed it, if it's after, changing the order doesn't
							// matter.
							if (mIters[cur_iter]->mCurHashMapNodeNum == node_index)
							{
								if ((mIters[cur_iter]->mCurHashNodep == nodep)
									&& (mIters[cur_iter]->mCurHashNodeKey == i))
								{
									// it's on the one we're deleting, we'll
									// fix the iterator quickly below.
								}
								else
								{
									// We're trying to remove an item on this
									// iterator's chain that this
									// iterator doesn't point to!  We need to do
									// the slow remove-and-shift-down case.
									need_shift = TRUE;
								}
							}
						}
					}
				}

				// Removing an item that isn't pointed to by all iterators
				if (need_shift)
				{
					return removeWithShift(local_id);
				}

				// Fix the iterators that point to this node/i pair, the
				// one we're deleting
				for (cur_iter = 0; cur_iter < MAX_ITERS; cur_iter++)
				{
					if (mIters[cur_iter])
					{
						// We only care if the hash map node is on the one
						// that we're working on.  If it's before, we've already
						// traversed it, if it's after, changing the order doesn't
						// matter.
						if (mIters[cur_iter]->mCurHashMapNodeNum == node_index)
						{
							if ((mIters[cur_iter]->mCurHashNodep == nodep)
								&& (mIters[cur_iter]->mCurHashNodeKey == i))
							{
								// We can handle the case where we're deleting 
								// the element we're on trivially (sort of).
								if (nodep->mCount > 1)
								{
									// If we're not going to delete this node,
									// it's OK.
									mIters[cur_iter]->mCurHashNodeKey--;
								}
								else
								{
									// We're going to delete this node, because this
									// is the last element on it.
									
									// Find the next node, and then back up one.
									mIters[cur_iter]->next();
									mIters[cur_iter]->mCurHashNodeKey--;
								}
							}
						}
					}
				}

				// 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.

				LLLocalIDHashNode<DATA_TYPE, SIZE> *prevp = &mNodes[node_index];
				LLLocalIDHashNode<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[local_id & 0xff])
					{
						// 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;
	} while (nodep);

	return FALSE;
}

template <class DATA_TYPE, int SIZE>
BOOL LLLocalIDHashMap<DATA_TYPE, SIZE>::removeWithShift(const U32 local_id)
{
	const S32 node_index = local_id & 0xFF;
	LLLocalIDHashNode<DATA_TYPE, SIZE>* nodep = &mNodes[node_index];
	LLLocalIDHashNode<DATA_TYPE, SIZE>* prevp = NULL;
	BOOL found = FALSE;

	do // First node guaranteed to be there
	{
		const S32 count = nodep->mCount;
		S32 i;
		for (i = 0; i < count; i++)
		{
			if (nodep->mKey[i] == local_id)
			{
				// Found the item.  Start shifting items from later
				// in the list over this item.
				found = TRUE;
			}

			if (found)
			{
				// If there is an iterator on this node, we need to 
				// back it up.
				S32 cur_iter;
				for (cur_iter = 0; cur_iter <MAX_ITERS; cur_iter++)
				{
					LLLocalIDHashMapIter<DATA_TYPE, SIZE>* iter;
					iter = mIters[cur_iter];
					// If an iterator is on this node,i pair, then back it up.
					if (iter
						&& iter->mCurHashMapNodeNum == node_index
						&& iter->mCurHashNodep == nodep
						&& iter->mCurHashNodeKey == i)
					{
						if (i > 0)
						{
							// Don't need to move iterator nodep, since 
							// we're in the same node.
							iter->mCurHashNodeKey--;
						}
						else if (prevp)
						{
							// need to go the previous node, last item
							iter->mCurHashNodep = prevp;
							iter->mCurHashNodeKey = prevp->mCount - 1;
						}
						else
						{
							// we're on the first item in the list, but
							// need to go back anyhow.

							// BUG: If this deletion empties the list, 
							// iter->done() will be wrong until
							// iter->next() is called.
							iter->mCurHashNodeKey = -1;
						}
					}
				}

				// Copy data from the next position into this position.
				if (i < count-1)
				{
					// we're not on the last item in the node,
					// so we can copy within the node
					nodep->mKey[i] = nodep->mKey[i+1];
					nodep->mData[i] = nodep->mData[i+1];
				}
				else if (nodep->mNextNodep)
				{
					// we're on the last item in the node,
					// but there's a next node we can copy from
					nodep->mKey[i] = nodep->mNextNodep->mKey[0];
					nodep->mData[i] = nodep->mNextNodep->mData[0];
				}
				else
				{
					// We're on the last position in the list.
					// No one to copy from.  Replace with nothing.
					nodep->mKey[i] = 0;
					nodep->mData[i] = mNull;
				}
			}
		}

		// Last node in chain, so delete the last node
		if (found
			&& !nodep->mNextNodep)
		{
			// delete the last item off the last node
			nodep->mCount--;

			if (nodep->mCount == 0)
			{
				// We deleted the last element!
				if (nodep != &mNodes[node_index])
				{
					// Always have a prevp if we're not the head.
					llassert(prevp);

					// 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 nodep;
					nodep = NULL;
				}
			}

			// Deleted last item in chain, so we're done.
			return found;
		}

		prevp = nodep;
		nodep = nodep->mNextNodep;
	} while (nodep);

	return found;
}

template <class DATA_TYPE, int SIZE>
void LLLocalIDHashMap<DATA_TYPE, SIZE>::removeIter(LLLocalIDHashMapIter<DATA_TYPE, SIZE> *iter)
{
	S32 i;
	for (i = 0; i < MAX_ITERS; i++)
	{
		if (mIters[i] == iter)
		{
			mIters[i] = NULL;
			mIterCount--;
			return;
		}
	}
	llerrs << "Iterator " << iter << " not found for removal in hash map!" << llendl;
}

template <class DATA_TYPE, int SIZE>
void LLLocalIDHashMap<DATA_TYPE, SIZE>::addIter(LLLocalIDHashMapIter<DATA_TYPE, SIZE> *iter)
{
	S32 i;
	for (i = 0; i < MAX_ITERS; i++)
	{
		if (mIters[i] == NULL)
		{
			mIters[i] = iter;
			mIterCount++;
			return;
		}
	}
	llerrs << "More than " << MAX_ITERS << " iterating over a map simultaneously!" << llendl;
}



//
// LLLocalIDHashMapIter Implementation
//
template <class DATA_TYPE, int SIZE>
LLLocalIDHashMapIter<DATA_TYPE, SIZE>::LLLocalIDHashMapIter(LLLocalIDHashMap<DATA_TYPE, SIZE> *hash_mapp)
{
	mHashMapp = NULL;
	setMap(hash_mapp);
}

template <class DATA_TYPE, int SIZE>
LLLocalIDHashMapIter<DATA_TYPE, SIZE>::~LLLocalIDHashMapIter()
{
	if (mHashMapp)
	{
		mHashMapp->removeIter(this);
	}
}

template <class DATA_TYPE, int SIZE>
void LLLocalIDHashMapIter<DATA_TYPE, SIZE>::setMap(LLLocalIDHashMap<DATA_TYPE, SIZE> *hash_mapp)
{
	if (mHashMapp)
	{
		mHashMapp->removeIter(this);
	}
	mHashMapp = hash_mapp;
	if (mHashMapp)
	{
		mHashMapp->addIter(this);
	}

	mCurHashNodep = NULL;
	mCurHashMapNodeNum = -1;
	mCurHashNodeKey = 0;
}

template <class DATA_TYPE, int SIZE>
inline void LLLocalIDHashMapIter<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)
		{

			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 LLLocalIDHashMapIter<DATA_TYPE, SIZE>::done() const
{
	return mCurHashNodep ? FALSE : TRUE;
}

template <class DATA_TYPE, int SIZE>
inline S32 LLLocalIDHashMapIter<DATA_TYPE, SIZE>::currentBin() const
{
	if (  (mCurHashMapNodeNum > 255)
		||(mCurHashMapNodeNum < 0))
	{
		return 0;
	}
	else
	{
		return mCurHashMapNodeNum;
	}
}

template <class DATA_TYPE, int SIZE>
inline void LLLocalIDHashMapIter<DATA_TYPE, SIZE>::setBin(S32 bin)
{
	// Iterate through until we find the first non-empty node;
	S32 i;
	bin = llclamp(bin, 0, 255);
	for (i = bin; i < 256; i++)
	{
		if (mHashMapp->mNodes[i].mCount)
		{

			mCurHashNodep = &mHashMapp->mNodes[i];
			mCurHashMapNodeNum = i;
			mCurHashNodeKey = 0;
			return;
		}
	}
	for (i = 0; i < bin; i++)
	{
		if (mHashMapp->mNodes[i].mCount)
		{

			mCurHashNodep = &mHashMapp->mNodes[i];
			mCurHashMapNodeNum = i;
			mCurHashNodeKey = 0;
			return;
		}
	}
	// Completely empty!
	mCurHashNodep = NULL;
}

template <class DATA_TYPE, int SIZE>
inline DATA_TYPE &LLLocalIDHashMapIter<DATA_TYPE, SIZE>::current()
{
	if (!mCurHashNodep)
	{
		return mNull;
	}
	return mCurHashNodep->mData[mCurHashNodeKey];
}

template <class DATA_TYPE, int SIZE>
inline void LLLocalIDHashMapIter<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;
	return;
}

#endif // LL_LLLOCALIDHASHMAP_H