1
// Copyright (C) 2006-2011 by Kat'Oun
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#ifndef __IRR_MAP_H_INCLUDED__
6
#define __IRR_MAP_H_INCLUDED__
16
//! map template for associative arrays using a red-black tree
17
template <class KeyType, class ValueType>
20
//! red/black tree for map
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
ValueTypeRB getValue() const
57
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
61
ValueTypeRB& getValue()
63
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
67
KeyTypeRB getKey() const
69
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
75
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
79
bool isLeftChild() const
81
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
82
return (Parent != 0) && (Parent->getLeftChild()==this);
85
bool isRightChild() const
87
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
88
return (Parent!=0) && (Parent->getRightChild()==this);
93
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
94
return (LeftChild==0) && (RightChild==0);
97
unsigned int getLevel() const
102
return getParent()->getLevel() + 1;
108
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
114
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
134
typedef RBTree<KeyType,ValueType> Node;
141
Iterator() : Root(0), Cur(0) {}
143
// Constructor(Node*)
144
Iterator(Node* root) : Root(root)
150
Iterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
152
void reset(bool atLowest=true)
162
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
171
Iterator& operator=(const Iterator& src)
196
_IRR_DEBUG_BREAK_IF(atEnd()) // access violation
203
Node* getMin(Node* n)
205
while(n && n->getLeftChild())
206
n = n->getLeftChild();
210
Node* getMax(Node* n)
212
while(n && n->getRightChild())
213
n = n->getRightChild();
223
if (Cur->getRightChild())
225
// If current node has a right child, the next higher node is the
226
// node with lowest key beneath the right child.
227
Cur = getMin(Cur->getRightChild());
229
else if (Cur->isLeftChild())
231
// No right child? Well if current node is a left child then
232
// the next higher node is the parent
233
Cur = Cur->getParent();
237
// Current node neither is left child nor has a right child.
238
// Ie it is either right child or root
239
// The next higher node is the parent of the first non-right
240
// child (ie either a left child or the root) up in the
241
// hierarchy. Root's parent is 0.
242
while(Cur->isRightChild())
243
Cur = Cur->getParent();
244
Cur = Cur->getParent();
254
if (Cur->getLeftChild())
256
// If current node has a left child, the next lower node is the
257
// node with highest key beneath the left child.
258
Cur = getMax(Cur->getLeftChild());
260
else if (Cur->isRightChild())
262
// No left child? Well if current node is a right child then
263
// the next lower node is the parent
264
Cur = Cur->getParent();
268
// Current node neither is right child nor has a left child.
269
// Ie it is either left child or root
270
// The next higher node is the parent of the first non-left
271
// child (ie either a right child or the root) up in the
272
// hierarchy. Root's parent is 0.
274
while(Cur->isLeftChild())
275
Cur = Cur->getParent();
276
Cur = Cur->getParent();
286
//! Parent First Iterator.
287
/** Traverses the tree from top to bottom. Typical usage is
288
when storing the tree structure, because when reading it
289
later (and inserting elements) the tree structure will
291
class ParentFirstIterator
296
ParentFirstIterator() : Root(0), Cur(0)
301
explicit ParentFirstIterator(Node* root) : Root(root), Cur(0)
314
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
324
ParentFirstIterator& operator=(const ParentFirstIterator& src)
345
_IRR_DEBUG_BREAK_IF(atEnd()) // access violation
358
// First we try down to the left
359
if (Cur->getLeftChild())
361
Cur = Cur->getLeftChild();
363
else if (Cur->getRightChild())
365
// No left child? The we go down to the right.
366
Cur = Cur->getRightChild();
370
// No children? Move up in the hierarcy until
371
// we either reach 0 (and are finished) or
372
// find a right uncle.
375
// But if parent is left child and has a right "uncle" the parent
376
// has already been processed but the uncle hasn't. Move to
378
if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
380
Cur = Cur->getParent()->getRightChild();
383
Cur = Cur->getParent();
391
}; // ParentFirstIterator
394
//! Parent Last Iterator
395
/** Traverse the tree from bottom to top.
396
Typical usage is when deleting all elements in the tree
397
because you must delete the children before you delete
399
class ParentLastIterator
403
ParentLastIterator() : Root(0), Cur(0) {}
405
explicit ParentLastIterator(Node* root) : Root(root), Cur(0)
417
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
426
ParentLastIterator& operator=(const ParentLastIterator& src)
445
_IRR_DEBUG_BREAK_IF(atEnd()) // access violation
451
Node* getMin(Node* n)
453
while(n!=0 && (n->getLeftChild()!=0 || n->getRightChild()!=0))
455
if (n->getLeftChild())
456
n = n->getLeftChild();
458
n = n->getRightChild();
469
// Note: Starting point is the node as far down to the left as possible.
471
// If current node has an uncle to the right, go to the
472
// node as far down to the left from the uncle as possible
473
// else just go up a level to the parent.
474
if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
476
Cur = getMin(Cur->getParent()->getRightChild());
479
Cur = Cur->getParent();
484
}; // ParentLastIterator
487
// AccessClass is a temporary class used with the [] operator.
488
// It makes it possible to have different behavior in situations like:
489
// myTree["Foo"] = 32;
490
// If "Foo" already exists update its value else insert a new element.
491
// int i = myTree["Foo"]
492
// If "Foo" exists return its value.
495
// Let map be the only one who can instantiate this class.
496
friend class map<KeyType, ValueType>;
500
// Assignment operator. Handles the myTree["Foo"] = 32; situation
501
void operator=(const ValueType& value)
503
// Just use the Set method, it handles already exist/not exist situation
507
// ValueType operator
510
Node* node = Tree.find(Key);
513
_IRR_DEBUG_BREAK_IF(node==0) // access violation
515
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
516
return node->getValue();
521
AccessClass(map& tree, const KeyType& key) : Tree(tree), Key(key) {}
531
map() : Root(0), Size(0) {}
539
//------------------------------
541
//------------------------------
543
//! Inserts a new node into the tree
544
/** \param keyNew: the index for this value
545
\param v: the value to insert
546
\return True if successful, false if it fails (already exists) */
547
bool insert(const KeyType& keyNew, const ValueType& v)
549
// First insert node the "usual" way (no fancy balance logic yet)
550
Node* newNode = new Node(keyNew,v);
551
if (!insert(newNode))
554
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
558
// Then attend a balancing party
559
while (!newNode->isRoot() && (newNode->getParent()->isRed()))
561
if (newNode->getParent()->isLeftChild())
563
// If newNode is a left child, get its right 'uncle'
564
Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
565
if ( newNodesUncle!=0 && newNodesUncle->isRed())
567
// case 1 - change the colours
568
newNode->getParent()->setBlack();
569
newNodesUncle->setBlack();
570
newNode->getParent()->getParent()->setRed();
571
// Move newNode up the tree
572
newNode = newNode->getParent()->getParent();
576
// newNodesUncle is a black node
577
if ( newNode->isRightChild())
579
// and newNode is to the right
580
// case 2 - move newNode up and rotate
581
newNode = newNode->getParent();
585
newNode->getParent()->setBlack();
586
newNode->getParent()->getParent()->setRed();
587
rotateRight(newNode->getParent()->getParent());
592
// If newNode is a right child, get its left 'uncle'
593
Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
594
if ( newNodesUncle!=0 && newNodesUncle->isRed())
596
// case 1 - change the colours
597
newNode->getParent()->setBlack();
598
newNodesUncle->setBlack();
599
newNode->getParent()->getParent()->setRed();
600
// Move newNode up the tree
601
newNode = newNode->getParent()->getParent();
605
// newNodesUncle is a black node
606
if (newNode->isLeftChild())
608
// and newNode is to the left
609
// case 2 - move newNode up and rotate
610
newNode = newNode->getParent();
611
rotateRight(newNode);
614
newNode->getParent()->setBlack();
615
newNode->getParent()->getParent()->setRed();
616
rotateLeft(newNode->getParent()->getParent());
621
// Color the root black
626
//! Replaces the value if the key already exists, otherwise inserts a new element.
627
/** \param k The index for this value
628
\param v The new value of */
629
void set(const KeyType& k, const ValueType& v)
638
//! Removes a node from the tree and returns it.
639
/** The returned node must be deleted by the user
640
\param k the key to remove
641
\return A pointer to the node, or 0 if not found */
642
Node* delink(const KeyType& k)
648
// Rotate p down to the left until it has no right child, will get there
650
while(p->getRightChild())
652
// "Pull up my right child and let it knock me down to the left"
655
// p now has no right child but might have a left child
656
Node* left = p->getLeftChild();
658
// Let p's parent point to p's child instead of point to p
659
if (p->isLeftChild())
660
p->getParent()->setLeftChild(left);
662
else if (p->isRightChild())
663
p->getParent()->setRightChild(left);
667
// p has no parent => p is the root.
668
// Let the left child be the new root.
672
// p is now gone from the tree in the sense that
673
// no one is pointing at it, so return it.
679
//! Removes a node from the tree and deletes it.
680
/** \return True if the node was found and deleted */
681
bool remove(const KeyType& k)
686
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
690
// Rotate p down to the left until it has no right child, will get there
692
while(p->getRightChild())
694
// "Pull up my right child and let it knock me down to the left"
697
// p now has no right child but might have a left child
698
Node* left = p->getLeftChild();
700
// Let p's parent point to p's child instead of point to p
701
if (p->isLeftChild())
702
p->getParent()->setLeftChild(left);
704
else if (p->isRightChild())
705
p->getParent()->setRightChild(left);
709
// p has no parent => p is the root.
710
// Let the left child be the new root.
714
// p is now gone from the tree in the sense that
715
// no one is pointing at it. Let's get rid of it.
722
//! Clear the entire tree
725
ParentLastIterator i(getParentLastIterator());
729
Node* p = i.getNode();
730
i++; // Increment it before it is deleted
731
// else iterator will get quite confused.
738
//! Is the tree empty?
739
//! \return Returns true if empty, false if not
742
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
746
//! \deprecated Use empty() instead. This method may be removed by Irrlicht 1.9
747
_IRR_DEPRECATED_ bool isEmpty() const
752
//! Search for a node with the specified key.
753
//! \param keyToFind: The key to find
754
//! \return Returns 0 if node couldn't be found.
755
Node* find(const KeyType& keyToFind) const
761
KeyType key(pNode->getKey());
763
if (keyToFind == key)
765
else if (keyToFind < key)
766
pNode = pNode->getLeftChild();
767
else //keyToFind > key
768
pNode = pNode->getRightChild();
774
//! Gets the root element.
775
//! \return Returns a pointer to the root node, or
776
//! 0 if the tree is empty.
777
Node* getRoot() const
782
//! Returns the number of nodes in the tree.
788
//! Swap the content of this map container with the content of another map
789
/** Afterwards this object will contain the content of the other object and the other
790
object will contain the content of this object. Iterators will afterwards be valid for
792
\param other Swap content with this object */
793
void swap(map<KeyType, ValueType>& other)
795
core::swap(Root, other.Root);
796
core::swap(Size, other.Size);
799
//------------------------------
801
//------------------------------
803
//! Returns an iterator
804
Iterator getIterator()
806
Iterator it(getRoot());
809
//! Returns a ParentFirstIterator.
810
//! Traverses the tree from top to bottom. Typical usage is
811
//! when storing the tree structure, because when reading it
812
//! later (and inserting elements) the tree structure will
814
ParentFirstIterator getParentFirstIterator()
816
ParentFirstIterator it(getRoot());
819
//! Returns a ParentLastIterator to traverse the tree from
821
//! Typical usage is when deleting all elements in the tree
822
//! because you must delete the children before you delete
824
ParentLastIterator getParentLastIterator()
826
ParentLastIterator it(getRoot());
830
//------------------------------
832
//------------------------------
834
//! operator [] for access to elements
835
/** for example myMap["key"] */
836
AccessClass operator[](const KeyType& k)
838
return AccessClass(*this, k);
842
//------------------------------
844
//------------------------------
845
// Copy constructor and assignment operator deliberately
846
// defined but not implemented. The tree should never be
847
// copied, pass along references to it instead.
848
explicit map(const map& src);
849
map& operator = (const map& src);
851
//! Set node as new root.
852
/** The node will be set to black, otherwise core dumps may arise
853
(patch provided by rogerborg).
854
\param newRoot Node which will be the new root
856
void setRoot(Node* newRoot)
866
//! Insert a node into the tree without using any fancy balancing logic.
867
/** \return false if that key already exist in the tree. */
868
bool insert(Node* newNode)
870
bool result=true; // Assume success
880
KeyType keyNew = newNode->getKey();
883
KeyType key(pNode->getKey());
890
else if (keyNew < key)
892
if (pNode->getLeftChild() == 0)
894
pNode->setLeftChild(newNode);
898
pNode = pNode->getLeftChild();
902
if (pNode->getRightChild()==0)
904
pNode->setRightChild(newNode);
909
pNode = pNode->getRightChild();
918
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
923
//! Pull up node's right child and let it knock node down to the left
924
void rotateLeft(Node* p)
926
Node* right = p->getRightChild();
928
p->setRightChild(right->getLeftChild());
930
if (p->isLeftChild())
931
p->getParent()->setLeftChild(right);
932
else if (p->isRightChild())
933
p->getParent()->setRightChild(right);
937
right->setLeftChild(p);
941
//! Pull up node's left child and let it knock node down to the right
942
void rotateRight(Node* p)
944
Node* left = p->getLeftChild();
946
p->setLeftChild(left->getRightChild());
948
if (p->isLeftChild())
949
p->getParent()->setLeftChild(left);
950
else if (p->isRightChild())
951
p->getParent()->setRightChild(left);
955
left->setRightChild(p);
958
//------------------------------
960
//------------------------------
961
Node* Root; // The top node. 0 if empty.
962
u32 Size; // Number of nodes in the tree
965
} // end namespace core
966
} // end namespace irr
968
#endif // __IRR_MAP_H_INCLUDED__