Added simple integer and pointer hashes
This commit is contained in:
parent
2cf895daf9
commit
796a3d3639
@ -29,6 +29,8 @@ UTIL_FILES = \
|
|||||||
fwd.hpp \
|
fwd.hpp \
|
||||||
guid.cpp \
|
guid.cpp \
|
||||||
guid.hpp \
|
guid.hpp \
|
||||||
|
hash.cpp \
|
||||||
|
hash.hpp \
|
||||||
io.cpp \
|
io.cpp \
|
||||||
io.hpp \
|
io.hpp \
|
||||||
ip.cpp \
|
ip.cpp \
|
||||||
|
53
hash.cpp
Normal file
53
hash.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of libgim.
|
||||||
|
*
|
||||||
|
* Waif is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* Waif is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2010 Danny Robson <danny@blubinc.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "hash.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
// Thomas Wang's 32 bit mixing function
|
||||||
|
uint32_t
|
||||||
|
hash (uint32_t key) {
|
||||||
|
uint32_t c2 = 0x27d4eb2d; // a prime or an odd constant
|
||||||
|
key = (key ^ 61) ^ (key >> 16);
|
||||||
|
key = key + (key << 3);
|
||||||
|
key = key ^ (key >> 4);
|
||||||
|
key = key * c2;
|
||||||
|
key = key ^ (key >> 15);
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Thomas Wang's 64 bit mixing function
|
||||||
|
uint64_t
|
||||||
|
hash (uint64_t key) {
|
||||||
|
key = (~key) + (key << 21); // key = (key << 21) - key - 1;
|
||||||
|
key = key ^ (key >> 24);
|
||||||
|
key = (key + (key << 3)) + (key << 8); // key * 265
|
||||||
|
key = key ^ (key >> 14);
|
||||||
|
key = (key + (key << 2)) + (key << 4); // key * 21
|
||||||
|
key = key ^ (key >> 28);
|
||||||
|
key = key + (key << 31);
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uintptr_t
|
||||||
|
hash (void *const key)
|
||||||
|
{ return hash ((uintptr_t)key); }
|
29
hash.hpp
Normal file
29
hash.hpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of libgim.
|
||||||
|
*
|
||||||
|
* Waif is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* Waif is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2010 Danny Robson <danny@blubinc.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_HASH_HPP
|
||||||
|
#define __UTIL_HASH_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
uint32_t hash (uint32_t key);
|
||||||
|
uint64_t hash (uint64_t key);
|
||||||
|
uintptr_t hash (void * const);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user