|
@@ -8,7 +8,7 @@
|
|
|
#include "container_types.h"
|
|
#include "container_types.h"
|
|
|
#include "vector.h"
|
|
#include "vector.h"
|
|
|
|
|
|
|
|
-// #define RBTREE_VERIFY
|
|
|
|
|
|
|
+#define RBTREE_VERIFY
|
|
|
|
|
|
|
|
namespace crown
|
|
namespace crown
|
|
|
{
|
|
{
|
|
@@ -46,9 +46,9 @@ namespace map
|
|
|
|
|
|
|
|
namespace map_internal
|
|
namespace map_internal
|
|
|
{
|
|
{
|
|
|
- const uint32_t BLACK = 0xB1B1B1B1u;
|
|
|
|
|
- const uint32_t RED = 0xEDEDEDEDu;
|
|
|
|
|
- const uint32_t NIL = 0xFFFFFFFFu;
|
|
|
|
|
|
|
+ const uint32_t BLACK = 0xb1b1b1b1u;
|
|
|
|
|
+ const uint32_t RED = 0xededededu;
|
|
|
|
|
+ const uint32_t NIL = 0xffffffff;
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t root(const Map<TKey, TValue>& m)
|
|
inline uint32_t root(const Map<TKey, TValue>& m)
|
|
@@ -80,26 +80,27 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t color(const Map<TKey, TValue>& m, uint32_t n)
|
|
inline uint32_t color(const Map<TKey, TValue>& m, uint32_t n)
|
|
|
{
|
|
{
|
|
|
|
|
+ if (n == NIL) return BLACK;
|
|
|
CE_ASSERT(n < vector::size(m._data), "Index out of bounds (size = %d, n = %d)", vector::size(m._data), n);
|
|
CE_ASSERT(n < vector::size(m._data), "Index out of bounds (size = %d, n = %d)", vector::size(m._data), n);
|
|
|
return m._data[n].color;
|
|
return m._data[n].color;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- #ifdef RBTREE_VERIFY
|
|
|
|
|
|
|
+#ifdef RBTREE_VERIFY
|
|
|
template<typename TKey, typename TValue>
|
|
template<typename TKey, typename TValue>
|
|
|
inline int32_t dbg_verify(Map<TKey, TValue>& m, uint32_t n)
|
|
inline int32_t dbg_verify(Map<TKey, TValue>& m, uint32_t n)
|
|
|
{
|
|
{
|
|
|
- if (n == m._sentinel)
|
|
|
|
|
|
|
+ if (n == NIL)
|
|
|
{
|
|
{
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (left(m, n) != m._sentinel)
|
|
|
|
|
|
|
+ if (left(m, n) != NIL)
|
|
|
{
|
|
{
|
|
|
CE_ASSERT(parent(m, left(m, n)) == n, "Bad RBTree");
|
|
CE_ASSERT(parent(m, left(m, n)) == n, "Bad RBTree");
|
|
|
CE_ASSERT(m._data[left(m, n)].key < m._data[n].key, "Bad RBTree");
|
|
CE_ASSERT(m._data[left(m, n)].key < m._data[n].key, "Bad RBTree");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (right(m, n) != m._sentinel)
|
|
|
|
|
|
|
+ if (right(m, n) != NIL)
|
|
|
{
|
|
{
|
|
|
CE_ASSERT(parent(m, right(m, n)) == n, "Bad RBTree");
|
|
CE_ASSERT(parent(m, right(m, n)) == n, "Bad RBTree");
|
|
|
CE_ASSERT(m._data[n].key < m._data[right(m, n)].key, "Bad RBTree");
|
|
CE_ASSERT(m._data[n].key < m._data[right(m, n)].key, "Bad RBTree");
|
|
@@ -134,20 +135,16 @@ namespace map_internal
|
|
|
printf("\n");
|
|
printf("\n");
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
- #endif
|
|
|
|
|
|
|
+#endif // RBTREE_VERIFY
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t min(const Map<TKey, TValue>& m, uint32_t x)
|
|
inline uint32_t min(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
{
|
|
|
- if (x == m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (x == NIL)
|
|
|
return x;
|
|
return x;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- while (left(m, x) != m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ while (left(m, x) != NIL)
|
|
|
x = left(m, x);
|
|
x = left(m, x);
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
return x;
|
|
return x;
|
|
|
}
|
|
}
|
|
@@ -155,15 +152,11 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t max(const Map<TKey, TValue>& m, uint32_t x)
|
|
inline uint32_t max(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
{
|
|
|
- if (x == m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (x == NIL)
|
|
|
return x;
|
|
return x;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- while (right(m, x) != m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ while (right(m, x) != NIL)
|
|
|
x = right(m, x);
|
|
x = right(m, x);
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
return x;
|
|
return x;
|
|
|
}
|
|
}
|
|
@@ -171,10 +164,8 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t successor(const Map<TKey, TValue>& m, uint32_t x)
|
|
inline uint32_t successor(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
{
|
|
|
- if (right(m, x) != m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (right(m, x) != NIL)
|
|
|
return min(m, right(m, x));
|
|
return min(m, right(m, x));
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
uint32_t y = parent(m, x);
|
|
uint32_t y = parent(m, x);
|
|
|
|
|
|
|
@@ -190,10 +181,8 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t predecessor(const Map<TKey, TValue>& m, uint32_t x)
|
|
inline uint32_t predecessor(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
{
|
|
|
- if (left(m, x) != m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (left(m, x) != NIL)
|
|
|
return max(m, left(m, x));
|
|
return max(m, left(m, x));
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
uint32_t y = parent(m, x);
|
|
uint32_t y = parent(m, x);
|
|
|
|
|
|
|
@@ -214,7 +203,7 @@ namespace map_internal
|
|
|
uint32_t y = right(m, x);
|
|
uint32_t y = right(m, x);
|
|
|
m._data[x].right = left(m, y);
|
|
m._data[x].right = left(m, y);
|
|
|
|
|
|
|
|
- if (left(m, y) != m._sentinel)
|
|
|
|
|
|
|
+ if (left(m, y) != NIL)
|
|
|
{
|
|
{
|
|
|
m._data[left(m, y)].parent = x;
|
|
m._data[left(m, y)].parent = x;
|
|
|
}
|
|
}
|
|
@@ -249,7 +238,7 @@ namespace map_internal
|
|
|
uint32_t y = left(m, x);
|
|
uint32_t y = left(m, x);
|
|
|
m._data[x].left = right(m, y);
|
|
m._data[x].left = right(m, y);
|
|
|
|
|
|
|
|
- if (right(m, y) != m._sentinel)
|
|
|
|
|
|
|
+ if (right(m, y) != NIL)
|
|
|
{
|
|
{
|
|
|
m._data[right(m, y)].parent = x;
|
|
m._data[right(m, y)].parent = x;
|
|
|
}
|
|
}
|
|
@@ -316,10 +305,9 @@ namespace map_internal
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- #ifdef RBTREE_VERIFY
|
|
|
|
|
- dbg_verify(m, m._root);
|
|
|
|
|
- #endif
|
|
|
|
|
-
|
|
|
|
|
|
|
+#ifdef RBTREE_VERIFY
|
|
|
|
|
+ dbg_verify(m, m._root);
|
|
|
|
|
+#endif
|
|
|
vector::pop_back(m._data);
|
|
vector::pop_back(m._data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -395,30 +383,24 @@ namespace map_internal
|
|
|
{
|
|
{
|
|
|
uint32_t x = m._root;
|
|
uint32_t x = m._root;
|
|
|
|
|
|
|
|
- while (x != m._sentinel)
|
|
|
|
|
|
|
+ while (x != NIL)
|
|
|
{
|
|
{
|
|
|
if (m._data[x].key < key)
|
|
if (m._data[x].key < key)
|
|
|
{
|
|
{
|
|
|
- if (right(m, x) == m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (right(m, x) == NIL)
|
|
|
return x;
|
|
return x;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
x = right(m, x);
|
|
x = right(m, x);
|
|
|
}
|
|
}
|
|
|
else if (key < m._data[x].key)
|
|
else if (key < m._data[x].key)
|
|
|
{
|
|
{
|
|
|
- if (left(m, x) == m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (left(m, x) == NIL)
|
|
|
return x;
|
|
return x;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
x = left(m, x);
|
|
x = left(m, x);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
- {
|
|
|
|
|
break;
|
|
break;
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
return x;
|
|
@@ -429,7 +411,7 @@ namespace map_internal
|
|
|
{
|
|
{
|
|
|
uint32_t p = inner_find(m, key);
|
|
uint32_t p = inner_find(m, key);
|
|
|
|
|
|
|
|
- if (p != m._sentinel && m._data[p].key == key)
|
|
|
|
|
|
|
+ if (p != NIL && m._data[p].key == key)
|
|
|
return p;
|
|
return p;
|
|
|
|
|
|
|
|
return NIL;
|
|
return NIL;
|
|
@@ -440,42 +422,34 @@ namespace map_internal
|
|
|
{
|
|
{
|
|
|
uint32_t p = inner_find(m, key);
|
|
uint32_t p = inner_find(m, key);
|
|
|
|
|
|
|
|
- if (p != m._sentinel && m._data[p].key == key)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (p != NIL && m._data[p].key == key)
|
|
|
return p;
|
|
return p;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
typename Map<TKey, TValue>::Node n;
|
|
typename Map<TKey, TValue>::Node n;
|
|
|
n.key = key;
|
|
n.key = key;
|
|
|
n.value = TValue();
|
|
n.value = TValue();
|
|
|
n.color = RED;
|
|
n.color = RED;
|
|
|
- n.left = m._sentinel;
|
|
|
|
|
- n.right = m._sentinel;
|
|
|
|
|
|
|
+ n.left = NIL;
|
|
|
|
|
+ n.right = NIL;
|
|
|
n.parent = NIL;
|
|
n.parent = NIL;
|
|
|
|
|
|
|
|
- if (p == m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (p == NIL)
|
|
|
m._root = n;
|
|
m._root = n;
|
|
|
- }
|
|
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
if (key < m._data[p].key)
|
|
if (key < m._data[p].key)
|
|
|
- {
|
|
|
|
|
m._data[p].left = n;
|
|
m._data[p].left = n;
|
|
|
- }
|
|
|
|
|
else
|
|
else
|
|
|
- {
|
|
|
|
|
m._data[p].right = n;
|
|
m._data[p].right = n;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
m._data[n].parent = p;
|
|
m._data[n].parent = p;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
add_fixup(m, n);
|
|
add_fixup(m, n);
|
|
|
m._data[m._root].color = BLACK;
|
|
m._data[m._root].color = BLACK;
|
|
|
- #ifdef RBTREE_VERIFY
|
|
|
|
|
- dbg_verify(m, m._root);
|
|
|
|
|
- #endif
|
|
|
|
|
|
|
+#ifdef RBTREE_VERIFY
|
|
|
|
|
+ dbg_verify(m, m._root);
|
|
|
|
|
+#endif
|
|
|
return n;
|
|
return n;
|
|
|
}
|
|
}
|
|
|
} // namespace map_internal
|
|
} // namespace map_internal
|
|
@@ -485,8 +459,7 @@ namespace map
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
uint32_t size(const Map<TKey, TValue>& m)
|
|
uint32_t size(const Map<TKey, TValue>& m)
|
|
|
{
|
|
{
|
|
|
- CE_ASSERT(vector::size(m._data) > 0, "Bad Map"); // There should be at least sentinel
|
|
|
|
|
- return vector::size(m._data) - 1;
|
|
|
|
|
|
|
+ return vector::size(m._data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
@@ -500,10 +473,8 @@ namespace map
|
|
|
{
|
|
{
|
|
|
uint32_t p = map_internal::inner_find(m, key);
|
|
uint32_t p = map_internal::inner_find(m, key);
|
|
|
|
|
|
|
|
- if (p != m._sentinel && m._data[p].key == key)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (p != map_internal::NIL && m._data[p].key == key)
|
|
|
return m._data[p].value;
|
|
return m._data[p].value;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
return deffault;
|
|
return deffault;
|
|
|
}
|
|
}
|
|
@@ -511,22 +482,24 @@ namespace map
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline void set(Map<TKey, TValue>& m, const TKey& key, const TValue& value)
|
|
inline void set(Map<TKey, TValue>& m, const TKey& key, const TValue& value)
|
|
|
{
|
|
{
|
|
|
|
|
+ using namespace map_internal;
|
|
|
|
|
+
|
|
|
typename Map<TKey, TValue>::Node node;
|
|
typename Map<TKey, TValue>::Node node;
|
|
|
node.key = key;
|
|
node.key = key;
|
|
|
node.value = value;
|
|
node.value = value;
|
|
|
- node.color = map_internal::RED;
|
|
|
|
|
- node.left = m._sentinel;
|
|
|
|
|
- node.right = m._sentinel;
|
|
|
|
|
- node.parent = map_internal::NIL;
|
|
|
|
|
|
|
+ node.color = RED;
|
|
|
|
|
+ node.left = NIL;
|
|
|
|
|
+ node.right = NIL;
|
|
|
|
|
+ node.parent = NIL;
|
|
|
uint32_t n = vector::push_back(m._data, node);
|
|
uint32_t n = vector::push_back(m._data, node);
|
|
|
uint32_t x = m._root;
|
|
uint32_t x = m._root;
|
|
|
- uint32_t y = map_internal::NIL;
|
|
|
|
|
|
|
+ uint32_t y = NIL;
|
|
|
|
|
|
|
|
- if (x == m._sentinel)
|
|
|
|
|
|
|
+ if (x == NIL)
|
|
|
m._root = n;
|
|
m._root = n;
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- while (x != m._sentinel)
|
|
|
|
|
|
|
+ while (x != NIL)
|
|
|
{
|
|
{
|
|
|
y = x;
|
|
y = x;
|
|
|
|
|
|
|
@@ -544,11 +517,11 @@ namespace map
|
|
|
m._data[n].parent = y;
|
|
m._data[n].parent = y;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- map_internal::insert_fixup(m, n);
|
|
|
|
|
- m._data[m._root].color = map_internal::BLACK;
|
|
|
|
|
- #ifdef RBTREE_VERIFY
|
|
|
|
|
- map_internal::dbg_verify(m, m._root);
|
|
|
|
|
- #endif
|
|
|
|
|
|
|
+ insert_fixup(m, n);
|
|
|
|
|
+ m._data[m._root].color = BLACK;
|
|
|
|
|
+#ifdef RBTREE_VERIFY
|
|
|
|
|
+ dbg_verify(m, m._root);
|
|
|
|
|
+#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
@@ -559,48 +532,32 @@ namespace map
|
|
|
uint32_t n = inner_find(m, key);
|
|
uint32_t n = inner_find(m, key);
|
|
|
|
|
|
|
|
if (!(m._data[n].key == key))
|
|
if (!(m._data[n].key == key))
|
|
|
- {
|
|
|
|
|
return;
|
|
return;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
uint32_t x;
|
|
uint32_t x;
|
|
|
uint32_t y;
|
|
uint32_t y;
|
|
|
|
|
|
|
|
- if (left(m, n) == m._sentinel || right(m, n) == m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (left(m, n) == NIL || right(m, n) == NIL)
|
|
|
y = n;
|
|
y = n;
|
|
|
- }
|
|
|
|
|
else
|
|
else
|
|
|
- {
|
|
|
|
|
y = successor(m, n);
|
|
y = successor(m, n);
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- if (left(m, y) != m._sentinel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (left(m, y) != NIL)
|
|
|
x = left(m, y);
|
|
x = left(m, y);
|
|
|
- }
|
|
|
|
|
else
|
|
else
|
|
|
- {
|
|
|
|
|
x = right(m, y);
|
|
x = right(m, y);
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
m._data[x].parent = parent(m, y);
|
|
m._data[x].parent = parent(m, y);
|
|
|
|
|
|
|
|
- if (parent(m, y) != map_internal::NIL)
|
|
|
|
|
|
|
+ if (parent(m, y) != NIL)
|
|
|
{
|
|
{
|
|
|
if (y == left(m, parent(m, y)))
|
|
if (y == left(m, parent(m, y)))
|
|
|
- {
|
|
|
|
|
m._data[parent(m, y)].left = x;
|
|
m._data[parent(m, y)].left = x;
|
|
|
- }
|
|
|
|
|
else
|
|
else
|
|
|
- {
|
|
|
|
|
m._data[parent(m, y)].right = x;
|
|
m._data[parent(m, y)].right = x;
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
- {
|
|
|
|
|
m._root = x;
|
|
m._root = x;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
if (y != n)
|
|
if (y != n)
|
|
|
{
|
|
{
|
|
@@ -609,42 +566,42 @@ namespace map
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Do the fixup
|
|
// Do the fixup
|
|
|
- if (color(m, y) == map_internal::BLACK)
|
|
|
|
|
|
|
+ if (color(m, y) == BLACK)
|
|
|
{
|
|
{
|
|
|
uint32_t y;
|
|
uint32_t y;
|
|
|
|
|
|
|
|
- while (x != m._root && color(m, x) == map_internal::BLACK)
|
|
|
|
|
|
|
+ while (x != m._root && color(m, x) == BLACK)
|
|
|
{
|
|
{
|
|
|
if (x == left(m, parent(m, x)))
|
|
if (x == left(m, parent(m, x)))
|
|
|
{
|
|
{
|
|
|
y = right(m, parent(m, x));
|
|
y = right(m, parent(m, x));
|
|
|
|
|
|
|
|
- if (color(m, y) == map_internal::RED)
|
|
|
|
|
|
|
+ if (color(m, y) == RED)
|
|
|
{
|
|
{
|
|
|
- m._data[y].color = map_internal::BLACK;
|
|
|
|
|
- m._data[parent(m, x)].color = map_internal::RED;
|
|
|
|
|
|
|
+ m._data[y].color = BLACK;
|
|
|
|
|
+ m._data[parent(m, x)].color = RED;
|
|
|
rotate_left(m, parent(m, x));
|
|
rotate_left(m, parent(m, x));
|
|
|
y = right(m, parent(m, x));
|
|
y = right(m, parent(m, x));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (color(m, left(m, y)) == map_internal::BLACK && color(m, right(m, y)) == map_internal::BLACK)
|
|
|
|
|
|
|
+ if (color(m, left(m, y)) == BLACK && color(m, right(m, y)) == BLACK)
|
|
|
{
|
|
{
|
|
|
- m._data[y].color = map_internal::RED;
|
|
|
|
|
|
|
+ m._data[y].color = RED;
|
|
|
x = parent(m, x);
|
|
x = parent(m, x);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if (color(m, right(m, y)) == map_internal::BLACK)
|
|
|
|
|
|
|
+ if (color(m, right(m, y)) == BLACK)
|
|
|
{
|
|
{
|
|
|
- m._data[left(m, y)].color = map_internal::BLACK;
|
|
|
|
|
- m._data[y].color = map_internal::RED;
|
|
|
|
|
|
|
+ m._data[left(m, y)].color = BLACK;
|
|
|
|
|
+ m._data[y].color = RED;
|
|
|
rotate_right(m, y);
|
|
rotate_right(m, y);
|
|
|
y = right(m, parent(m, x));
|
|
y = right(m, parent(m, x));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
m._data[y].color = color(m, parent(m, x));
|
|
m._data[y].color = color(m, parent(m, x));
|
|
|
- m._data[parent(m, x)].color = map_internal::BLACK;
|
|
|
|
|
- m._data[right(m, y)].color = map_internal::BLACK;
|
|
|
|
|
|
|
+ m._data[parent(m, x)].color = BLACK;
|
|
|
|
|
+ m._data[right(m, y)].color = BLACK;
|
|
|
rotate_left(m, parent(m, x));
|
|
rotate_left(m, parent(m, x));
|
|
|
x = m._root;
|
|
x = m._root;
|
|
|
}
|
|
}
|
|
@@ -653,69 +610,58 @@ namespace map
|
|
|
{
|
|
{
|
|
|
y = left(m, parent(m, x));
|
|
y = left(m, parent(m, x));
|
|
|
|
|
|
|
|
- if (color(m, y) == map_internal::RED)
|
|
|
|
|
|
|
+ if (color(m, y) == RED)
|
|
|
{
|
|
{
|
|
|
- m._data[y].color = map_internal::BLACK;
|
|
|
|
|
- m._data[parent(m, x)].color = map_internal::RED;
|
|
|
|
|
|
|
+ m._data[y].color = BLACK;
|
|
|
|
|
+ m._data[parent(m, x)].color = RED;
|
|
|
rotate_right(m, parent(m, x));
|
|
rotate_right(m, parent(m, x));
|
|
|
y = left(m, parent(m, x));
|
|
y = left(m, parent(m, x));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (color(m, right(m, y)) == map_internal::BLACK && color(m, left(m, y)) == map_internal::BLACK)
|
|
|
|
|
|
|
+ if (color(m, right(m, y)) == BLACK && color(m, left(m, y)) == BLACK)
|
|
|
{
|
|
{
|
|
|
- m._data[y].color = map_internal::RED;
|
|
|
|
|
|
|
+ m._data[y].color = RED;
|
|
|
x = parent(m, x);
|
|
x = parent(m, x);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if (color(m, left(m, y)) == map_internal::BLACK)
|
|
|
|
|
|
|
+ if (color(m, left(m, y)) == BLACK)
|
|
|
{
|
|
{
|
|
|
- m._data[right(m, y)].color = map_internal::BLACK;
|
|
|
|
|
- m._data[y].color = map_internal::RED;
|
|
|
|
|
|
|
+ m._data[right(m, y)].color = BLACK;
|
|
|
|
|
+ m._data[y].color = RED;
|
|
|
rotate_left(m, y);
|
|
rotate_left(m, y);
|
|
|
y = left(m, parent(m, x));
|
|
y = left(m, parent(m, x));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
m._data[y].color = color(m, parent(m, x));
|
|
m._data[y].color = color(m, parent(m, x));
|
|
|
- m._data[parent(m, x)].color = map_internal::BLACK;
|
|
|
|
|
- m._data[left(m, y)].color = map_internal::BLACK;
|
|
|
|
|
|
|
+ m._data[parent(m, x)].color = BLACK;
|
|
|
|
|
+ m._data[left(m, y)].color = BLACK;
|
|
|
rotate_right(m, parent(m, x));
|
|
rotate_right(m, parent(m, x));
|
|
|
x = m._root;
|
|
x = m._root;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- m._data[x].color = map_internal::BLACK;
|
|
|
|
|
|
|
+ m._data[x].color = BLACK;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
destroy(m, y);
|
|
destroy(m, y);
|
|
|
- #ifdef RBTREE_VERIFY
|
|
|
|
|
- map_internal::dbg_verify(m, m._root);
|
|
|
|
|
- #endif
|
|
|
|
|
|
|
+#ifdef RBTREE_VERIFY
|
|
|
|
|
+ dbg_verify(m, m._root);
|
|
|
|
|
+#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
void clear(Map<TKey, TValue>& m)
|
|
void clear(Map<TKey, TValue>& m)
|
|
|
{
|
|
{
|
|
|
vector::clear(m._data);
|
|
vector::clear(m._data);
|
|
|
-
|
|
|
|
|
- m._root = 0;
|
|
|
|
|
- m._sentinel = 0;
|
|
|
|
|
-
|
|
|
|
|
- typename Map<TKey, TValue>::Node r;
|
|
|
|
|
- r.key = TKey();
|
|
|
|
|
- r.value = TValue();
|
|
|
|
|
- r.left = map_internal::NIL;
|
|
|
|
|
- r.right = map_internal::NIL;
|
|
|
|
|
- r.parent = map_internal::NIL;
|
|
|
|
|
- r.color = map_internal::BLACK;
|
|
|
|
|
- vector::push_back(m._data, r);
|
|
|
|
|
|
|
+ m._root = map_internal::NIL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
const typename Map<TKey, TValue>::Node* begin(const Map<TKey, TValue>& m)
|
|
const typename Map<TKey, TValue>::Node* begin(const Map<TKey, TValue>& m)
|
|
|
{
|
|
{
|
|
|
- return vector::begin(m._data) + 1; // Skip sentinel at index 0
|
|
|
|
|
|
|
+ return vector::begin(m._data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
@@ -727,9 +673,9 @@ namespace map
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|
|
|
inline Map<TKey, TValue>::Map(Allocator& a)
|
|
inline Map<TKey, TValue>::Map(Allocator& a)
|
|
|
- : _data(a)
|
|
|
|
|
|
|
+ : _root(map_internal::NIL)
|
|
|
|
|
+ , _data(a)
|
|
|
{
|
|
{
|
|
|
- map::clear(*this);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
template <typename TKey, typename TValue>
|