summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/simple_set.h
blob: b0e2793cd7654b62fdb1d6faf06237f7a397d085 (plain) (blame)
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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*********************************************************************

    simple_set.h

    A STL-like set class.

    Copyright Nicola Salmoria and the MAME Team.
    Visit http://mamedev.org for licensing and usage restrictions.

*********************************************************************/

#pragma once

#ifndef __SIMPLE_SET_H__
#define __SIMPLE_SET_H__

#ifdef SIMPLE_SET_DEBUG
#include <iostream>
#endif


// Predeclarations
template <class T> class avl_tree_node;
template <class T> class simple_set_iterator;


//
// ======================> simple_set
// A shiny stl-like set interface wrapping an AVL tree
//
// PUBLIC OPERATIONS:
// size, empty, clear, insert, remove, find, contains, merge, & assignment.
//

template <class T>
class simple_set
{
	friend class simple_set_iterator<T>;
	typedef avl_tree_node<T> tree_node;

public:
	// Construction
	simple_set(resource_pool &pool = global_resource_pool())
		: m_root(NULL),
			m_pool(pool)
	{ }

	simple_set(const simple_set& rhs)
		: m_root(NULL)
	{
		*this = rhs;
	}

	~simple_set()
	{
		clear();
	}


	// A reference to the resource pool
	resource_pool &pool() const { return m_pool; }


	// Returns number of elements in the tree -- O(n)
	int size() const
	{
		if (empty()) return 0;

		const tree_node* currentNode = m_root;
		const int nodeCount = sizeRecurse(currentNode);
		return nodeCount;
	}


	// Test for emptiness -- O(1).
	bool empty() const
	{
		return m_root == NULL;
	}


	// Empty the tree -- O(n).
	void clear()
	{
		clearRecurse(m_root);
	}


	// Insert x into the avl tree; duplicates are ignored -- O(log n).
	bool insert(const T& x)
	{
		bool retVal = insert(x, m_root);

		// Whether the node was successfully inserted or not (i.e. wasn't a duplicate)
		return retVal;
	}


	// Remove x from the tree. Nothing is done if x is not found -- O(n).
	bool remove(const T& x)
	{
		// First find the node in the tree
		tree_node* currNode = find(x, m_root);

		// Only do this when the current node is valid
		if (currNode)
		{
			// See if it's a leaf
			if (currNode->isLeaf())
			{
				// If we're a leaf and we have no parent, then the tree will be emptied
				if (!currNode->parent)
				{
					m_root = NULL;
				}

				// If it's a leaf node, simply remove it
				removeNode(currNode);
				pool_free(m_pool, currNode);
			}
			else
			{
				// Get the parent object
				tree_node* parentNode = currNode->parent;

				// Remove the child and reconnect the smallest node in the right sub tree
				// (in order successor)
				tree_node* replaceNode = findMin(currNode->right);

				// See if there's even a right-most node
				if (!replaceNode)
				{
					// Get the largest node on the left (because the right doesn't exist)
					replaceNode = findMax(currNode->left);
				}

				// Disconnect the replacement node's branch
				removeNode(replaceNode);

				// Disconnect the current node
				removeNode(currNode);

				// Get the current node's left and right branches
				tree_node* left = currNode->left;
				tree_node* right = currNode->right;

				// We no longer need this node
				pool_free(m_pool, currNode);

				// Check to see if we removed the root node
				if (!parentNode)
				{
					// Merge the branches into the parent node of what we deleted
					merge(replaceNode, parentNode);
					merge(left, parentNode);
					merge(right, parentNode);

					// Now we're the the root
					m_root = parentNode;
				}
				else
				{
					// Merge the branches into the parent node of what we
					// deleted, we let the merge algorithm decide where to
					// put the branches
					merge(replaceNode, parentNode);
					merge(left, parentNode);
					merge(right, parentNode);
				}
			}

			// Balance the tree
			balanceTree();

			// The node was found and removed successfully
			return true;
		}
		else
		{
			// The node was not found
			return false;
		}
	}


	// Find item x in the tree. Returns a pointer to the matching item
	// or NULL if not found -- O(log n)
	T* find(const T& x) const
	{
		tree_node* found = find(x, m_root);
		if (found == NULL) return NULL;
		return &found->element;
	}


	// Is the data present in the set? -- O(log n)
	bool contains(const T& x) const
	{
		if (find(x) != NULL)
			return true;
		else
			return false;
	}


	// Merge a different tree with ours -- O(n).
	bool merge(const simple_set<T>& b)
	{
		tree_node* c = b->clone();
		bool retVal = merge(c->m_root, m_root);

		// Re-balance the tree if the merge was successful
		if (retVal)
		{
			balanceTree();
		}
		else
		{
			pool_free(m_pool, c);
		}

		return retVal;
	}


	// Replace this set with another -- O(n)
	const simple_set& operator=(const simple_set& rhs)
	{
		// Don't clone if it's the same pointer
		if (this != &rhs)
		{
			clear();

			m_root = clone(rhs.m_root);
		}

		return *this;
	}


#ifdef SIMPLE_SET_DEBUG
	// Debug -- O(n log n)
	void printTree(std::ostream& out = std::cout) const
	{
		if(empty())
		{
			out << "Empty tree" << std::endl;
		}
		else
		{
			printTree(out, m_root);
		}
	}
#endif


private:
	// The AVL tree's root
	tree_node* m_root;

	// Resource pool where objects are freed
	resource_pool& m_pool;


	// Find a node in the tree
	tree_node* findNode(const T& x) const
	{
		tree_node* node = find(x, m_root);
		if (node)
		{
			return node;
		}
		else
		{
			return NULL;
		}
	}


	// Insert item x into a subtree t (root) -- O(log n)
	bool insert(const T& x, tree_node*& t)
	{
		if (t == NULL)
		{
			t = pool_alloc(m_pool, tree_node(x, NULL, NULL, NULL));

			// An empty sub-tree here, insertion successful
			return true;
		}
		else if (x < t->element)
		{
			// O(log n)
			bool retVal = insert(x, t->left);

			if (retVal)
			{
				t->left->setParent(t);
				if(t->balanceFactor() < -1)
				{
					// See if it went left of the left
					if(x < t->left->element)
					{
						rotateWithLeftChild(t);
					}
					else
					{
						// The element goes on the right of the left
						doubleWithLeftChild(t);
					}
				}
			}

			return retVal;
		}
		else if (t->element < x)
		{
			bool retVal = insert(x, t->right);

			// Only do this if the insertion was successful
			if (retVal)
			{
				t->right->setParent(t);

				if (t->balanceFactor() > 1)
				{
					// See if it went right of the right
					if(t->right->element < x)
					{
						rotateWithRightChild(t);
					}
					else
					{
						// The element goes on the left of the right
						doubleWithRightChild(t);
					}
				}
			}

			return retVal;
		}
		else
		{
			return false;  // Duplicate
		}
	}


	// Recursively free all nodes in the tree -- O(n).
	void clearRecurse(tree_node*& t) const
	{
		if(t != NULL)
		{
			clearRecurse(t->left);
			clearRecurse(t->right);

			pool_free(m_pool, t);
		}
		t = NULL;
	}


	// Merge a tree with this one.  Private because external care is required.
	bool merge(tree_node* b, tree_node*& t)
	{
		if (!b)
		{
			return false;
		}
		else
		{
			bool retVal = false;

			if (t == NULL)
			{
				// Set this element to that subtree
				t = b;

				// The parent here should be NULL anyway, but we
				// set it just to be sure. This pointer will be
				// used as a flag to indicate where in the call
				// stack the tree was actually set.
				//
				// The middle layers of this method's call will
				// all have their parent references in tact since
				// no operations took place there.
				//
				//t->parent = NULL;
				t->setParent(NULL);

				// We were successful in merging
				retVal = true;
			}
			else if (b->element < t->element)
			{
				retVal = merge(b, t->left);

				// Only do this if the insertion actually took place
				if (retVal && !t->left->parent)
				{
					t->left->setParent(t);
				}
			}
			else if (t->element < b->element)
			{
				retVal = merge(b, t->right);

				// Only do this if the insertion was successful
				if (retVal && !t->right->parent)
				{
					t->right->setParent(t);
				}

				return retVal;
			}

			return retVal;
		}
	}


	// Find the smallest item's node in a subtree t -- O(log n).
	tree_node* findMin(tree_node* t) const
	{
		if(t == NULL)
		{
			return t;
		}

		while(t->left != NULL)
		{
			t = t->left;
		}

		return t;
	}


	// Find the smallest item's node in a subtree t -- O(log n).
	tree_node* findMax(tree_node* t) const
	{
		if(t == NULL)
		{
			return t;
		}

		while(t->right != NULL)
		{
			t = t->right;
		}

		return t;
	}


	// Find item x's node in subtree t -- O(log n)
	tree_node* find(const T& x, tree_node* t) const
	{
		while(t != NULL)
		{
			if (x < t->element)
			{
				t = t->left;
			}
			else if (t->element < x)
			{
				t = t->right;
			}
			else
			{
				return t;   // Match
			}
		}

		return NULL;   // No match
	}


	// Clone a subtree -- O(n)
	tree_node* clone(const tree_node* t) const
	{
		if(t == NULL)
		{
			return NULL;
		}
		else
		{
			// Create a node with the left and right nodes and a parent set to NULL
			tree_node* retVal = pool_alloc(m_pool, tree_node(t->element, NULL, clone(t->left), clone(t->right)));

			// Now set our children's parent node reference
			if (retVal->left) { retVal->left->setParent(retVal); }
			if (retVal->right) { retVal->right->setParent(retVal); }

			return retVal;
		}
	}


	// Rotate binary tree node with left child.
	// Single rotation for case 1 -- O(1).
	void rotateWithLeftChild(tree_node*& k2) const
	{
		tree_node* k1 = k2->left;
		tree_node* k2Parent = k2->parent;

		k2->setLeft(k1->right);
		if (k2->left) { k2->left->setParent(k2); }

		k1->setRight(k2);
		if (k1->right) { k1->right->setParent(k1); }

		k2 = k1;
		k2->setParent(k2Parent);
	}


	// Rotate binary tree node with right child.
	// Single rotation for case 4 -- O(1).
	void rotateWithRightChild(tree_node*& k1) const
	{
		tree_node* k2 = k1->right;
		tree_node* k1Parent = k1->parent;

		k1->setRight(k2->left);
		if (k1->right) { k1->right->setParent(k1); }

		k2->setLeft(k1);
		if (k2->left) { k2->left->setParent(k2); }

		k1 = k2;
		k1->setParent(k1Parent);
	}


	// Double rotate binary tree node: first left child
	// with its right child; then node k3 with new left child.
	// Double rotation for case 2 -- O(1).
	void doubleWithLeftChild(tree_node*& k3) const
	{
		rotateWithRightChild(k3->left);
		rotateWithLeftChild(k3);
	}


	// Double rotate binary tree node: first right child
	// with its left child; then node k1 with new right child.
	// Double rotation for case 3 -- O(1).
	void doubleWithRightChild(tree_node*& k1) const
	{
		rotateWithLeftChild(k1->right);
		rotateWithRightChild(k1);
	}


	// Removes a node. Returns true if the node was on the left side of its parent -- O(1).
	void removeNode(tree_node*& node)
	{
		// It is a leaf, simply remove the item and disconnect the parent
		if (node->isLeft())
		{
			node->parent->setLeft(NULL);
		}
		else // (node == node->parent->right)
		{
			if (node->parent) { node->parent->setRight(NULL); }
		}

		node->setParent(NULL);
	}


	// Swap one node with another -- O(1).
	void replaceNode(tree_node*& node1, tree_node*& node2)
	{
		// Save both parent references
		simple_set<T>* node1Parent = node1->parent;
		simple_set<T>* node2Parent = node2->parent;

		// First move node2 into node1's place
		if (node1Parent)
		{
			if (isLeft(node1))
			{
				node1Parent->setLeft(node2);
			}
			else // node1 is on the right
			{
				node1Parent->setRight(node2);
			}
		}
		node2->setParent(node1Parent);

		// Now move node1 into node2's place
		if (node2Parent)
		{
			if (isLeft(node2))
			{
				node2Parent->setLeft(node1);
			}
			else // node2 is on the right
			{
				node2Parent->setRight(node1);
			}
		}
		node1->setParent(node2Parent);
	}


	// Balances the tree starting at the root node
	void balanceTree() { balanceTree(m_root); }


	// Balance the tree starting at the given node -- O(n).
	void balanceTree(tree_node*& node)
	{
		if (node)
		{
			// First see what the balance factor for this node is
			int balFactor = node->balanceFactor();

			if (balFactor < -1)
			{
				// See if we're heavy left of the left
				if(node->left->balanceFactor() < 0)
				{
					rotateWithLeftChild(node);
				}
				else // if (node->left->balanceFactor() > 0)
				{
					// We're heavy on the right of the left
					doubleWithLeftChild(node);
				}
			}
			else if (balFactor > 1)
			{
				// See if it we're heavy right of the right
				if(node->right->balanceFactor() > 0)
				{
					rotateWithRightChild(node);
				}
				else // if (node->right->balanceFactor() < 0)
				{
					// The element goes on the left of the right
					doubleWithRightChild(node);
				}
			}
			else // if (balFactor >= -1 && balFactor <= 1)
			{
				// We're balanced here, but are our children balanced?
				balanceTree(node->left);
				balanceTree(node->right);
			}
		}
	}


	// Recursive helper function for public size()
	int sizeRecurse(const tree_node* currentNode) const
	{
		int nodeCount = 1;
		if (currentNode->left != NULL)
			nodeCount += sizeRecurse(currentNode->left);
		if (currentNode->right != NULL)
			nodeCount += sizeRecurse(currentNode->right);
		return nodeCount;
	}


#ifdef SIMPLE_SET_DEBUG
	// Debug.  Print from the start node, down -- O(n log n).
	void printTree(std::ostream& out, tree_node* t=NULL, int numTabs=0, char lr='_') const
	{
		if(t != NULL)
		{
			for (int i =0; i < numTabs; i++) { out << "  "; } out << "|_" << lr << "__ ";
			out << t->element << " {h = " << t->height() << ", b = " << t->balanceFactor() << "} ";
			// TODO: Reinstate out << std::hex << t << " (p = " << t->parent << ")" << std::dec;
			out << std::endl;

			printTree(out, t->left, numTabs + 1, '<');
			printTree(out, t->right, numTabs + 1, '>');
		}
	}
#endif
};


//
// ======================> avl_tree_node
// Member nodes of the simple_set's AVL tree
//

template <class T> class avl_tree_node
{
	friend class simple_set<T>;
	friend class simple_set_iterator<T>;
	typedef avl_tree_node<T> tree_node;

public:
	// Construction
	avl_tree_node(const T& theElement, avl_tree_node* p, avl_tree_node* lt, avl_tree_node* rt)
		: element(theElement),
		parent(p),
		left(lt),
		right(rt),
		m_height(1),
		m_balanceFactor(0)
	{ }


	// Are we to our parent's left?
	bool isLeft()
	{
		if (parent && this == parent->left)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	// Are we a leaf node?
	bool isLeaf() { return !left && !right; }


	// Set the parent pointer
	void setParent(tree_node* p)
	{
		// Set our new parent
		parent = p;

		// If we have a valid parent, set its height
		if (parent)
		{
			// Set the parent's height to include this tree. If the parent
			// already has a tree that is taller than the one we're attaching
			// then the parent's height remains unchanged
			int rightHeight = (parent->right ? parent->right->m_height : 0);
			int leftHeight = (parent->left ? parent->left->m_height : 0);

			// The height of the tallest branch + 1
			parent->m_height = maxInt(rightHeight, leftHeight) + 1;

			// Also set the balance factor
			parent->m_balanceFactor = rightHeight - leftHeight;
		}
	}


	// Set the left child pointer
	void setLeft(tree_node* l)
	{
		// Set our new left node
		left = l;

		// Set the height and balance factor
		int rightHeight = (right ? right->m_height : 0);
		int leftHeight = (left ? left->m_height : 0);

		m_height = maxInt(rightHeight, leftHeight) + 1;
		m_balanceFactor = (right ? right->m_height : 0) - (left ? left->m_height : 0);
	}


	// Set the right child pointer
	void setRight(tree_node* r)
	{
		// Set our new right node
		right = r;

		// Set the height and balance factor
		int rightHeight = (right ? right->m_height : 0);
		int leftHeight = (left ? left->m_height : 0);

		m_height = maxInt(rightHeight, leftHeight) + 1;
		m_balanceFactor = (right ? right->m_height : 0) - (left ? left->m_height : 0);
	}


	// Recover the height
	int height() const
	{
		// The height is equal to the maximum of the right or left side's height plus 1
		// Trading memory for operation time can be done O(n) like this =>
		//  return max(left ? left->height() : 0, right ? right->height() : 0) + 1;
		return m_height;
	}


	// Recover the balance factor
	int balanceFactor() const
	{
		// The weight of a node is equal to the difference between
		// the weight of the left subtree and the weight of the
		// right subtree
		//
		// O(n) version =>
		//  return (right ? right->height() : 0) - (left ? left->height() : 0);
		//
		return m_balanceFactor;
	}


private:
	// Calculates all of the heights for this node and its ancestors -- O(log n).
	void calcHeights()
	{
		// Calculate our own height -- O(1)
		m_height = maxInt(left ? left->m_height : 0, right ? right->m_height : 0) + 1;

		// And our parent's height (and recurse) -- O(log n)
		if (parent)
		{
			parent->calcHeights();
		}
	}


	// Utility function - TODO replace
	int maxInt(const int& lhs, const int& rhs) const
	{
		return lhs > rhs ? lhs : rhs;
	}


private:
	T element;

	avl_tree_node* parent;
	avl_tree_node* left;
	avl_tree_node* right;

	int m_height;
	int m_balanceFactor;
};


//
// ======================> simple_set_iterator
// Iterator that allows for various set (AVL tree) navigation methods
// Points to elements of the set, rather than AVL tree nodes.
//
// PUBLIC OPERATIONS:
// current, first, last, next, count, indexof, byindex
//

template <class T>
class simple_set_iterator
{
	typedef avl_tree_node<T> tree_node;

public:
	enum TraversalType { PRE_ORDER, IN_ORDER, POST_ORDER, LEVEL_ORDER };

public:
	// construction
	simple_set_iterator(simple_set<T>& set, const TraversalType& tt=IN_ORDER)
		: m_set(&set),
			m_traversalType(tt),
			m_currentNode(NULL),
			m_endNode(NULL) { }

	~simple_set_iterator() { }


	// getters
	T* current() const { return m_currentNode; }


	// reset and return first item
	T* first()
	{
		m_currentNode = m_set->m_root;
		switch (m_traversalType)
		{
			case IN_ORDER:
			{
				// The current node is the smallest value
				m_currentNode = m_set->findMin(m_set->m_root);

				// The end case is the largest value
				m_endNode = m_set->findMax(m_set->m_root);

				return &m_currentNode->element;
			}

			default:
			{
				// TODO (better error message):
				printf("simple_set_iterator: Traversal type not yet supported.\n");
				return NULL;
			}
		}
		return NULL;
	}


	T* last()
	{
		return NULL;
	}


	// advance according to current state and traversal type
	T* next()
	{
		if (m_currentNode == NULL) return NULL;

		switch (m_traversalType)
		{
			case IN_ORDER:
			{
				// You are at the end
				if (m_currentNode == m_endNode)
					return NULL;

				if (m_currentNode->right != NULL)
				{
					// Gather the furthest left node of right subtree
					m_currentNode = m_currentNode->right;
					while (m_currentNode->left != NULL)
					{
						m_currentNode = m_currentNode->left;
					}
				}
				else
				{
					// No right subtree?  Move up the tree, looking for a left child link.
					tree_node* p = m_currentNode->parent;
					while (p != NULL && m_currentNode == p->right)
					{
						m_currentNode = p;
						p = p->parent;
					}
					m_currentNode = p;
				}

				return &m_currentNode->element;
			}

			default:
			{
				// TODO (better error message):
				printf("simple_set_iterator: Traversal type not yet supported.\n");
				return NULL;
			}
		}

		return NULL;
	}


	// return the number of items available
	int count()
	{
		return m_set->size();
	}


	// return the index of a given item in the virtual list
	// note: this function is destructive to any in-progress iterations!
	int indexof(T inData)
	{
		int index = 0;
		for (T* data = first(); data != last(); data = next(), index++)
			if (!(*data < inData) && !(inData < *data))
				return index;
		return -1;
	}


	// return the indexed item in the list
	// note: this function is destructive to any in-progress iterations!
	T* byindex(int index)
	{
		int count = 0;
		for (T* data = first(); data != last(); data = next(), count++)
			if (count == index)
				return data;
		return NULL;
	}


private:
	simple_set<T>* m_set;

	TraversalType m_traversalType;
	tree_node* m_currentNode;
	tree_node* m_endNode;
};

#endif