added more functions to RedBlackTable

This commit is contained in:
hyung-hwan 2015-03-02 14:42:55 +00:00
parent 0ece41a9e0
commit e26a49bcb8
2 changed files with 119 additions and 20 deletions

View File

@ -116,11 +116,6 @@ public:
return this->pair_tree.getMpool (); return this->pair_tree.getMpool ();
} }
qse_size_t getCapacity() const
{
return this->pair_tree.getCapacity ();
}
qse_size_t getSize() const qse_size_t getSize() const
{ {
return this->pair_tree.getSize (); return this->pair_tree.getSize ();
@ -247,14 +242,14 @@ public:
return this->pair_tree.clear (clear_mpool); return this->pair_tree.clear (clear_mpool);
} }
Iterator getIterator (qse_size_t index = 0) Iterator getIterator (typename Iterator::Mode mode = Iterator::ASCENDING) const
{ {
return this->pair_tree.getIterator (index); return this->pair_tree.getIterator (mode);
} }
ConstIterator getConstIterator (qse_size_t index = 0) const ConstIterator getConstIterator (typename ConstIterator::Mode mode = ConstIterator::ASCENDING) const
{ {
return this->pair_tree.getConstIterator (index); return this->pair_tree.getConstIterator (mode);
} }
protected: protected:

View File

@ -92,12 +92,18 @@ public:
SelfType* getParent () { return this->parent; } SelfType* getParent () { return this->parent; }
const SelfType* getParent () const { return this->parent; } const SelfType* getParent () const { return this->parent; }
SelfType* getParentNode () { return this->parent; }
const SelfType* getParentNode () const { return this->parent; }
SelfType* getLeft () { return this->left; } SelfType* getLeft () { return this->left; }
const SelfType* getLeftConst () const { return this->left; } const SelfType* getLeft () const { return this->left; }
SelfType* getLeftNode () { return this->left; }
const SelfType* getLeftNode () const { return this->left; }
SelfType* getRight () { return this->right; } SelfType* getRight () { return this->right; }
const SelfType* getRightConst () const { return this->right; } const SelfType* getRight () const { return this->right; }
SelfType* getRightNode () { return this->right; }
const SelfType* getRightNode () const { return this->right; }
//void setBlack () { this->color = BLACK; } //void setBlack () { this->color = BLACK; }
//void setRed () { this->color = RED; } //void setRed () { this->color = RED; }
@ -127,6 +133,8 @@ public:
typedef RedBlackTreeNode<T,COMPARATOR> Node; typedef RedBlackTreeNode<T,COMPARATOR> Node;
typedef RedBlackTreeIterator<T,COMPARATOR,GET_NODE,GET_T> SelfType; typedef RedBlackTreeIterator<T,COMPARATOR,GET_NODE,GET_T> SelfType;
typedef RedBlackTreeComparator<T> DefaultComparator;
typedef Node* (Node::*GetChild) (); typedef Node* (Node::*GetChild) ();
enum Mode enum Mode
@ -157,11 +165,11 @@ public:
this->get_right = &Node::getRight; this->get_right = &Node::getRight;
} }
this->__get_next_node (); this->__move_to_next_node ();
} }
protected: protected:
void __get_next_node () void __move_to_next_node ()
{ {
QSE_ASSERT (this->current != QSE_NULL); QSE_ASSERT (this->current != QSE_NULL);
@ -200,7 +208,7 @@ protected:
} }
} }
void get_next_node () void move_to_next_node ()
{ {
if (pending_action == 1) if (pending_action == 1)
{ {
@ -233,20 +241,20 @@ protected:
} }
} }
this->__get_next_node (); this->__move_to_next_node ();
} }
public: public:
SelfType& operator++ () // prefix increment SelfType& operator++ () // prefix increment
{ {
this->get_next_node (); this->move_to_next_node ();
return *this; return *this;
} }
SelfType operator++ (int) // postfix increment SelfType operator++ (int) // postfix increment
{ {
SelfType saved (*this); SelfType saved (*this);
this->get_next_node (); this->move_to_next_node ();
return saved; return saved;
} }
@ -392,12 +400,12 @@ public:
return this->node_count <= 0; return this->node_count <= 0;
} }
Node* getRoot () Node* getRootNode ()
{ {
return this->root; return this->root;
} }
const Node* getRoot () const const Node* getRootNode () const
{ {
return this->root; return this->root;
} }
@ -428,6 +436,25 @@ protected:
return QSE_NULL; return QSE_NULL;
} }
template <typename MT, typename MCOMPARATOR>
Node* heterofind_node (const MT& datum) const
{
MCOMPARATOR mcomparator;
Node* node = this->root;
// normal binary tree search
while (node->notNil())
{
int n = mcomparator (datum, node->value);
if (n == 0) return node;
if (n > 0) node = node->right;
else /* if (n < 0) */ node = node->left;
}
return QSE_NULL;
}
void rotate (Node* pivot, bool leftwise) void rotate (Node* pivot, bool leftwise)
{ {
/* /*
@ -736,6 +763,60 @@ protected:
} }
public: public:
Node* findNode (const T& datum)
{
return this->find_node (datum);
}
const Node* findNode (const T& datum) const
{
return this->find_node (datum);
}
T* findValue (const T& datum)
{
Node* b = this->findNode (datum);
if (!b) return QSE_NULL;
return &b->value;
}
const T* findValue (const T& datum) const
{
const Node* b = this->findNode (datum);
if (!b) return QSE_NULL;
return &b->value;
}
template <typename MT, typename MCOMPARATOR>
Node* heterofindNode (const MT& datum)
{
return this->heterofind_node<MT,MCOMPARATOR> (datum);
}
template <typename MT, typename MCOMPARATOR>
const Node* heterofindNode (const MT& datum) const
{
return this->heterofind_node<MT,MCOMPARATOR> (datum);
}
template <typename MT, typename MCOMPARATOR>
T* heterofindValue(const MT& datum)
{
Node* b = this->heterofind_node<MT,MCOMPARATOR> (datum);
if (!b) return QSE_NULL;
return &b->value;
}
template <typename MT, typename MCOMPARATOR>
const T* heterofindValue(const MT& datum) const
{
Node* b = this->heterofind_node<MT,MCOMPARATOR> (datum);
if (!b) return QSE_NULL;
return &b->value;
}
Node* search (const T& datum) Node* search (const T& datum)
{ {
return this->find_node (datum); return this->find_node (datum);
@ -746,6 +827,18 @@ public:
return this->find_node (datum); return this->find_node (datum);
} }
template <typename MT, typename MCOMPARATOR>
Node* heterosearch (const MT& datum)
{
return this->heterofind_node<MT,MCOMPARATOR> (datum);
}
template <typename MT, typename MCOMPARATOR>
const Node* heterosearch (const MT& datum) const
{
return this->heterofind_node<MT,MCOMPARATOR> (datum);
}
Node* inject (const T& datum, int mode, bool* injected = QSE_NULL) Node* inject (const T& datum, int mode, bool* injected = QSE_NULL)
{ {
Node* x_cur = this->root; Node* x_cur = this->root;
@ -830,10 +923,21 @@ public:
return 0; return 0;
} }
template <typename MT, typename MCOMPARATOR>
int heteroremove (const MT& datum)
{
Node* node = this->template heterofind_node<MT,MCOMPARATOR> (datum);
if (node == QSE_NULL) return -1;
this->remove_node (node);
return 0;
}
void clear (bool clear_mpool = false) void clear (bool clear_mpool = false)
{ {
while (this->root->notNil()) this->remove_node (this->root); while (this->root->notNil()) this->remove_node (this->root);
QSE_ASSERT (this->root = this->nil); QSE_ASSERT (this->root = this->nil);
QSE_ASSERT (this->node_count == 0);
} }
Iterator getIterator (typename Iterator::Mode mode = Iterator::ASCENDING) const Iterator getIterator (typename Iterator::Mode mode = Iterator::ASCENDING) const