diff --git a/src/lol/core/map.h b/src/lol/core/map.h index 023636c3..254579d9 100644 --- a/src/lol/core/map.h +++ b/src/lol/core/map.h @@ -20,37 +20,34 @@ namespace lol { -/* A stupidly linear map for now */ +/* A stupidly linear map for now. */ template class Map : protected Hash { public: + /* I choose to make this inline because passing the key by reference + * is usually suboptimal. */ inline V const& operator[] (K const &key) const { + /* Look for the hash in our table and return the value. */ uint32_t hash = ((Hash const &)*this)(key); for (int i = 0; i < m_array.Count(); ++i) if (m_array[i].m1 == hash) if (m_array[i].m2 == key) return m_array[i].m3; + /* XXX: this in an error! */ return V(); } inline V & operator[] (K const &key) { - return const_cast((const_cast const &>(*this))[key]); - } - - inline V & Set(K const &key, V const &val) - { + /* Look for the hash in our table and return the value if found. */ uint32_t hash = ((Hash const &)*this)(key); for (int i = 0; i < m_array.Count(); ++i) if (m_array[i].m1 == hash) if (m_array[i].m2 == key) - { - m_array[i].m3.~V(); - return m_array[i].m3 = val; - } - - m_array.Push(hash, key, val); + return m_array[i].m3; + /* If not found, insert a new value. */ + m_array.Push(hash, key, V()); return m_array.Last().m3; } @@ -66,6 +63,16 @@ public: } } + inline bool HasKey(K const &key) + { + uint32_t hash = ((Hash const &)*this)(key); + for (int i = 0; i < m_array.Count(); ++i) + if (m_array[i].m1 == hash) + if (m_array[i].m2 == key) + return true; + return false; + } + private: Array m_array; }; diff --git a/test/Makefile.am b/test/Makefile.am index 3084f470..458d75be 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -22,7 +22,7 @@ endif testsuite_SOURCES = testsuite.cpp \ unit/vector.cpp unit/matrix.cpp unit/half.cpp unit/trig.cpp \ unit/build.cpp unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp \ - unit/array.cpp unit/rotation.cpp unit/string.cpp + unit/array.cpp unit/rotation.cpp unit/string.cpp unit/map.cpp testsuite_CPPFLAGS = @LOL_CFLAGS@ testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ testsuite_DEPENDENCIES = $(top_builddir)/src/liblol.a diff --git a/test/testsuite.vcxproj b/test/testsuite.vcxproj index bb8cb86b..3263513e 100644 --- a/test/testsuite.vcxproj +++ b/test/testsuite.vcxproj @@ -41,10 +41,12 @@ + + @@ -73,4 +75,4 @@ - \ No newline at end of file + diff --git a/test/unit/map.cpp b/test/unit/map.cpp new file mode 100644 index 00000000..fb21dcea --- /dev/null +++ b/test/unit/map.cpp @@ -0,0 +1,63 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include "core.h" +#include "lol/unit.h" + +namespace lol +{ + +LOLUNIT_FIXTURE(MapTest) +{ + void SetUp() {} + + void TearDown() {} + + LOLUNIT_TEST(MapDeclare) + { + Map m1; + Map m2; + Map m3; + Map m4; + } + + LOLUNIT_TEST(MapSet) + { + Map map; + + for (int i = 0; i < 100000; i++) + map[i] = -1; + + for (int i = 0; i < 100000; i++) + map[i] = i; + + for (int i = 0; i < 100000; i++) + LOLUNIT_ASSERT_EQUAL(map[i], i); + } + + LOLUNIT_TEST(MapHasKey) + { + Map map; + + map[0] = 1; + map[2] = 2; + + LOLUNIT_ASSERT(map.HasKey(0)); + LOLUNIT_ASSERT(!map.HasKey(1)); + LOLUNIT_ASSERT(map.HasKey(2)); + } +}; + +} /* namespace lol */ +