5 #ifndef __IRR_MAP_H_INCLUDED__ 6 #define __IRR_MAP_H_INCLUDED__ 17 template <
class KeyType,
class ValueType>
21 template <
class KeyTypeRB,
class ValueTypeRB>
26 RBTree(
const KeyTypeRB& k,
const ValueTypeRB&
v)
27 : LeftChild(0), RightChild(0), Parent(0), Key(k),
28 Value(
v), IsRed(
true) {}
30 void setLeftChild(RBTree*
p)
37 void setRightChild(RBTree*
p)
44 void setParent(RBTree*
p) { Parent=
p; }
46 void setValue(
const ValueTypeRB&
v) { Value =
v; }
48 void setRed() { IsRed =
true; }
49 void setBlack() { IsRed =
false; }
51 RBTree* getLeftChild()
const {
return LeftChild; }
52 RBTree* getRightChild()
const {
return RightChild; }
53 RBTree* getParent()
const {
return Parent; }
55 const ValueTypeRB& getValue()
const 60 ValueTypeRB& getValue()
65 const KeyTypeRB& getKey()
const 75 bool isLeftChild()
const 77 return (Parent != 0) && (Parent->getLeftChild()==
this);
80 bool isRightChild()
const 82 return (Parent!=0) && (Parent->getRightChild()==
this);
87 return (LeftChild==0) && (RightChild==0);
90 unsigned int getLevel()
const 95 return getParent()->getLevel() + 1;
125 typedef RBTree<KeyType,ValueType>
Node;
197 while(
n &&
n->getLeftChild())
198 n =
n->getLeftChild();
204 while(
n &&
n->getRightChild())
205 n =
n->getRightChild();
215 if (Cur->getRightChild())
219 Cur = getMin(Cur->getRightChild());
221 else if (Cur->isLeftChild())
225 Cur = Cur->getParent();
234 while(Cur->isRightChild())
235 Cur = Cur->getParent();
236 Cur = Cur->getParent();
246 if (Cur->getLeftChild())
250 Cur = getMax(Cur->getLeftChild());
252 else if (Cur->isRightChild())
256 Cur = Cur->getParent();
266 while(Cur->isLeftChild())
267 Cur = Cur->getParent();
268 Cur = Cur->getParent();
345 while(
n &&
n->getLeftChild())
346 n =
n->getLeftChild();
352 while(
n &&
n->getRightChild())
353 n =
n->getRightChild();
363 if (Cur->getRightChild())
367 Cur = getMin(Cur->getRightChild());
369 else if (Cur->isLeftChild())
373 Cur = Cur->getParent();
382 while(Cur->isRightChild())
383 Cur = Cur->getParent();
384 Cur = Cur->getParent();
394 if (Cur->getLeftChild())
398 Cur = getMax(Cur->getLeftChild());
400 else if (Cur->isRightChild())
404 Cur = Cur->getParent();
414 while(Cur->isLeftChild())
415 Cur = Cur->getParent();
416 Cur = Cur->getParent();
489 if (Cur->getLeftChild())
491 Cur = Cur->getLeftChild();
493 else if (Cur->getRightChild())
496 Cur = Cur->getRightChild();
508 if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
510 Cur = Cur->getParent()->getRightChild();
513 Cur = Cur->getParent();
582 while(
n!=0 && (
n->getLeftChild()!=0 ||
n->getRightChild()!=0))
584 if (
n->getLeftChild())
585 n =
n->getLeftChild();
587 n =
n->getRightChild();
603 if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
605 Cur = getMin(Cur->getParent()->getRightChild());
608 Cur = Cur->getParent();
625 friend class map<KeyType, ValueType>;
644 return node->getValue();
649 AccessClass(
map& tree,
const KeyType& key) : Tree(tree), Key(key) {}
659 map() : Root(0), Size(0) {}
680 bool insert(
const KeyType& keyNew,
const ValueType&
v)
691 while (!newNode->isRoot() && (newNode->getParent()->isRed()))
693 if (newNode->getParent()->isLeftChild())
696 Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
697 if ( newNodesUncle!=0 && newNodesUncle->isRed())
700 newNode->getParent()->setBlack();
701 newNodesUncle->setBlack();
702 newNode->getParent()->getParent()->setRed();
704 newNode = newNode->getParent()->getParent();
709 if ( newNode->isRightChild())
713 newNode = newNode->getParent();
717 newNode->getParent()->setBlack();
718 newNode->getParent()->getParent()->setRed();
719 rotateRight(newNode->getParent()->getParent());
725 Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
726 if ( newNodesUncle!=0 && newNodesUncle->isRed())
729 newNode->getParent()->setBlack();
730 newNodesUncle->setBlack();
731 newNode->getParent()->getParent()->setRed();
733 newNode = newNode->getParent()->getParent();
738 if (newNode->isLeftChild())
742 newNode = newNode->getParent();
743 rotateRight(newNode);
746 newNode->getParent()->setBlack();
747 newNode->getParent()->getParent()->setRed();
748 rotateLeft(newNode->getParent()->getParent());
761 void set(
const KeyType& k,
const ValueType&
v)
782 while(
p->getRightChild())
791 if (
p->isLeftChild())
792 p->getParent()->setLeftChild(
left);
794 else if (
p->isRightChild())
795 p->getParent()->setRightChild(
left);
830 while(
p->getRightChild())
839 if (
p->isLeftChild())
840 p->getParent()->setLeftChild(
left);
842 else if (
p->isRightChild())
843 p->getParent()->setRightChild(
left);
867 Node*
p = i.getNode();
898 const KeyType& key=pNode->getKey();
900 if (keyToFind == key)
902 else if (keyToFind < key)
903 pNode = pNode->getLeftChild();
905 pNode = pNode->getRightChild();
961 ParentFirstIterator it(
getRoot());
972 ParentLastIterator it(
getRoot());
984 return AccessClass(*
this, k);
1002 void setRoot(
Node* newRoot)
1026 const KeyType& keyNew = newNode->getKey();
1029 const KeyType& key=pNode->getKey();
1036 else if (keyNew < key)
1038 if (pNode->getLeftChild() == 0)
1040 pNode->setLeftChild(newNode);
1044 pNode = pNode->getLeftChild();
1048 if (pNode->getRightChild()==0)
1050 pNode->setRightChild(newNode);
1055 pNode = pNode->getRightChild();
1069 void rotateLeft(
Node*
p)
1073 p->setRightChild(
right->getLeftChild());
1075 if (
p->isLeftChild())
1076 p->getParent()->setLeftChild(
right);
1077 else if (
p->isRightChild())
1078 p->getParent()->setRightChild(
right);
1087 void rotateRight(
Node*
p)
1091 p->setLeftChild(
left->getRightChild());
1093 if (
p->isLeftChild())
1094 p->getParent()->setLeftChild(
left);
1095 else if (
p->isRightChild())
1096 p->getParent()->setRightChild(
left);
1100 left->setRightChild(
p);
1113 #endif // __IRR_MAP_H_INCLUDED__
ConstIterator(const ConstIterator &src)
#define _IRR_DEPRECATED_
Defines a deprecated macro which generates a warning at compile time.
_IRR_DEPRECATED_ bool isEmpty() const
ConstIterator & operator=(const ConstIterator &src)
ParentLastIterator & operator=(const ParentLastIterator &src)
void swap(map< KeyType, ValueType > &other)
Swap the content of this map container with the content of another map.
void set(const KeyType &k, const ValueType &v)
Replaces the value if the key already exists, otherwise inserts a new element.
ParentLastIterator getParentLastIterator() const
map template for associative arrays using a red-black tree
RBTree< KeyType, ValueType > Node
ConstIterator(const Iterator &src)
void clear()
Clear the entire tree.
Everything in the Irrlicht Engine can be found in this namespace.
const Node * operator->()
ParentFirstIterator & operator=(const ParentFirstIterator &src)
Iterator getIterator() const
Returns an iterator.
bool insert(const KeyType &keyNew, const ValueType &v)
Inserts a new node into the tree.
ConstIterator(const Node *root)
GLsizei const GLfloat * value
unsigned int u32
32 bit unsigned variable.
Iterator(const Iterator &src)
ParentFirstIterator(Node *root)
ParentLastIterator(Node *root)
const Node * getNode() const
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
ConstIterator getConstIterator() const
Returns a Constiterator.
u32 size() const
Returns the number of nodes in the tree.
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Iterator & operator=(const Iterator &src)
Node * find(const KeyType &keyToFind) const
void reset(bool atLowest=true)
bool remove(const KeyType &k)
Removes a node from the tree and deletes it.
const Node & operator *()
Node * delink(const KeyType &k)
Removes a node from the tree and returns it.
void reset(bool atLowest=true)
void operator=(const ValueType &value)
AccessClass operator[](const KeyType &k)
operator [] for access to elements
ParentFirstIterator getParentFirstIterator() const
bool remove(Node *p)
Removes a node from the tree and deletes it.