Better int hashing. This was going to bite me sooner or later.

This commit is contained in:
Andreas Kling 2018-10-27 09:33:24 +02:00
parent ec07761d0f
commit 601d0d1739
Notes: sideshowbarker 2024-07-19 18:37:42 +09:00
4 changed files with 32 additions and 5 deletions

20
AK/HashFunctions.h Normal file
View file

@ -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)));
}

View file

@ -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); }
};

View file

@ -33,7 +33,6 @@ int main(int, char**)
for (auto& it : tab) {
printf("%s\n", it.value.s.characters());
}
return 0;
}
{

View file

@ -47,5 +47,5 @@ clean:
rm -f $(OBJS) $(PROGRAM)
$(PROGRAM): $(OBJS)
$(CXX) $(LDFLAGS) -o $@ $(OBJS)
$(CXX) -o $@ $(OBJS) $(LDFLAGS)