1 /* EINA - EFL data type library
2 * Copyright (C) 2002-2008 Carsten Haitzler
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
19 #ifndef EINA_INLINE_HASH_X_
20 #define EINA_INLINE_HASH_X_
23 djb2 hash algorithm was first reported by dan bernstein, and was the old
24 default hash function for evas.
27 eina_hash_djb2(const char *key, int len)
29 unsigned int hash_num = 5381;
30 const unsigned char *ptr;
33 for (ptr = (unsigned char *)key; len; ptr++, len--)
34 hash_num = ((hash_num << 5) + hash_num) ^ *ptr; /* hash * 33 ^ c */
40 eina_hash_djb2_len(const char *key, int *plen)
42 unsigned int hash_num = 5381;
44 const unsigned char *ptr;
48 for (ptr = (unsigned char *)key; *ptr; ptr++, len++)
49 hash_num = ((hash_num << 5) + hash_num) ^ *ptr; /* hash * 33 ^ c */
57 eina_hash_int32(const unsigned int *pkey, int len)
59 unsigned int key = *pkey;
63 key = ~key + (key << 15);
73 eina_hash_int64(const unsigned long int *pkey, int len)
75 unsigned long int key = *pkey;
79 key = ~key + (key << 18);
88 static inline unsigned int _rotl32(unsigned int x, char r)
90 return (x << r) | (x >> (32 - r));
93 static inline unsigned int _fmix32(unsigned int h)
105 eina_hash_murmur3(const char *key, int len)
107 const unsigned char * data = (const unsigned char*)key;
108 const int nblocks = len / 4;
109 unsigned int h1 = 0, k1;
110 unsigned int c1 = 0xcc9e2d51;
111 unsigned int c2 = 0x1b873593;
112 const unsigned int * blocks = (const unsigned int *)(data + nblocks*4);
114 const unsigned char *tail;
116 for(i = -nblocks; i; i++)
121 k1 = _rotl32(k1, 15);
125 h1 = _rotl32(h1, 13);
126 h1 = h1*5+0xe6546b64;
129 tail = (const unsigned char*)(data + nblocks*4);
142 k1 = _rotl32(k1, 16);