arsa  2.7
Classes | Public Types | Public Member Functions | List of all members
irr::core::map< KeyType, ValueType > Class Template Reference

map template for associative arrays using a red-black tree More...

#include <irrMap.h>

Classes

class  AccessClass
 
class  ConstIterator
 Const Iterator. More...
 
class  Iterator
 Normal Iterator. More...
 
class  ParentFirstIterator
 Parent First Iterator. More...
 
class  ParentLastIterator
 Parent Last Iterator. More...
 

Public Types

typedef RBTree< KeyType, ValueType > Node
 
typedef KeyType key_type
 
typedef ValueType value_type
 
typedef u32 size_type
 

Public Member Functions

 map ()
 
 ~map ()
 
bool insert (const KeyType &keyNew, const ValueType &v)
 Inserts a new node into the tree. More...
 
void set (const KeyType &k, const ValueType &v)
 Replaces the value if the key already exists, otherwise inserts a new element. More...
 
Nodedelink (const KeyType &k)
 Removes a node from the tree and returns it. More...
 
bool remove (const KeyType &k)
 Removes a node from the tree and deletes it. More...
 
bool remove (Node *p)
 Removes a node from the tree and deletes it. More...
 
void clear ()
 Clear the entire tree. More...
 
bool empty () const
 
_IRR_DEPRECATED_ bool isEmpty () const
 
Nodefind (const KeyType &keyToFind) const
 
NodegetRoot () const
 
u32 size () const
 Returns the number of nodes in the tree. More...
 
void swap (map< KeyType, ValueType > &other)
 Swap the content of this map container with the content of another map. More...
 
Iterator getIterator () const
 Returns an iterator. More...
 
ConstIterator getConstIterator () const
 Returns a Constiterator. More...
 
ParentFirstIterator getParentFirstIterator () const
 
ParentLastIterator getParentLastIterator () const
 
AccessClass operator[] (const KeyType &k)
 operator [] for access to elements More...
 

Detailed Description

template<class KeyType, class ValueType>
class irr::core::map< KeyType, ValueType >

map template for associative arrays using a red-black tree

Definition at line 18 of file irrMap.h.

Member Typedef Documentation

◆ key_type

template<class KeyType, class ValueType>
typedef KeyType irr::core::map< KeyType, ValueType >::key_type

Definition at line 668 of file irrMap.h.

◆ Node

template<class KeyType, class ValueType>
typedef RBTree<KeyType,ValueType> irr::core::map< KeyType, ValueType >::Node

Definition at line 125 of file irrMap.h.

◆ size_type

template<class KeyType, class ValueType>
typedef u32 irr::core::map< KeyType, ValueType >::size_type

Definition at line 670 of file irrMap.h.

◆ value_type

template<class KeyType, class ValueType>
typedef ValueType irr::core::map< KeyType, ValueType >::value_type

Definition at line 669 of file irrMap.h.

Constructor & Destructor Documentation

◆ map()

template<class KeyType, class ValueType>
irr::core::map< KeyType, ValueType >::map ( )
inline

Definition at line 659 of file irrMap.h.

659 : Root(0), Size(0) {}

◆ ~map()

template<class KeyType, class ValueType>
irr::core::map< KeyType, ValueType >::~map ( )
inline

Definition at line 662 of file irrMap.h.

663  {
664  clear();
665  }
void clear()
Clear the entire tree.
Definition: irrMap.h:861

Member Function Documentation

◆ clear()

template<class KeyType, class ValueType>
void irr::core::map< KeyType, ValueType >::clear ( void  )
inline

Clear the entire tree.

Definition at line 861 of file irrMap.h.

862  {
863  ParentLastIterator i(getParentLastIterator());
864 
865  while(!i.atEnd())
866  {
867  Node* p = i.getNode();
868  i++; // Increment it before it is deleted
869  // else iterator will get quite confused.
870  delete p;
871  }
872  Root = 0;
873  Size= 0;
874  }
GLfloat GLfloat p
ParentLastIterator getParentLastIterator() const
Definition: irrMap.h:970
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125

◆ delink()

template<class KeyType, class ValueType>
Node* irr::core::map< KeyType, ValueType >::delink ( const KeyType &  k)
inline

Removes a node from the tree and returns it.

The returned node must be deleted by the user

Parameters
kthe key to remove
Returns
A pointer to the node, or 0 if not found

Definition at line 774 of file irrMap.h.

775  {
776  Node* p = find(k);
777  if (p == 0)
778  return 0;
779 
780  // Rotate p down to the left until it has no right child, will get there
781  // sooner or later.
782  while(p->getRightChild())
783  {
784  // "Pull up my right child and let it knock me down to the left"
785  rotateLeft(p);
786  }
787  // p now has no right child but might have a left child
788  Node* left = p->getLeftChild();
789 
790  // Let p's parent point to p's child instead of point to p
791  if (p->isLeftChild())
792  p->getParent()->setLeftChild(left);
793 
794  else if (p->isRightChild())
795  p->getParent()->setRightChild(left);
796 
797  else
798  {
799  // p has no parent => p is the root.
800  // Let the left child be the new root.
801  setRoot(left);
802  }
803 
804  // p is now gone from the tree in the sense that
805  // no one is pointing at it, so return it.
806 
807  --Size;
808  return p;
809  }
GLfloat GLfloat p
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125
Node * find(const KeyType &keyToFind) const
Definition: irrMap.h:892
GLint left

◆ empty()

template<class KeyType, class ValueType>
bool irr::core::map< KeyType, ValueType >::empty ( ) const
inline

Is the tree empty?

Returns
Returns true if empty, false if not

Definition at line 878 of file irrMap.h.

879  {
880  return Root == 0;
881  }

◆ find()

template<class KeyType, class ValueType>
Node* irr::core::map< KeyType, ValueType >::find ( const KeyType &  keyToFind) const
inline

Search for a node with the specified key.

Parameters
keyToFindThe key to find
Returns
Returns 0 if node couldn't be found.

Definition at line 892 of file irrMap.h.

893  {
894  Node* pNode = Root;
895 
896  while(pNode!=0)
897  {
898  const KeyType& key=pNode->getKey();
899 
900  if (keyToFind == key)
901  return pNode;
902  else if (keyToFind < key)
903  pNode = pNode->getLeftChild();
904  else //keyToFind > key
905  pNode = pNode->getRightChild();
906  }
907 
908  return 0;
909  }
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125

◆ getConstIterator()

template<class KeyType, class ValueType>
ConstIterator irr::core::map< KeyType, ValueType >::getConstIterator ( ) const
inline

Returns a Constiterator.

Definition at line 948 of file irrMap.h.

949  {
950  Iterator it(getRoot());
951  return it;
952  }
Node * getRoot() const
Definition: irrMap.h:914

◆ getIterator()

template<class KeyType, class ValueType>
Iterator irr::core::map< KeyType, ValueType >::getIterator ( ) const
inline

Returns an iterator.

Definition at line 941 of file irrMap.h.

942  {
943  Iterator it(getRoot());
944  return it;
945  }
Node * getRoot() const
Definition: irrMap.h:914

◆ getParentFirstIterator()

template<class KeyType, class ValueType>
ParentFirstIterator irr::core::map< KeyType, ValueType >::getParentFirstIterator ( ) const
inline

Returns a ParentFirstIterator. Traverses the tree from top to bottom. Typical usage is when storing the tree structure, because when reading it later (and inserting elements) the tree structure will be the same.

Definition at line 959 of file irrMap.h.

960  {
961  ParentFirstIterator it(getRoot());
962  return it;
963  }
Node * getRoot() const
Definition: irrMap.h:914

◆ getParentLastIterator()

template<class KeyType, class ValueType>
ParentLastIterator irr::core::map< KeyType, ValueType >::getParentLastIterator ( ) const
inline

Returns a ParentLastIterator to traverse the tree from bottom to top. Typical usage is when deleting all elements in the tree because you must delete the children before you delete their parent.

Definition at line 970 of file irrMap.h.

971  {
972  ParentLastIterator it(getRoot());
973  return it;
974  }
Node * getRoot() const
Definition: irrMap.h:914

◆ getRoot()

template<class KeyType, class ValueType>
Node* irr::core::map< KeyType, ValueType >::getRoot ( ) const
inline

Gets the root element.

Returns
Returns a pointer to the root node, or 0 if the tree is empty.

Definition at line 914 of file irrMap.h.

915  {
916  return Root;
917  }

◆ insert()

template<class KeyType, class ValueType>
bool irr::core::map< KeyType, ValueType >::insert ( const KeyType &  keyNew,
const ValueType &  v 
)
inline

Inserts a new node into the tree.

Parameters
keyNewthe index for this value
vthe value to insert
Returns
True if successful, false if it fails (already exists)

Definition at line 680 of file irrMap.h.

681  {
682  // First insert node the "usual" way (no fancy balance logic yet)
683  Node* newNode = new Node(keyNew,v);
684  if (!insert(newNode))
685  {
686  delete newNode;
687  return false;
688  }
689 
690  // Then attend a balancing party
691  while (!newNode->isRoot() && (newNode->getParent()->isRed()))
692  {
693  if (newNode->getParent()->isLeftChild())
694  {
695  // If newNode is a left child, get its right 'uncle'
696  Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
697  if ( newNodesUncle!=0 && newNodesUncle->isRed())
698  {
699  // case 1 - change the colors
700  newNode->getParent()->setBlack();
701  newNodesUncle->setBlack();
702  newNode->getParent()->getParent()->setRed();
703  // Move newNode up the tree
704  newNode = newNode->getParent()->getParent();
705  }
706  else
707  {
708  // newNodesUncle is a black node
709  if ( newNode->isRightChild())
710  {
711  // and newNode is to the right
712  // case 2 - move newNode up and rotate
713  newNode = newNode->getParent();
714  rotateLeft(newNode);
715  }
716  // case 3
717  newNode->getParent()->setBlack();
718  newNode->getParent()->getParent()->setRed();
719  rotateRight(newNode->getParent()->getParent());
720  }
721  }
722  else
723  {
724  // If newNode is a right child, get its left 'uncle'
725  Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
726  if ( newNodesUncle!=0 && newNodesUncle->isRed())
727  {
728  // case 1 - change the colors
729  newNode->getParent()->setBlack();
730  newNodesUncle->setBlack();
731  newNode->getParent()->getParent()->setRed();
732  // Move newNode up the tree
733  newNode = newNode->getParent()->getParent();
734  }
735  else
736  {
737  // newNodesUncle is a black node
738  if (newNode->isLeftChild())
739  {
740  // and newNode is to the left
741  // case 2 - move newNode up and rotate
742  newNode = newNode->getParent();
743  rotateRight(newNode);
744  }
745  // case 3
746  newNode->getParent()->setBlack();
747  newNode->getParent()->getParent()->setRed();
748  rotateLeft(newNode->getParent()->getParent());
749  }
750 
751  }
752  }
753  // Color the root black
754  Root->setBlack();
755  return true;
756  }
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125
const GLdouble * v
Definition: SDL_opengl.h:2064
bool insert(const KeyType &keyNew, const ValueType &v)
Inserts a new node into the tree.
Definition: irrMap.h:680

◆ isEmpty()

template<class KeyType, class ValueType>
_IRR_DEPRECATED_ bool irr::core::map< KeyType, ValueType >::isEmpty ( ) const
inline
Deprecated:
Use empty() instead. This method may be removed by Irrlicht 1.9

Definition at line 884 of file irrMap.h.

885  {
886  return empty();
887  }
bool empty() const
Definition: irrMap.h:878

◆ operator[]()

template<class KeyType, class ValueType>
AccessClass irr::core::map< KeyType, ValueType >::operator[] ( const KeyType &  k)
inline

operator [] for access to elements

for example myMap["key"]

Definition at line 982 of file irrMap.h.

983  {
984  return AccessClass(*this, k);
985  }

◆ remove() [1/2]

template<class KeyType, class ValueType>
bool irr::core::map< KeyType, ValueType >::remove ( const KeyType &  k)
inline

Removes a node from the tree and deletes it.

Returns
True if the node was found and deleted

Definition at line 813 of file irrMap.h.

814  {
815  Node* p = find(k);
816  return remove(p);
817  }
GLfloat GLfloat p
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125
Node * find(const KeyType &keyToFind) const
Definition: irrMap.h:892
bool remove(const KeyType &k)
Removes a node from the tree and deletes it.
Definition: irrMap.h:813

◆ remove() [2/2]

template<class KeyType, class ValueType>
bool irr::core::map< KeyType, ValueType >::remove ( Node p)
inline

Removes a node from the tree and deletes it.

Returns
True if the node was found and deleted

Definition at line 821 of file irrMap.h.

822  {
823  if (p == 0)
824  {
825  return false;
826  }
827 
828  // Rotate p down to the left until it has no right child, will get there
829  // sooner or later.
830  while(p->getRightChild())
831  {
832  // "Pull up my right child and let it knock me down to the left"
833  rotateLeft(p);
834  }
835  // p now has no right child but might have a left child
836  Node* left = p->getLeftChild();
837 
838  // Let p's parent point to p's child instead of point to p
839  if (p->isLeftChild())
840  p->getParent()->setLeftChild(left);
841 
842  else if (p->isRightChild())
843  p->getParent()->setRightChild(left);
844 
845  else
846  {
847  // p has no parent => p is the root.
848  // Let the left child be the new root.
849  setRoot(left);
850  }
851 
852  // p is now gone from the tree in the sense that
853  // no one is pointing at it. Let's get rid of it.
854  delete p;
855 
856  --Size;
857  return true;
858  }
GLfloat GLfloat p
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125
GLint left

◆ set()

template<class KeyType, class ValueType>
void irr::core::map< KeyType, ValueType >::set ( const KeyType &  k,
const ValueType &  v 
)
inline

Replaces the value if the key already exists, otherwise inserts a new element.

Parameters
kThe index for this value
vThe new value of

Definition at line 761 of file irrMap.h.

762  {
763  Node* p = find(k);
764  if (p)
765  p->setValue(v);
766  else
767  insert(k,v);
768  }
GLfloat GLfloat p
RBTree< KeyType, ValueType > Node
Definition: irrMap.h:125
const GLdouble * v
Definition: SDL_opengl.h:2064
bool insert(const KeyType &keyNew, const ValueType &v)
Inserts a new node into the tree.
Definition: irrMap.h:680
Node * find(const KeyType &keyToFind) const
Definition: irrMap.h:892

◆ size()

template<class KeyType, class ValueType>
u32 irr::core::map< KeyType, ValueType >::size ( ) const
inline

Returns the number of nodes in the tree.

Definition at line 920 of file irrMap.h.

921  {
922  return Size;
923  }

◆ swap()

template<class KeyType, class ValueType>
void irr::core::map< KeyType, ValueType >::swap ( map< KeyType, ValueType > &  other)
inline

Swap the content of this map container with the content of another map.

Afterwards this object will contain the content of the other object and the other object will contain the content of this object. Iterators will afterwards be valid for the swapped object.

Parameters
otherSwap content with this object

Definition at line 930 of file irrMap.h.

931  {
932  core::swap(Root, other.Root);
933  core::swap(Size, other.Size);
934  }
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition: irrMath.h:178

The documentation for this class was generated from the following file: