From: cedric Date: Mon, 8 Dec 2008 17:31:55 +0000 (+0000) Subject: Add integer and pointer hash table helper. X-Git-Tag: submit/2.0alpha-wayland/20121127.222009~1718 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5294bc31eeea87ff2151c435c2ff95c6d8183a19;p=profile%2Fivi%2Feina.git Add integer and pointer hash table helper. git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@38026 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- diff --git a/src/include/eina_hash.h b/src/include/eina_hash.h index b7e7d6d..f37d1f2 100644 --- a/src/include/eina_hash.h +++ b/src/include/eina_hash.h @@ -68,6 +68,9 @@ EAPI Eina_Hash * eina_hash_new(Eina_Key_Length key_length_cb, Eina_Free_Cb data_free_cb); EAPI Eina_Hash * eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb); EAPI Eina_Hash * eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb); +EAPI Eina_Hash * eina_hash_int32_new(Eina_Free_Cb data_free_cb); +EAPI Eina_Hash * eina_hash_int64_new(Eina_Free_Cb data_free_cb); +EAPI Eina_Hash * eina_hash_pointer_new(Eina_Free_Cb data_free_cb); EAPI Eina_Bool eina_hash_add(Eina_Hash *hash, const void *key, const void *data); EAPI Eina_Bool eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data); @@ -114,6 +117,10 @@ EAPI int eina_hash_superfast(const char *key, int len); /* Hash function first reported by dan bernstein many years ago in comp.lang.c */ static inline int eina_hash_djb2(const char *key, int len); +/* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */ +static inline int eina_hash_int32(unsigned int *pkey, __UNUSED__ int len); +static inline int eina_hash_int64(unsigned long int *pkey, __UNUSED__ int len); + #include "eina_inline_hash.x" /** diff --git a/src/include/eina_inline_hash.x b/src/include/eina_inline_hash.x index 9808893..0149c0d 100644 --- a/src/include/eina_inline_hash.x +++ b/src/include/eina_inline_hash.x @@ -53,4 +53,32 @@ eina_hash_djb2_len(const char *key, int *plen) return (int)hash_num; } +static inline int +eina_hash_int32(unsigned int *pkey, __UNUSED__ int len) +{ + unsigned int key = *pkey; + + key = ~key + (key << 15); + key = key ^ (key >> 12); + key = key + (key << 2); + key = key ^ (key >> 4); + key = key * 2057; + key = key ^ (key >> 16); + return key; +} + +static inline int +eina_hash_int64(unsigned long int *pkey, __UNUSED__ int len) +{ + unsigned long int key = *pkey; + + key = (~key) + (key << 18); + key = key ^ (key >> 31); + key = key * 21; + key = key ^ (key >> 11); + key = key + (key << 6); + key = key ^ (key >> 22); + return (int) key; +} + #endif diff --git a/src/lib/eina_hash.c b/src/lib/eina_hash.c index dea2882..d20f2ba 100644 --- a/src/lib/eina_hash.c +++ b/src/lib/eina_hash.c @@ -349,6 +349,41 @@ _eina_string_key_cmp(const char *key1, __UNUSED__ int key1_length, return strcmp(key1, key2); } +static unsigned int +_eina_int32_key_length(__UNUSED__ const uint32_t *key) +{ + return 4; +} + +static int +_eina_int32_key_cmp(const uint32_t *key1, __UNUSED__ int key1_length, + const uint32_t *key2, __UNUSED__ int key2_length) +{ + if (*key1 > *key2) + return 1; + if (*key1 < *key2) + return -1; + return 0; +} + +static unsigned int +_eina_int64_key_length(__UNUSED__ const uint32_t *key) +{ + return 8; +} + +static int +_eina_int64_key_cmp(const uint64_t *key1, __UNUSED__ int key1_length, + const uint64_t *key2, __UNUSED__ int key2_length) +{ + if (*key1 > *key2) + return 1; + if (*key1 < *key2) + return -1; + return 0; +} + + static Eina_Bool _eina_foreach_cb(const Eina_Hash *hash, Eina_Hash_Tuple *data, Eina_Hash_Foreach_Data *fdata) { @@ -605,6 +640,40 @@ eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb) data_free_cb); } +EAPI Eina_Hash * +eina_hash_int32_new(Eina_Free_Cb data_free_cb) +{ + return eina_hash_new(EINA_KEY_LENGTH(_eina_int32_key_length), + EINA_KEY_CMP(_eina_int32_key_cmp), + EINA_KEY_HASH(eina_hash_int32), + data_free_cb); +} + +EAPI Eina_Hash * +eina_hash_int64_new(Eina_Free_Cb data_free_cb) +{ + return eina_hash_new(EINA_KEY_LENGTH(_eina_int64_key_length), + EINA_KEY_CMP(_eina_int64_key_cmp), + EINA_KEY_HASH(eina_hash_int64), + data_free_cb); +} + +EAPI Eina_Hash * +eina_hash_pointer_new(Eina_Free_Cb data_free_cb) +{ +#ifdef __LP64__ + return eina_hash_new(EINA_KEY_LENGTH(_eina_int64_key_length), + EINA_KEY_CMP(_eina_int64_key_cmp), + EINA_KEY_HASH(eina_hash_int64), + data_free_cb); +#else + return eina_hash_new(EINA_KEY_LENGTH(_eina_int32_key_length), + EINA_KEY_CMP(_eina_int32_key_cmp), + EINA_KEY_HASH(eina_hash_int32), + data_free_cb); +#endif +} + /** * Retrieves the number of buckets available in the given hash table. * @param hash The given hash table.