Browse Source

Work on the HashMap

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
19deb0849c
5 changed files with 55 additions and 16 deletions
  1. 15 12
      include/anki/util/Functions.h
  2. 17 1
      include/anki/util/HashMap.h
  3. 1 1
      src/util/Memory.cpp
  4. 3 2
      src/util/Thread.cpp
  5. 19 0
      tests/util/HashMap.cpp

+ 15 - 12
include/anki/util/Functions.h

@@ -162,21 +162,24 @@ inline U32 countBits(U32 number)
 #endif
 #endif
 }
 }
 
 
-/// Call an object constructor.
-template<typename T, typename... TArgs>
-void construct(T* p, TArgs&&... args)
+/// Check if types are the same.
+template<class T, class Y>
+struct TypesAreTheSame
 {
 {
-	// Placement new
-	::new(reinterpret_cast<void*>(p)) T(std::forward<TArgs>(args)...);
-}
+    enum
+	{
+		m_value = 0
+	};
+};
 
 
-/// Call destructor.
-template<typename T>
-void destruct(T* p)
+template<class T>
+struct TypesAreTheSame<T, T>
 {
 {
-	p->~T();
-}
-
+    enum
+	{
+		m_value = 1
+	};
+};
 /// @}
 /// @}
 
 
 } // end namespace anki
 } // end namespace anki

+ 17 - 1
include/anki/util/HashMap.h

@@ -8,12 +8,22 @@
 #include "anki/util/Allocator.h"
 #include "anki/util/Allocator.h"
 #include "anki/util/NonCopyable.h"
 #include "anki/util/NonCopyable.h"
 #include "anki/util/Ptr.h"
 #include "anki/util/Ptr.h"
+#include "anki/util/Functions.h"
 
 
 namespace anki {
 namespace anki {
 
 
 /// @addtogroup util_private
 /// @addtogroup util_private
 /// @{
 /// @{
 
 
+#define ANKI_ENABLE_HASH_MAP(Class_) \
+	template<typename TKey, typename TValue, typename THasher, \
+		typename TCompare, typename TNode> \
+	friend class HashMap; \
+	U64 m_hash = 0; \
+	Class_* m_left = nullptr; \
+	Class_* m_right = nullptr; \
+	Class_* m_parent = nullptr;
+
 /// HashMap node. It's not a traditional bucket because it doesn't contain more
 /// HashMap node. It's not a traditional bucket because it doesn't contain more
 /// than one values.
 /// than one values.
 template<typename TKey, typename TValue>
 template<typename TKey, typename TValue>
@@ -166,6 +176,10 @@ public:
 	using Iterator = HashMapIterator<Node*, Pointer, Reference>;
 	using Iterator = HashMapIterator<Node*, Pointer, Reference>;
 	using ConstIterator =
 	using ConstIterator =
 		HashMapIterator<const Node*, ConstPointer, ConstReference>;
 		HashMapIterator<const Node*, ConstPointer, ConstReference>;
+	enum
+	{
+		T_NODE_SAME_AS_T_VALUE = TypesAreTheSame<Value, Node>::m_value
+	};
 
 
 	/// Default constructor.
 	/// Default constructor.
 	HashMap()
 	HashMap()
@@ -264,8 +278,10 @@ public:
 	template<typename TAllocator>
 	template<typename TAllocator>
 	void pushBack(Pointer x)
 	void pushBack(Pointer x)
 	{
 	{
+		static_assert(T_NODE_SAME_AS_T_VALUE == true, "Cannot use that");
 		ANKI_ASSERT(x);
 		ANKI_ASSERT(x);
-		// TODO
+		ANKI_ASSERT(x->m_hash != 0);
+		pushBackNode(x);
 	}
 	}
 
 
 	/// Construct an element inside the map.
 	/// Construct an element inside the map.

+ 1 - 1
src/util/Memory.cpp

@@ -533,7 +533,7 @@ void ChainMemoryPool::create(
 	{
 	{
 		ANKI_CREATION_OOM_ACTION();
 		ANKI_CREATION_OOM_ACTION();
 	}
 	}
-	construct(m_lock);
+	::new(m_lock) SpinLock();
 
 
 	// Initial size should be > 0
 	// Initial size should be > 0
 	ANKI_ASSERT(m_initSize > 0 && "Wrong arg");
 	ANKI_ASSERT(m_initSize > 0 && "Wrong arg");

+ 3 - 2
src/util/Thread.cpp

@@ -113,7 +113,7 @@ Threadpool::DummyTask Threadpool::m_dummyTask;
 //==============================================================================
 //==============================================================================
 Threadpool::Threadpool(U32 threadsCount)
 Threadpool::Threadpool(U32 threadsCount)
 #if !ANKI_DISABLE_THREADPOOL_THREADING
 #if !ANKI_DISABLE_THREADPOOL_THREADING
-:	m_barrier(threadsCount + 1)
+	: m_barrier(threadsCount + 1)
 #endif
 #endif
 {
 {
 	m_threadsCount = threadsCount;
 	m_threadsCount = threadsCount;
@@ -132,7 +132,8 @@ Threadpool::Threadpool(U32 threadsCount)
 
 
 	while(threadsCount-- != 0)
 	while(threadsCount-- != 0)
 	{
 	{
-		construct(&m_threads[threadsCount], threadsCount, this);
+		::new(&m_threads[threadsCount]) detail::ThreadpoolThread(
+			threadsCount, this);
 	}
 	}
 #endif
 #endif
 }
 }

+ 19 - 0
tests/util/HashMap.cpp

@@ -180,3 +180,22 @@ ANKI_TEST(Util, HashMap)
 		akMap.destroy(alloc);
 		akMap.destroy(alloc);
 	}
 	}
 }
 }
+
+class Hashable
+{
+	ANKI_ENABLE_HASH_MAP(Hashable)
+public:
+	Hashable(U64 hash, int x)
+		: m_hash(hash)
+		, m_x(x)
+	{}
+
+	int m_x;
+};
+
+ANKI_TEST(Util, HashMap_enableHashMap)
+{
+	Hashable a(1, 1);
+	Hashable b(2, 2);
+	Hashable c(10, 10);
+}