#ifndef __IRR_MAP_H_INCLUDED__
#define __IRR_MAP_H_INCLUDED__
#include "irrTypes.h"
#include "irrMath.h"
namespace irr
{
namespace core
{
template <class KeyType, class ValueType>
class map
{
template <class KeyTypeRB, class ValueTypeRB>
class RBTree
{
public:
RBTree(const KeyTypeRB& k, const ValueTypeRB& v)
: LeftChild(0), RightChild(0), Parent(0), Key(k),
Value(v), IsRed(true) {}
void setLeftChild(RBTree* p)
{
LeftChild=p;
if (p)
p->setParent(this);
}
void setRightChild(RBTree* p)
{
RightChild=p;
if (p)
p->setParent(this);
}
void setParent(RBTree* p) { Parent=p; }
void setValue(const ValueTypeRB& v) { Value = v; }
void setRed() { IsRed = true; }
void setBlack() { IsRed = false; }
RBTree* getLeftChild() const { return LeftChild; }
RBTree* getRightChild() const { return RightChild; }
RBTree* getParent() const { return Parent; }
ValueTypeRB getValue() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Value;
}
KeyTypeRB getKey() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Key;
}
bool isRoot() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Parent==0;
}
bool isLeftChild() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return (Parent != 0) && (Parent->getLeftChild()==this);
}
bool isRightChild() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return (Parent!=0) && (Parent->getRightChild()==this);
}
bool isLeaf() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return (LeftChild==0) && (RightChild==0);
}
unsigned int getLevel() const
{
if (isRoot())
return 1;
else
return getParent()->getLevel() + 1;
}
bool isRed() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return IsRed;
}
bool isBlack() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return !IsRed;
}
private:
RBTree();
RBTree* LeftChild;
RBTree* RightChild;
RBTree* Parent;
KeyTypeRB Key;
ValueTypeRB Value;
bool IsRed;
};
public:
typedef RBTree<KeyType,ValueType> Node;
class Iterator
{
public:
Iterator() : Root(0), Cur(0) {}
Iterator(Node* root) : Root(root)
{
reset();
}
Iterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
void reset(bool atLowest=true)
{
if (atLowest)
Cur = getMin(Root);
else
Cur = getMax(Root);
}
bool atEnd() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Cur==0;
}
Node* getNode()
{
return Cur;
}
Iterator& operator=(const Iterator& src)
{
Root = src.Root;
Cur = src.Cur;
return (*this);
}
void operator++(int)
{
inc();
}
void operator--(int)
{
dec();
}
Node* operator -> ()
{
return getNode();
}
Node& operator* ()
{
_IRR_DEBUG_BREAK_IF(atEnd())
return *Cur;
}
private:
Node* getMin(Node* n)
{
while(n && n->getLeftChild())
n = n->getLeftChild();
return n;
}
Node* getMax(Node* n)
{
while(n && n->getRightChild())
n = n->getRightChild();
return n;
}
void inc()
{
if (Cur==0)
return;
if (Cur->getRightChild())
{
Cur = getMin(Cur->getRightChild());
}
else if (Cur->isLeftChild())
{
Cur = Cur->getParent();
}
else
{
while(Cur->isRightChild())
Cur = Cur->getParent();
Cur = Cur->getParent();
}
}
void dec()
{
if (Cur==0)
return;
if (Cur->getLeftChild())
{
Cur = getMax(Cur->getLeftChild());
}
else if (Cur->isRightChild())
{
Cur = Cur->getParent();
}
else
{
while(Cur->isLeftChild())
Cur = Cur->getParent();
Cur = Cur->getParent();
}
}
Node* Root;
Node* Cur;
};
class ParentFirstIterator
{
public:
ParentFirstIterator() : Root(0), Cur(0)
{
}
explicit ParentFirstIterator(Node* root) : Root(root), Cur(0)
{
reset();
}
void reset()
{
Cur = Root;
}
bool atEnd() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Cur==0;
}
Node* getNode()
{
return Cur;
}
ParentFirstIterator& operator=(const ParentFirstIterator& src)
{
Root = src.Root;
Cur = src.Cur;
return (*this);
}
void operator++(int)
{
inc();
}
Node* operator -> ()
{
return getNode();
}
Node& operator* ()
{
_IRR_DEBUG_BREAK_IF(atEnd())
return *getNode();
}
private:
void inc()
{
if (Cur==0)
return;
if (Cur->getLeftChild())
{
Cur = Cur->getLeftChild();
}
else if (Cur->getRightChild())
{
Cur = Cur->getRightChild();
}
else
{
while (Cur!=0)
{
if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
{
Cur = Cur->getParent()->getRightChild();
return;
}
Cur = Cur->getParent();
}
}
}
Node* Root;
Node* Cur;
};
class ParentLastIterator
{
public:
ParentLastIterator() : Root(0), Cur(0) {}
explicit ParentLastIterator(Node* root) : Root(root), Cur(0)
{
reset();
}
void reset()
{
Cur = getMin(Root);
}
bool atEnd() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Cur==0;
}
Node* getNode()
{
return Cur;
}
ParentLastIterator& operator=(const ParentLastIterator& src)
{
Root = src.Root;
Cur = src.Cur;
return (*this);
}
void operator++(int)
{
inc();
}
Node* operator -> ()
{
return getNode();
}
Node& operator* ()
{
_IRR_DEBUG_BREAK_IF(atEnd())
return *getNode();
}
private:
Node* getMin(Node* n)
{
while(n!=0 && (n->getLeftChild()!=0 || n->getRightChild()!=0))
{
if (n->getLeftChild())
n = n->getLeftChild();
else
n = n->getRightChild();
}
return n;
}
void inc()
{
if (Cur==0)
return;
if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
{
Cur = getMin(Cur->getParent()->getRightChild());
}
else
Cur = Cur->getParent();
}
Node* Root;
Node* Cur;
};
class AccessClass
{
friend class map<KeyType, ValueType>;
public:
void operator=(const ValueType& value)
{
Tree.set(Key,value);
}
operator ValueType()
{
Node* node = Tree.find(Key);
_IRR_DEBUG_BREAK_IF(node==0)
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return node->getValue();
}
private:
AccessClass(map& tree, const KeyType& key) : Tree(tree), Key(key) {}
AccessClass();
map& Tree;
const KeyType& Key;
};
map() : Root(0), Size(0) {}
~map()
{
clear();
}
bool insert(const KeyType& keyNew, const ValueType& v)
{
Node* newNode = new Node(keyNew,v);
if (!insert(newNode))
{
delete newNode;
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return false;
}
while (!newNode->isRoot() && (newNode->getParent()->isRed()))
{
if (newNode->getParent()->isLeftChild())
{
Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
if ( newNodesUncle!=0 && newNodesUncle->isRed())
{
newNode->getParent()->setBlack();
newNodesUncle->setBlack();
newNode->getParent()->getParent()->setRed();
newNode = newNode->getParent()->getParent();
}
else
{
if ( newNode->isRightChild())
{
newNode = newNode->getParent();
rotateLeft(newNode);
}
newNode->getParent()->setBlack();
newNode->getParent()->getParent()->setRed();
rotateRight(newNode->getParent()->getParent());
}
}
else
{
Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
if ( newNodesUncle!=0 && newNodesUncle->isRed())
{
newNode->getParent()->setBlack();
newNodesUncle->setBlack();
newNode->getParent()->getParent()->setRed();
newNode = newNode->getParent()->getParent();
}
else
{
if (newNode->isLeftChild())
{
newNode = newNode->getParent();
rotateRight(newNode);
}
newNode->getParent()->setBlack();
newNode->getParent()->getParent()->setRed();
rotateLeft(newNode->getParent()->getParent());
}
}
}
Root->setBlack();
return true;
}
void set(const KeyType& k, const ValueType& v)
{
Node* p = find(k);
if (p)
p->setValue(v);
else
insert(k,v);
}
Node* delink(const KeyType& k)
{
Node* p = find(k);
if (p == 0)
return 0;
while(p->getRightChild())
{
rotateLeft(p);
}
Node* left = p->getLeftChild();
if (p->isLeftChild())
p->getParent()->setLeftChild(left);
else if (p->isRightChild())
p->getParent()->setRightChild(left);
else
{
setRoot(left);
}
--Size;
return p;
}
bool remove(const KeyType& k)
{
Node* p = find(k);
if (p == 0)
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return false;
}
while(p->getRightChild())
{
rotateLeft(p);
}
Node* left = p->getLeftChild();
if (p->isLeftChild())
p->getParent()->setLeftChild(left);
else if (p->isRightChild())
p->getParent()->setRightChild(left);
else
{
setRoot(left);
}
delete p;
--Size;
return true;
}
void clear()
{
ParentLastIterator i(getParentLastIterator());
while(!i.atEnd())
{
Node* p = i.getNode();
i++;
delete p;
}
Root = 0;
Size= 0;
}
bool empty() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Root == 0;
}
_IRR_DEPRECATED_ bool isEmpty() const
{
return empty();
}
Node* find(const KeyType& keyToFind) const
{
Node* pNode = Root;
while(pNode!=0)
{
KeyType key(pNode->getKey());
if (keyToFind == key)
return pNode;
else if (keyToFind < key)
pNode = pNode->getLeftChild();
else
pNode = pNode->getRightChild();
}
return 0;
}
Node* getRoot() const
{
return Root;
}
u32 size() const
{
return Size;
}
void swap(map<KeyType, ValueType>& other)
{
core::swap(Root, other.Root);
core::swap(Size, other.Size);
}
Iterator getIterator()
{
Iterator it(getRoot());
return it;
}
ParentFirstIterator getParentFirstIterator()
{
ParentFirstIterator it(getRoot());
return it;
}
ParentLastIterator getParentLastIterator()
{
ParentLastIterator it(getRoot());
return it;
}
AccessClass operator[](const KeyType& k)
{
return AccessClass(*this, k);
}
private:
explicit map(const map& src);
map& operator = (const map& src);
void setRoot(Node* newRoot)
{
Root = newRoot;
if (Root != 0)
{
Root->setParent(0);
Root->setBlack();
}
}
bool insert(Node* newNode)
{
bool result=true;
if (Root==0)
{
setRoot(newNode);
Size = 1;
}
else
{
Node* pNode = Root;
KeyType keyNew = newNode->getKey();
while (pNode)
{
KeyType key(pNode->getKey());
if (keyNew == key)
{
result = false;
pNode = 0;
}
else if (keyNew < key)
{
if (pNode->getLeftChild() == 0)
{
pNode->setLeftChild(newNode);
pNode = 0;
}
else
pNode = pNode->getLeftChild();
}
else
{
if (pNode->getRightChild()==0)
{
pNode->setRightChild(newNode);
pNode = 0;
}
else
{
pNode = pNode->getRightChild();
}
}
}
if (result)
++Size;
}
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return result;
}
void rotateLeft(Node* p)
{
Node* right = p->getRightChild();
p->setRightChild(right->getLeftChild());
if (p->isLeftChild())
p->getParent()->setLeftChild(right);
else if (p->isRightChild())
p->getParent()->setRightChild(right);
else
setRoot(right);
right->setLeftChild(p);
}
void rotateRight(Node* p)
{
Node* left = p->getLeftChild();
p->setLeftChild(left->getRightChild());
if (p->isLeftChild())
p->getParent()->setLeftChild(left);
else if (p->isRightChild())
p->getParent()->setRightChild(left);
else
setRoot(left);
left->setRightChild(p);
}
Node* Root;
u32 Size;
};
}
}
#endif