2008-09-01 05:42:16 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2002-2008 Carsten Haitzler
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
#ifndef EINA_INLINE_HASH_X_
|
|
|
|
#define EINA_INLINE_HASH_X_
|
|
|
|
|
|
|
|
/*
|
|
|
|
djb2 hash algorithm was first reported by dan bernstein, and was the old
|
|
|
|
default hash function for evas.
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
eina_hash_djb2(const char *key, int len)
|
|
|
|
{
|
|
|
|
unsigned int hash_num = 5381;
|
|
|
|
const unsigned char *ptr;
|
|
|
|
|
|
|
|
if (!key) return 0;
|
|
|
|
for (ptr = (unsigned char *)key; len; ptr++, len--)
|
|
|
|
hash_num = ((hash_num << 5) + hash_num) ^ *ptr; /* hash * 33 ^ c */
|
|
|
|
|
|
|
|
return (int)hash_num;
|
|
|
|
}
|
|
|
|
|
2008-09-15 06:38:42 -07:00
|
|
|
static inline int
|
|
|
|
eina_hash_djb2_len(const char *key, int *plen)
|
|
|
|
{
|
|
|
|
unsigned int hash_num = 5381;
|
|
|
|
int len = 0;
|
|
|
|
const unsigned char *ptr;
|
|
|
|
|
|
|
|
if (!key) return 0;
|
|
|
|
|
|
|
|
for (ptr = (unsigned char *)key; *ptr; ptr++, len++)
|
|
|
|
hash_num = ((hash_num << 5) + hash_num) ^ *ptr; /* hash * 33 ^ c */
|
|
|
|
|
|
|
|
*plen = len + 1;
|
|
|
|
|
|
|
|
return (int)hash_num;
|
|
|
|
}
|
|
|
|
|
2008-12-08 09:31:55 -08:00
|
|
|
static inline int
|
2009-03-19 18:00:04 -07:00
|
|
|
eina_hash_int32(const unsigned int *pkey, int len)
|
2008-12-08 09:31:55 -08:00
|
|
|
{
|
|
|
|
unsigned int key = *pkey;
|
|
|
|
|
2008-12-09 03:01:12 -08:00
|
|
|
(void) len;
|
|
|
|
|
2011-03-28 14:37:35 -07:00
|
|
|
key = ~key + (key << 15);
|
|
|
|
key ^= key >> 12;
|
|
|
|
key += key << 2;
|
|
|
|
key ^= key >> 4;
|
|
|
|
key *= 2057;
|
|
|
|
key ^= key >> 16;
|
2008-12-08 09:31:55 -08:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2009-03-19 18:00:04 -07:00
|
|
|
eina_hash_int64(const unsigned long int *pkey, int len)
|
2008-12-08 09:31:55 -08:00
|
|
|
{
|
|
|
|
unsigned long int key = *pkey;
|
|
|
|
|
2008-12-09 03:01:12 -08:00
|
|
|
(void) len;
|
|
|
|
|
2011-03-28 14:37:35 -07:00
|
|
|
key = ~key + (key << 18);
|
|
|
|
key ^= key >> 31;
|
|
|
|
key *= 21;
|
|
|
|
key ^= key >> 11;
|
|
|
|
key += key << 6;
|
|
|
|
key ^= key >> 22;
|
2008-12-08 09:31:55 -08:00
|
|
|
return (int) key;
|
|
|
|
}
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
#endif
|