|
|
@@ -8,7 +8,7 @@
|
|
|
#include "container_types.h"
|
|
|
#include "vector.h"
|
|
|
|
|
|
-#define RBTREE_VERIFY
|
|
|
+// #define RBTREE_VERIFY
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
@@ -46,9 +46,9 @@ namespace map
|
|
|
|
|
|
namespace map_internal
|
|
|
{
|
|
|
- const uint32_t BLACK = 0xb1b1b1b1u;
|
|
|
- const uint32_t RED = 0xededededu;
|
|
|
- const uint32_t NIL = 0xffffffff;
|
|
|
+ const uint32_t BLACK = 0xB1B1B1B1u;
|
|
|
+ const uint32_t RED = 0xEDEDEDEDu;
|
|
|
+ const uint32_t NIL = 0xFFFFFFFFu;
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t root(const Map<TKey, TValue>& m)
|
|
|
@@ -80,30 +80,29 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
|
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);
|
|
|
return m._data[n].color;
|
|
|
}
|
|
|
|
|
|
-#ifdef RBTREE_VERIFY
|
|
|
+ #ifdef RBTREE_VERIFY
|
|
|
template<typename TKey, typename TValue>
|
|
|
inline int32_t dbg_verify(Map<TKey, TValue>& m, uint32_t n)
|
|
|
{
|
|
|
- if (n == NIL)
|
|
|
+ if (n == m._sentinel)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- if (left(m, n) != NIL)
|
|
|
+ if (left(m, n) != m._sentinel)
|
|
|
{
|
|
|
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)].pair.first < m._data[n].pair.first, "Bad RBTree");
|
|
|
}
|
|
|
|
|
|
- if (right(m, n) != NIL)
|
|
|
+ if (right(m, n) != m._sentinel)
|
|
|
{
|
|
|
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].pair.first < m._data[right(m, n)].pair.first, "Bad RBTree");
|
|
|
}
|
|
|
|
|
|
int32_t bhL = dbg_verify(m, left(m, n));
|
|
|
@@ -135,16 +134,20 @@ namespace map_internal
|
|
|
printf("\n");
|
|
|
return 0;
|
|
|
}
|
|
|
-#endif // RBTREE_VERIFY
|
|
|
+ #endif
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t min(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
|
- if (x == NIL)
|
|
|
+ if (x == m._sentinel)
|
|
|
+ {
|
|
|
return x;
|
|
|
+ }
|
|
|
|
|
|
- while (left(m, x) != NIL)
|
|
|
+ while (left(m, x) != m._sentinel)
|
|
|
+ {
|
|
|
x = left(m, x);
|
|
|
+ }
|
|
|
|
|
|
return x;
|
|
|
}
|
|
|
@@ -152,11 +155,15 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t max(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
|
- if (x == NIL)
|
|
|
+ if (x == m._sentinel)
|
|
|
+ {
|
|
|
return x;
|
|
|
+ }
|
|
|
|
|
|
- while (right(m, x) != NIL)
|
|
|
+ while (right(m, x) != m._sentinel)
|
|
|
+ {
|
|
|
x = right(m, x);
|
|
|
+ }
|
|
|
|
|
|
return x;
|
|
|
}
|
|
|
@@ -164,8 +171,10 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t successor(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
|
- if (right(m, x) != NIL)
|
|
|
+ if (right(m, x) != m._sentinel)
|
|
|
+ {
|
|
|
return min(m, right(m, x));
|
|
|
+ }
|
|
|
|
|
|
uint32_t y = parent(m, x);
|
|
|
|
|
|
@@ -181,8 +190,10 @@ namespace map_internal
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline uint32_t predecessor(const Map<TKey, TValue>& m, uint32_t x)
|
|
|
{
|
|
|
- if (left(m, x) != NIL)
|
|
|
+ if (left(m, x) != m._sentinel)
|
|
|
+ {
|
|
|
return max(m, left(m, x));
|
|
|
+ }
|
|
|
|
|
|
uint32_t y = parent(m, x);
|
|
|
|
|
|
@@ -203,7 +214,7 @@ namespace map_internal
|
|
|
uint32_t y = right(m, x);
|
|
|
m._data[x].right = left(m, y);
|
|
|
|
|
|
- if (left(m, y) != NIL)
|
|
|
+ if (left(m, y) != m._sentinel)
|
|
|
{
|
|
|
m._data[left(m, y)].parent = x;
|
|
|
}
|
|
|
@@ -238,7 +249,7 @@ namespace map_internal
|
|
|
uint32_t y = left(m, x);
|
|
|
m._data[x].left = right(m, y);
|
|
|
|
|
|
- if (right(m, y) != NIL)
|
|
|
+ if (right(m, y) != m._sentinel)
|
|
|
{
|
|
|
m._data[right(m, y)].parent = x;
|
|
|
}
|
|
|
@@ -305,9 +316,10 @@ 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);
|
|
|
}
|
|
|
|
|
|
@@ -383,24 +395,30 @@ namespace map_internal
|
|
|
{
|
|
|
uint32_t x = m._root;
|
|
|
|
|
|
- while (x != NIL)
|
|
|
+ while (x != m._sentinel)
|
|
|
{
|
|
|
- if (m._data[x].key < key)
|
|
|
+ if (m._data[x].pair.first < key)
|
|
|
{
|
|
|
- if (right(m, x) == NIL)
|
|
|
+ if (right(m, x) == m._sentinel)
|
|
|
+ {
|
|
|
return x;
|
|
|
+ }
|
|
|
|
|
|
x = right(m, x);
|
|
|
}
|
|
|
- else if (key < m._data[x].key)
|
|
|
+ else if (key < m._data[x].pair.first)
|
|
|
{
|
|
|
- if (left(m, x) == NIL)
|
|
|
+ if (left(m, x) == m._sentinel)
|
|
|
+ {
|
|
|
return x;
|
|
|
+ }
|
|
|
|
|
|
x = left(m, x);
|
|
|
}
|
|
|
else
|
|
|
+ {
|
|
|
break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return x;
|
|
|
@@ -411,7 +429,7 @@ namespace map_internal
|
|
|
{
|
|
|
uint32_t p = inner_find(m, key);
|
|
|
|
|
|
- if (p != NIL && m._data[p].key == key)
|
|
|
+ if (p != m._sentinel && m._data[p].pair.first == key)
|
|
|
return p;
|
|
|
|
|
|
return NIL;
|
|
|
@@ -422,34 +440,45 @@ namespace map_internal
|
|
|
{
|
|
|
uint32_t p = inner_find(m, key);
|
|
|
|
|
|
- if (p != NIL && m._data[p].key == key)
|
|
|
+ if (p != m._sentinel && m._data[p].pair.first == key)
|
|
|
+ {
|
|
|
+ printf("Found\n");
|
|
|
return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ printf("Not found, adding...\n");
|
|
|
|
|
|
typename Map<TKey, TValue>::Node n;
|
|
|
n.key = key;
|
|
|
n.value = TValue();
|
|
|
n.color = RED;
|
|
|
- n.left = NIL;
|
|
|
- n.right = NIL;
|
|
|
+ n.left = m._sentinel;
|
|
|
+ n.right = m._sentinel;
|
|
|
n.parent = NIL;
|
|
|
|
|
|
- if (p == NIL)
|
|
|
+ if (p == m._sentinel)
|
|
|
+ {
|
|
|
m._root = n;
|
|
|
+ }
|
|
|
else
|
|
|
{
|
|
|
- if (key < m._data[p].key)
|
|
|
+ if (key < m._data[p].pair.first)
|
|
|
+ {
|
|
|
m._data[p].left = n;
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
m._data[p].right = n;
|
|
|
+ }
|
|
|
|
|
|
m._data[n].parent = p;
|
|
|
}
|
|
|
|
|
|
add_fixup(m, n);
|
|
|
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;
|
|
|
}
|
|
|
} // namespace map_internal
|
|
|
@@ -459,7 +488,8 @@ namespace map
|
|
|
template <typename TKey, typename TValue>
|
|
|
uint32_t size(const Map<TKey, TValue>& m)
|
|
|
{
|
|
|
- return vector::size(m._data);
|
|
|
+ CE_ASSERT(vector::size(m._data) > 0, "Bad Map"); // There should be at least sentinel
|
|
|
+ return vector::size(m._data) - 1;
|
|
|
}
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
@@ -473,8 +503,10 @@ namespace map
|
|
|
{
|
|
|
uint32_t p = map_internal::inner_find(m, key);
|
|
|
|
|
|
- if (p != map_internal::NIL && m._data[p].key == key)
|
|
|
- return m._data[p].value;
|
|
|
+ if (p != m._sentinel && m._data[p].pair.first == key)
|
|
|
+ {
|
|
|
+ return m._data[p].pair.second;
|
|
|
+ }
|
|
|
|
|
|
return deffault;
|
|
|
}
|
|
|
@@ -482,34 +514,32 @@ namespace map
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline void set(Map<TKey, TValue>& m, const TKey& key, const TValue& value)
|
|
|
{
|
|
|
- using namespace map_internal;
|
|
|
-
|
|
|
- typename Map<TKey, TValue>::Node node;
|
|
|
- node.key = key;
|
|
|
- node.value = value;
|
|
|
- node.color = RED;
|
|
|
- node.left = NIL;
|
|
|
- node.right = NIL;
|
|
|
- node.parent = NIL;
|
|
|
+ typename Map<TKey, TValue>::Node node(*m._data._allocator);
|
|
|
+ node.pair.first = key;
|
|
|
+ node.pair.second = value;
|
|
|
+ node.color = map_internal::RED;
|
|
|
+ node.left = m._sentinel;
|
|
|
+ node.right = m._sentinel;
|
|
|
+ node.parent = map_internal::NIL;
|
|
|
uint32_t n = vector::push_back(m._data, node);
|
|
|
uint32_t x = m._root;
|
|
|
- uint32_t y = NIL;
|
|
|
+ uint32_t y = map_internal::NIL;
|
|
|
|
|
|
- if (x == NIL)
|
|
|
+ if (x == m._sentinel)
|
|
|
m._root = n;
|
|
|
else
|
|
|
{
|
|
|
- while (x != NIL)
|
|
|
+ while (x != m._sentinel)
|
|
|
{
|
|
|
y = x;
|
|
|
|
|
|
- if (key < m._data[x].key)
|
|
|
+ if (key < m._data[x].pair.first)
|
|
|
x = m._data[x].left;
|
|
|
else
|
|
|
x = m._data[x].right;
|
|
|
}
|
|
|
|
|
|
- if (key < m._data[y].key)
|
|
|
+ if (key < m._data[y].pair.first)
|
|
|
m._data[y].left = n;
|
|
|
else
|
|
|
m._data[y].right = n;
|
|
|
@@ -517,11 +547,11 @@ namespace map
|
|
|
m._data[n].parent = y;
|
|
|
}
|
|
|
|
|
|
- insert_fixup(m, n);
|
|
|
- m._data[m._root].color = BLACK;
|
|
|
-#ifdef RBTREE_VERIFY
|
|
|
- dbg_verify(m, m._root);
|
|
|
-#endif
|
|
|
+ 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
|
|
|
}
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
@@ -531,77 +561,93 @@ namespace map
|
|
|
|
|
|
uint32_t n = inner_find(m, key);
|
|
|
|
|
|
- if (!(m._data[n].key == key))
|
|
|
+ if (!(m._data[n].pair.first == key))
|
|
|
+ {
|
|
|
return;
|
|
|
+ }
|
|
|
|
|
|
uint32_t x;
|
|
|
uint32_t y;
|
|
|
|
|
|
- if (left(m, n) == NIL || right(m, n) == NIL)
|
|
|
+ if (left(m, n) == m._sentinel || right(m, n) == m._sentinel)
|
|
|
+ {
|
|
|
y = n;
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
y = successor(m, n);
|
|
|
+ }
|
|
|
|
|
|
- if (left(m, y) != NIL)
|
|
|
+ if (left(m, y) != m._sentinel)
|
|
|
+ {
|
|
|
x = left(m, y);
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
x = right(m, y);
|
|
|
+ }
|
|
|
|
|
|
m._data[x].parent = parent(m, y);
|
|
|
|
|
|
- if (parent(m, y) != NIL)
|
|
|
+ if (parent(m, y) != map_internal::NIL)
|
|
|
{
|
|
|
if (y == left(m, parent(m, y)))
|
|
|
+ {
|
|
|
m._data[parent(m, y)].left = x;
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
m._data[parent(m, y)].right = x;
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
+ {
|
|
|
m._root = x;
|
|
|
+ }
|
|
|
|
|
|
if (y != n)
|
|
|
{
|
|
|
- m._data[n].key = m._data[y].key;
|
|
|
- m._data[n].value = m._data[y].value;
|
|
|
+ m._data[n].pair.first = m._data[y].pair.first;
|
|
|
+ m._data[n].pair.second = m._data[y].pair.second;
|
|
|
}
|
|
|
|
|
|
// Do the fixup
|
|
|
- if (color(m, y) == BLACK)
|
|
|
+ if (color(m, y) == map_internal::BLACK)
|
|
|
{
|
|
|
uint32_t y;
|
|
|
|
|
|
- while (x != m._root && color(m, x) == BLACK)
|
|
|
+ while (x != m._root && color(m, x) == map_internal::BLACK)
|
|
|
{
|
|
|
if (x == left(m, parent(m, x)))
|
|
|
{
|
|
|
y = right(m, parent(m, x));
|
|
|
|
|
|
- if (color(m, y) == RED)
|
|
|
+ if (color(m, y) == map_internal::RED)
|
|
|
{
|
|
|
- m._data[y].color = BLACK;
|
|
|
- m._data[parent(m, x)].color = RED;
|
|
|
+ m._data[y].color = map_internal::BLACK;
|
|
|
+ m._data[parent(m, x)].color = map_internal::RED;
|
|
|
rotate_left(m, parent(m, x));
|
|
|
y = right(m, parent(m, x));
|
|
|
}
|
|
|
|
|
|
- if (color(m, left(m, y)) == BLACK && color(m, right(m, y)) == BLACK)
|
|
|
+ if (color(m, left(m, y)) == map_internal::BLACK && color(m, right(m, y)) == map_internal::BLACK)
|
|
|
{
|
|
|
- m._data[y].color = RED;
|
|
|
+ m._data[y].color = map_internal::RED;
|
|
|
x = parent(m, x);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (color(m, right(m, y)) == BLACK)
|
|
|
+ if (color(m, right(m, y)) == map_internal::BLACK)
|
|
|
{
|
|
|
- m._data[left(m, y)].color = BLACK;
|
|
|
- m._data[y].color = RED;
|
|
|
+ m._data[left(m, y)].color = map_internal::BLACK;
|
|
|
+ m._data[y].color = map_internal::RED;
|
|
|
rotate_right(m, y);
|
|
|
y = right(m, parent(m, x));
|
|
|
}
|
|
|
|
|
|
m._data[y].color = color(m, parent(m, x));
|
|
|
- m._data[parent(m, x)].color = BLACK;
|
|
|
- m._data[right(m, y)].color = BLACK;
|
|
|
+ m._data[parent(m, x)].color = map_internal::BLACK;
|
|
|
+ m._data[right(m, y)].color = map_internal::BLACK;
|
|
|
rotate_left(m, parent(m, x));
|
|
|
x = m._root;
|
|
|
}
|
|
|
@@ -610,58 +656,67 @@ namespace map
|
|
|
{
|
|
|
y = left(m, parent(m, x));
|
|
|
|
|
|
- if (color(m, y) == RED)
|
|
|
+ if (color(m, y) == map_internal::RED)
|
|
|
{
|
|
|
- m._data[y].color = BLACK;
|
|
|
- m._data[parent(m, x)].color = RED;
|
|
|
+ m._data[y].color = map_internal::BLACK;
|
|
|
+ m._data[parent(m, x)].color = map_internal::RED;
|
|
|
rotate_right(m, parent(m, x));
|
|
|
y = left(m, parent(m, x));
|
|
|
}
|
|
|
|
|
|
- if (color(m, right(m, y)) == BLACK && color(m, left(m, y)) == BLACK)
|
|
|
+ if (color(m, right(m, y)) == map_internal::BLACK && color(m, left(m, y)) == map_internal::BLACK)
|
|
|
{
|
|
|
- m._data[y].color = RED;
|
|
|
+ m._data[y].color = map_internal::RED;
|
|
|
x = parent(m, x);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (color(m, left(m, y)) == BLACK)
|
|
|
+ if (color(m, left(m, y)) == map_internal::BLACK)
|
|
|
{
|
|
|
- m._data[right(m, y)].color = BLACK;
|
|
|
- m._data[y].color = RED;
|
|
|
+ m._data[right(m, y)].color = map_internal::BLACK;
|
|
|
+ m._data[y].color = map_internal::RED;
|
|
|
rotate_left(m, y);
|
|
|
y = left(m, parent(m, x));
|
|
|
}
|
|
|
|
|
|
m._data[y].color = color(m, parent(m, x));
|
|
|
- m._data[parent(m, x)].color = BLACK;
|
|
|
- m._data[left(m, y)].color = BLACK;
|
|
|
+ m._data[parent(m, x)].color = map_internal::BLACK;
|
|
|
+ m._data[left(m, y)].color = map_internal::BLACK;
|
|
|
rotate_right(m, parent(m, x));
|
|
|
x = m._root;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- m._data[x].color = BLACK;
|
|
|
+ m._data[x].color = map_internal::BLACK;
|
|
|
}
|
|
|
|
|
|
destroy(m, y);
|
|
|
-#ifdef RBTREE_VERIFY
|
|
|
- dbg_verify(m, m._root);
|
|
|
-#endif
|
|
|
+ #ifdef RBTREE_VERIFY
|
|
|
+ map_internal::dbg_verify(m, m._root);
|
|
|
+ #endif
|
|
|
}
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
void clear(Map<TKey, TValue>& m)
|
|
|
{
|
|
|
vector::clear(m._data);
|
|
|
- m._root = map_internal::NIL;
|
|
|
+
|
|
|
+ m._root = 0;
|
|
|
+ m._sentinel = 0;
|
|
|
+
|
|
|
+ typename Map<TKey, TValue>::Node r(*m._data._allocator);
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
const typename Map<TKey, TValue>::Node* begin(const Map<TKey, TValue>& m)
|
|
|
{
|
|
|
- return vector::begin(m._data);
|
|
|
+ return vector::begin(m._data) + 1; // Skip sentinel at index 0
|
|
|
}
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
@@ -673,9 +728,9 @@ namespace map
|
|
|
|
|
|
template <typename TKey, typename TValue>
|
|
|
inline Map<TKey, TValue>::Map(Allocator& a)
|
|
|
- : _root(map_internal::NIL)
|
|
|
- , _data(a)
|
|
|
+ : _data(a)
|
|
|
{
|
|
|
+ map::clear(*this);
|
|
|
}
|
|
|
|
|
|
template <typename TKey, typename TValue>
|