Daniele Bartolini 10 лет назад
Родитель
Сommit
dbb8278a05
2 измененных файлов с 81 добавлено и 136 удалено
  1. 0 1
      src/core/containers/container_types.h
  2. 81 135
      src/core/containers/map.h

+ 0 - 1
src/core/containers/container_types.h

@@ -139,7 +139,6 @@ struct Map
 	};
 
 	uint32_t _root;
-	uint32_t _sentinel;
 	Vector<Node> _data;
 };
 

+ 81 - 135
src/core/containers/map.h

@@ -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 = 0xFFFFFFFFu;
+	const uint32_t BLACK = 0xb1b1b1b1u;
+	const uint32_t RED = 0xededededu;
+	const uint32_t NIL = 0xffffffff;
 
 	template <typename TKey, typename TValue>
 	inline uint32_t root(const Map<TKey, TValue>& m)
@@ -80,26 +80,27 @@ 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 == m._sentinel)
+		if (n == NIL)
 		{
 			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(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(m._data[n].key < m._data[right(m, n)].key, "Bad RBTree");
@@ -134,20 +135,16 @@ namespace map_internal
 		printf("\n");
 		return 0;
 	}
-	#endif
+#endif // RBTREE_VERIFY
 
 	template <typename TKey, typename TValue>
 	inline uint32_t min(const Map<TKey, TValue>& m, uint32_t x)
 	{
-		if (x == m._sentinel)
-		{
+		if (x == NIL)
 			return x;
-		}
 
-		while (left(m, x) != m._sentinel)
-		{
+		while (left(m, x) != NIL)
 			x = left(m, x);
-		}
 
 		return x;
 	}
@@ -155,15 +152,11 @@ namespace map_internal
 	template <typename TKey, typename TValue>
 	inline uint32_t max(const Map<TKey, TValue>& m, uint32_t x)
 	{
-		if (x == m._sentinel)
-		{
+		if (x == NIL)
 			return x;
-		}
 
-		while (right(m, x) != m._sentinel)
-		{
+		while (right(m, x) != NIL)
 			x = right(m, x);
-		}
 
 		return x;
 	}
@@ -171,10 +164,8 @@ namespace map_internal
 	template <typename TKey, typename TValue>
 	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));
-		}
 
 		uint32_t y = parent(m, x);
 
@@ -190,10 +181,8 @@ namespace map_internal
 	template <typename TKey, typename TValue>
 	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));
-		}
 
 		uint32_t y = parent(m, x);
 
@@ -214,7 +203,7 @@ namespace map_internal
 		uint32_t y = right(m, x);
 		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;
 		}
@@ -249,7 +238,7 @@ namespace map_internal
 		uint32_t y = left(m, x);
 		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;
 		}
@@ -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);
 	}
 
@@ -395,30 +383,24 @@ namespace map_internal
 	{
 		uint32_t x = m._root;
 
-		while (x != m._sentinel)
+		while (x != NIL)
 		{
 			if (m._data[x].key < key)
 			{
-				if (right(m, x) == m._sentinel)
-				{
+				if (right(m, x) == NIL)
 					return x;
-				}
 
 				x = right(m, x);
 			}
 			else if (key < m._data[x].key)
 			{
-				if (left(m, x) == m._sentinel)
-				{
+				if (left(m, x) == NIL)
 					return x;
-				}
 
 				x = left(m, x);
 			}
 			else
-			{
 				break;
-			}
 		}
 
 		return x;
@@ -429,7 +411,7 @@ namespace map_internal
 	{
 		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 NIL;
@@ -440,42 +422,34 @@ namespace map_internal
 	{
 		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;
-		}
 
 		typename Map<TKey, TValue>::Node n;
 		n.key = key;
 		n.value = TValue();
 		n.color = RED;
-		n.left = m._sentinel;
-		n.right = m._sentinel;
+		n.left = NIL;
+		n.right = NIL;
 		n.parent = NIL;
 
-		if (p == m._sentinel)
-		{
+		if (p == NIL)
 			m._root = n;
-		}
 		else
 		{
 			if (key < m._data[p].key)
-			{
 				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
@@ -485,8 +459,7 @@ namespace map
 	template <typename TKey, typename TValue>
 	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>
@@ -500,10 +473,8 @@ namespace map
 	{
 		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 deffault;
 	}
@@ -511,22 +482,24 @@ 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 = 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 x = m._root;
-		uint32_t y = map_internal::NIL;
+		uint32_t y = NIL;
 
-		if (x == m._sentinel)
+		if (x == NIL)
 			m._root = n;
 		else
 		{
-			while (x != m._sentinel)
+			while (x != NIL)
 			{
 				y = x;
 
@@ -544,11 +517,11 @@ namespace map
 			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>
@@ -559,48 +532,32 @@ namespace map
 		uint32_t n = inner_find(m, key);
 
 		if (!(m._data[n].key == key))
-		{
 			return;
-		}
 
 		uint32_t x;
 		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;
-		}
 		else
-		{
 			y = successor(m, n);
-		}
 
-		if (left(m, y) != m._sentinel)
-		{
+		if (left(m, y) != NIL)
 			x = left(m, y);
-		}
 		else
-		{
 			x = right(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)))
-			{
 				m._data[parent(m, y)].left = x;
-			}
 			else
-			{
 				m._data[parent(m, y)].right = x;
-			}
 		}
 		else
-		{
 			m._root = x;
-		}
 
 		if (y != n)
 		{
@@ -609,42 +566,42 @@ namespace map
 		}
 
 		// Do the fixup
-		if (color(m, y) == map_internal::BLACK)
+		if (color(m, y) == BLACK)
 		{
 			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)))
 				{
 					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));
 						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);
 					}
 					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);
 							y = right(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));
 						x = m._root;
 					}
@@ -653,69 +610,58 @@ namespace map
 				{
 					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));
 						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);
 					}
 					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);
 							y = left(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));
 						x = m._root;
 					}
 				}
 			}
 
-			m._data[x].color = map_internal::BLACK;
+			m._data[x].color = BLACK;
 		}
 
 		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>
 	void clear(Map<TKey, TValue>& m)
 	{
 		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>
 	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>
@@ -727,9 +673,9 @@ namespace map
 
 template <typename TKey, typename TValue>
 inline Map<TKey, TValue>::Map(Allocator& a)
-	: _data(a)
+	: _root(map_internal::NIL)
+	, _data(a)
 {
-	map::clear(*this);
 }
 
 template <typename TKey, typename TValue>