aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/linked_lists.h
blob: 0162a077a251af258e0828c102c3b9c996afbd41 (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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/** 
 * @file linked_lists.h
 * @brief LLLinkedList class header amd implementation file.
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * 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.
 * $/LicenseInfo$
 */

#ifndef LL_LINKED_LISTS_H
#define LL_LINKED_LISTS_H

/** 
 * Provides a standard doubly linked list for fun and profit
 * Utilizes a neat trick off of Flipcode where the back pointer is a
 * pointer to a pointer, allowing easier transfer of nodes between lists, &c
 * And a template class, of course
 */

#include "llerror.h"


template <class DATA_TYPE> class LLLinkedList
{
public:
	friend class LLLinkNode;
	// External interface

	// basic constructor
	LLLinkedList() : mHead(NULL), mCurrentp(NULL), mInsertBefore(NULL)
	{
		mCurrentp = mHead.mNextp;
		mCurrentOperatingp = mHead.mNextp;
		mCount = 0;
	}

	// basic constructor
	LLLinkedList(BOOL	(*insert_before)(DATA_TYPE *data_new, DATA_TYPE *data_tested)) : mHead(NULL), mCurrentp(NULL), mInsertBefore(insert_before)
	{
		mCurrentp = mHead.mNextp;
		mCurrentOperatingp = mHead.mNextp;
		mCount = 0;
	}

	// destructor destroys list and nodes, but not data in nodes
	~LLLinkedList()
	{
		removeAllNodes();
	}

	// set mInsertBefore
	void setInsertBefore(BOOL (*insert_before)(DATA_TYPE *data_new, DATA_TYPE *data_tested))
	{
		mInsertBefore = insert_before;
	}

	//
	// WARNING!!!!!!!
	// addData and addDataSorted are NOT O(1) operations, but O(n) because they check
	// for existence of the data in the linked list first.  Why, I don't know - djs
	// If you don't care about dupes, use addDataNoCheck
	//

	// put data into a node and stick it at the front of the list
	inline BOOL addData(DATA_TYPE *data);

	// put data into a node and sort into list by mInsertBefore()
	// calls normal add if mInsertBefore isn't set
	inline BOOL addDataSorted(DATA_TYPE *data);

	inline BOOL addDataNoCheck(DATA_TYPE *data);

	// bubbleSortList
	// does an improved bubble sort of the list . . . works best with almost sorted data
	// does nothing if mInsertBefore isn't set
	// Nota Bene: Swaps are accomplished by swapping data pointers
	inline void bubbleSortList();

	// put data into a node and stick it at the end of the list
	inline BOOL addDataAtEnd(DATA_TYPE *data);

	// returns number of items in the list
	inline S32 getLength() const;

	inline BOOL isEmpty();

	// search the list starting at mHead.mNextp and remove the link with mDatap == data
	// leave mCurrentp and mCurrentOperatingp on the next entry
	// return TRUE if found, FALSE if not found
	inline BOOL removeData(DATA_TYPE *data);

		// search the list starting at mHead.mNextp and delete the link with mDatap == data
	// leave mCurrentp and mCurrentOperatingp on the next entry
	// return TRUE if found, FALSE if not found
	inline BOOL deleteData(DATA_TYPE *data);

	// remove all nodes from the list and delete the associated data
	inline void deleteAllData();

	// remove all nodes from the list but do not delete data
	inline void removeAllNodes();

	// check to see if data is in list
	// if TRUE then mCurrentp and mCurrentOperatingp point to data
	inline BOOL	checkData(DATA_TYPE *data);

	// place mCurrentp on first node
	inline void resetList();

	// return the data currently pointed to, set mCurentOperatingp to that node and bump mCurrentp
	inline DATA_TYPE	*getCurrentData();

	// same as getCurrentData() but a more intuitive name for the operation
	inline DATA_TYPE	*getNextData();

	// reset the list and return the data currently pointed to, set mCurentOperatingp to that node and bump mCurrentp
	inline DATA_TYPE	*getFirstData();

	// reset the list and return the data at position n, set mCurentOperatingp to that node and bump mCurrentp
	// Note: n is zero-based
	inline DATA_TYPE	*getNthData( U32 n);

	// reset the list and return the last data in it, set mCurentOperatingp to that node and bump mCurrentp
	inline DATA_TYPE	*getLastData();

	// remove the Node at mCurentOperatingp
	// leave mCurrentp and mCurentOperatingp on the next entry
	inline void removeCurrentData();

	// remove the Node at mCurentOperatingp and add it to newlist
	// leave mCurrentp and mCurentOperatingp on the next entry
	void moveCurrentData(LLLinkedList *newlist, BOOL b_sort);

	BOOL moveData(DATA_TYPE *data, LLLinkedList *newlist, BOOL b_sort);

	// delete the Node at mCurentOperatingp
	// leave mCurrentp anf mCurentOperatingp on the next entry
	void deleteCurrentData();

private:
	// node that actually contains the data
	class LLLinkNode
	{
	public:
		// assign the mDatap pointer
		LLLinkNode(DATA_TYPE *data) : mDatap(data), mNextp(NULL), mPrevpp(NULL)
		{
		}

		// destructor does not, by default, destroy associated data
		// however, the mDatap must be NULL to ensure that we aren't causing memory leaks
		~LLLinkNode()
		{
			if (mDatap)
			{
				llerror("Attempting to call LLLinkNode destructor with a non-null mDatap!", 1);
			}
		}

		// delete associated data and NULL out pointer
		void deleteData()
		{
			delete mDatap;
			mDatap = NULL;
		}

		// NULL out pointer
		void removeData()
		{
			mDatap = NULL;
		}

		DATA_TYPE	*mDatap;
		LLLinkNode	*mNextp;
		LLLinkNode	**mPrevpp;
	};

	// add a node at the front of the list
	void addData(LLLinkNode *node)
	{
		// don't allow NULL to be passed to addData
		if (!node)
		{
			llerror("NULL pointer passed to LLLinkedList::addData", 0);
		}

		// add the node to the front of the list
		node->mPrevpp = &mHead.mNextp;
		node->mNextp = mHead.mNextp;

		// if there's something in the list, fix its back pointer
		if (node->mNextp)
		{
			node->mNextp->mPrevpp = &node->mNextp;
		}

		mHead.mNextp = node;
	}

	LLLinkNode			mHead;															// fake head node. . . makes pointer operations faster and easier
	LLLinkNode			*mCurrentp;														// mCurrentp is the Node that getCurrentData returns
	LLLinkNode			*mCurrentOperatingp;											// this is the node that the various mumbleCurrentData functions act on
	BOOL				(*mInsertBefore)(DATA_TYPE *data_new, DATA_TYPE *data_tested);	// user function set to allow sorted lists
	U32					mCount;
};

template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::addData(DATA_TYPE *data)
{
	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::addData", 0);
	}

	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	if ( checkData(data))
	{
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
		return FALSE;
	}

	// make the new node
	LLLinkNode *temp = new LLLinkNode(data);

	// add the node to the front of the list
	temp->mPrevpp = &mHead.mNextp;
	temp->mNextp = mHead.mNextp;

	// if there's something in the list, fix its back pointer
	if (temp->mNextp)
	{
		temp->mNextp->mPrevpp = &temp->mNextp;
	}

	mHead.mNextp = temp;
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
	mCount++;
	return TRUE;
}


template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::addDataNoCheck(DATA_TYPE *data)
{
	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::addData", 0);
	}

	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	// make the new node
	LLLinkNode *temp = new LLLinkNode(data);

	// add the node to the front of the list
	temp->mPrevpp = &mHead.mNextp;
	temp->mNextp = mHead.mNextp;

	// if there's something in the list, fix its back pointer
	if (temp->mNextp)
	{
		temp->mNextp->mPrevpp = &temp->mNextp;
	}

	mHead.mNextp = temp;
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
	mCount++;
	return TRUE;
}


template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::addDataSorted(DATA_TYPE *data)
{
	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;
	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::addDataSorted", 0);
	}

	if (checkData(data))
	{
		// restore
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
		return FALSE;
	}

	// mInsertBefore not set?
	if (!mInsertBefore)
	{
		addData(data);
		// restore
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
		return FALSE;
	}

	// empty list?
	if (!mHead.mNextp)
	{
		addData(data);
		// restore
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
		return TRUE;
	}

	// make the new node
	LLLinkNode *temp = new LLLinkNode(data);

	// walk the list until mInsertBefore returns true 
	mCurrentp = mHead.mNextp;
	while (mCurrentp->mNextp)
	{
		if (mInsertBefore(data, mCurrentp->mDatap))
		{
			// insert before the current one
			temp->mPrevpp = mCurrentp->mPrevpp;
			temp->mNextp = mCurrentp;
			*(temp->mPrevpp) = temp;
			mCurrentp->mPrevpp = &temp->mNextp;
			// restore
			mCurrentp = tcurr;
			mCurrentOperatingp = tcurrop;
			mCount++;
			return TRUE;
		}
		else
		{
			mCurrentp = mCurrentp->mNextp;
		}
	}

	// on the last element, add before?
	if (mInsertBefore(data, mCurrentp->mDatap))
	{
		// insert before the current one
		temp->mPrevpp = mCurrentp->mPrevpp;
		temp->mNextp = mCurrentp;
		*(temp->mPrevpp) = temp;
		mCurrentp->mPrevpp = &temp->mNextp;
		// restore
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
	}
	else // insert after
	{
		temp->mPrevpp = &mCurrentp->mNextp;
		temp->mNextp = NULL;
		mCurrentp->mNextp = temp;

		// restore
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
	}
	mCount++;
	return TRUE;
}

template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::bubbleSortList()
{
	// mInsertBefore not set
	if (!mInsertBefore)
	{
		return;
	}

	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	BOOL		b_swapped = FALSE;
	DATA_TYPE	*temp;

	// Nota Bene: This will break if more than 0x7FFFFFFF members in list!
	S32			length = 0x7FFFFFFF;
	S32			count = 0;
	do
	{
		b_swapped = FALSE;
		mCurrentp = mHead.mNextp;
		count = 0;
		while (  (count + 1 < length)
			   &&(mCurrentp))
		{
			if (mCurrentp->mNextp)
			{
				if (!mInsertBefore(mCurrentp->mDatap, mCurrentp->mNextp->mDatap))
				{
					// swap data pointers!
					temp = mCurrentp->mDatap;
					mCurrentp->mDatap = mCurrentp->mNextp->mDatap;
					mCurrentp->mNextp->mDatap = temp;
					b_swapped = TRUE;
				}
			}
			else
			{
				break;
			}
			count++;
			mCurrentp = mCurrentp->mNextp;
		}
		length = count;
	} while (b_swapped);

	// restore
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
}


template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::addDataAtEnd(DATA_TYPE *data)
{
	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::addData", 0);
	}

	if (checkData(data))
	{
		mCurrentp = tcurr;
		mCurrentOperatingp = tcurrop;
		return FALSE;
	}

	// make the new node
	LLLinkNode *temp = new LLLinkNode(data);

	// add the node to the end of the list

	// if empty, add to the front and be done with it
	if (!mHead.mNextp)
	{
		temp->mPrevpp = &mHead.mNextp;
		temp->mNextp = NULL;
		mHead.mNextp = temp;
	}
	else
	{
		// otherwise, walk to the end of the list
		mCurrentp = mHead.mNextp;
		while (mCurrentp->mNextp)
		{
			mCurrentp = mCurrentp->mNextp;
		}
		temp->mPrevpp = &mCurrentp->mNextp;
		temp->mNextp = NULL;
		mCurrentp->mNextp = temp;
	}

	// restore
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
	mCount++;
	return TRUE;
}


// returns number of items in the list
template <class DATA_TYPE>
S32 LLLinkedList<DATA_TYPE>::getLength() const
{
//	S32	length = 0;
//	for (LLLinkNode* temp = mHead.mNextp; temp != NULL; temp = temp->mNextp)
//	{
//		length++;
//	}
	return mCount;
}


template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::isEmpty()
{
	return (mCount == 0);
}


// search the list starting at mHead.mNextp and remove the link with mDatap == data
// leave mCurrentp and mCurrentOperatingp on the next entry
// return TRUE if found, FALSE if not found
template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::removeData(DATA_TYPE *data)
{
	BOOL b_found = FALSE;
	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::removeData", 0);
	}

	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;

	while (mCurrentOperatingp)
	{
		if (mCurrentOperatingp->mDatap == data)
		{
			b_found = TRUE;

			// remove the node

			// if there is a next one, fix it
			if (mCurrentOperatingp->mNextp)
			{
				mCurrentOperatingp->mNextp->mPrevpp = mCurrentOperatingp->mPrevpp;
			}
			*(mCurrentOperatingp->mPrevpp) = mCurrentOperatingp->mNextp;

			// remove the LLLinkNode

			// if we were on the one we want to delete, bump the cached copies
			if (mCurrentOperatingp == tcurrop)
			{
				tcurrop = tcurr = mCurrentOperatingp->mNextp;
			}
			else if (mCurrentOperatingp == tcurr)
			{
				tcurrop = tcurr = mCurrentOperatingp->mNextp;
			}

			mCurrentp = mCurrentOperatingp->mNextp;

			mCurrentOperatingp->removeData();
			delete mCurrentOperatingp;
			mCurrentOperatingp = mCurrentp;
			mCount--;
			break;
		}
		mCurrentOperatingp = mCurrentOperatingp->mNextp;
	}
	// restore
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
	return b_found;
}

// search the list starting at mHead.mNextp and delete the link with mDatap == data
// leave mCurrentp and mCurrentOperatingp on the next entry
// return TRUE if found, FALSE if not found
template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::deleteData(DATA_TYPE *data)
{
	BOOL b_found = FALSE;
	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::removeData", 0);
	}

	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;

	while (mCurrentOperatingp)
	{
		if (mCurrentOperatingp->mDatap == data)
		{
			b_found = TRUE;

			// remove the node
			// if there is a next one, fix it
			if (mCurrentOperatingp->mNextp)
			{
				mCurrentOperatingp->mNextp->mPrevpp = mCurrentOperatingp->mPrevpp;
			}
			*(mCurrentOperatingp->mPrevpp) = mCurrentOperatingp->mNextp;

			// delete the LLLinkNode
			// if we were on the one we want to delete, bump the cached copies
			if (mCurrentOperatingp == tcurrop)
			{
				tcurrop = tcurr = mCurrentOperatingp->mNextp;
			}

			// and delete the associated data
			llassert(mCurrentOperatingp);
			mCurrentp = mCurrentOperatingp->mNextp;
			mCurrentOperatingp->deleteData();
			delete mCurrentOperatingp;
			mCurrentOperatingp = mCurrentp;
			mCount--;
			break;
		}
		mCurrentOperatingp = mCurrentOperatingp->mNextp;
	}
	// restore
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
	return b_found;
}

	// remove all nodes from the list and delete the associated data
template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::deleteAllData()
{
	LLLinkNode *temp;
	// reset mCurrentp
	mCurrentp = mHead.mNextp;

	while (mCurrentp)
	{
		temp = mCurrentp->mNextp;
		mCurrentp->deleteData();
		delete mCurrentp;
		mCurrentp = temp;
	}

	// reset mHead and mCurrentp
	mHead.mNextp = NULL;
	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;
	mCount = 0;
}

// remove all nodes from the list but do not delete data
template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::removeAllNodes()
{
	LLLinkNode *temp;
	// reset mCurrentp
	mCurrentp = mHead.mNextp;

	while (mCurrentp)
	{
		temp = mCurrentp->mNextp;
		mCurrentp->removeData();
		delete mCurrentp;
		mCurrentp = temp;
	}

	// reset mHead and mCurrentp
	mHead.mNextp = NULL;
	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;
	mCount = 0;
}

// check to see if data is in list
// if TRUE then mCurrentp and mCurrentOperatingp point to data
template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::checkData(DATA_TYPE *data)
{
	// reset mCurrentp
	mCurrentp = mHead.mNextp;

	while (mCurrentp)
	{
		if (mCurrentp->mDatap == data)
		{
			mCurrentOperatingp = mCurrentp;
			return TRUE;
		}
		mCurrentp = mCurrentp->mNextp;
	}
	mCurrentOperatingp = mCurrentp;
	return FALSE;
}

// place mCurrentp on first node
template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::resetList()
{
	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;
}

// return the data currently pointed to, set mCurentOperatingp to that node and bump mCurrentp
template <class DATA_TYPE>
DATA_TYPE *LLLinkedList<DATA_TYPE>::getCurrentData()
{
	if (mCurrentp)
	{
		mCurrentOperatingp = mCurrentp;
		mCurrentp = mCurrentp->mNextp;
		return mCurrentOperatingp->mDatap;
	}
	else
	{
		return NULL;
	}
}

// same as getCurrentData() but a more intuitive name for the operation
template <class DATA_TYPE>
DATA_TYPE *LLLinkedList<DATA_TYPE>::getNextData()
{
	if (mCurrentp)
	{
		mCurrentOperatingp = mCurrentp;
		mCurrentp = mCurrentp->mNextp;
		return mCurrentOperatingp->mDatap;
	}
	else
	{
		return NULL;
	}
}

// reset the list and return the data currently pointed to, set mCurentOperatingp to that node and bump mCurrentp
template <class DATA_TYPE>
DATA_TYPE *LLLinkedList<DATA_TYPE>::getFirstData()
{
	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;
	if (mCurrentp)
	{
		mCurrentOperatingp = mCurrentp;
		mCurrentp = mCurrentp->mNextp;
		return mCurrentOperatingp->mDatap;
	}
	else
	{
		return NULL;
	}
}

// Note: n is zero-based
template <class DATA_TYPE>
DATA_TYPE	*LLLinkedList<DATA_TYPE>::getNthData( U32 n )
{
	mCurrentOperatingp = mHead.mNextp;

	// if empty, return NULL
	if (!mCurrentOperatingp)
	{
		return NULL;
	}

	for( U32 i = 0; i < n; i++ )
	{
		mCurrentOperatingp = mCurrentOperatingp->mNextp;
		if( !mCurrentOperatingp )
		{
			return NULL;
		}
	}

	mCurrentp = mCurrentOperatingp->mNextp;
	return mCurrentOperatingp->mDatap;
}


// reset the list and return the last data in it, set mCurentOperatingp to that node and bump mCurrentp
template <class DATA_TYPE>
DATA_TYPE *LLLinkedList<DATA_TYPE>::getLastData()
{
	mCurrentOperatingp = mHead.mNextp;

	// if empty, return NULL
	if (!mCurrentOperatingp)
		return NULL;

	// walk until we're pointing at the last entry
	while (mCurrentOperatingp->mNextp)
	{
		mCurrentOperatingp = mCurrentOperatingp->mNextp;
	}
	mCurrentp = mCurrentOperatingp->mNextp;
	return mCurrentOperatingp->mDatap;
}

// remove the Node at mCurentOperatingp
// leave mCurrentp and mCurentOperatingp on the next entry
// return TRUE if found, FALSE if not found
template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::removeCurrentData()
{
	if (mCurrentOperatingp)
	{
		// remove the node
		// if there is a next one, fix it
		if (mCurrentOperatingp->mNextp)
		{
			mCurrentOperatingp->mNextp->mPrevpp = mCurrentOperatingp->mPrevpp;
		}
		*(mCurrentOperatingp->mPrevpp) = mCurrentOperatingp->mNextp;

		// remove the LLLinkNode
		mCurrentp = mCurrentOperatingp->mNextp;

		mCurrentOperatingp->removeData();
		delete mCurrentOperatingp;
		mCount--;
		mCurrentOperatingp = mCurrentp;
	}
}

// remove the Node at mCurentOperatingp and add it to newlist
// leave mCurrentp and mCurentOperatingp on the next entry
// return TRUE if found, FALSE if not found
template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::moveCurrentData(LLLinkedList *newlist, BOOL b_sort)
{
	if (mCurrentOperatingp)
	{
		// remove the node
		// if there is a next one, fix it
		if (mCurrentOperatingp->mNextp)
		{
			mCurrentOperatingp->mNextp->mPrevpp = mCurrentOperatingp->mPrevpp;
		}
		*(mCurrentOperatingp->mPrevpp) = mCurrentOperatingp->mNextp;

		// remove the LLLinkNode
		mCurrentp = mCurrentOperatingp->mNextp;
		// move the node to the new list
		newlist->addData(mCurrentOperatingp);
		if (b_sort)
			bubbleSortList();
		mCurrentOperatingp = mCurrentp;
	}
}

template <class DATA_TYPE>
BOOL LLLinkedList<DATA_TYPE>::moveData(DATA_TYPE *data, LLLinkedList *newlist, BOOL b_sort)
{
	BOOL b_found = FALSE;
	// don't allow NULL to be passed to addData
	if (!data)
	{
		llerror("NULL pointer passed to LLLinkedList::removeData", 0);
	}

	LLLinkNode *tcurr = mCurrentp;
	LLLinkNode *tcurrop = mCurrentOperatingp;

	mCurrentp = mHead.mNextp;
	mCurrentOperatingp = mHead.mNextp;

	while (mCurrentOperatingp)
	{
		if (mCurrentOperatingp->mDatap == data)
		{
			b_found = TRUE;

			// remove the node

			// if there is a next one, fix it
			if (mCurrentOperatingp->mNextp)
			{
				mCurrentOperatingp->mNextp->mPrevpp = mCurrentOperatingp->mPrevpp;
			}
			*(mCurrentOperatingp->mPrevpp) = mCurrentOperatingp->mNextp;

			// if we were on the one we want to delete, bump the cached copies
			if (  (mCurrentOperatingp == tcurrop)
				||(mCurrentOperatingp == tcurr))
			{
				tcurrop = tcurr = mCurrentOperatingp->mNextp;
			}

			// remove the LLLinkNode
			mCurrentp = mCurrentOperatingp->mNextp;
			// move the node to the new list
			newlist->addData(mCurrentOperatingp);
			if (b_sort)
				newlist->bubbleSortList();
			mCurrentOperatingp = mCurrentp;
			break;
		}
		mCurrentOperatingp = mCurrentOperatingp->mNextp;
	}
	// restore
	mCurrentp = tcurr;
	mCurrentOperatingp = tcurrop;
	return b_found;
}

// delete the Node at mCurentOperatingp
// leave mCurrentp anf mCurentOperatingp on the next entry
// return TRUE if found, FALSE if not found
template <class DATA_TYPE>
void LLLinkedList<DATA_TYPE>::deleteCurrentData()
{
	if (mCurrentOperatingp)
	{
		// remove the node
		// if there is a next one, fix it
		if (mCurrentOperatingp->mNextp)
		{
			mCurrentOperatingp->mNextp->mPrevpp = mCurrentOperatingp->mPrevpp;
		}
		*(mCurrentOperatingp->mPrevpp) = mCurrentOperatingp->mNextp;

		// remove the LLLinkNode
		mCurrentp = mCurrentOperatingp->mNextp;

		mCurrentOperatingp->deleteData();
		if (mCurrentOperatingp->mDatap)
			llerror("This is impossible!", 0);
		delete mCurrentOperatingp;
		mCurrentOperatingp = mCurrentp;
		mCount--;
	}
}

#endif