mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
Better int hashing. This was going to bite me sooner or later.
This commit is contained in:
parent
ec07761d0f
commit
601d0d1739
Notes:
sideshowbarker
2024-07-19 18:37:42 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/601d0d17391
4 changed files with 32 additions and 5 deletions
20
AK/HashFunctions.h
Normal file
20
AK/HashFunctions.h
Normal 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)));
|
||||
}
|
||||
|
14
AK/Traits.h
14
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); }
|
||||
};
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@ int main(int, char**)
|
|||
for (auto& it : tab) {
|
||||
printf("%s\n", it.value.s.characters());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
{
|
||||
|
|
|
@ -47,5 +47,5 @@ clean:
|
|||
rm -f $(OBJS) $(PROGRAM)
|
||||
|
||||
$(PROGRAM): $(OBJS)
|
||||
$(CXX) $(LDFLAGS) -o $@ $(OBJS)
|
||||
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||
|
||||
|
|
Loading…
Reference in a new issue