| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #pragma once
- #undef min
- #undef max
- #include <cstdlib>
- #include "Assert.h"
- #include "Types.h"
- #include "Allocator.h"
- namespace crown
- {
- enum RBTreeNodeColor { RED, BLACK };
- template<typename TKey, typename TValue>
- struct RBTreePair
- {
- public:
- RBTreePair(const TKey& k, const TValue& v):
- key(k), value(v)
- { }
- RBTreePair(const RBTreePair& p):
- key(p.key), value(p.value)
- { }
- TKey key;
- TValue value;
- };
- template<typename TKey, typename TValue>
- struct RBTreeNode
- {
- RBTreeNode(const TKey& key, const TValue& value):
- item(key, value)
- {
- left = NULL;
- right = NULL;
- parent = NULL;
- color = BLACK;
- }
- RBTreePair<TKey, TValue> item;
- struct RBTreeNode<TKey, TValue>* left;
- struct RBTreeNode<TKey, TValue>* right;
- struct RBTreeNode<TKey, TValue>* parent;
- enum RBTreeNodeColor color;
- };
- template<typename TKey, typename TValue>
- class RBTree
- {
- protected:
- typedef RBTreeNode<TKey, TValue> Node;
- typedef RBTreePair<TKey, TValue> Pair;
- public:
- RBTree(Allocator& allocator);
- ~RBTree();
- Pair& add(const TKey& key, const TValue& value);
- void remove(const TKey& key);
- bool contains(const TKey& key) const;
- void clear();
- inline int32_t size() const
- {
- return m_size;
- }
- protected:
- Node* find_or_add(TKey key);
- private:
- Allocator& m_allocator;
- Node* m_root;
- Node* m_sentinel;
- int32_t m_size;
- Node* predecessor(Node* n) const;
- Node* successor(Node* n) const;
- Node* min(Node* n) const;
- Node* max(Node* n) const;
- inline void rotate_left(Node* n);
- inline void rotate_right(Node* n);
- Node* inner_find(TKey key) const;
- void add_fixup(Node* n);
- void inner_clear(Node* n);
- #ifdef RBTREE_VERIFY
- int32_t dbg_verify(Node* n) const;
- #endif
- };
- template<typename TKey, typename TValue>
- RBTree<TKey, TValue>::RBTree(Allocator& allocator) : m_allocator(allocator)
- {
- m_sentinel = CE_NEW(m_allocator, Node)(TKey(), TValue());
- m_root = m_sentinel;
- m_size = 0;
- }
- template<typename TKey, typename TValue>
- RBTree<TKey, TValue>::~RBTree()
- {
- clear();
- CE_DELETE(m_allocator, m_sentinel);
- }
- template<typename TKey, typename TValue>
- RBTreePair<TKey, TValue>& RBTree<TKey, TValue>::add(const TKey& key, const TValue& value)
- {
- Node* n = CE_NEW(m_allocator, Node)(key, value);
- n->color = RED;
- n->left = m_sentinel;
- n->right = m_sentinel;
- Pair& pair = n->item;
- Node* x = m_root;
- Node* y = NULL;
- if (x == m_sentinel)
- {
- m_root = n;
- }
- else
- {
- while (x != m_sentinel)
- {
- y = x;
- if (key < x->item.key)
- {
- x = x->left;
- }
- else
- {
- x = x->right;
- }
- }
- if (key < y->item.key)
- {
- y->left = n;
- }
- else
- {
- y->right = n;
- }
- n->parent = y;
- }
- add_fixup(n);
- m_root->color = BLACK;
- m_size++;
- #ifdef RBTREE_VERIFY
- dbg_verify(m_root);
- #endif
- return pair;
- }
- template<typename TKey, typename TValue>
- void RBTree<TKey, TValue>::remove(const TKey& key)
- {
- Node* n = inner_find(key);
- if (!(n->item.key == key))
- {
- return;
- }
- Node* x;
- Node* y;
- if (n->left == m_sentinel || n->right == m_sentinel)
- {
- y = n;
- }
- else
- {
- y = successor(n);
- }
- if (y->left != m_sentinel)
- {
- x = y->left;
- }
- else
- {
- x = y->right;
- }
- x->parent = y->parent;
- if (y->parent != NULL)
- {
- if (y == y->parent->left)
- {
- y->parent->left = x;
- }
- else
- {
- y->parent->right = x;
- }
- }
- else
- {
- m_root = x;
- }
- if (y != n)
- {
- n->item = y->item;
- }
- //Do the fixup
- if (y->color == BLACK)
- {
- Node* y;
- while (x != m_root && x->color == BLACK)
- {
- if (x == x->parent->left)
- {
- y = x->parent->right;
- if (y->color == RED)
- {
- y->color = BLACK;
- x->parent->color = RED;
- rotate_left(x->parent);
- y = x->parent->right;
- }
- if (y->left->color == BLACK && y->right->color == BLACK)
- {
- y->color = RED;
- x = x->parent;
- }
- else
- {
- if (y->right->color == BLACK)
- {
- y->left->color = BLACK;
- y->color = RED;
- rotate_right(y);
- y = x->parent->right;
- }
- y->color = x->parent->color;
- x->parent->color = BLACK;
- y->right->color = BLACK;
- rotate_left(x->parent);
- x = m_root;
- }
- }
- else
- {
- y = x->parent->left;
- if (y->color == RED)
- {
- y->color = BLACK;
- x->parent->color = RED;
- rotate_right(x->parent);
- y = x->parent->left;
- }
- if (y->right->color == BLACK && y->left->color == BLACK)
- {
- y->color = RED;
- x = x->parent;
- }
- else
- {
- if (y->left->color == BLACK)
- {
- y->right->color = BLACK;
- y->color = RED;
- rotate_left(y);
- y = x->parent->left;
- }
- y->color = x->parent->color;
- x->parent->color = BLACK;
- y->left->color = BLACK;
- rotate_right(x->parent);
- x = m_root;
- }
- }
- }
- x->color = BLACK;
- }
- CE_DELETE(m_allocator, y);
- m_size -= 1;
- #ifdef RBTREE_VERIFY
- dbg_verify(m_root);
- #endif
- }
- template<typename TKey, typename TValue>
- RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::find_or_add(TKey key)
- {
- Node* p = inner_find(key);
- if (p != m_sentinel && p->item.key == key)
- {
- return p;
- }
- Node* n = CE_NEW(m_allocator, Node)(key, TValue());
- n->color = RED;
- n->left = m_sentinel;
- n->right = m_sentinel;
- if (p == m_sentinel)
- {
- m_root = n;
- }
- else
- {
- if (key < p->item.key)
- {
- p->left = n;
- }
- else
- {
- p->right = n;
- }
- n->parent = p;
- }
- add_fixup(n);
- m_root->color = BLACK;
- m_size++;
- #ifdef RBTREE_VERIFY
- dbg_verify(m_root);
- #endif
- return n;
- }
- template<typename TKey, typename TValue>
- void RBTree<TKey, TValue>::clear()
- {
- Node* tmp = m_root;
- m_root = m_sentinel;
- inner_clear(tmp);
- m_size = 0;
- }
- template<typename TKey, typename TValue>
- bool RBTree<TKey, TValue>::contains(const TKey& key) const
- {
- Node* n = inner_find(key);
- if (n == m_sentinel || !(n->item.key == key))
- {
- return false;
- }
- return true;
- }
- /* Inner utilities */
- template<typename TKey, typename TValue>
- RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::inner_find(TKey key) const
- {
- Node* x = m_root;
- while (x != m_sentinel)
- {
- if (key > x->item.key)
- {
- if (x->right == m_sentinel)
- {
- return x;
- }
- x = x->right;
- }
- else if (key < x->item.key)
- {
- if (x->left == m_sentinel)
- {
- return x;
- }
- x = x->left;
- }
- else
- {
- break;
- }
- }
- return x;
- }
- template<typename TKey, typename TValue>
- void RBTree<TKey, TValue>::add_fixup(Node* n)
- {
- Node* x;
- Node* y;
- while (n!=m_root && n->parent->color==RED)
- {
- x = n->parent;
- if (x == x->parent->left)
- {
- y = x->parent->right;
- if (y->color == RED)
- {
- x->color = BLACK;
- y->color = BLACK;
- x->parent->color = RED;
- n = x->parent;
- continue;
- }
- else
- {
- if (n == x->right)
- {
- n = x;
- rotate_left(n);
- x = n->parent;
- }
- x->color = BLACK;
- x->parent->color = RED;
- rotate_right(x->parent);
- }
- }
- else
- {
- y = x->parent->left;
- if (y->color == RED)
- {
- x->color = BLACK;
- y->color = BLACK;
- x->parent->color = RED;
- n = x->parent;
- continue;
- }
- else
- {
- if (n == x->left)
- {
- n = x;
- rotate_right(n);
- x = n->parent;
- }
- x->color = BLACK;
- x->parent->color = RED;
- rotate_left(x->parent);
- }
- }
- }
- }
- template<typename TKey, typename TValue>
- void RBTree<TKey, TValue>::inner_clear(Node* n)
- {
- if (n == m_sentinel)
- {
- return;
- }
- Node* tmp;
-
- tmp = n->left;
- n->left = NULL;
- inner_clear(tmp);
- tmp = n->right;
- n->right = NULL;
- inner_clear(tmp);
- CE_DELETE(m_allocator, n);
- }
- template<typename TKey, typename TValue>
- RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::predecessor(Node* x) const
- {
- if (x->left != m_sentinel)
- {
- return max(x->left);
- }
- Node* y = x->parent;
- while (y != NULL && x == y->left)
- {
- x = y;
- y = y->parent;
- }
- return y;
- }
- template<typename TKey, typename TValue>
- RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::successor(Node* x) const
- {
- if (x->right != m_sentinel)
- {
- return min(x->right);
- }
- Node* y = x->parent;
- while (y != NULL && x == y->right)
- {
- x = y;
- y = y->parent;
- }
- return y;
- }
- template<typename TKey, typename TValue>
- RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::min(Node* x) const
- {
- if (x == m_sentinel)
- {
- return x;
- }
- while (x->left != m_sentinel)
- {
- x = x->left;
- }
- return x;
- }
- template<typename TKey, typename TValue>
- RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::max(Node* x) const
- {
- if (x == m_sentinel)
- {
- return x;
- }
- while (x->right != m_sentinel)
- {
- x = x->right;
- }
- return x;
- }
- template<typename TKey, typename TValue>
- inline void RBTree<TKey, TValue>::rotate_left(Node* x)
- {
- Node* y = x->right;
- x->right = y->left;
- if (y->left != m_sentinel)
- {
- y->left->parent = x;
- }
- y->parent = x->parent;
- if (x->parent == NULL)
- {
- m_root = y;
- }
- else
- {
- if (x == x->parent->left)
- {
- x->parent->left = y;
- }
- else
- {
- x->parent->right = y;
- }
- }
- y->left = x;
- x->parent = y;
- }
- template<typename TKey, typename TValue>
- inline void RBTree<TKey, TValue>::rotate_right(Node* x)
- {
- Node* y = x->left;
- x->left = y->right;
- if (y->right != m_sentinel)
- {
- y->right->parent = x;
- }
- y->parent = x->parent;
- if (x->parent == NULL)
- {
- m_root = y;
- }
- else
- {
- if (x == x->parent->left)
- {
- x->parent->left = y;
- }
- else
- {
- x->parent->right = y;
- }
- }
- y->right = x;
- x->parent = y;
- }
- #ifdef RBTREE_VERIFY
- template<typename TKey, typename TValue>
- int32_t RBTree<TKey, TValue>::dbg_verify(Node* n) const
- {
- if (n == m_sentinel)
- {
- return 0;
- }
- if (n->left != m_sentinel)
- {
- CE_ASSERT(n->left->parent == n);
- CE_ASSERT(n->item.key > n->left->item.key);
- }
- if (n->right != m_sentinel)
- {
- CE_ASSERT(n->right->parent == n);
- CE_ASSERT(n->item.key < n->right->item.key);
- }
- int32_t bhL = dbg_verify(n->left);
- int32_t bhR = dbg_verify(n->right);
- CE_ASSERT(bhL == bhR);
- if (n->color == BLACK)
- {
- bhL += 1;
- }
- else
- {
- if (n->parent != NULL && n->parent->color == RED)
- {
- CE_ASSERT(false);
- }
- }
- return bhL;
- }
- #endif
- } // namespace crown
|