diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-27 09:33:24 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-27 09:33:24 +0200 |
commit | 601d0d1739139a0467a994165057310e84663803 (patch) | |
tree | 136a6cfe505038c2163beae2532d0c0c05f5a995 | |
parent | ec07761d0fb654ac7a43089ad865a88340e67f3e (diff) | |
download | serenity-601d0d1739139a0467a994165057310e84663803.zip |
Better int hashing. This was going to bite me sooner or later.
-rw-r--r-- | AK/HashFunctions.h | 20 | ||||
-rw-r--r-- | AK/Traits.h | 14 | ||||
-rw-r--r-- | AK/test.cpp | 1 | ||||
-rw-r--r-- | Widgets/Makefile | 2 |
4 files changed, 32 insertions, 5 deletions
diff --git a/AK/HashFunctions.h b/AK/HashFunctions.h new file mode 100644 index 0000000000..6f9829c0cb --- /dev/null +++ b/AK/HashFunctions.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Types.h" + +inline unsigned intHash(dword key) +{ + key += ~(key << 15); + key ^= (key >> 10); + key += (key << 3); + key ^= (key >> 6); + key += ~(key << 11); + key ^= (key >> 16); + return key; +} + +inline unsigned pairIntHash(dword key1, dword key2) +{ + return intHash((intHash(key1) * 209) ^ (intHash(key2 * 413))); +} + diff --git a/AK/Traits.h b/AK/Traits.h index aa0d307e57..d9e64b0435 100644 --- a/AK/Traits.h +++ b/AK/Traits.h @@ -1,6 +1,7 @@ #pragma once #include "kstdio.h" +#include "HashFunctions.h" namespace AK { @@ -11,19 +12,26 @@ struct Traits template<> struct Traits<int> { - static unsigned hash(int i) { return i; } + static unsigned hash(int i) { return intHash(i); } static void dump(int i) { kprintf("%d", i); } }; template<> struct Traits<unsigned> { - static unsigned hash(unsigned u) { return u; } + static unsigned hash(unsigned u) { return intHash(u); } static void dump(unsigned u) { kprintf("%u", u); } }; template<typename T> struct Traits<T*> { - static unsigned hash(const T* p) { return (unsigned)p; } + static unsigned hash(const T* p) + { +#ifdef SERENITY + return intHash((dword)p); +#else + return intHash((ptrdiff_t)p & 0xffffffff); +#endif + } static void dump(const T* p) { kprintf("%p", p); } }; diff --git a/AK/test.cpp b/AK/test.cpp index a181da2c4d..0b359d59bc 100644 --- a/AK/test.cpp +++ b/AK/test.cpp @@ -33,7 +33,6 @@ int main(int, char**) for (auto& it : tab) { printf("%s\n", it.value.s.characters()); } - return 0; } { diff --git a/Widgets/Makefile b/Widgets/Makefile index 52835f08f5..32add4ee28 100644 --- a/Widgets/Makefile +++ b/Widgets/Makefile @@ -47,5 +47,5 @@ clean: rm -f $(OBJS) $(PROGRAM) $(PROGRAM): $(OBJS) - $(CXX) $(LDFLAGS) -o $@ $(OBJS) + $(CXX) -o $@ $(OBJS) $(LDFLAGS) |