00001
00002
00003
00004
00005 #ifndef __IRR_MAP_H_INCLUDED__
00006 #define __IRR_MAP_H_INCLUDED__
00007
00008 #include "irrTypes.h"
00009
00010 namespace irr
00011 {
00012 namespace core
00013 {
00014
00016 template <class KeyType, class ValueType>
00017 class map
00018 {
00020 template <class KeyTypeRB, class ValueTypeRB>
00021 class RBTree
00022 {
00023 public:
00024
00025 RBTree(const KeyTypeRB& k, const ValueTypeRB& v)
00026 : LeftChild(0), RightChild(0), Parent(0), Key(k),
00027 Value(v), IsRed(true) {}
00028
00029 ~RBTree() {}
00030
00031 void setLeftChild(RBTree* p)
00032 {
00033 LeftChild=p;
00034 if (p)
00035 p->setParent(this);
00036 }
00037
00038 void setRightChild(RBTree* p)
00039 {
00040 RightChild=p;
00041 if (p)
00042 p->setParent(this);
00043 }
00044
00045 void setParent(RBTree* p) { Parent=p; }
00046
00047 void setValue(const ValueTypeRB& v) { Value = v; }
00048
00049 void setRed() { IsRed = true; }
00050 void setBlack() { IsRed = false; }
00051
00052 RBTree* getLeftChild() const { return LeftChild; }
00053 RBTree* getRightChild() const { return RightChild; }
00054 RBTree* getParent() const { return Parent; }
00055
00056 ValueTypeRB getValue() const
00057 {
00058 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00059 return Value;
00060 }
00061
00062 KeyTypeRB getKey() const
00063 {
00064 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00065 return Key;
00066 }
00067
00068 bool isRoot() const
00069 {
00070 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00071 return Parent==0;
00072 }
00073
00074 bool isLeftChild() const
00075 {
00076 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00077 return (Parent != 0) && (Parent->getLeftChild()==this);
00078 }
00079
00080 bool isRightChild() const
00081 {
00082 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00083 return (Parent!=0) && (Parent->getRightChild()==this);
00084 }
00085
00086 bool isLeaf() const
00087 {
00088 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00089 return (LeftChild==0) && (RightChild==0);
00090 }
00091
00092 unsigned int getLevel() const
00093 {
00094 if (isRoot())
00095 return 1;
00096 else
00097 return getParent()->getLevel() + 1;
00098 }
00099
00100
00101 bool isRed() const
00102 {
00103 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00104 return IsRed;
00105 }
00106
00107 bool isBlack() const
00108 {
00109 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00110 return !IsRed;
00111 }
00112
00113 private:
00114 RBTree();
00115
00116 RBTree* LeftChild;
00117 RBTree* RightChild;
00118
00119 RBTree* Parent;
00120
00121 KeyTypeRB Key;
00122 ValueTypeRB Value;
00123
00124 bool IsRed;
00125 };
00126
00127 public:
00128
00129 typedef RBTree<KeyType,ValueType> Node;
00130
00132 class Iterator
00133 {
00134 public:
00135
00136 Iterator() : Root(0), Cur(0) {}
00137
00138
00139 Iterator(Node* root) : Root(root)
00140 {
00141 reset();
00142 }
00143
00144
00145 Iterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
00146
00147 void reset(bool atLowest=true)
00148 {
00149 if (atLowest)
00150 Cur = getMin(Root);
00151 else
00152 Cur = getMax(Root);
00153 }
00154
00155 bool atEnd() const
00156 {
00157 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00158 return Cur==0;
00159 }
00160
00161 Node* getNode()
00162 {
00163 return Cur;
00164 }
00165
00166 Iterator& operator=(const Iterator& src)
00167 {
00168 Root = src.Root;
00169 Cur = src.Cur;
00170 return (*this);
00171 }
00172
00173 void operator++(int)
00174 {
00175 inc();
00176 }
00177
00178 void operator--(int)
00179 {
00180 dec();
00181 }
00182
00183
00184 Node* operator -> ()
00185 {
00186 return getNode();
00187 }
00188
00189 Node& operator* ()
00190 {
00191 if (atEnd())
00192 throw "Iterator at end";
00193
00194 return *Cur;
00195 }
00196
00197 private:
00198
00199 Node* getMin(Node* n)
00200 {
00201 while(n && n->getLeftChild())
00202 n = n->getLeftChild();
00203 return n;
00204 }
00205
00206 Node* getMax(Node* n)
00207 {
00208 while(n && n->getRightChild())
00209 n = n->getRightChild();
00210 return n;
00211 }
00212
00213 void inc()
00214 {
00215
00216 if (Cur==0)
00217 return;
00218
00219 if (Cur->getRightChild())
00220 {
00221
00222
00223 Cur = getMin(Cur->getRightChild());
00224 }
00225 else if (Cur->isLeftChild())
00226 {
00227
00228
00229 Cur = Cur->getParent();
00230 }
00231 else
00232 {
00233
00234
00235
00236
00237
00238 while(Cur->isRightChild())
00239 Cur = Cur->getParent();
00240 Cur = Cur->getParent();
00241 }
00242 }
00243
00244 void dec()
00245 {
00246
00247 if (Cur==0)
00248 return;
00249
00250 if (Cur->getLeftChild())
00251 {
00252
00253
00254 Cur = getMax(Cur->getLeftChild());
00255 }
00256 else if (Cur->isRightChild())
00257 {
00258
00259
00260 Cur = Cur->getParent();
00261 }
00262 else
00263 {
00264
00265
00266
00267
00268
00269
00270 while(Cur->isLeftChild())
00271 Cur = Cur->getParent();
00272 Cur = Cur->getParent();
00273 }
00274 }
00275
00276 Node* Root;
00277 Node* Cur;
00278 };
00279
00280
00281
00287 class ParentFirstIterator
00288 {
00289 public:
00290
00291
00292 ParentFirstIterator() : Root(0), Cur(0)
00293 {
00294 }
00295
00296
00297 explicit ParentFirstIterator(Node* root) : Root(root), Cur(0)
00298 {
00299 reset();
00300 }
00301
00302 void reset()
00303 {
00304 Cur = Root;
00305 }
00306
00307
00308 bool atEnd() const
00309 {
00310 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00311 return Cur==0;
00312 }
00313
00314 Node* getNode()
00315 {
00316 return Cur;
00317 }
00318
00319
00320 ParentFirstIterator& operator=(const ParentFirstIterator& src)
00321 {
00322 Root = src.Root;
00323 Cur = src.Cur;
00324 return (*this);
00325 }
00326
00327
00328 void operator++(int)
00329 {
00330 inc();
00331 }
00332
00333
00334 Node* operator -> ()
00335 {
00336 return getNode();
00337 }
00338
00339 Node& operator* ()
00340 {
00341 if (atEnd())
00342 throw "ParentFirstIterator at end";
00343 return *getNode();
00344 }
00345
00346 private:
00347
00348 void inc()
00349 {
00350
00351 if (Cur==0)
00352 return;
00353
00354
00355 if (Cur->getLeftChild())
00356 {
00357 Cur = Cur->getLeftChild();
00358 }
00359 else if (Cur->getRightChild())
00360 {
00361
00362 Cur = Cur->getRightChild();
00363 }
00364 else
00365 {
00366
00367
00368
00369 while (Cur!=0)
00370 {
00371
00372
00373
00374 if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
00375 {
00376 Cur = Cur->getParent()->getRightChild();
00377 return;
00378 }
00379 Cur = Cur->getParent();
00380 }
00381 }
00382 }
00383
00384 Node* Root;
00385 Node* Cur;
00386
00387 };
00388
00389
00395 class ParentLastIterator
00396 {
00397 public:
00398
00399 ParentLastIterator() : Root(0), Cur(0) {}
00400
00401 explicit ParentLastIterator(Node* root) : Root(root), Cur(0)
00402 {
00403 reset();
00404 }
00405
00406 void reset()
00407 {
00408 Cur = getMin(Root);
00409 }
00410
00411 bool atEnd() const
00412 {
00413 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00414 return Cur==0;
00415 }
00416
00417 Node* getNode()
00418 {
00419 return Cur;
00420 }
00421
00422 ParentLastIterator& operator=(const ParentLastIterator& src)
00423 {
00424 Root = src.Root;
00425 Cur = src.Cur;
00426 return (*this);
00427 }
00428
00429 void operator++(int)
00430 {
00431 inc();
00432 }
00433
00434 Node* operator -> ()
00435 {
00436 return getNode();
00437 }
00438
00439 Node& operator* ()
00440 {
00441 if (atEnd())
00442 throw "ParentLastIterator at end";
00443 return *getNode();
00444 }
00445 private:
00446
00447 Node* getMin(Node* n)
00448 {
00449 while(n!=0 && (n->getLeftChild()!=0 || n->getRightChild()!=0))
00450 {
00451 if (n->getLeftChild())
00452 n = n->getLeftChild();
00453 else
00454 n = n->getRightChild();
00455 }
00456 return n;
00457 }
00458
00459 void inc()
00460 {
00461
00462 if (Cur==0)
00463 return;
00464
00465
00466
00467
00468
00469
00470 if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
00471 {
00472 Cur = getMin(Cur->getParent()->getRightChild());
00473 }
00474 else
00475 Cur = Cur->getParent();
00476 }
00477
00478
00479 Node* Root;
00480 Node* Cur;
00481 };
00482
00483
00484
00485
00486
00487
00488
00489
00490 class AccessClass
00491 {
00492
00493 friend class map<KeyType, ValueType>;
00494
00495 public:
00496
00497
00498 void operator=(const ValueType& value)
00499 {
00500
00501 Tree.set(Key,value);
00502 }
00503
00504
00505 operator ValueType()
00506 {
00507 Node* node = Tree.find(Key);
00508
00509
00510 if (node==0)
00511 throw "Item not found";
00512
00513 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00514 return node->getValue();
00515 }
00516
00517 private:
00518
00519 AccessClass(map& tree, const KeyType& key) : Tree(tree), Key(key) {}
00520
00521 AccessClass();
00522
00523 map& Tree;
00524 const KeyType& Key;
00525 };
00526
00527
00528
00529 map() : Root(0), Size(0) {}
00530
00531
00532 ~map()
00533 {
00534 clear();
00535 }
00536
00537
00538
00539
00540
00546 bool insert(const KeyType& keyNew, const ValueType& v)
00547 {
00548
00549 Node* newNode = new Node(keyNew,v);
00550 if (!insert(newNode))
00551 {
00552 delete newNode;
00553 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00554 return false;
00555 }
00556
00557
00558 while (!newNode->isRoot() && (newNode->getParent()->isRed()))
00559 {
00560 if (newNode->getParent()->isLeftChild())
00561 {
00562
00563 Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
00564 if ( newNodesUncle!=0 && newNodesUncle->isRed())
00565 {
00566
00567 newNode->getParent()->setBlack();
00568 newNodesUncle->setBlack();
00569 newNode->getParent()->getParent()->setRed();
00570
00571 newNode = newNode->getParent()->getParent();
00572 }
00573 else
00574 {
00575
00576 if ( newNode->isRightChild())
00577 {
00578
00579
00580 newNode = newNode->getParent();
00581 rotateLeft(newNode);
00582 }
00583
00584 newNode->getParent()->setBlack();
00585 newNode->getParent()->getParent()->setRed();
00586 rotateRight(newNode->getParent()->getParent());
00587 }
00588 }
00589 else
00590 {
00591
00592 Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
00593 if ( newNodesUncle!=0 && newNodesUncle->isRed())
00594 {
00595
00596 newNode->getParent()->setBlack();
00597 newNodesUncle->setBlack();
00598 newNode->getParent()->getParent()->setRed();
00599
00600 newNode = newNode->getParent()->getParent();
00601 }
00602 else
00603 {
00604
00605 if (newNode->isLeftChild())
00606 {
00607
00608
00609 newNode = newNode->getParent();
00610 rotateRight(newNode);
00611 }
00612
00613 newNode->getParent()->setBlack();
00614 newNode->getParent()->getParent()->setRed();
00615 rotateLeft(newNode->getParent()->getParent());
00616 }
00617
00618 }
00619 }
00620
00621 Root->setBlack();
00622 return true;
00623 }
00624
00629 void set(const KeyType& k, const ValueType& v)
00630 {
00631 Node* p = find(k);
00632 if (p)
00633 p->setValue(v);
00634 else
00635 insert(k,v);
00636 }
00637
00642 Node* delink(const KeyType& k)
00643 {
00644 Node* p = find(k);
00645 if (p == 0)
00646 return 0;
00647
00648
00649
00650 while(p->getRightChild())
00651 {
00652
00653 rotateLeft(p);
00654 }
00655
00656 Node* left = p->getLeftChild();
00657
00658
00659 if (p->isLeftChild())
00660 p->getParent()->setLeftChild(left);
00661
00662 else if (p->isRightChild())
00663 p->getParent()->setRightChild(left);
00664
00665 else
00666 {
00667
00668
00669 setRoot(left);
00670 }
00671
00672
00673
00674
00675 --Size;
00676 return p;
00677 }
00678
00681 bool remove(const KeyType& k)
00682 {
00683 Node* p = find(k);
00684 if (p == 0)
00685 {
00686 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00687 return false;
00688 }
00689
00690
00691
00692 while(p->getRightChild())
00693 {
00694
00695 rotateLeft(p);
00696 }
00697
00698 Node* left = p->getLeftChild();
00699
00700
00701 if (p->isLeftChild())
00702 p->getParent()->setLeftChild(left);
00703
00704 else if (p->isRightChild())
00705 p->getParent()->setRightChild(left);
00706
00707 else
00708 {
00709
00710
00711 setRoot(left);
00712 }
00713
00714
00715
00716 delete p;
00717
00718 --Size;
00719 return true;
00720 }
00721
00723 void clear()
00724 {
00725 ParentLastIterator i(getParentLastIterator());
00726
00727 while(!i.atEnd())
00728 {
00729 Node* p = i.getNode();
00730 i++;
00731
00732 delete p;
00733 }
00734 Root = 0;
00735 Size= 0;
00736 }
00737
00740 bool isEmpty() const
00741 {
00742 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00743 return Root == 0;
00744 }
00745
00749 Node* find(const KeyType& keyToFind) const
00750 {
00751 Node* pNode = Root;
00752
00753 while(pNode!=0)
00754 {
00755 KeyType key(pNode->getKey());
00756
00757 if (keyToFind == key)
00758 return pNode;
00759 else if (keyToFind < key)
00760 pNode = pNode->getLeftChild();
00761 else
00762 pNode = pNode->getRightChild();
00763 }
00764
00765 return 0;
00766 }
00767
00771 Node* getRoot() const
00772 {
00773 return Root;
00774 }
00775
00777 u32 size() const
00778 {
00779 return Size;
00780 }
00781
00782
00783
00784
00785
00787 Iterator getIterator()
00788 {
00789 Iterator it(getRoot());
00790 return it;
00791 }
00797 ParentFirstIterator getParentFirstIterator()
00798 {
00799 ParentFirstIterator it(getRoot());
00800 return it;
00801 }
00807 ParentLastIterator getParentLastIterator()
00808 {
00809 ParentLastIterator it(getRoot());
00810 return it;
00811 }
00812
00813
00814
00815
00816
00818
00819 AccessClass operator[](const KeyType& k)
00820 {
00821 return AccessClass(*this, k);
00822 }
00823 private:
00824
00825
00826
00827
00828
00829
00830
00831 explicit map(const map& src);
00832 map& operator = (const map& src);
00833
00835
00839 void setRoot(Node* newRoot)
00840 {
00841 Root = newRoot;
00842 if (Root != 0)
00843 {
00844 Root->setParent(0);
00845 Root->setBlack();
00846 }
00847 }
00848
00850
00851 bool insert(Node* newNode)
00852 {
00853 bool result=true;
00854
00855 if (Root==0)
00856 {
00857 setRoot(newNode);
00858 Size = 1;
00859 }
00860 else
00861 {
00862 Node* pNode = Root;
00863 KeyType keyNew = newNode->getKey();
00864 while (pNode)
00865 {
00866 KeyType key(pNode->getKey());
00867
00868 if (keyNew == key)
00869 {
00870 result = false;
00871 pNode = 0;
00872 }
00873 else if (keyNew < key)
00874 {
00875 if (pNode->getLeftChild() == 0)
00876 {
00877 pNode->setLeftChild(newNode);
00878 pNode = 0;
00879 }
00880 else
00881 pNode = pNode->getLeftChild();
00882 }
00883 else
00884 {
00885 if (pNode->getRightChild()==0)
00886 {
00887 pNode->setRightChild(newNode);
00888 pNode = 0;
00889 }
00890 else
00891 {
00892 pNode = pNode->getRightChild();
00893 }
00894 }
00895 }
00896
00897 if (result)
00898 ++Size;
00899 }
00900
00901 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00902 return result;
00903 }
00904
00907 void rotateLeft(Node* p)
00908 {
00909 Node* right = p->getRightChild();
00910
00911 p->setRightChild(right->getLeftChild());
00912
00913 if (p->isLeftChild())
00914 p->getParent()->setLeftChild(right);
00915 else if (p->isRightChild())
00916 p->getParent()->setRightChild(right);
00917 else
00918 setRoot(right);
00919
00920 right->setLeftChild(p);
00921 }
00922
00925 void rotateRight(Node* p)
00926 {
00927
00928 Node* left = p->getLeftChild();
00929
00930 p->setLeftChild(left->getRightChild());
00931
00932 if (p->isLeftChild())
00933 p->getParent()->setLeftChild(left);
00934 else if (p->isRightChild())
00935 p->getParent()->setRightChild(left);
00936 else
00937 setRoot(left);
00938
00939 left->setRightChild(p);
00940 }
00941
00942
00943
00944
00945 Node* Root;
00946 u32 Size;
00947 };
00948
00949 }
00950 }
00951
00952 #endif // __IRR_MAP_H_INCLUDED__
00953