MeVisLab Resolution Independence API
Loading...
Searching...
No Matches
SbMap.h
Go to the documentation of this file.
1
3/*
4 * SbMap is based on BinTree - Binary tree template class written by
5 * Per Nilsson, It is Freeware and not copyrighted.
6 *
7 _______________________________________________________________________
8 _______________________________________________________________________
9 |
10 |
11 | Description:
12 | SbMap is a container that associates objects of type KeyType with
13 | objects of type ValueType. No two elements will have the same key.
14 | SbMap has the important property that inserting a new element into
15 | a map does not invalidate iterators that point to existing elements.
16 | Erasing an element from a map also does not invalidate any iterators,
17 | except, of course, for iterators that actually point to the element
18 | that is being erased.
19 |
20 | Classes:
21 |
22 | // The map item class.
23 | template <class KeyType, class ValueType> class SbMapItem
24 |
25 | // The map class
26 | template <class KeyType, class ValueType> class SbMap
27 | {
28 | public:
29 |
30 | // Regular low->high (++) and high->low (--) iterator
31 | class Iterator
32 |
33 | // Top to bottom iterator
34 | class ParentFirstIterator
35 |
36 | // Bottom to top iterator
37 | class ParentLastIterator
38 |
39 | // Top to bottom, level by level iterator
40 | class ByLevelIterator
41 | }
42 |
43 | Requirements:
44 |
45 | The KeyType class must support:
46 | 1. < and == operations.
47 | 2. Copy construction.
48 |
49 | The ValueType must support:
50 | 1. Copy construction.
51 | 2. Assignment operation (=) if SbMapItem::setValue is used
52 |
53 |
54 | Author(s) : Per Nilsson, Felix Ritter
55 |
56 _______________________________________________________________________
57 _______________________________________________________________________
58 */
59
60#ifndef _SB_MAP_
61#define _SB_MAP_
62
63#include "SoShaderSystem.h"
64
66template <class KeyType, class ValueType> class SbMapItem
67{
68public:
69 // Constructor(Key, Value)
70 SbMapItem(const KeyType& k, const ValueType& v)
71 : mKey(k)
72 , mValue(v)
73 , mLeftChild(nullptr)
74 , mRightChild(nullptr)
75 , mParent(nullptr)
76 , mIsRed(true)
77 {}
78
79 // Destructor
80 virtual ~SbMapItem()
81 {}
82
83 // Set methods
84
85 // set*Child also updates the the child's parent attribute.
87 {
88 mLeftChild = p;
89 if (p)
90 p->setParent(this);
91 }
93 {
94 mRightChild = p;
95 if (p)
96 p->setParent(this);
97 }
99 {
100 mParent = p;
101 }
102
103 void setValue(const ValueType& v)
104 {
105 mValue = v;
106 }
107 // Note: Deliberately no SetKey, that could easily screw up the tree.
108 // If a key shall be changed, delete node from tree and insert a new one instead.
109
110 void setRed()
111 {
112 mIsRed = true;
113 }
114 void setBlack()
115 {
116 mIsRed = false;
117 }
118
119 // Get methods
120
122 {
123 return mLeftChild;
124 }
126 {
127 return mRightChild;
128 }
130 {
131 return mParent;
132 }
133
134 ValueType getValue()
135 {
136 return mValue;
137 }
138 const ValueType& getValue() const
139 {
140 return mValue;
141 }
143 {
144 return mKey;
145 }
146 const KeyType& getKey() const
147 {
148 return mKey;
149 }
150
151 // Some more or less useful queries
152 bool isRoot() const
153 {
154 return mParent == nullptr;
155 }
156 bool isLeftChild() const
157 {
158 return mParent != nullptr && mParent->getLeftChild() == this;
159 }
160 bool isRightChild() const
161 {
162 return mParent != nullptr && mParent->getRightChild() == this;
163 }
164 bool isLeaf() const
165 {
166 return mLeftChild == nullptr && mRightChild == nullptr;
167 }
168 unsigned int GetLevel() const
169 {
170 return (isRoot()) ? 1 : getParent()->GetLevel() + 1;
171 }
172
173 bool isRed() const
174 {
175 return mIsRed;
176 };
177 bool isBlack() const
178 {
179 return !mIsRed;
180 };
181
182private:
183 // Default constructor - deliberately not implemented
184 SbMapItem();
185
186 SbMapItem* mLeftChild;
187 SbMapItem* mRightChild;
188 SbMapItem* mParent;
189
190 KeyType mKey;
191 ValueType mValue;
192
193 bool mIsRed;
194};
195
197template <class KeyType, class ValueType> class SbMap
198{
199public:
201
204 {
205 public:
206 // Default Constructor
208 : mRoot(nullptr)
209 , mCur(nullptr)
210 {}
211
212 // Constructor(Node *)
214 : mRoot(root)
215 {
216 reset();
217 }
218
219 // Copy constructor
220 Iterator(const Iterator& src)
221 : mRoot(src.mRoot)
222 , mCur(src.mCur)
223 {}
224
225 // reset the iterator
226 // atLowest : true - reset at lowest key value (and then ++ your way through)
227 // false - reset at highest key value (and then -- your way through)
228 void reset(bool atLowest = true)
229 {
230 mCur = (atLowest) ? minNode(mRoot) : maxNode(mRoot);
231 }
232
233 // Has the iterator reached the end?
234 bool atEnd() const
235 {
236 return mCur == nullptr;
237 }
239 {
240 return mCur;
241 }
242
243 // Assignment operator
245 {
246 mRoot = src.mRoot;
247 mCur = src.mCur;
248 return *this;
249 }
250
251 // Increment operator
252 void operator++(int)
253 {
254 Inc();
255 }
256
257 // Decrement operator
258 void operator--(int)
259 {
260 Dec();
261 }
262
263 // Access operators
265 {
266 return getNode();
267 }
269 {
270 if (atEnd()) {
271 throw "Iterator at end";
272 }
273 return *getNode();
274 }
275
276 private:
277 // minNode: Returns node with lowest key from n and down.
278 // Ie the node all down to the left
279 Node* minNode(Node* n)
280 {
281 while (n && n->getLeftChild())
282 n = n->getLeftChild();
283 return n;
284 }
285 // maxNode: Returns node with highest key from n and down.
286 // Ie the node all down to the right
287 Node* maxNode(Node* n)
288 {
289 while (n && n->getRightChild())
290 n = n->getRightChild();
291 return n;
292 }
293
294 // ++
295 void Inc()
296 {
297 // Already at end?
298 if (mCur == nullptr)
299 return;
300
301 if (mCur->getRightChild()) {
302 // If current node has a right child, the next higher node is the
303 // node with lowest key beneath the right child.
304 mCur = minNode(mCur->getRightChild());
305 }
306 else if (mCur->isLeftChild()) {
307 // No right child? Well if current node is a left child then
308 // the next higher node is the parent
309 mCur = mCur->getParent();
310 }
311 else {
312 // Current node neither is left child nor has a right child.
313 // Ie it is either right child or root
314 // The next higher node is the parent of the first non-right
315 // child (ie either a left child or the root) up in the
316 // hierarchy. Root's parent is 0.
317 while (mCur->isRightChild())
318 mCur = mCur->getParent();
319 mCur = mCur->getParent();
320 }
321 }
322
323 // --
324 void Dec()
325 {
326 // Already at end?
327 if (mCur == nullptr)
328 return;
329
330 if (mCur->getLeftChild()) {
331 // If current node has a left child, the next lower node is the
332 // node with highest key beneath the left child.
333 mCur = maxNode(mCur->getLeftChild());
334 }
335 else if (mCur->isRightChild()) {
336 // No left child? Well if current node is a right child then
337 // the next lower node is the parent
338 mCur = mCur->getParent();
339 }
340 else {
341 // Current node neither is right child nor has a left child.
342 // Ie it is either left child or root
343 // The next higher node is the parent of the first non-left
344 // child (ie either a right child or the root) up in the
345 // hierarchy. Root's parent is 0.
346
347 while (mCur->isLeftChild())
348 mCur = mCur->getParent();
349 mCur = mCur->getParent();
350 }
351 }
352
353 Node* mRoot;
354 Node* mCur;
355 }; // Iterator
356
358 // Traverses the tree from top to bottom. Typical usage is
359 // when storing the tree structure, because when reading it
360 // later (and inserting elements) the tree structure will
361 // be the same.
363 {
364 public:
365 // Default constructor
367 : mRoot(nullptr)
368 , mCur(nullptr)
369 {}
370
371 // Constructor(Node*)
372 explicit ParentFirstIterator(Node* root)
373 : mRoot(root)
374 , mCur(nullptr)
375 {
376 reset();
377 }
378
379 void reset()
380 {
381 mCur = mRoot;
382 };
383
384 // Has the iterator reached the end?
385 bool atEnd() const
386 {
387 return mCur == nullptr;
388 }
390 {
391 return mCur;
392 }
393
394 // Assignment operator
396 {
397 mRoot = src.mRoot;
398 mCur = src.mCur;
399 return *this;
400 }
401
402 // Increment operator
403 void operator++(int)
404 {
405 Inc();
406 }
407
408 // Access operators
410 {
411 return getNode();
412 }
414 {
415 if (atEnd())
416 throw "ParentFirstIterator at end";
417 return *getNode();
418 }
419
420 private:
421 void Inc()
422 {
423 // Already at end?
424 if (mCur == nullptr)
425 return;
426
427 // First we try down to the left
428 if (mCur->getLeftChild()) {
429 mCur = mCur->getLeftChild();
430 }
431 else if (mCur->getRightChild()) {
432 // No left child? The we go down to the right.
433 mCur = mCur->getRightChild();
434 }
435 else {
436 // No children? Move up in the hierarcy until
437 // we either reach 0 (and are finished) or
438 // find a right uncle.
439 while (mCur != nullptr) {
440 // But if parent is left child and has a right "uncle" the parent
441 // has already been processed but the uncle hasn't. Move to
442 // the uncle.
443 if (mCur->isLeftChild() && mCur->getParent()->getRightChild()) {
444 mCur = mCur->getParent()->getRightChild();
445 return;
446 }
447 mCur = mCur->getParent();
448 }
449 }
450 }
451
452 Node* mRoot;
453 Node* mCur;
454
455 }; // ParentFirstIterator
456
458 // Typical usage is when deleting all elements in the tree
459 // because you must delete the children before you delete
460 // their parent.
462 {
463 public:
464 // Default constructor
466 : mRoot(nullptr)
467 , mCur(nullptr)
468 {}
469
470 // Constructor(Node*)
471 explicit ParentLastIterator(Node* root)
472 : mRoot(root)
473 , mCur(nullptr)
474 {
475 reset();
476 }
477
478 void reset()
479 {
480 mCur = minNode(mRoot);
481 }
482
483 // Has the iterator reached the end?
484 bool atEnd() const
485 {
486 return mCur == nullptr;
487 }
489 {
490 return mCur;
491 }
492
493 // Assignment operator
495 {
496 mRoot = src.mRoot;
497 mCur = src.mCur;
498 return *this;
499 }
500
501 // Increment operator
502 void operator++(int)
503 {
504 Inc();
505 }
506
507 // Access operators
509 {
510 return getNode();
511 }
513 {
514 if (atEnd())
515 throw "ParentLastIterator at end";
516 return *getNode();
517 }
518
519 private:
520 // minNode: Returns the node as far down to the left as possible.
521 Node* minNode(Node* n)
522 {
523 while (n != nullptr && (n->getLeftChild() != 0 || n->getRightChild() != 0)) {
524 n = (n->getLeftChild()) ? n->getLeftChild() : n->getRightChild();
525 }
526 return n;
527 }
528
529 void Inc()
530 {
531 // Already at end?
532 if (mCur == nullptr)
533 return;
534
535 // Note: Starting point is the node as far down to the left as possible.
536
537 // If current node has an uncle to the right, go to the
538 // node as far down to the left from the uncle as possible
539 // else just go up a level to the parent.
540 if (mCur->isLeftChild() && mCur->getParent()->getRightChild()) {
541 mCur = minNode(mCur->getParent()->getRightChild());
542 }
543 else {
544 mCur = mCur->getParent();
545 }
546 }
547
548 Node* mRoot;
549 Node* mCur;
550 }; // ParentLastIterator
551
553 // ByLevelIterator. Traverse tree top to bottom, level by level left to right.
554 // Typical usage : I don't know. Perhaps if the tree should be displayed
555 // in some fancy way.
556 // It is the most memory consuming of all iterators as it will allocate an
557 // array of (size()+1)/2 Node*s.
558 // Impl. desc:
559 // mArray[0] is the current node we're looking at, initially set to mRoot.
560 // When ++:ing the children of mArray[0] (if any) are put last in the
561 // array and the array is shifted down 1 step
563 {
564 public:
565 // Default constructor
567 : mRoot(nullptr)
568 , mArray(nullptr)
569 , mSize(0)
570 {}
571
572 // Constructor(treeRoot, elementsInTree)
573 ByLevelIterator(Node* root, unsigned int size)
574 : mRoot(root)
575 , mSize(size)
576 , mArray(nullptr)
577 {
578 reset();
579 }
580
581 // Copy constructor
583 : mRoot(src.mRoot)
584 , mSize(src.mSize)
585 , mEndPos(src.mEndPos)
586 {
587 if (src.mArray != nullptr) {
588 mArray = new Node*[(mSize + 1) / 2];
589 memcpy(mArray, src.mArray, sizeof(Node*) * (mSize + 1) / 2);
590 }
591 else
592 mArray = 0;
593 }
594
595 // Destructor
597 {
598 if (mArray != nullptr) {
599 delete[] mArray;
600 mArray = 0;
601 }
602 }
603
604 void reset()
605 {
606 if (mSize > 0) {
607 // Only allocate the first time reset is called
608 if (mArray == nullptr) {
609 // mArray must be able to hold the maximum "width" of the tree which
610 // at most can be (NumberOfNodesInTree + 1 ) / 2
611 mArray = new Node*[(mSize + 1) / 2];
612 }
613 // Initialize the array with 1 element, the mRoot.
614 mArray[0] = mRoot;
615 mEndPos = 1;
616 }
617 else
618 mEndPos = 0;
619 }
620
621 // Has the iterator reached the end?
622 bool atEnd() const
623 {
624 return mEndPos == 0;
625 }
627 {
628 return mArray[0];
629 }
630
631 // Assignment Operator
633 {
634 mRoot = src.mRoot;
635 mSize = src.mSize;
636 if (src.mArray != nullptr) {
637 mArray = new Node*[(mSize + 1) / 2];
638 memcpy(mArray, src.mArray, sizeof(Node*) * (mSize + 1) / 2);
639 }
640 else
641 mArray = 0;
642
643 return *this;
644 }
645
646 // Increment operator
647 void operator++(int)
648 {
649 Inc();
650 }
651
652 // Access operators
654 {
655 return getNode();
656 }
658 {
659 if (atEnd())
660 throw "ParentLastIterator at end";
661 return *getNode();
662 }
663
664 private:
665 void Inc()
666 {
667 if (mEndPos == 0)
668 return;
669
670 // Current node is mArray[0]
671 Node* pNode = mArray[0];
672
673 // Move the array down one notch, ie we have a new current node
674 // (the one than just was mArray[1])
675 for (unsigned int i = 0; i < mEndPos; i++) {
676 mArray[i] = mArray[i + 1];
677 }
678 mEndPos--;
679
680 Node* pChild = pNode->getLeftChild();
681 if (pChild) { // Append the left child of the former current node
682 mArray[mEndPos] = pChild;
683 mEndPos++;
684 }
685
686 pChild = pNode->getRightChild();
687 if (pChild) { // Append the right child of the former current node
688 mArray[mEndPos] = pChild;
689 mEndPos++;
690 }
691 }
692
693 Node** mArray;
694 Node* mRoot;
695 unsigned int mSize;
696 unsigned int mEndPos;
697 }; // ByLevelIterator
698
700 // It makes it possible to have different behavior in situations like:
701 // myTree["Foo"] = 32;
702 // If "Foo" already exist, just update its value else insert a new
703 // element.
704 // int i = myTree["Foo"]
705 // If "Foo" exists return its value, else throw an exception.
706 //
707 class AccessClass
708 {
709 // Let SbMap be the only one who can instantiate this class.
710 friend class SbMap<KeyType, ValueType>;
711
712 public:
713 // Assignment operator. Handles the myTree["Foo"] = 32; situation
714 operator=(const ValueType & value)
715 {
716 // Just use the set method, it handles already exist/not exist situation
717 mTree.set(mKey, value);
718 }
719
720 // ValueType operator
721 operator ValueType()
722 {
723 Node* node = mTree.find(mKey);
724
725 // Not found
726 if (node == nullptr) {
727 throw "Item not found";
728 }
729
730 return node->getValue();
731 }
732
733
734 private:
735 // Private Construction
736 AccessClass(SbMap& tree, const KeyType& key)
737 : mTree(tree)
738 , mKey(key)
739 {}
740
741 // Default constructor
742 AccessClass();
743
744 SbMap& mTree;
745 const KeyType& mKey;
746 }; // AccessClass
747
748
749 // Constructor.
751 : mRoot(nullptr)
752 , mSize(0)
753 {}
754
755 // Copy constructor
756 explicit SbMap(const SbMap& src)
757 : mRoot(nullptr)
758 , mSize(0)
759 {
760 *this = src;
761 }
762
763 // Destructor
765 {
766 clear();
767 }
768
769 // Assignment operator
770 SbMap& operator=(const SbMap& src)
771 {
772 clear();
773
774 for (ParentFirstIterator i(src.getParentFirstIterator()); !i.atEnd(); i++) {
775 set(i->getKey(), i->getValue());
776 }
777 }
778
779
780 bool insert(const KeyType& keyNew, const ValueType& v)
781 {
782 // First insert node the "usual" way (no fancy balance logic yet)
783 Node* newNode = new Node(keyNew, v);
784 if (!insert(newNode)) {
785 delete newNode;
786 return false;
787 }
788
789 // Then attend a balancing party
790 while (!newNode->isRoot() && (newNode->getParent()->isRed())) {
791 if (newNode->getParent()->isLeftChild()) {
792 // If newNode is a left child, get its right 'uncle'
793 Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
794 if (newNodesUncle != nullptr && newNodesUncle->isRed()) {
795 // case 1 - change the colours
796 newNode->getParent()->setBlack();
797 newNodesUncle->setBlack();
798 newNode->getParent()->getParent()->setRed();
799 // Move newNode up the tree
800 newNode = newNode->getParent()->getParent();
801 }
802 else {
803 // newNodesUncle is a black node
804 if (newNode->isRightChild()) {
805 // and newNode is to the right
806 // case 2 - move newNode up and rotate
807 newNode = newNode->getParent();
808 RotateLeft(newNode);
809 }
810 // case 3
811 newNode->getParent()->setBlack();
812 newNode->getParent()->getParent()->setRed();
813 RotateRight(newNode->getParent()->getParent());
814 }
815 }
816 else {
817 // If newNode is a right child, get its left 'uncle'
818 Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
819 if (newNodesUncle != nullptr && newNodesUncle->isRed()) {
820 // case 1 - change the colours
821 newNode->getParent()->setBlack();
822 newNodesUncle->setBlack();
823 newNode->getParent()->getParent()->setRed();
824 // Move newNode up the tree
825 newNode = newNode->getParent()->getParent();
826 }
827 else {
828 // newNodesUncle is a black node
829 if (newNode->isLeftChild()) {
830 // and newNode is to the left
831 // case 2 - move newNode up and rotate
832 newNode = newNode->getParent();
833 RotateRight(newNode);
834 }
835 // case 3
836 newNode->getParent()->setBlack();
837 newNode->getParent()->getParent()->setRed();
838 RotateLeft(newNode->getParent()->getParent());
839 }
840 }
841 }
842 // Color the root black
843 mRoot->setBlack();
844 return true;
845 }
846
847 // set. If the key already exist just replace the value
848 // else insert a new element.
849 void set(const KeyType& k, const ValueType& v)
850 {
851 Node* p = find(k);
852 if (p) {
853 p->setValue(v);
854 }
855 else
856 insert(k, v);
857 }
858
859 // Remove a node.Return true if the node could
860 // be found (and was removed) in the tree.
861 bool remove(const KeyType& k)
862 {
863 Node* p = find(k);
864 if (p == nullptr)
865 return false;
866
867 // Rotate p down to the left until it has no right child, will get there
868 // sooner or later.
869 while (p->getRightChild()) {
870 // "Pull up my right child and let it knock me down to the left"
871 RotateLeft(p);
872 }
873 // p now has no right child but might have a left child
874 Node* left = p->getLeftChild();
875
876 // Let p's parent point to p's child instead of point to p
877 if (p->isLeftChild()) {
878 p->getParent()->setLeftChild(left);
879 }
880 else if (p->isRightChild()) {
881 p->getParent()->setRightChild(left);
882 }
883 else {
884 // p has no parent => p is the root.
885 // Let the left child be the new root.
886 setRoot(left);
887 }
888
889 // p is now gone from the tree in the sense that
890 // no one is pointing at it. Let's get rid of it.
891 delete p;
892
893 mSize--;
894 return true;
895 }
896
897 // Wipe out the entire tree.
898 void clear()
899 {
901
902 while (!i.atEnd()) {
903 Node* p = i.getNode();
904 i++; // Increment it before it is deleted
905 // else iterator will get quite confused.
906 delete p;
907 }
908 mRoot = 0;
909 mSize = 0;
910 }
911
912 // Is the tree empty?
913 bool isEmpty() const
914 {
915 return mRoot == nullptr;
916 }
917
918 // Search for the node.
919 // Returns 0 if node couldn't be found.
920 Node* find(const KeyType& keyToFind) const
921 {
922 Node* pNode = mRoot;
923
924 while (pNode != nullptr) {
925 KeyType key(pNode->getKey());
926
927 if (keyToFind == key) {
928 // Found it! Return it! Wheee!
929 return pNode;
930 }
931 else if (keyToFind < key) {
932 pNode = pNode->getLeftChild();
933 }
934 else { // keyToFind > key
935 pNode = pNode->getRightChild();
936 }
937 }
938
939 return nullptr;
940 }
941
942 // Get the root element. 0 if tree is empty.
943 Node* getRoot() const
944 {
945 return mRoot;
946 }
947
948 // Number of nodes in the tree.
949 unsigned int size() const
950 {
951 return mSize;
952 }
953
955 {
956 Iterator it(getRoot());
957 return it;
958 }
970 {
972 return it;
973 }
974
975 // operator [] for access to elements
977 {
978 return AccessClass(*this, k);
979 }
980
981private:
982 void setRoot(Node* newRoot)
983 {
984 mRoot = newRoot;
985 if (mRoot != nullptr)
986 mRoot->setParent(0);
987 }
988
989 // insert a node into the tree without using any fancy balancing logic.
990 // Returns false if that key already exist in the tree.
991 bool insert(Node* newNode)
992 {
993 bool result = true; // Assume success
994
995 if (mRoot == nullptr) {
996 setRoot(newNode);
997 mSize = 1;
998 }
999 else {
1000 Node* pNode = mRoot;
1001 KeyType keyNew = newNode->getKey();
1002 while (pNode) {
1003 KeyType key(pNode->getKey());
1004
1005 if (keyNew == key) {
1006 result = false;
1007 pNode = 0;
1008 }
1009 else if (keyNew < key) {
1010 if (pNode->getLeftChild() == 0) {
1011 pNode->setLeftChild(newNode);
1012 pNode = 0;
1013 }
1014 else {
1015 pNode = pNode->getLeftChild();
1016 }
1017 }
1018 else {
1019 // keyNew > key
1020 if (pNode->getRightChild() == 0) {
1021 pNode->setRightChild(newNode);
1022 pNode = 0;
1023 }
1024 else {
1025 pNode = pNode->getRightChild();
1026 }
1027 }
1028 }
1029
1030 if (result) {
1031 mSize++;
1032 }
1033 }
1034
1035 return result;
1036 }
1037
1038 // Rotate left.
1039 // Pull up node's right child and let it knock node down to the left
1040 void RotateLeft(Node* p)
1041 {
1042 Node* right = p->getRightChild();
1043
1044 p->setRightChild(right->getLeftChild());
1045
1046 if (p->isLeftChild()) {
1047 p->getParent()->setLeftChild(right);
1048 }
1049 else if (p->isRightChild()) {
1050 p->getParent()->setRightChild(right);
1051 }
1052 else {
1053 setRoot(right);
1054 }
1055 right->setLeftChild(p);
1056 }
1057
1058 // Rotate right.
1059 // Pull up node's left child and let it knock node down to the right
1060 void RotateRight(Node* p)
1061 {
1062 Node* left = p->getLeftChild();
1063
1064 p->setLeftChild(left->getRightChild());
1065
1066 if (p->isLeftChild()) {
1067 p->getParent()->setLeftChild(left);
1068 }
1069 else if (p->isRightChild()) {
1070 p->getParent()->setRightChild(left);
1071 }
1072 else {
1073 setRoot(left);
1074 }
1075 left->setRightChild(p);
1076 }
1077
1078 Node* mRoot; // The top node. 0 if empty.
1079 unsigned int mSize; // Number of nodes in the tree
1080};
1081
1082#endif // _SB_MAP_
KeyType
SoKeyGrabber is a general facility to grab keyboard events.
Class SbMapItem is the element type of the SbMap tree.
Definition SbMap.h:67
SbMapItem * getParent() const
Definition SbMap.h:129
bool isRoot() const
Definition SbMap.h:152
const KeyType & getKey() const
Definition SbMap.h:146
KeyType getKey()
Definition SbMap.h:142
SbMapItem(const KeyType &k, const ValueType &v)
Definition SbMap.h:70
bool isBlack() const
Definition SbMap.h:177
bool isRightChild() const
Definition SbMap.h:160
void setLeftChild(SbMapItem *p)
Definition SbMap.h:86
void setRightChild(SbMapItem *p)
Definition SbMap.h:92
void setValue(const ValueType &v)
Definition SbMap.h:103
bool isLeftChild() const
Definition SbMap.h:156
unsigned int GetLevel() const
Definition SbMap.h:168
bool isRed() const
Definition SbMap.h:173
void setParent(SbMapItem *p)
Definition SbMap.h:98
virtual ~SbMapItem()
Definition SbMap.h:80
const ValueType & getValue() const
Definition SbMap.h:138
SbMapItem * getLeftChild() const
Definition SbMap.h:121
SbMapItem * getRightChild() const
Definition SbMap.h:125
ValueType getValue()
Definition SbMap.h:134
bool isLeaf() const
Definition SbMap.h:164
void setBlack()
Definition SbMap.h:114
void setRed()
Definition SbMap.h:110
AccessClass is a temporary class used with the [] operator on an SbMap.
Definition SbMap.h:708
operator=(const ValueType &value)
Definition SbMap.h:714
SbMap::ByLevelIterator for an SbMap, traversing the map top to bottom, level by level left to right....
Definition SbMap.h:563
ByLevelIterator(const ByLevelIterator &src)
Definition SbMap.h:582
Node * operator->()
Definition SbMap.h:653
bool atEnd() const
Definition SbMap.h:622
void operator++(int)
Definition SbMap.h:647
ByLevelIterator(Node *root, unsigned int size)
Definition SbMap.h:573
ByLevelIterator & operator=(const ByLevelIterator &src)
Definition SbMap.h:632
Regular low->high (++) and high->low (–) iterator class for an SbMap.
Definition SbMap.h:204
Iterator(Node *root)
Definition SbMap.h:213
Iterator(const Iterator &src)
Definition SbMap.h:220
void operator--(int)
Definition SbMap.h:258
Node & operator*()
Definition SbMap.h:268
bool atEnd() const
Definition SbMap.h:234
Iterator & operator=(const Iterator &src)
Definition SbMap.h:244
void reset(bool atLowest=true)
Definition SbMap.h:228
void operator++(int)
Definition SbMap.h:252
Node * operator->()
Definition SbMap.h:264
Node * getNode()
Definition SbMap.h:238
SbMap::ParentFirstIterator for an SbMap, traversing the map from top to bottom.
Definition SbMap.h:363
ParentFirstIterator(Node *root)
Definition SbMap.h:372
ParentFirstIterator & operator=(const ParentFirstIterator &src)
Definition SbMap.h:395
SbMap::ParentFirstIterator for an SbMap, traversing the map from bottom to top.
Definition SbMap.h:462
ParentLastIterator & operator=(const ParentLastIterator &src)
Definition SbMap.h:494
bool atEnd() const
Definition SbMap.h:484
ParentLastIterator(Node *root)
Definition SbMap.h:471
Open Inventor container that associates objects of type KeyType with objects of type ValueType.
Definition SbMap.h:198
Iterator getIterator()
Definition SbMap.h:954
SbMap(const SbMap &src)
Definition SbMap.h:756
ParentFirstIterator getParentFirstIterator()
Definition SbMap.h:959
ByLevelIterator getByLevelIterator()
Definition SbMap.h:969
void set(const KeyType &k, const ValueType &v)
Definition SbMap.h:849
~SbMap()
Definition SbMap.h:764
AccessClass operator[](const KeyType &k)
Definition SbMap.h:976
bool insert(const KeyType &keyNew, const ValueType &v)
Definition SbMap.h:780
SbMapItem< KeyType, ValueType > Node
Definition SbMap.h:200
ParentLastIterator getParentLastIterator()
Definition SbMap.h:964
unsigned int size() const
Definition SbMap.h:949
void clear()
Definition SbMap.h:898
Node * find(const KeyType &keyToFind) const
Definition SbMap.h:920
Node * getRoot() const
Definition SbMap.h:943
SbMap & operator=(const SbMap &src)
Definition SbMap.h:770
bool remove(const KeyType &k)
Definition SbMap.h:861
SbMap()
Definition SbMap.h:750
bool isEmpty() const
Definition SbMap.h:913