|
IrrlichtEngine
|
00001 // Copyright (C) 2006-2011 by Kat'Oun 00002 // This file is part of the "Irrlicht Engine". 00003 // For conditions of distribution and use, see copyright notice in irrlicht.h 00004 00005 #ifndef __IRR_MAP_H_INCLUDED__ 00006 #define __IRR_MAP_H_INCLUDED__ 00007 00008 #include "irrTypes.h" 00009 #include "irrMath.h" 00010 00011 namespace irr 00012 { 00013 namespace core 00014 { 00015 00017 template <class KeyType, class ValueType> 00018 class map 00019 { 00021 template <class KeyTypeRB, class ValueTypeRB> 00022 class RBTree 00023 { 00024 public: 00025 00026 RBTree(const KeyTypeRB& k, const ValueTypeRB& v) 00027 : LeftChild(0), RightChild(0), Parent(0), Key(k), 00028 Value(v), IsRed(true) {} 00029 00030 void setLeftChild(RBTree* p) 00031 { 00032 LeftChild=p; 00033 if (p) 00034 p->setParent(this); 00035 } 00036 00037 void setRightChild(RBTree* p) 00038 { 00039 RightChild=p; 00040 if (p) 00041 p->setParent(this); 00042 } 00043 00044 void setParent(RBTree* p) { Parent=p; } 00045 00046 void setValue(const ValueTypeRB& v) { Value = v; } 00047 00048 void setRed() { IsRed = true; } 00049 void setBlack() { IsRed = false; } 00050 00051 RBTree* getLeftChild() const { return LeftChild; } 00052 RBTree* getRightChild() const { return RightChild; } 00053 RBTree* getParent() const { return Parent; } 00054 00055 const ValueTypeRB& getValue() const 00056 { 00057 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00058 return Value; 00059 } 00060 00061 ValueTypeRB& getValue() 00062 { 00063 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00064 return Value; 00065 } 00066 00067 const KeyTypeRB& getKey() const 00068 { 00069 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00070 return Key; 00071 } 00072 00073 bool isRoot() const 00074 { 00075 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00076 return Parent==0; 00077 } 00078 00079 bool isLeftChild() const 00080 { 00081 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00082 return (Parent != 0) && (Parent->getLeftChild()==this); 00083 } 00084 00085 bool isRightChild() const 00086 { 00087 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00088 return (Parent!=0) && (Parent->getRightChild()==this); 00089 } 00090 00091 bool isLeaf() const 00092 { 00093 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00094 return (LeftChild==0) && (RightChild==0); 00095 } 00096 00097 unsigned int getLevel() const 00098 { 00099 if (isRoot()) 00100 return 1; 00101 else 00102 return getParent()->getLevel() + 1; 00103 } 00104 00105 00106 bool isRed() const 00107 { 00108 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00109 return IsRed; 00110 } 00111 00112 bool isBlack() const 00113 { 00114 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00115 return !IsRed; 00116 } 00117 00118 private: 00119 RBTree(); 00120 00121 RBTree* LeftChild; 00122 RBTree* RightChild; 00123 00124 RBTree* Parent; 00125 00126 KeyTypeRB Key; 00127 ValueTypeRB Value; 00128 00129 bool IsRed; 00130 }; // RBTree 00131 00132 public: 00133 00134 typedef RBTree<KeyType,ValueType> Node; 00135 00137 class Iterator 00138 { 00139 public: 00140 00141 Iterator() : Root(0), Cur(0) {} 00142 00143 // Constructor(Node*) 00144 Iterator(Node* root) : Root(root) 00145 { 00146 reset(); 00147 } 00148 00149 // Copy constructor 00150 Iterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {} 00151 00152 void reset(bool atLowest=true) 00153 { 00154 if (atLowest) 00155 Cur = getMin(Root); 00156 else 00157 Cur = getMax(Root); 00158 } 00159 00160 bool atEnd() const 00161 { 00162 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00163 return Cur==0; 00164 } 00165 00166 Node* getNode() const 00167 { 00168 return Cur; 00169 } 00170 00171 Iterator& operator=(const Iterator& src) 00172 { 00173 Root = src.Root; 00174 Cur = src.Cur; 00175 return (*this); 00176 } 00177 00178 void operator++(int) 00179 { 00180 inc(); 00181 } 00182 00183 void operator--(int) 00184 { 00185 dec(); 00186 } 00187 00188 Node* operator->() 00189 { 00190 return getNode(); 00191 } 00192 00193 Node& operator*() 00194 { 00195 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation 00196 00197 return *Cur; 00198 } 00199 00200 private: 00201 00202 Node* getMin(Node* n) const 00203 { 00204 while(n && n->getLeftChild()) 00205 n = n->getLeftChild(); 00206 return n; 00207 } 00208 00209 Node* getMax(Node* n) const 00210 { 00211 while(n && n->getRightChild()) 00212 n = n->getRightChild(); 00213 return n; 00214 } 00215 00216 void inc() 00217 { 00218 // Already at end? 00219 if (Cur==0) 00220 return; 00221 00222 if (Cur->getRightChild()) 00223 { 00224 // If current node has a right child, the next higher node is the 00225 // node with lowest key beneath the right child. 00226 Cur = getMin(Cur->getRightChild()); 00227 } 00228 else if (Cur->isLeftChild()) 00229 { 00230 // No right child? Well if current node is a left child then 00231 // the next higher node is the parent 00232 Cur = Cur->getParent(); 00233 } 00234 else 00235 { 00236 // Current node neither is left child nor has a right child. 00237 // Ie it is either right child or root 00238 // The next higher node is the parent of the first non-right 00239 // child (ie either a left child or the root) up in the 00240 // hierarchy. Root's parent is 0. 00241 while(Cur->isRightChild()) 00242 Cur = Cur->getParent(); 00243 Cur = Cur->getParent(); 00244 } 00245 } 00246 00247 void dec() 00248 { 00249 // Already at end? 00250 if (Cur==0) 00251 return; 00252 00253 if (Cur->getLeftChild()) 00254 { 00255 // If current node has a left child, the next lower node is the 00256 // node with highest key beneath the left child. 00257 Cur = getMax(Cur->getLeftChild()); 00258 } 00259 else if (Cur->isRightChild()) 00260 { 00261 // No left child? Well if current node is a right child then 00262 // the next lower node is the parent 00263 Cur = Cur->getParent(); 00264 } 00265 else 00266 { 00267 // Current node neither is right child nor has a left child. 00268 // Ie it is either left child or root 00269 // The next higher node is the parent of the first non-left 00270 // child (ie either a right child or the root) up in the 00271 // hierarchy. Root's parent is 0. 00272 00273 while(Cur->isLeftChild()) 00274 Cur = Cur->getParent(); 00275 Cur = Cur->getParent(); 00276 } 00277 } 00278 00279 Node* Root; 00280 Node* Cur; 00281 }; // Iterator 00282 00284 class ConstIterator 00285 { 00286 public: 00287 00288 ConstIterator() : Root(0), Cur(0) {} 00289 00290 // Constructor(Node*) 00291 ConstIterator(const Node* root) : Root(root) 00292 { 00293 reset(); 00294 } 00295 00296 // Copy constructor 00297 ConstIterator(const ConstIterator& src) : Root(src.Root), Cur(src.Cur) {} 00298 ConstIterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {} 00299 00300 void reset(bool atLowest=true) 00301 { 00302 if (atLowest) 00303 Cur = getMin(Root); 00304 else 00305 Cur = getMax(Root); 00306 } 00307 00308 bool atEnd() const 00309 { 00310 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00311 return Cur==0; 00312 } 00313 00314 const Node* getNode() const 00315 { 00316 return Cur; 00317 } 00318 00319 ConstIterator& operator=(const ConstIterator& src) 00320 { 00321 Root = src.Root; 00322 Cur = src.Cur; 00323 return (*this); 00324 } 00325 00326 void operator++(int) 00327 { 00328 inc(); 00329 } 00330 00331 void operator--(int) 00332 { 00333 dec(); 00334 } 00335 00336 const Node* operator->() 00337 { 00338 return getNode(); 00339 } 00340 00341 const Node& operator*() 00342 { 00343 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation 00344 00345 return *Cur; 00346 } 00347 00348 private: 00349 00350 const Node* getMin(const Node* n) const 00351 { 00352 while(n && n->getLeftChild()) 00353 n = n->getLeftChild(); 00354 return n; 00355 } 00356 00357 const Node* getMax(const Node* n) const 00358 { 00359 while(n && n->getRightChild()) 00360 n = n->getRightChild(); 00361 return n; 00362 } 00363 00364 void inc() 00365 { 00366 // Already at end? 00367 if (Cur==0) 00368 return; 00369 00370 if (Cur->getRightChild()) 00371 { 00372 // If current node has a right child, the next higher node is the 00373 // node with lowest key beneath the right child. 00374 Cur = getMin(Cur->getRightChild()); 00375 } 00376 else if (Cur->isLeftChild()) 00377 { 00378 // No right child? Well if current node is a left child then 00379 // the next higher node is the parent 00380 Cur = Cur->getParent(); 00381 } 00382 else 00383 { 00384 // Current node neither is left child nor has a right child. 00385 // Ie it is either right child or root 00386 // The next higher node is the parent of the first non-right 00387 // child (ie either a left child or the root) up in the 00388 // hierarchy. Root's parent is 0. 00389 while(Cur->isRightChild()) 00390 Cur = Cur->getParent(); 00391 Cur = Cur->getParent(); 00392 } 00393 } 00394 00395 void dec() 00396 { 00397 // Already at end? 00398 if (Cur==0) 00399 return; 00400 00401 if (Cur->getLeftChild()) 00402 { 00403 // If current node has a left child, the next lower node is the 00404 // node with highest key beneath the left child. 00405 Cur = getMax(Cur->getLeftChild()); 00406 } 00407 else if (Cur->isRightChild()) 00408 { 00409 // No left child? Well if current node is a right child then 00410 // the next lower node is the parent 00411 Cur = Cur->getParent(); 00412 } 00413 else 00414 { 00415 // Current node neither is right child nor has a left child. 00416 // Ie it is either left child or root 00417 // The next higher node is the parent of the first non-left 00418 // child (ie either a right child or the root) up in the 00419 // hierarchy. Root's parent is 0. 00420 00421 while(Cur->isLeftChild()) 00422 Cur = Cur->getParent(); 00423 Cur = Cur->getParent(); 00424 } 00425 } 00426 00427 const Node* Root; 00428 const Node* Cur; 00429 }; // ConstIterator 00430 00431 00433 00437 class ParentFirstIterator 00438 { 00439 public: 00440 00441 ParentFirstIterator() : Root(0), Cur(0) {} 00442 00443 explicit ParentFirstIterator(Node* root) : Root(root), Cur(0) 00444 { 00445 reset(); 00446 } 00447 00448 void reset() 00449 { 00450 Cur = Root; 00451 } 00452 00453 bool atEnd() const 00454 { 00455 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00456 return Cur==0; 00457 } 00458 00459 Node* getNode() 00460 { 00461 return Cur; 00462 } 00463 00464 ParentFirstIterator& operator=(const ParentFirstIterator& src) 00465 { 00466 Root = src.Root; 00467 Cur = src.Cur; 00468 return (*this); 00469 } 00470 00471 void operator++(int) 00472 { 00473 inc(); 00474 } 00475 00476 Node* operator -> () 00477 { 00478 return getNode(); 00479 } 00480 00481 Node& operator* () 00482 { 00483 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation 00484 00485 return *getNode(); 00486 } 00487 00488 private: 00489 00490 void inc() 00491 { 00492 // Already at end? 00493 if (Cur==0) 00494 return; 00495 00496 // First we try down to the left 00497 if (Cur->getLeftChild()) 00498 { 00499 Cur = Cur->getLeftChild(); 00500 } 00501 else if (Cur->getRightChild()) 00502 { 00503 // No left child? The we go down to the right. 00504 Cur = Cur->getRightChild(); 00505 } 00506 else 00507 { 00508 // No children? Move up in the hierarcy until 00509 // we either reach 0 (and are finished) or 00510 // find a right uncle. 00511 while (Cur!=0) 00512 { 00513 // But if parent is left child and has a right "uncle" the parent 00514 // has already been processed but the uncle hasn't. Move to 00515 // the uncle. 00516 if (Cur->isLeftChild() && Cur->getParent()->getRightChild()) 00517 { 00518 Cur = Cur->getParent()->getRightChild(); 00519 return; 00520 } 00521 Cur = Cur->getParent(); 00522 } 00523 } 00524 } 00525 00526 Node* Root; 00527 Node* Cur; 00528 00529 }; // ParentFirstIterator 00530 00531 00533 00537 class ParentLastIterator 00538 { 00539 public: 00540 00541 ParentLastIterator() : Root(0), Cur(0) {} 00542 00543 explicit ParentLastIterator(Node* root) : Root(root), Cur(0) 00544 { 00545 reset(); 00546 } 00547 00548 void reset() 00549 { 00550 Cur = getMin(Root); 00551 } 00552 00553 bool atEnd() const 00554 { 00555 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00556 return Cur==0; 00557 } 00558 00559 Node* getNode() 00560 { 00561 return Cur; 00562 } 00563 00564 ParentLastIterator& operator=(const ParentLastIterator& src) 00565 { 00566 Root = src.Root; 00567 Cur = src.Cur; 00568 return (*this); 00569 } 00570 00571 void operator++(int) 00572 { 00573 inc(); 00574 } 00575 00576 Node* operator -> () 00577 { 00578 return getNode(); 00579 } 00580 00581 Node& operator* () 00582 { 00583 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation 00584 00585 return *getNode(); 00586 } 00587 private: 00588 00589 Node* getMin(Node* n) 00590 { 00591 while(n!=0 && (n->getLeftChild()!=0 || n->getRightChild()!=0)) 00592 { 00593 if (n->getLeftChild()) 00594 n = n->getLeftChild(); 00595 else 00596 n = n->getRightChild(); 00597 } 00598 return n; 00599 } 00600 00601 void inc() 00602 { 00603 // Already at end? 00604 if (Cur==0) 00605 return; 00606 00607 // Note: Starting point is the node as far down to the left as possible. 00608 00609 // If current node has an uncle to the right, go to the 00610 // node as far down to the left from the uncle as possible 00611 // else just go up a level to the parent. 00612 if (Cur->isLeftChild() && Cur->getParent()->getRightChild()) 00613 { 00614 Cur = getMin(Cur->getParent()->getRightChild()); 00615 } 00616 else 00617 Cur = Cur->getParent(); 00618 } 00619 00620 Node* Root; 00621 Node* Cur; 00622 }; // ParentLastIterator 00623 00624 00625 // AccessClass is a temporary class used with the [] operator. 00626 // It makes it possible to have different behavior in situations like: 00627 // myTree["Foo"] = 32; 00628 // If "Foo" already exists update its value else insert a new element. 00629 // int i = myTree["Foo"] 00630 // If "Foo" exists return its value. 00631 class AccessClass 00632 { 00633 // Let map be the only one who can instantiate this class. 00634 friend class map<KeyType, ValueType>; 00635 00636 public: 00637 00638 // Assignment operator. Handles the myTree["Foo"] = 32; situation 00639 void operator=(const ValueType& value) 00640 { 00641 // Just use the Set method, it handles already exist/not exist situation 00642 Tree.set(Key,value); 00643 } 00644 00645 // ValueType operator 00646 operator ValueType() 00647 { 00648 Node* node = Tree.find(Key); 00649 00650 // Not found 00651 _IRR_DEBUG_BREAK_IF(node==0) // access violation 00652 00653 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00654 return node->getValue(); 00655 } 00656 00657 private: 00658 00659 AccessClass(map& tree, const KeyType& key) : Tree(tree), Key(key) {} 00660 00661 AccessClass(); 00662 00663 map& Tree; 00664 const KeyType& Key; 00665 }; // AccessClass 00666 00667 00668 // Constructor. 00669 map() : Root(0), Size(0) {} 00670 00671 // Destructor 00672 ~map() 00673 { 00674 clear(); 00675 } 00676 00677 //------------------------------ 00678 // Public Commands 00679 //------------------------------ 00680 00682 00685 bool insert(const KeyType& keyNew, const ValueType& v) 00686 { 00687 // First insert node the "usual" way (no fancy balance logic yet) 00688 Node* newNode = new Node(keyNew,v); 00689 if (!insert(newNode)) 00690 { 00691 delete newNode; 00692 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00693 return false; 00694 } 00695 00696 // Then attend a balancing party 00697 while (!newNode->isRoot() && (newNode->getParent()->isRed())) 00698 { 00699 if (newNode->getParent()->isLeftChild()) 00700 { 00701 // If newNode is a left child, get its right 'uncle' 00702 Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild(); 00703 if ( newNodesUncle!=0 && newNodesUncle->isRed()) 00704 { 00705 // case 1 - change the colours 00706 newNode->getParent()->setBlack(); 00707 newNodesUncle->setBlack(); 00708 newNode->getParent()->getParent()->setRed(); 00709 // Move newNode up the tree 00710 newNode = newNode->getParent()->getParent(); 00711 } 00712 else 00713 { 00714 // newNodesUncle is a black node 00715 if ( newNode->isRightChild()) 00716 { 00717 // and newNode is to the right 00718 // case 2 - move newNode up and rotate 00719 newNode = newNode->getParent(); 00720 rotateLeft(newNode); 00721 } 00722 // case 3 00723 newNode->getParent()->setBlack(); 00724 newNode->getParent()->getParent()->setRed(); 00725 rotateRight(newNode->getParent()->getParent()); 00726 } 00727 } 00728 else 00729 { 00730 // If newNode is a right child, get its left 'uncle' 00731 Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild(); 00732 if ( newNodesUncle!=0 && newNodesUncle->isRed()) 00733 { 00734 // case 1 - change the colours 00735 newNode->getParent()->setBlack(); 00736 newNodesUncle->setBlack(); 00737 newNode->getParent()->getParent()->setRed(); 00738 // Move newNode up the tree 00739 newNode = newNode->getParent()->getParent(); 00740 } 00741 else 00742 { 00743 // newNodesUncle is a black node 00744 if (newNode->isLeftChild()) 00745 { 00746 // and newNode is to the left 00747 // case 2 - move newNode up and rotate 00748 newNode = newNode->getParent(); 00749 rotateRight(newNode); 00750 } 00751 // case 3 00752 newNode->getParent()->setBlack(); 00753 newNode->getParent()->getParent()->setRed(); 00754 rotateLeft(newNode->getParent()->getParent()); 00755 } 00756 00757 } 00758 } 00759 // Color the root black 00760 Root->setBlack(); 00761 return true; 00762 } 00763 00765 00767 void set(const KeyType& k, const ValueType& v) 00768 { 00769 Node* p = find(k); 00770 if (p) 00771 p->setValue(v); 00772 else 00773 insert(k,v); 00774 } 00775 00777 00780 Node* delink(const KeyType& k) 00781 { 00782 Node* p = find(k); 00783 if (p == 0) 00784 return 0; 00785 00786 // Rotate p down to the left until it has no right child, will get there 00787 // sooner or later. 00788 while(p->getRightChild()) 00789 { 00790 // "Pull up my right child and let it knock me down to the left" 00791 rotateLeft(p); 00792 } 00793 // p now has no right child but might have a left child 00794 Node* left = p->getLeftChild(); 00795 00796 // Let p's parent point to p's child instead of point to p 00797 if (p->isLeftChild()) 00798 p->getParent()->setLeftChild(left); 00799 00800 else if (p->isRightChild()) 00801 p->getParent()->setRightChild(left); 00802 00803 else 00804 { 00805 // p has no parent => p is the root. 00806 // Let the left child be the new root. 00807 setRoot(left); 00808 } 00809 00810 // p is now gone from the tree in the sense that 00811 // no one is pointing at it, so return it. 00812 00813 --Size; 00814 return p; 00815 } 00816 00818 00819 bool remove(const KeyType& k) 00820 { 00821 Node* p = find(k); 00822 return remove(p); 00823 } 00824 00826 00827 bool remove(Node* p) 00828 { 00829 if (p == 0) 00830 { 00831 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00832 return false; 00833 } 00834 00835 // Rotate p down to the left until it has no right child, will get there 00836 // sooner or later. 00837 while(p->getRightChild()) 00838 { 00839 // "Pull up my right child and let it knock me down to the left" 00840 rotateLeft(p); 00841 } 00842 // p now has no right child but might have a left child 00843 Node* left = p->getLeftChild(); 00844 00845 // Let p's parent point to p's child instead of point to p 00846 if (p->isLeftChild()) 00847 p->getParent()->setLeftChild(left); 00848 00849 else if (p->isRightChild()) 00850 p->getParent()->setRightChild(left); 00851 00852 else 00853 { 00854 // p has no parent => p is the root. 00855 // Let the left child be the new root. 00856 setRoot(left); 00857 } 00858 00859 // p is now gone from the tree in the sense that 00860 // no one is pointing at it. Let's get rid of it. 00861 delete p; 00862 00863 --Size; 00864 return true; 00865 } 00866 00868 void clear() 00869 { 00870 ParentLastIterator i(getParentLastIterator()); 00871 00872 while(!i.atEnd()) 00873 { 00874 Node* p = i.getNode(); 00875 i++; // Increment it before it is deleted 00876 // else iterator will get quite confused. 00877 delete p; 00878 } 00879 Root = 0; 00880 Size= 0; 00881 } 00882 00885 bool empty() const 00886 { 00887 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 00888 return Root == 0; 00889 } 00890 00892 _IRR_DEPRECATED_ bool isEmpty() const 00893 { 00894 return empty(); 00895 } 00896 00900 Node* find(const KeyType& keyToFind) const 00901 { 00902 Node* pNode = Root; 00903 00904 while(pNode!=0) 00905 { 00906 KeyType key(pNode->getKey()); 00907 00908 if (keyToFind == key) 00909 return pNode; 00910 else if (keyToFind < key) 00911 pNode = pNode->getLeftChild(); 00912 else //keyToFind > key 00913 pNode = pNode->getRightChild(); 00914 } 00915 00916 return 0; 00917 } 00918 00922 Node* getRoot() const 00923 { 00924 return Root; 00925 } 00926 00928 u32 size() const 00929 { 00930 return Size; 00931 } 00932 00934 00938 void swap(map<KeyType, ValueType>& other) 00939 { 00940 core::swap(Root, other.Root); 00941 core::swap(Size, other.Size); 00942 } 00943 00944 //------------------------------ 00945 // Public Iterators 00946 //------------------------------ 00947 00949 Iterator getIterator() const 00950 { 00951 Iterator it(getRoot()); 00952 return it; 00953 } 00954 00956 ConstIterator getConstIterator() const 00957 { 00958 Iterator it(getRoot()); 00959 return it; 00960 } 00961 00967 ParentFirstIterator getParentFirstIterator() const 00968 { 00969 ParentFirstIterator it(getRoot()); 00970 return it; 00971 } 00972 00978 ParentLastIterator getParentLastIterator() const 00979 { 00980 ParentLastIterator it(getRoot()); 00981 return it; 00982 } 00983 00984 //------------------------------ 00985 // Public Operators 00986 //------------------------------ 00987 00989 00990 AccessClass operator[](const KeyType& k) 00991 { 00992 return AccessClass(*this, k); 00993 } 00994 private: 00995 00996 //------------------------------ 00997 // Disabled methods 00998 //------------------------------ 00999 // Copy constructor and assignment operator deliberately 01000 // defined but not implemented. The tree should never be 01001 // copied, pass along references to it instead. 01002 explicit map(const map& src); 01003 map& operator = (const map& src); 01004 01006 01010 void setRoot(Node* newRoot) 01011 { 01012 Root = newRoot; 01013 if (Root != 0) 01014 { 01015 Root->setParent(0); 01016 Root->setBlack(); 01017 } 01018 } 01019 01021 01022 bool insert(Node* newNode) 01023 { 01024 bool result=true; // Assume success 01025 01026 if (Root==0) 01027 { 01028 setRoot(newNode); 01029 Size = 1; 01030 } 01031 else 01032 { 01033 Node* pNode = Root; 01034 KeyType keyNew = newNode->getKey(); 01035 while (pNode) 01036 { 01037 KeyType key(pNode->getKey()); 01038 01039 if (keyNew == key) 01040 { 01041 result = false; 01042 pNode = 0; 01043 } 01044 else if (keyNew < key) 01045 { 01046 if (pNode->getLeftChild() == 0) 01047 { 01048 pNode->setLeftChild(newNode); 01049 pNode = 0; 01050 } 01051 else 01052 pNode = pNode->getLeftChild(); 01053 } 01054 else // keyNew > key 01055 { 01056 if (pNode->getRightChild()==0) 01057 { 01058 pNode->setRightChild(newNode); 01059 pNode = 0; 01060 } 01061 else 01062 { 01063 pNode = pNode->getRightChild(); 01064 } 01065 } 01066 } 01067 01068 if (result) 01069 ++Size; 01070 } 01071 01072 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 01073 return result; 01074 } 01075 01078 void rotateLeft(Node* p) 01079 { 01080 Node* right = p->getRightChild(); 01081 01082 p->setRightChild(right->getLeftChild()); 01083 01084 if (p->isLeftChild()) 01085 p->getParent()->setLeftChild(right); 01086 else if (p->isRightChild()) 01087 p->getParent()->setRightChild(right); 01088 else 01089 setRoot(right); 01090 01091 right->setLeftChild(p); 01092 } 01093 01096 void rotateRight(Node* p) 01097 { 01098 Node* left = p->getLeftChild(); 01099 01100 p->setLeftChild(left->getRightChild()); 01101 01102 if (p->isLeftChild()) 01103 p->getParent()->setLeftChild(left); 01104 else if (p->isRightChild()) 01105 p->getParent()->setRightChild(left); 01106 else 01107 setRoot(left); 01108 01109 left->setRightChild(p); 01110 } 01111 01112 //------------------------------ 01113 // Private Members 01114 //------------------------------ 01115 Node* Root; // The top node. 0 if empty. 01116 u32 Size; // Number of nodes in the tree 01117 }; 01118 01119 } // end namespace core 01120 } // end namespace irr 01121 01122 #endif // __IRR_MAP_H_INCLUDED__ 01123