1 // Copyright (C) 2006-2012 by Kat'Oun
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __IRR_MAP_H_INCLUDED__
\r
6 #define __IRR_MAP_H_INCLUDED__
\r
8 #include "irrTypes.h"
\r
16 //! map template for associative arrays using a red-black tree
\r
17 template <class KeyType, class ValueType>
\r
20 //! red/black tree for map
\r
21 template <class KeyTypeRB, class ValueTypeRB>
\r
26 RBTree(const KeyTypeRB& k, const ValueTypeRB& v)
\r
27 : LeftChild(0), RightChild(0), Parent(0), Key(k),
\r
28 Value(v), IsRed(true) {}
\r
30 void setLeftChild(RBTree* p)
\r
37 void setRightChild(RBTree* p)
\r
44 void setParent(RBTree* p) { Parent=p; }
\r
46 void setValue(const ValueTypeRB& v) { Value = v; }
\r
48 void setRed() { IsRed = true; }
\r
49 void setBlack() { IsRed = false; }
\r
51 RBTree* getLeftChild() const { return LeftChild; }
\r
52 RBTree* getRightChild() const { return RightChild; }
\r
53 RBTree* getParent() const { return Parent; }
\r
55 const ValueTypeRB& getValue() const
\r
60 ValueTypeRB& getValue()
\r
65 const KeyTypeRB& getKey() const
\r
75 bool isLeftChild() const
\r
77 return (Parent != 0) && (Parent->getLeftChild()==this);
\r
80 bool isRightChild() const
\r
82 return (Parent!=0) && (Parent->getRightChild()==this);
\r
87 return (LeftChild==0) && (RightChild==0);
\r
90 unsigned int getLevel() const
\r
95 return getParent()->getLevel() + 1;
\r
104 bool isBlack() const
\r
113 RBTree* RightChild;
\r
125 typedef RBTree<KeyType,ValueType> Node;
\r
126 // We need the forward declaration for the friend declaration
\r
127 class ConstIterator;
\r
129 //! Normal Iterator
\r
132 friend class ConstIterator;
\r
135 Iterator() : Root(0), Cur(0) {}
\r
137 // Constructor(Node*)
\r
138 Iterator(Node* root) : Root(root)
\r
143 // Copy constructor
\r
144 Iterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
\r
146 void reset(bool atLowest=true)
\r
149 Cur = getMin(Root);
\r
151 Cur = getMax(Root);
\r
159 Node* getNode() const
\r
164 Iterator& operator=(const Iterator& src)
\r
171 void operator++(int)
\r
176 void operator--(int)
\r
188 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
\r
195 Node* getMin(Node* n) const
\r
197 while(n && n->getLeftChild())
\r
198 n = n->getLeftChild();
\r
202 Node* getMax(Node* n) const
\r
204 while(n && n->getRightChild())
\r
205 n = n->getRightChild();
\r
215 if (Cur->getRightChild())
\r
217 // If current node has a right child, the next higher node is the
\r
218 // node with lowest key beneath the right child.
\r
219 Cur = getMin(Cur->getRightChild());
\r
221 else if (Cur->isLeftChild())
\r
223 // No right child? Well if current node is a left child then
\r
224 // the next higher node is the parent
\r
225 Cur = Cur->getParent();
\r
229 // Current node neither is left child nor has a right child.
\r
230 // I.e. it is either right child or root
\r
231 // The next higher node is the parent of the first non-right
\r
232 // child (i.e. either a left child or the root) up in the
\r
233 // hierarchy. Root's parent is 0.
\r
234 while(Cur->isRightChild())
\r
235 Cur = Cur->getParent();
\r
236 Cur = Cur->getParent();
\r
246 if (Cur->getLeftChild())
\r
248 // If current node has a left child, the next lower node is the
\r
249 // node with highest key beneath the left child.
\r
250 Cur = getMax(Cur->getLeftChild());
\r
252 else if (Cur->isRightChild())
\r
254 // No left child? Well if current node is a right child then
\r
255 // the next lower node is the parent
\r
256 Cur = Cur->getParent();
\r
260 // Current node neither is right child nor has a left child.
\r
261 // It is either left child or root
\r
262 // The next higher node is the parent of the first non-left
\r
263 // child (i.e. either a right child or the root) up in the
\r
264 // hierarchy. Root's parent is 0.
\r
266 while(Cur->isLeftChild())
\r
267 Cur = Cur->getParent();
\r
268 Cur = Cur->getParent();
\r
277 class ConstIterator
\r
279 friend class Iterator;
\r
282 ConstIterator() : Root(0), Cur(0) {}
\r
284 // Constructor(Node*)
\r
285 ConstIterator(const Node* root) : Root(root)
\r
290 // Copy constructor
\r
291 ConstIterator(const ConstIterator& src) : Root(src.Root), Cur(src.Cur) {}
\r
292 ConstIterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
\r
294 void reset(bool atLowest=true)
\r
297 Cur = getMin(Root);
\r
299 Cur = getMax(Root);
\r
307 const Node* getNode() const
\r
312 ConstIterator& operator=(const ConstIterator& src)
\r
319 void operator++(int)
\r
324 void operator--(int)
\r
329 const Node* operator->()
\r
334 const Node& operator*()
\r
336 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
\r
343 const Node* getMin(const Node* n) const
\r
345 while(n && n->getLeftChild())
\r
346 n = n->getLeftChild();
\r
350 const Node* getMax(const Node* n) const
\r
352 while(n && n->getRightChild())
\r
353 n = n->getRightChild();
\r
363 if (Cur->getRightChild())
\r
365 // If current node has a right child, the next higher node is the
\r
366 // node with lowest key beneath the right child.
\r
367 Cur = getMin(Cur->getRightChild());
\r
369 else if (Cur->isLeftChild())
\r
371 // No right child? Well if current node is a left child then
\r
372 // the next higher node is the parent
\r
373 Cur = Cur->getParent();
\r
377 // Current node neither is left child nor has a right child.
\r
378 // It is either right child or root
\r
379 // The next higher node is the parent of the first non-right
\r
380 // child (i.e. either a left child or the root) up in the
\r
381 // hierarchy. Root's parent is 0.
\r
382 while(Cur->isRightChild())
\r
383 Cur = Cur->getParent();
\r
384 Cur = Cur->getParent();
\r
394 if (Cur->getLeftChild())
\r
396 // If current node has a left child, the next lower node is the
\r
397 // node with highest key beneath the left child.
\r
398 Cur = getMax(Cur->getLeftChild());
\r
400 else if (Cur->isRightChild())
\r
402 // No left child? Well if current node is a right child then
\r
403 // the next lower node is the parent
\r
404 Cur = Cur->getParent();
\r
408 // Current node neither is right child nor has a left child.
\r
409 // It is either left child or root
\r
410 // The next higher node is the parent of the first non-left
\r
411 // child (i.e. either a right child or the root) up in the
\r
412 // hierarchy. Root's parent is 0.
\r
414 while(Cur->isLeftChild())
\r
415 Cur = Cur->getParent();
\r
416 Cur = Cur->getParent();
\r
422 }; // ConstIterator
\r
425 //! Parent First Iterator.
\r
426 /** Traverses the tree from top to bottom. Typical usage is
\r
427 when storing the tree structure, because when reading it
\r
428 later (and inserting elements) the tree structure will
\r
430 class ParentFirstIterator
\r
434 ParentFirstIterator() : Root(0), Cur(0) {}
\r
436 explicit ParentFirstIterator(Node* root) : Root(root), Cur(0)
\r
456 ParentFirstIterator& operator=(const ParentFirstIterator& src)
\r
463 void operator++(int)
\r
468 Node* operator -> ()
\r
475 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
\r
488 // First we try down to the left
\r
489 if (Cur->getLeftChild())
\r
491 Cur = Cur->getLeftChild();
\r
493 else if (Cur->getRightChild())
\r
495 // No left child? The we go down to the right.
\r
496 Cur = Cur->getRightChild();
\r
500 // No children? Move up in the hierarchy until
\r
501 // we either reach 0 (and are finished) or
\r
502 // find a right uncle.
\r
505 // But if parent is left child and has a right "uncle" the parent
\r
506 // has already been processed but the uncle hasn't. Move to
\r
508 if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
\r
510 Cur = Cur->getParent()->getRightChild();
\r
513 Cur = Cur->getParent();
\r
521 }; // ParentFirstIterator
\r
524 //! Parent Last Iterator
\r
525 /** Traverse the tree from bottom to top.
\r
526 Typical usage is when deleting all elements in the tree
\r
527 because you must delete the children before you delete
\r
529 class ParentLastIterator
\r
533 ParentLastIterator() : Root(0), Cur(0) {}
\r
535 explicit ParentLastIterator(Node* root) : Root(root), Cur(0)
\r
542 Cur = getMin(Root);
\r
555 ParentLastIterator& operator=(const ParentLastIterator& src)
\r
562 void operator++(int)
\r
567 Node* operator -> ()
\r
574 _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
\r
580 Node* getMin(Node* n)
\r
582 while(n!=0 && (n->getLeftChild()!=0 || n->getRightChild()!=0))
\r
584 if (n->getLeftChild())
\r
585 n = n->getLeftChild();
\r
587 n = n->getRightChild();
\r
598 // Note: Starting point is the node as far down to the left as possible.
\r
600 // If current node has an uncle to the right, go to the
\r
601 // node as far down to the left from the uncle as possible
\r
602 // else just go up a level to the parent.
\r
603 if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
\r
605 Cur = getMin(Cur->getParent()->getRightChild());
\r
608 Cur = Cur->getParent();
\r
613 }; // ParentLastIterator
\r
616 // AccessClass is a temporary class used with the [] operator.
\r
617 // It makes it possible to have different behavior in situations like:
\r
618 // myTree["Foo"] = 32;
\r
619 // If "Foo" already exists update its value else insert a new element.
\r
620 // int i = myTree["Foo"]
\r
621 // If "Foo" exists return its value.
\r
624 // Let map be the only one who can instantiate this class.
\r
625 friend class map<KeyType, ValueType>;
\r
629 // Assignment operator. Handles the myTree["Foo"] = 32; situation
\r
630 void operator=(const ValueType& value)
\r
632 // Just use the Set method, it handles already exist/not exist situation
\r
633 Tree.set(Key,value);
\r
636 // ValueType operator
\r
637 operator ValueType()
\r
639 Node* node = Tree.find(Key);
\r
642 _IRR_DEBUG_BREAK_IF(node==0) // access violation
\r
644 return node->getValue();
\r
649 AccessClass(map& tree, const KeyType& key) : Tree(tree), Key(key) {}
\r
654 const KeyType& Key;
\r
659 map() : Root(0), Size(0) {}
\r
668 typedef KeyType key_type;
\r
669 typedef ValueType value_type;
\r
670 typedef u32 size_type;
\r
672 //------------------------------
\r
674 //------------------------------
\r
676 //! Inserts a new node into the tree
\r
677 /** \param keyNew: the index for this value
\r
678 \param v: the value to insert
\r
679 \return True if successful, false if it fails (already exists) */
\r
680 bool insert(const KeyType& keyNew, const ValueType& v)
\r
682 // First insert node the "usual" way (no fancy balance logic yet)
\r
683 Node* newNode = new Node(keyNew,v);
\r
684 if (!insert(newNode))
\r
690 // Then attend a balancing party
\r
691 while (!newNode->isRoot() && (newNode->getParent()->isRed()))
\r
693 if (newNode->getParent()->isLeftChild())
\r
695 // If newNode is a left child, get its right 'uncle'
\r
696 Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
\r
697 if ( newNodesUncle!=0 && newNodesUncle->isRed())
\r
699 // case 1 - change the colors
\r
700 newNode->getParent()->setBlack();
\r
701 newNodesUncle->setBlack();
\r
702 newNode->getParent()->getParent()->setRed();
\r
703 // Move newNode up the tree
\r
704 newNode = newNode->getParent()->getParent();
\r
708 // newNodesUncle is a black node
\r
709 if ( newNode->isRightChild())
\r
711 // and newNode is to the right
\r
712 // case 2 - move newNode up and rotate
\r
713 newNode = newNode->getParent();
\r
714 rotateLeft(newNode);
\r
717 newNode->getParent()->setBlack();
\r
718 newNode->getParent()->getParent()->setRed();
\r
719 rotateRight(newNode->getParent()->getParent());
\r
724 // If newNode is a right child, get its left 'uncle'
\r
725 Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
\r
726 if ( newNodesUncle!=0 && newNodesUncle->isRed())
\r
728 // case 1 - change the colors
\r
729 newNode->getParent()->setBlack();
\r
730 newNodesUncle->setBlack();
\r
731 newNode->getParent()->getParent()->setRed();
\r
732 // Move newNode up the tree
\r
733 newNode = newNode->getParent()->getParent();
\r
737 // newNodesUncle is a black node
\r
738 if (newNode->isLeftChild())
\r
740 // and newNode is to the left
\r
741 // case 2 - move newNode up and rotate
\r
742 newNode = newNode->getParent();
\r
743 rotateRight(newNode);
\r
746 newNode->getParent()->setBlack();
\r
747 newNode->getParent()->getParent()->setRed();
\r
748 rotateLeft(newNode->getParent()->getParent());
\r
753 // Color the root black
\r
758 //! Replaces the value if the key already exists, otherwise inserts a new element.
\r
759 /** \param k The index for this value
\r
760 \param v The new value of */
\r
761 void set(const KeyType& k, const ValueType& v)
\r
770 //! Removes a node from the tree and returns it.
\r
771 /** The returned node must be deleted by the user
\r
772 \param k the key to remove
\r
773 \return A pointer to the node, or 0 if not found */
\r
774 Node* delink(const KeyType& k)
\r
780 // Rotate p down to the left until it has no right child, will get there
\r
781 // sooner or later.
\r
782 while(p->getRightChild())
\r
784 // "Pull up my right child and let it knock me down to the left"
\r
787 // p now has no right child but might have a left child
\r
788 Node* left = p->getLeftChild();
\r
790 // Let p's parent point to p's child instead of point to p
\r
791 if (p->isLeftChild())
\r
792 p->getParent()->setLeftChild(left);
\r
794 else if (p->isRightChild())
\r
795 p->getParent()->setRightChild(left);
\r
799 // p has no parent => p is the root.
\r
800 // Let the left child be the new root.
\r
804 // p is now gone from the tree in the sense that
\r
805 // no one is pointing at it, so return it.
\r
811 //! Removes a node from the tree and deletes it.
\r
812 /** \return True if the node was found and deleted */
\r
813 bool remove(const KeyType& k)
\r
819 //! Removes a node from the tree and deletes it.
\r
820 /** \return True if the node was found and deleted */
\r
821 bool remove(Node* p)
\r
828 // Rotate p down to the left until it has no right child, will get there
\r
829 // sooner or later.
\r
830 while(p->getRightChild())
\r
832 // "Pull up my right child and let it knock me down to the left"
\r
835 // p now has no right child but might have a left child
\r
836 Node* left = p->getLeftChild();
\r
838 // Let p's parent point to p's child instead of point to p
\r
839 if (p->isLeftChild())
\r
840 p->getParent()->setLeftChild(left);
\r
842 else if (p->isRightChild())
\r
843 p->getParent()->setRightChild(left);
\r
847 // p has no parent => p is the root.
\r
848 // Let the left child be the new root.
\r
852 // p is now gone from the tree in the sense that
\r
853 // no one is pointing at it. Let's get rid of it.
\r
860 //! Clear the entire tree
\r
863 ParentLastIterator i(getParentLastIterator());
\r
867 Node* p = i.getNode();
\r
868 i++; // Increment it before it is deleted
\r
869 // else iterator will get quite confused.
\r
876 //! Is the tree empty?
\r
877 //! \return Returns true if empty, false if not
\r
883 //! \deprecated Use empty() instead. This method may be removed by Irrlicht 1.9
\r
884 _IRR_DEPRECATED_ bool isEmpty() const
\r
889 //! Search for a node with the specified key.
\r
890 //! \param keyToFind: The key to find
\r
891 //! \return Returns 0 if node couldn't be found.
\r
892 Node* find(const KeyType& keyToFind) const
\r
894 Node* pNode = Root;
\r
898 const KeyType& key=pNode->getKey();
\r
900 if (keyToFind == key)
\r
902 else if (keyToFind < key)
\r
903 pNode = pNode->getLeftChild();
\r
904 else //keyToFind > key
\r
905 pNode = pNode->getRightChild();
\r
911 //! Gets the root element.
\r
912 //! \return Returns a pointer to the root node, or
\r
913 //! 0 if the tree is empty.
\r
914 Node* getRoot() const
\r
919 //! Returns the number of nodes in the tree.
\r
925 //! Swap the content of this map container with the content of another map
\r
926 /** Afterwards this object will contain the content of the other object and the other
\r
927 object will contain the content of this object. Iterators will afterwards be valid for
\r
928 the swapped object.
\r
929 \param other Swap content with this object */
\r
930 void swap(map<KeyType, ValueType>& other)
\r
932 core::swap(Root, other.Root);
\r
933 core::swap(Size, other.Size);
\r
936 //------------------------------
\r
937 // Public Iterators
\r
938 //------------------------------
\r
940 //! Returns an iterator
\r
941 Iterator getIterator() const
\r
943 Iterator it(getRoot());
\r
947 //! Returns a Constiterator
\r
948 ConstIterator getConstIterator() const
\r
950 Iterator it(getRoot());
\r
954 //! Returns a ParentFirstIterator.
\r
955 //! Traverses the tree from top to bottom. Typical usage is
\r
956 //! when storing the tree structure, because when reading it
\r
957 //! later (and inserting elements) the tree structure will
\r
959 ParentFirstIterator getParentFirstIterator() const
\r
961 ParentFirstIterator it(getRoot());
\r
965 //! Returns a ParentLastIterator to traverse the tree from
\r
967 //! Typical usage is when deleting all elements in the tree
\r
968 //! because you must delete the children before you delete
\r
970 ParentLastIterator getParentLastIterator() const
\r
972 ParentLastIterator it(getRoot());
\r
976 //------------------------------
\r
977 // Public Operators
\r
978 //------------------------------
\r
980 //! operator [] for access to elements
\r
981 /** for example myMap["key"] */
\r
982 AccessClass operator[](const KeyType& k)
\r
984 return AccessClass(*this, k);
\r
988 //------------------------------
\r
989 // Disabled methods
\r
990 //------------------------------
\r
991 // Copy constructor and assignment operator deliberately
\r
992 // defined but not implemented. The tree should never be
\r
993 // copied, pass along references to it instead.
\r
994 explicit map(const map& src);
\r
995 map& operator = (const map& src);
\r
997 //! Set node as new root.
\r
998 /** The node will be set to black, otherwise core dumps may arise
\r
999 (patch provided by rogerborg).
\r
1000 \param newRoot Node which will be the new root
\r
1002 void setRoot(Node* newRoot)
\r
1007 Root->setParent(0);
\r
1012 //! Insert a node into the tree without using any fancy balancing logic.
\r
1013 /** \return false if that key already exist in the tree. */
\r
1014 bool insert(Node* newNode)
\r
1016 bool result=true; // Assume success
\r
1025 Node* pNode = Root;
\r
1026 const KeyType& keyNew = newNode->getKey();
\r
1029 const KeyType& key=pNode->getKey();
\r
1031 if (keyNew == key)
\r
1036 else if (keyNew < key)
\r
1038 if (pNode->getLeftChild() == 0)
\r
1040 pNode->setLeftChild(newNode);
\r
1044 pNode = pNode->getLeftChild();
\r
1046 else // keyNew > key
\r
1048 if (pNode->getRightChild()==0)
\r
1050 pNode->setRightChild(newNode);
\r
1055 pNode = pNode->getRightChild();
\r
1068 //! Pull up node's right child and let it knock node down to the left
\r
1069 void rotateLeft(Node* p)
\r
1071 Node* right = p->getRightChild();
\r
1073 p->setRightChild(right->getLeftChild());
\r
1075 if (p->isLeftChild())
\r
1076 p->getParent()->setLeftChild(right);
\r
1077 else if (p->isRightChild())
\r
1078 p->getParent()->setRightChild(right);
\r
1082 right->setLeftChild(p);
\r
1086 //! Pull up node's left child and let it knock node down to the right
\r
1087 void rotateRight(Node* p)
\r
1089 Node* left = p->getLeftChild();
\r
1091 p->setLeftChild(left->getRightChild());
\r
1093 if (p->isLeftChild())
\r
1094 p->getParent()->setLeftChild(left);
\r
1095 else if (p->isRightChild())
\r
1096 p->getParent()->setRightChild(left);
\r
1100 left->setRightChild(p);
\r
1103 //------------------------------
\r
1104 // Private Members
\r
1105 //------------------------------
\r
1106 Node* Root; // The top node. 0 if empty.
\r
1107 u32 Size; // Number of nodes in the tree
\r
1110 } // end namespace core
\r
1111 } // end namespace irr
\r
1113 #endif // __IRR_MAP_H_INCLUDED__
\r