2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2002-2008 Carsten Haitzler, Gustavo Sverzut Barbieri,
|
|
|
|
* Vincent Torri, Jorge Luis Zapata Muga, Cedric Bail
|
|
|
|
*
|
|
|
|
* 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
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2009-06-04 02:02:29 -07:00
|
|
|
#include <stdio.h>
|
2008-08-28 00:46:42 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
* eina/src/include/eina_array.h,
* eina/src/include/eina_f16p16.h,
* eina/src/include/eina_accessor.h,
* eina/src/include/eina_list.h,
* eina/src/include/eina_iterator.h,
* eina/src/lib/eina_rectangle.c,
* eina/src/lib/eina_list.c,
* eina/src/lib/eina_array.c,
* eina/src/lib/eina_hash.c,
* eina/src/lib/eina_module.c,
* eina/src/lib/eina_stringshare.c,
* eina/src/lib/eina_benchmark.c: Fix for windows compilation.
SVN revision: 38663
2009-01-20 07:56:48 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <Evil.h>
|
|
|
|
#else
|
|
|
|
# include <stdint.h>
|
|
|
|
#endif
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_config.h"
|
|
|
|
#include "eina_private.h"
|
2008-08-28 09:35:43 -07:00
|
|
|
#include "eina_rbtree.h"
|
2008-07-30 06:35:49 -07:00
|
|
|
#include "eina_error.h"
|
2008-08-07 06:11:13 -07:00
|
|
|
|
2009-09-06 15:21:56 -07:00
|
|
|
/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
|
|
|
|
#include "eina_safety_checks.h"
|
|
|
|
#include "eina_hash.h"
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
2008-08-12 09:01:10 -07:00
|
|
|
* Local *
|
2008-07-30 05:46:55 -07:00
|
|
|
*============================================================================*/
|
2008-08-27 06:34:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @cond LOCAL
|
|
|
|
*/
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
#define EINA_MAGIC_CHECK_HASH(d) \
|
|
|
|
do { \
|
|
|
|
if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_HASH)) \
|
|
|
|
EINA_MAGIC_FAIL(d, EINA_MAGIC_HASH); \
|
|
|
|
} while(0)
|
|
|
|
|
2009-03-24 00:10:46 -07:00
|
|
|
#define EINA_MAGIC_CHECK_HASH_ITERATOR(d, ...) \
|
2008-12-07 05:28:29 -08:00
|
|
|
do { \
|
|
|
|
if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_HASH_ITERATOR)) \
|
2009-03-23 13:36:10 -07:00
|
|
|
{ \
|
|
|
|
EINA_MAGIC_FAIL(d, EINA_MAGIC_HASH_ITERATOR); \
|
2009-03-24 00:10:46 -07:00
|
|
|
return __VA_ARGS__; \
|
2009-03-23 13:36:10 -07:00
|
|
|
} \
|
2008-12-07 05:28:29 -08:00
|
|
|
} while(0)
|
|
|
|
|
2008-12-16 08:49:29 -08:00
|
|
|
#define EINA_HASH_BUCKET_SIZE 8
|
|
|
|
#define EINA_HASH_SMALL_BUCKET_SIZE 5
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
#define EINA_HASH_RBTREE_MASK 0xFFF
|
|
|
|
|
|
|
|
typedef struct _Eina_Hash_Head Eina_Hash_Head;
|
2008-07-30 05:46:55 -07:00
|
|
|
typedef struct _Eina_Hash_El Eina_Hash_El;
|
2008-12-05 18:21:17 -08:00
|
|
|
typedef struct _Eina_Hash_Foreach_Data Eina_Hash_Foreach_Data;
|
2008-08-11 09:30:16 -07:00
|
|
|
typedef struct _Eina_Iterator_Hash Eina_Iterator_Hash;
|
2008-08-28 09:35:43 -07:00
|
|
|
typedef struct _Eina_Hash_Each Eina_Hash_Each;
|
2008-08-11 09:30:16 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
struct _Eina_Hash
|
|
|
|
{
|
2008-08-06 08:46:57 -07:00
|
|
|
Eina_Key_Length key_length_cb;
|
|
|
|
Eina_Key_Cmp key_cmp_cb;
|
|
|
|
Eina_Key_Hash key_hash_cb;
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Free_Cb data_free_cb;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
Eina_Rbtree **buckets;
|
2008-12-16 08:49:29 -08:00
|
|
|
int size;
|
|
|
|
int mask;
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
int population;
|
2008-12-07 05:28:29 -08:00
|
|
|
|
* eina/src/include/eina_array.h,
* eina/src/include/eina_f16p16.h,
* eina/src/include/eina_accessor.h,
* eina/src/include/eina_list.h,
* eina/src/include/eina_iterator.h,
* eina/src/lib/eina_rectangle.c,
* eina/src/lib/eina_list.c,
* eina/src/lib/eina_array.c,
* eina/src/lib/eina_hash.c,
* eina/src/lib/eina_module.c,
* eina/src/lib/eina_stringshare.c,
* eina/src/lib/eina_benchmark.c: Fix for windows compilation.
SVN revision: 38663
2009-01-20 07:56:48 -08:00
|
|
|
EINA_MAGIC
|
2008-07-30 05:46:55 -07:00
|
|
|
};
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
struct _Eina_Hash_Head
|
|
|
|
{
|
|
|
|
EINA_RBTREE;
|
|
|
|
int hash;
|
|
|
|
|
|
|
|
Eina_Rbtree *head;
|
|
|
|
};
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
struct _Eina_Hash_El
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
EINA_RBTREE;
|
2008-08-11 09:30:16 -07:00
|
|
|
Eina_Hash_Tuple tuple;
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Bool begin : 1;
|
2008-08-11 09:30:16 -07:00
|
|
|
};
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-05 18:21:17 -08:00
|
|
|
struct _Eina_Hash_Foreach_Data
|
2008-08-11 09:30:16 -07:00
|
|
|
{
|
2008-12-05 18:21:17 -08:00
|
|
|
Eina_Hash_Foreach cb;
|
2008-08-11 09:30:16 -07:00
|
|
|
const void *fdata;
|
|
|
|
};
|
|
|
|
|
2008-08-11 10:22:36 -07:00
|
|
|
typedef void *(*Eina_Iterator_Get_Content_Callback)(Eina_Iterator_Hash *it);
|
|
|
|
#define FUNC_ITERATOR_GET_CONTENT(Function) ((Eina_Iterator_Get_Content_Callback)Function)
|
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
struct _Eina_Iterator_Hash
|
|
|
|
{
|
|
|
|
Eina_Iterator iterator;
|
|
|
|
|
2008-08-11 10:22:36 -07:00
|
|
|
Eina_Iterator_Get_Content_Callback get_content;
|
2008-08-11 09:30:16 -07:00
|
|
|
const Eina_Hash *hash;
|
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
Eina_Iterator *current;
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Iterator *list;
|
|
|
|
Eina_Hash_Head *eh;
|
2008-08-28 09:35:43 -07:00
|
|
|
Eina_Hash_El *el;
|
2008-08-11 09:30:16 -07:00
|
|
|
int bucket;
|
|
|
|
|
|
|
|
int index;
|
2008-12-07 05:28:29 -08:00
|
|
|
|
* eina/src/include/eina_array.h,
* eina/src/include/eina_f16p16.h,
* eina/src/include/eina_accessor.h,
* eina/src/include/eina_list.h,
* eina/src/include/eina_iterator.h,
* eina/src/lib/eina_rectangle.c,
* eina/src/lib/eina_list.c,
* eina/src/lib/eina_array.c,
* eina/src/lib/eina_hash.c,
* eina/src/lib/eina_module.c,
* eina/src/lib/eina_stringshare.c,
* eina/src/lib/eina_benchmark.c: Fix for windows compilation.
SVN revision: 38663
2009-01-20 07:56:48 -08:00
|
|
|
EINA_MAGIC
|
2008-07-30 05:46:55 -07:00
|
|
|
};
|
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
struct _Eina_Hash_Each
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Hash_Head *eh;
|
2008-08-28 09:35:43 -07:00
|
|
|
const Eina_Hash_El *el;
|
|
|
|
const void *data;
|
|
|
|
};
|
|
|
|
|
2008-08-26 01:59:25 -07:00
|
|
|
#undef get16bits
|
|
|
|
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
|
|
|
|
|| defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
|
2008-08-27 06:34:04 -07:00
|
|
|
# define get16bits(d) (*((const uint16_t *) (d)))
|
2008-08-26 01:59:25 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined (get16bits)
|
2008-08-27 06:34:04 -07:00
|
|
|
# define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
|
|
|
|
+(uint32_t)(((const uint8_t *)(d))[0]) )
|
2008-08-26 01:59:25 -07:00
|
|
|
#endif
|
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
static inline int
|
2008-09-24 10:14:29 -07:00
|
|
|
_eina_hash_hash_rbtree_cmp_hash(const Eina_Hash_Head *eh, const int *hash, __UNUSED__ int key_length, __UNUSED__ void *data)
|
|
|
|
{
|
|
|
|
return eh->hash - *hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Rbtree_Direction
|
|
|
|
_eina_hash_hash_rbtree_cmp_node(const Eina_Hash_Head *left, const Eina_Hash_Head *right, __UNUSED__ void *data)
|
|
|
|
{
|
|
|
|
if (left->hash - right->hash < 0)
|
|
|
|
return EINA_RBTREE_LEFT;
|
|
|
|
return EINA_RBTREE_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
_eina_hash_key_rbtree_cmp_key_data(const Eina_Hash_El *el, const Eina_Hash_Tuple *tuple, __UNUSED__ unsigned int key_length, Eina_Key_Cmp cmp)
|
2008-08-28 09:35:43 -07:00
|
|
|
{
|
2008-09-19 05:19:06 -07:00
|
|
|
int result;
|
|
|
|
|
|
|
|
result = cmp(el->tuple.key, el->tuple.key_length, tuple->key, tuple->key_length);
|
2008-09-24 10:14:29 -07:00
|
|
|
|
|
|
|
if (result == 0 && tuple->data && tuple->data != el->tuple.data)
|
|
|
|
return 1;
|
2008-09-19 05:19:06 -07:00
|
|
|
return result;
|
2008-08-28 09:35:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Rbtree_Direction
|
2008-09-24 10:14:29 -07:00
|
|
|
_eina_hash_key_rbtree_cmp_node(const Eina_Hash_El *left, const Eina_Hash_El *right, Eina_Key_Cmp cmp)
|
2008-08-28 09:35:43 -07:00
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
int result;
|
|
|
|
|
|
|
|
result = cmp(left->tuple.key, left->tuple.key_length,
|
|
|
|
right->tuple.key, right->tuple.key_length);
|
|
|
|
|
|
|
|
if (result < 0)
|
2008-08-28 09:35:43 -07:00
|
|
|
return EINA_RBTREE_LEFT;
|
|
|
|
return EINA_RBTREE_RIGHT;
|
|
|
|
}
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
static inline Eina_Bool
|
|
|
|
eina_hash_add_alloc_by_hash(Eina_Hash *hash,
|
|
|
|
const void *key, int key_length, int alloc_length,
|
|
|
|
int key_hash,
|
|
|
|
const void *data)
|
2008-08-28 09:35:43 -07:00
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Hash_El *el = NULL;
|
|
|
|
Eina_Hash_Head *eh;
|
|
|
|
Eina_Error error = 0;
|
|
|
|
int hash_num;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
|
2008-09-24 10:14:29 -07:00
|
|
|
error = EINA_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Apply eina mask to hash. */
|
2008-12-16 08:49:29 -08:00
|
|
|
hash_num = key_hash & hash->mask;
|
2008-09-24 10:14:29 -07:00
|
|
|
key_hash &= EINA_HASH_RBTREE_MASK;
|
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
if (!hash->buckets)
|
|
|
|
{
|
2008-12-16 08:49:29 -08:00
|
|
|
hash->buckets = malloc(sizeof (Eina_Rbtree*) * hash->size);
|
|
|
|
memset(hash->buckets, 0, sizeof (Eina_Rbtree*) * hash->size);
|
2008-12-09 09:39:48 -08:00
|
|
|
|
|
|
|
eh = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Look up for head node. */
|
|
|
|
eh = (Eina_Hash_Head*) eina_rbtree_inline_lookup(hash->buckets[hash_num],
|
|
|
|
&key_hash, 0,
|
|
|
|
EINA_RBTREE_CMP_KEY_CB(_eina_hash_hash_rbtree_cmp_hash), NULL);
|
|
|
|
}
|
2008-09-24 10:14:29 -07:00
|
|
|
|
|
|
|
if (!eh)
|
|
|
|
{
|
|
|
|
/* If not found allocate it and a element. */
|
|
|
|
eh = malloc(sizeof (Eina_Hash_Head) + sizeof (Eina_Hash_El) + alloc_length);
|
|
|
|
if (!eh) goto on_error;
|
|
|
|
eh->hash = key_hash;
|
|
|
|
eh->head = NULL;
|
|
|
|
|
|
|
|
hash->buckets[hash_num] = eina_rbtree_inline_insert(hash->buckets[hash_num], EINA_RBTREE_GET(eh),
|
|
|
|
EINA_RBTREE_CMP_NODE_CB(_eina_hash_hash_rbtree_cmp_node), NULL);
|
|
|
|
|
|
|
|
el = (Eina_Hash_El*) (eh + 1);
|
|
|
|
el->begin = EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!el)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Alloc every needed things
|
|
|
|
(No more lookup as we expect to support more than one item for one key).
|
|
|
|
*/
|
|
|
|
el = malloc(sizeof (Eina_Hash_El) + alloc_length);
|
|
|
|
if (!el) goto on_error;
|
|
|
|
el->begin = EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the element */
|
|
|
|
el->tuple.key_length = key_length;
|
|
|
|
el->tuple.data = (void *) data;
|
|
|
|
if (alloc_length > 0)
|
|
|
|
{
|
|
|
|
el->tuple.key = (char *) (el + 1);
|
|
|
|
memcpy((char *) el->tuple.key, key, alloc_length);
|
|
|
|
}
|
|
|
|
else
|
2008-08-28 09:35:43 -07:00
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
el->tuple.key = key;
|
2008-08-28 09:35:43 -07:00
|
|
|
}
|
2008-09-24 10:14:29 -07:00
|
|
|
|
|
|
|
/* add the new element to the hash. */
|
|
|
|
eh->head = eina_rbtree_inline_insert(eh->head, EINA_RBTREE_GET(el),
|
|
|
|
EINA_RBTREE_CMP_NODE_CB(_eina_hash_key_rbtree_cmp_node), hash->key_cmp_cb);
|
|
|
|
hash->population++;
|
2008-08-28 09:35:43 -07:00
|
|
|
return EINA_TRUE;
|
2008-09-24 10:14:29 -07:00
|
|
|
|
|
|
|
on_error:
|
|
|
|
eina_error_set(error);
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_eina_hash_rbtree_each(__UNUSED__ const Eina_Rbtree *container, const Eina_Hash_Head *eh, Eina_Hash_Each *data)
|
|
|
|
{
|
|
|
|
Eina_Iterator *it;
|
|
|
|
Eina_Hash_El *el;
|
|
|
|
Eina_Bool found = EINA_TRUE;
|
|
|
|
|
|
|
|
it = eina_rbtree_iterator_prefix(eh->head);
|
2009-02-27 08:32:22 -08:00
|
|
|
EINA_ITERATOR_FOREACH(it, el)
|
2008-09-24 10:14:29 -07:00
|
|
|
{
|
|
|
|
if (el->tuple.data == data->data)
|
|
|
|
{
|
|
|
|
data->el = el;
|
|
|
|
data->eh = (Eina_Hash_Head*) eh;
|
|
|
|
found = EINA_FALSE;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eina_iterator_free(it);
|
|
|
|
return found;
|
2008-08-28 09:35:43 -07:00
|
|
|
}
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
static inline Eina_Hash_El *
|
2008-09-24 10:14:29 -07:00
|
|
|
_eina_hash_find_by_hash(const Eina_Hash *hash, Eina_Hash_Tuple *tuple, int key_hash, Eina_Hash_Head **eh)
|
2008-08-06 08:46:57 -07:00
|
|
|
{
|
|
|
|
Eina_Hash_El *el;
|
2008-09-24 10:14:29 -07:00
|
|
|
int rb_hash = key_hash & EINA_HASH_RBTREE_MASK;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-16 08:49:29 -08:00
|
|
|
key_hash &= hash->mask;
|
2008-09-24 10:14:29 -07:00
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
if (!hash->buckets) return NULL;
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
*eh = (Eina_Hash_Head*) eina_rbtree_inline_lookup(hash->buckets[key_hash],
|
|
|
|
&rb_hash, 0,
|
|
|
|
EINA_RBTREE_CMP_KEY_CB(_eina_hash_hash_rbtree_cmp_hash), NULL);
|
|
|
|
if (!*eh) return NULL;
|
|
|
|
|
|
|
|
el = (Eina_Hash_El*) eina_rbtree_inline_lookup((*eh)->head,
|
|
|
|
tuple, 0,
|
|
|
|
EINA_RBTREE_CMP_KEY_CB(_eina_hash_key_rbtree_cmp_key_data), hash->key_cmp_cb);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
return el;
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Hash_El *
|
2008-09-24 10:14:29 -07:00
|
|
|
_eina_hash_find_by_data(const Eina_Hash *hash, const void *data, int *key_hash, Eina_Hash_Head **eh)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-28 09:35:43 -07:00
|
|
|
Eina_Hash_Each each;
|
|
|
|
Eina_Iterator *it;
|
2008-08-06 08:46:57 -07:00
|
|
|
int hash_num;
|
2008-07-30 05:46:55 -07:00
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
if (!hash->buckets) return NULL;
|
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
each.el = NULL;
|
|
|
|
each.data = data;
|
|
|
|
|
2008-12-16 08:49:29 -08:00
|
|
|
for (hash_num = 0; hash_num < hash->size; hash_num++)
|
2008-08-28 09:35:43 -07:00
|
|
|
{
|
2008-12-05 22:17:11 -08:00
|
|
|
if (!hash->buckets[hash_num])
|
|
|
|
continue;
|
2008-08-28 09:35:43 -07:00
|
|
|
it = eina_rbtree_iterator_prefix(hash->buckets[hash_num]);
|
|
|
|
eina_iterator_foreach(it, EINA_EACH(_eina_hash_rbtree_each), &each);
|
|
|
|
eina_iterator_free(it);
|
|
|
|
|
|
|
|
if (each.el)
|
|
|
|
{
|
2008-08-06 08:46:57 -07:00
|
|
|
*key_hash = hash_num;
|
2008-09-24 10:14:29 -07:00
|
|
|
*eh = each.eh;
|
2008-08-28 09:35:43 -07:00
|
|
|
return (Eina_Hash_El*) each.el;
|
|
|
|
}
|
|
|
|
}
|
2008-07-30 05:46:55 -07:00
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
return NULL;
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
static void
|
|
|
|
_eina_hash_el_free(Eina_Hash_El *el, Eina_Hash *hash)
|
|
|
|
{
|
|
|
|
if (hash->data_free_cb)
|
|
|
|
hash->data_free_cb(el->tuple.data);
|
2008-11-10 06:32:14 -08:00
|
|
|
if (el->begin == EINA_FALSE) free(el);
|
2008-09-24 10:14:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_hash_head_free(Eina_Hash_Head *eh, Eina_Hash *hash)
|
|
|
|
{
|
|
|
|
eina_rbtree_delete(eh->head, EINA_RBTREE_FREE_CB(_eina_hash_el_free), hash);
|
|
|
|
free(eh);
|
|
|
|
}
|
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
static Eina_Bool
|
|
|
|
_eina_hash_del_by_hash_el(Eina_Hash *hash, Eina_Hash_El *el, Eina_Hash_Head *eh, int key_hash)
|
|
|
|
{
|
|
|
|
eh->head = eina_rbtree_inline_remove(eh->head, EINA_RBTREE_GET(el), EINA_RBTREE_CMP_NODE_CB(_eina_hash_key_rbtree_cmp_node), hash->key_cmp_cb);
|
2008-12-11 05:47:58 -08:00
|
|
|
_eina_hash_el_free(el, hash);
|
2008-12-09 09:39:48 -08:00
|
|
|
|
|
|
|
if (!eh->head)
|
|
|
|
{
|
2008-12-16 08:49:29 -08:00
|
|
|
key_hash &= hash->mask;
|
2008-12-09 09:39:48 -08:00
|
|
|
|
|
|
|
hash->buckets[key_hash] = eina_rbtree_inline_remove(hash->buckets[key_hash], EINA_RBTREE_GET(eh), EINA_RBTREE_CMP_NODE_CB(_eina_hash_hash_rbtree_cmp_node), NULL);
|
|
|
|
free(eh);
|
|
|
|
}
|
|
|
|
|
|
|
|
hash->population--;
|
|
|
|
if (hash->population == 0)
|
|
|
|
{
|
|
|
|
free(hash->buckets);
|
|
|
|
hash->buckets = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_eina_hash_del_by_key_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash, const void *data)
|
|
|
|
{
|
|
|
|
Eina_Hash_El *el;
|
|
|
|
Eina_Hash_Head *eh;
|
|
|
|
Eina_Hash_Tuple tuple;
|
|
|
|
|
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
|
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
if (!hash->buckets) return EINA_FALSE;
|
|
|
|
|
|
|
|
tuple.key = (void *) key;
|
|
|
|
tuple.key_length = key_length;
|
|
|
|
tuple.data = (void *) data;
|
|
|
|
|
|
|
|
el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
|
|
|
|
if (!el) return EINA_FALSE;
|
|
|
|
return _eina_hash_del_by_hash_el(hash, el, eh, key_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_eina_hash_del_by_key(Eina_Hash *hash, const void *key, const void *data)
|
|
|
|
{
|
|
|
|
int key_length, key_hash;
|
|
|
|
|
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
|
|
|
if (!hash) return EINA_FALSE;
|
|
|
|
if (!key) return EINA_FALSE;
|
|
|
|
if (!hash->buckets) return EINA_FALSE;
|
|
|
|
|
2009-06-18 05:08:52 -07:00
|
|
|
key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
|
2008-12-09 09:39:48 -08:00
|
|
|
key_hash = hash->key_hash_cb(key, key_length);
|
|
|
|
return _eina_hash_del_by_key_hash(hash, key, key_length, key_hash, data);
|
|
|
|
}
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
static unsigned int
|
|
|
|
_eina_string_key_length(const char *key)
|
|
|
|
{
|
|
|
|
if (!key) return 0;
|
2009-01-23 08:11:45 -08:00
|
|
|
return (int)strlen(key) + 1;
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_eina_string_key_cmp(const char *key1, __UNUSED__ int key1_length,
|
|
|
|
const char *key2, __UNUSED__ int key2_length)
|
|
|
|
{
|
|
|
|
return strcmp(key1, key2);
|
|
|
|
}
|
|
|
|
|
2009-06-19 07:44:23 -07:00
|
|
|
static int
|
|
|
|
_eina_stringshared_key_cmp(const char *key1, __UNUSED__ int key1_length,
|
|
|
|
const char *key2, __UNUSED__ int key2_length)
|
|
|
|
{
|
|
|
|
return key1 - key2;
|
|
|
|
}
|
|
|
|
|
2008-12-08 09:31:55 -08:00
|
|
|
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)
|
|
|
|
{
|
2009-06-18 05:08:52 -07:00
|
|
|
return *key1 - *key2;
|
2008-12-08 09:31:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2009-06-18 05:08:52 -07:00
|
|
|
return *key1 - *key2;
|
2008-12-08 09:31:55 -08:00
|
|
|
}
|
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
static Eina_Bool
|
2008-12-05 18:21:17 -08:00
|
|
|
_eina_foreach_cb(const Eina_Hash *hash, Eina_Hash_Tuple *data, Eina_Hash_Foreach_Data *fdata)
|
2008-08-11 09:30:16 -07:00
|
|
|
{
|
|
|
|
return fdata->cb((Eina_Hash *) hash, data->key, data->data, (void*) fdata->fdata);
|
|
|
|
}
|
|
|
|
|
2008-08-11 10:22:36 -07:00
|
|
|
static void *
|
|
|
|
_eina_hash_iterator_data_get_content(Eina_Iterator_Hash *it)
|
|
|
|
{
|
2008-12-23 02:02:27 -08:00
|
|
|
Eina_Hash_El *stuff;
|
|
|
|
|
2009-03-23 13:36:10 -07:00
|
|
|
EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
|
2008-12-23 02:02:27 -08:00
|
|
|
|
|
|
|
stuff = it->el;
|
2008-08-11 10:22:36 -07:00
|
|
|
|
|
|
|
if (!stuff) return NULL;
|
|
|
|
return stuff->tuple.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
_eina_hash_iterator_key_get_content(Eina_Iterator_Hash *it)
|
|
|
|
{
|
2008-12-23 02:02:27 -08:00
|
|
|
Eina_Hash_El *stuff;
|
|
|
|
|
2009-03-23 13:36:10 -07:00
|
|
|
EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
|
2008-12-23 02:02:27 -08:00
|
|
|
|
|
|
|
stuff = it->el;
|
2008-08-11 10:22:36 -07:00
|
|
|
|
|
|
|
if (!stuff) return NULL;
|
|
|
|
return (void *) stuff->tuple.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Hash_Tuple *
|
|
|
|
_eina_hash_iterator_tuple_get_content(Eina_Iterator_Hash *it)
|
|
|
|
{
|
2008-12-23 02:02:27 -08:00
|
|
|
Eina_Hash_El *stuff;
|
|
|
|
|
2009-03-23 13:36:10 -07:00
|
|
|
EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
|
2008-12-23 02:02:27 -08:00
|
|
|
|
|
|
|
stuff = it->el;
|
2008-08-11 10:22:36 -07:00
|
|
|
|
|
|
|
if (!stuff) return NULL;
|
|
|
|
return &stuff->tuple;
|
|
|
|
}
|
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
static Eina_Bool
|
2008-08-11 10:22:36 -07:00
|
|
|
_eina_hash_iterator_next(Eina_Iterator_Hash *it, void **data)
|
2008-08-11 09:30:16 -07:00
|
|
|
{
|
2008-08-28 09:35:43 -07:00
|
|
|
Eina_Bool ok;
|
2008-08-11 09:30:16 -07:00
|
|
|
int bucket;
|
|
|
|
|
2008-10-20 07:45:01 -07:00
|
|
|
if (!(it->index < it->hash->population)) return EINA_FALSE;
|
2008-08-11 09:30:16 -07:00
|
|
|
if (it->current == NULL)
|
|
|
|
{
|
2008-08-28 09:35:43 -07:00
|
|
|
ok = EINA_FALSE;
|
2008-08-11 09:30:16 -07:00
|
|
|
bucket = 0;
|
|
|
|
it->index = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
ok = eina_iterator_next(it->list, (void**) &it->el);
|
2008-08-28 09:35:43 -07:00
|
|
|
if (!ok)
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
eina_iterator_free(it->list);
|
|
|
|
it->list = NULL;
|
|
|
|
|
|
|
|
ok = eina_iterator_next(it->current, (void**) &it->eh);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
eina_iterator_free(it->current);
|
|
|
|
it->current = NULL;
|
2008-10-20 07:45:01 -07:00
|
|
|
it->bucket++;
|
2008-09-24 10:14:29 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->list = eina_rbtree_iterator_prefix(it->eh->head);
|
|
|
|
ok = eina_iterator_next(it->list, (void**) &it->el);
|
|
|
|
}
|
2008-08-28 09:35:43 -07:00
|
|
|
}
|
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
bucket = it->bucket;
|
|
|
|
}
|
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
if (ok == EINA_FALSE)
|
2008-08-11 09:30:16 -07:00
|
|
|
{
|
2008-12-16 08:49:29 -08:00
|
|
|
while (bucket < it->hash->size)
|
2008-08-11 09:30:16 -07:00
|
|
|
{
|
|
|
|
if (it->hash->buckets[bucket] != NULL)
|
|
|
|
{
|
2008-08-28 09:35:43 -07:00
|
|
|
it->current = eina_rbtree_iterator_prefix(it->hash->buckets[bucket]);
|
2008-09-24 10:14:29 -07:00
|
|
|
ok = eina_iterator_next(it->current, (void**) &it->eh);
|
2008-08-28 09:35:43 -07:00
|
|
|
if (ok) break ;
|
|
|
|
eina_iterator_free(it->current);
|
|
|
|
it->current = NULL;
|
2008-08-11 09:30:16 -07:00
|
|
|
}
|
|
|
|
++bucket;
|
|
|
|
}
|
2008-11-20 05:26:18 -08:00
|
|
|
if (it->list) eina_iterator_free(it->list);
|
2008-09-24 10:14:29 -07:00
|
|
|
it->list = eina_rbtree_iterator_prefix(it->eh->head);
|
|
|
|
ok = eina_iterator_next(it->list, (void**) &it->el);
|
2008-12-16 08:49:29 -08:00
|
|
|
if (bucket == it->hash->size) ok = EINA_FALSE;
|
2008-08-11 09:30:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
it->index++;
|
|
|
|
it->bucket = bucket;
|
|
|
|
|
2009-08-25 14:05:00 -07:00
|
|
|
if (ok)
|
2008-08-11 10:22:36 -07:00
|
|
|
*data = it->get_content(it);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
2008-08-28 09:35:43 -07:00
|
|
|
return ok;
|
2008-08-11 09:30:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
_eina_hash_iterator_get_container(Eina_Iterator_Hash *it)
|
|
|
|
{
|
2009-03-23 13:36:10 -07:00
|
|
|
EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
|
2008-08-11 09:30:16 -07:00
|
|
|
return (void *) it->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_hash_iterator_free(Eina_Iterator_Hash *it)
|
|
|
|
{
|
2009-03-24 00:10:46 -07:00
|
|
|
EINA_MAGIC_CHECK_HASH_ITERATOR(it);
|
2008-08-28 09:35:43 -07:00
|
|
|
if (it->current) eina_iterator_free(it->current);
|
2008-11-20 05:26:18 -08:00
|
|
|
if (it->list) eina_iterator_free(it->list);
|
2008-08-11 09:30:16 -07:00
|
|
|
free(it);
|
|
|
|
}
|
|
|
|
|
2008-08-27 06:34:04 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
2008-08-06 08:46:57 -07:00
|
|
|
* Global *
|
2008-07-30 05:46:55 -07:00
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
2008-08-06 08:46:57 -07:00
|
|
|
* API *
|
2008-07-30 05:46:55 -07:00
|
|
|
*============================================================================*/
|
2008-08-27 06:34:04 -07:00
|
|
|
|
2008-09-18 22:22:43 -07:00
|
|
|
/**
|
|
|
|
* @addtogroup Eina_Hash_Group Hash Table
|
2008-08-27 06:34:04 -07:00
|
|
|
*
|
|
|
|
* @brief give a small description here : what it is for, what it does
|
|
|
|
* , etc...
|
|
|
|
*
|
|
|
|
* Hash API. Give some hints about the use (functions that must be
|
|
|
|
* used like init / shutdown), general use, etc... Give also a link to
|
|
|
|
* tutorial below.
|
|
|
|
*
|
|
|
|
* @section hashtable_algo Algorithm
|
|
|
|
*
|
|
|
|
* Give here the algorithm used in the implementation
|
|
|
|
*
|
|
|
|
* @section hashtable_perf Performance
|
|
|
|
*
|
|
|
|
* Give some hints about performance if it is possible, and an image !
|
|
|
|
*
|
|
|
|
* @section hashtable_tutorial Tutorial
|
|
|
|
*
|
|
|
|
* Here is a fantastic tutorial about our hash table
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
EAPI Eina_Hash *
|
|
|
|
eina_hash_new(Eina_Key_Length key_length_cb,
|
|
|
|
Eina_Key_Cmp key_cmp_cb,
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Key_Hash key_hash_cb,
|
2008-12-16 08:49:29 -08:00
|
|
|
Eina_Free_Cb data_free_cb,
|
|
|
|
int buckets_power_size)
|
2008-08-06 08:46:57 -07:00
|
|
|
{
|
|
|
|
/* FIXME: Use mempool. */
|
|
|
|
Eina_Hash *new;
|
|
|
|
|
|
|
|
eina_error_set(0);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key_cmp_cb, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key_hash_cb, NULL);
|
|
|
|
EINA_SAFETY_ON_TRUE_RETURN_VAL(buckets_power_size < 3, NULL);
|
|
|
|
EINA_SAFETY_ON_TRUE_RETURN_VAL(buckets_power_size > 16, NULL);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
new = malloc(sizeof (Eina_Hash));
|
2008-08-06 08:46:57 -07:00
|
|
|
if (!new) goto on_error;
|
|
|
|
|
|
|
|
new->key_length_cb = key_length_cb;
|
|
|
|
new->key_cmp_cb = key_cmp_cb;
|
|
|
|
new->key_hash_cb = key_hash_cb;
|
2008-09-24 10:14:29 -07:00
|
|
|
new->data_free_cb = data_free_cb;
|
2008-12-09 09:39:48 -08:00
|
|
|
new->buckets = NULL;
|
2008-09-24 10:14:29 -07:00
|
|
|
new->population = 0;
|
|
|
|
|
2008-12-16 08:49:29 -08:00
|
|
|
new->size = 1 << buckets_power_size;
|
|
|
|
new->mask = new->size - 1;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_SET(new, EINA_MAGIC_HASH);
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
return new;
|
|
|
|
|
|
|
|
on_error:
|
2008-08-07 06:11:13 -07:00
|
|
|
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
|
2008-08-06 08:46:57 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Hash *
|
2008-09-24 10:14:29 -07:00
|
|
|
eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb)
|
2008-08-06 08:46:57 -07:00
|
|
|
{
|
|
|
|
return eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
|
|
|
|
EINA_KEY_CMP(_eina_string_key_cmp),
|
2008-09-24 10:14:29 -07:00
|
|
|
EINA_KEY_HASH(eina_hash_djb2),
|
2008-12-16 08:49:29 -08:00
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Hash *
|
2008-09-24 10:14:29 -07:00
|
|
|
eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb)
|
2008-08-06 08:46:57 -07:00
|
|
|
{
|
|
|
|
return eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
|
|
|
|
EINA_KEY_CMP(_eina_string_key_cmp),
|
2008-09-24 10:14:29 -07:00
|
|
|
EINA_KEY_HASH(eina_hash_superfast),
|
2008-12-16 08:49:29 -08:00
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Hash *
|
|
|
|
eina_hash_string_small_new(Eina_Free_Cb data_free_cb)
|
|
|
|
{
|
|
|
|
return eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
|
|
|
|
EINA_KEY_CMP(_eina_string_key_cmp),
|
|
|
|
EINA_KEY_HASH(eina_hash_superfast),
|
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_SMALL_BUCKET_SIZE);
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
2008-12-08 09:31:55 -08:00
|
|
|
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),
|
2008-12-16 08:49:29 -08:00
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
2008-12-08 09:31:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2008-12-16 08:49:29 -08:00
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
2008-12-08 09:31:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2008-12-16 08:49:29 -08:00
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
2008-12-08 09:31:55 -08:00
|
|
|
#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),
|
2008-12-16 08:49:29 -08:00
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
2008-12-08 09:31:55 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-19 07:44:23 -07:00
|
|
|
EAPI Eina_Hash *
|
|
|
|
eina_hash_stringshared_new(Eina_Free_Cb data_free_cb)
|
|
|
|
{
|
|
|
|
return eina_hash_new(NULL,
|
|
|
|
EINA_KEY_CMP(_eina_stringshared_key_cmp),
|
|
|
|
EINA_KEY_HASH(eina_hash_superfast),
|
|
|
|
data_free_cb,
|
|
|
|
EINA_HASH_BUCKET_SIZE);
|
|
|
|
}
|
|
|
|
|
2008-08-27 06:34:04 -07:00
|
|
|
/**
|
|
|
|
* Retrieves the number of buckets available in the given hash table.
|
|
|
|
* @param hash The given hash table.
|
|
|
|
* @return @c 256 if @p hash is not @c NULL. @c 0 otherwise.
|
|
|
|
*/
|
|
|
|
EAPI int
|
|
|
|
eina_hash_population(const Eina_Hash *hash)
|
|
|
|
{
|
2008-12-29 04:41:46 -08:00
|
|
|
if (!hash) return 0;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-08-27 06:34:04 -07:00
|
|
|
return hash->population;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free an entire hash table
|
|
|
|
* @param hash The hash table to be freed
|
|
|
|
*
|
|
|
|
* This function frees up all the memory allocated to storing the specified
|
2009-10-23 21:30:52 -07:00
|
|
|
* hash tale pointed to by @p hash. If no data_free_cb has been passed to the
|
|
|
|
* hash at creation time, any entries in the table that the program
|
2008-08-27 06:34:04 -07:00
|
|
|
* has no more pointers for elsewhere may now be lost, so this should only be
|
|
|
|
* called if the program has lready freed any allocated data in the hash table
|
|
|
|
* or has the pointers for data in teh table stored elswehere as well.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
|
|
|
* extern Eina_Hash *hash;
|
|
|
|
*
|
|
|
|
* eina_hash_free(hash);
|
|
|
|
* hash = NULL;
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
EAPI void
|
|
|
|
eina_hash_free(Eina_Hash *hash)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(hash);
|
2008-08-27 06:34:04 -07:00
|
|
|
|
2008-12-09 09:39:48 -08:00
|
|
|
if (hash->buckets)
|
2009-01-26 21:11:16 -08:00
|
|
|
{
|
|
|
|
for (i = 0; i < hash->size; i++)
|
|
|
|
eina_rbtree_delete(hash->buckets[i], EINA_RBTREE_FREE_CB(_eina_hash_head_free), hash);
|
|
|
|
free(hash->buckets);
|
|
|
|
}
|
2008-08-27 06:34:04 -07:00
|
|
|
free(hash);
|
|
|
|
}
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
/**
|
|
|
|
* Adds an entry to the given hash table.
|
|
|
|
*
|
|
|
|
* @p key is expected to be a unique string within the hash table.
|
|
|
|
* Otherwise, you cannot be sure which inserted data pointer will be
|
|
|
|
* accessed with @ref eina_hash_find , and removed with
|
|
|
|
* @ref eina_hash_del .
|
|
|
|
*
|
|
|
|
* @p key_hash is expected to always match @p key. Otherwise, you
|
|
|
|
* cannot be sure to find it again with @ref eina_hash_find_by_hash.
|
|
|
|
*
|
|
|
|
* Key strings are case sensitive.
|
|
|
|
*
|
|
|
|
* @ref eina_error_get should be used to determine if an
|
|
|
|
* allocation error occurred during this function.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table. Can be @c NULL.
|
|
|
|
* @param key A unique key. Can be @c NULL.
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param key_length Should be the length of @p key (don't forget to count '\\0' for string).
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key_hash The hash that will always match key.
|
|
|
|
* @param data Data to associate with the string given by @p key.
|
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
|
|
|
*/
|
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_add_by_hash(Eina_Hash *hash,
|
|
|
|
const void *key, int key_length, int key_hash,
|
|
|
|
const void *data)
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
return eina_hash_add_alloc_by_hash(hash, key, key_length, key_length, key_hash, data);
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an entry to the given hash table and does not duplicate the string key.
|
|
|
|
*
|
|
|
|
* @p key is expected to be a unique string within the hash table.
|
|
|
|
* Otherwise, you cannot be sure which inserted data pointer will be
|
|
|
|
* accessed with @ref eina_hash_find , and removed with
|
|
|
|
* @ref eina_hash_del . This call does not make a copy of the key so it must
|
|
|
|
* be a string constant or stored elsewhere (in the object being added) etc.
|
|
|
|
*
|
|
|
|
* @p key_hash is expected to always match @p key. Otherwise, you
|
|
|
|
* cannot be sure to find it again with @ref eina_hash_find_by_hash.
|
|
|
|
*
|
|
|
|
* Key strings are case sensitive.
|
|
|
|
*
|
|
|
|
* @ref eina_error_get should be used to determine if an
|
|
|
|
* allocation error occurred during this function.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table. Can be @c NULL.
|
|
|
|
* @param key A unique key. Can be @c NULL.
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param key_length Should be the length of @p key (don't forget to count '\\0' for string).
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key_hash The hash that will always match key.
|
|
|
|
* @param data Data to associate with the string given by @p key.
|
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
|
|
|
*/
|
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_direct_add_by_hash(Eina_Hash *hash,
|
|
|
|
const void *key, int key_length, int key_hash,
|
|
|
|
const void *data)
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
return eina_hash_add_alloc_by_hash(hash, key, key_length, 0, key_hash, data);
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
|
|
|
* Adds an entry to the given hash table.
|
|
|
|
*
|
|
|
|
* @p key is expected to be a unique string within the hash table.
|
|
|
|
* Otherwise, you cannot be sure which inserted data pointer will be
|
|
|
|
* accessed with @ref eina_hash_find , and removed with
|
|
|
|
* @ref eina_hash_del .
|
|
|
|
*
|
|
|
|
* Key strings are case sensitive.
|
|
|
|
*
|
2008-08-08 22:47:15 -07:00
|
|
|
* @ref eina_error_get() should be used to determine if an
|
2008-07-30 05:46:55 -07:00
|
|
|
* allocation error occurred during this function.
|
|
|
|
*
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param hash The given hash table. Can be @c NULL.
|
|
|
|
* @param key A unique key. Can be @c NULL.
|
2008-07-30 05:46:55 -07:00
|
|
|
* @param data Data to associate with the string given by @p key.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2008-08-06 08:46:57 -07:00
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_add(Eina_Hash *hash, const void *key, const void *data)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-06 08:46:57 -07:00
|
|
|
unsigned int key_length;
|
|
|
|
int key_hash;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2009-06-18 05:08:52 -07:00
|
|
|
key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
|
2008-08-06 08:46:57 -07:00
|
|
|
key_hash = hash->key_hash_cb(key, key_length);
|
|
|
|
|
|
|
|
return eina_hash_add_by_hash(hash, key, key_length, key_hash, data);
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
|
|
|
* Adds an entry to the given hash table and does not duplicate the string key.
|
|
|
|
*
|
|
|
|
* @p key is expected to be a unique string within the hash table.
|
|
|
|
* Otherwise, you cannot be sure which inserted data pointer will be
|
|
|
|
* accessed with @ref eina_hash_find , and removed with
|
|
|
|
* @ref eina_hash_del . This call does not make a copy of the key so it must
|
|
|
|
* be a string constant or stored elsewhere (in the object being added) etc.
|
|
|
|
*
|
|
|
|
* Key strings are case sensitive.
|
|
|
|
*
|
2008-08-08 22:47:15 -07:00
|
|
|
* @ref eina_error_get() should be used to determine if an
|
2008-07-30 05:46:55 -07:00
|
|
|
* allocation error occurred during this function.
|
|
|
|
*
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param hash The given hash table. Can be @c NULL.
|
|
|
|
* @param key A unique key. Can be @c NULL.
|
2008-07-30 05:46:55 -07:00
|
|
|
* @param data Data to associate with the string given by @p key.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2008-08-06 08:46:57 -07:00
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-06 08:46:57 -07:00
|
|
|
int key_length;
|
|
|
|
int key_hash;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2009-06-18 05:08:52 -07:00
|
|
|
key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
|
2008-08-06 08:46:57 -07:00
|
|
|
key_hash = hash->key_hash_cb(key, key_length);
|
|
|
|
|
|
|
|
return eina_hash_direct_add_by_hash(hash, key, key_length, key_hash, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-12-05 22:58:56 -08:00
|
|
|
* Removes the entry identified by @p key and @p key_hash from the given
|
2008-08-06 08:46:57 -07:00
|
|
|
* hash table.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table.
|
2008-12-05 22:58:56 -08:00
|
|
|
* @param key The key. Cannot be @c NULL.
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param key_length Should be the length of @p key (don't forget to count '\\0' for string).
|
2008-12-05 22:58:56 -08:00
|
|
|
* @param key_hash The hash that always match the key.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
2008-12-05 22:58:56 -08:00
|
|
|
*
|
|
|
|
* @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
|
|
|
|
* @note if you don't have the key, use eina_hash_del_by_data() instead.
|
2008-08-06 08:46:57 -07:00
|
|
|
*/
|
|
|
|
EAPI Eina_Bool
|
2008-12-09 09:39:48 -08:00
|
|
|
eina_hash_del_by_key_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash)
|
2008-08-06 08:46:57 -07:00
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
|
2008-12-09 09:39:48 -08:00
|
|
|
return _eina_hash_del_by_key_hash(hash, key, key_length, key_hash, NULL);
|
2008-12-05 22:58:56 -08:00
|
|
|
}
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-05 22:58:56 -08:00
|
|
|
/**
|
|
|
|
* Removes the entry identified by @p key from the given hash table.
|
|
|
|
*
|
|
|
|
* This version will calculate key length and hash by using functions
|
|
|
|
* provided to hash creation function.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table.
|
|
|
|
* @param key The key. Cannot be @c NULL.
|
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
|
|
|
*
|
|
|
|
* @note if you already have the key_hash, use eina_hash_del_by_key_hash() instead.
|
|
|
|
* @note if you don't have the key, use eina_hash_del_by_data() instead.
|
|
|
|
*/
|
|
|
|
EAPI Eina_Bool
|
2008-12-09 09:39:48 -08:00
|
|
|
eina_hash_del_by_key(Eina_Hash *hash, const void *key)
|
2008-12-05 22:58:56 -08:00
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
|
2008-12-09 09:39:48 -08:00
|
|
|
return _eina_hash_del_by_key(hash, key, NULL);
|
2008-12-05 22:58:56 -08:00
|
|
|
}
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-05 22:58:56 -08:00
|
|
|
/**
|
|
|
|
* Removes the entry identified by @p data from the given hash table.
|
|
|
|
*
|
|
|
|
* This version is slow since there is no quick access to nodes based on data.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table.
|
|
|
|
* @param data The data value to search and remove.
|
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
|
|
|
*
|
|
|
|
* @note if you already have the key, use eina_hash_del_by_key() or eina_hash_del_by_key_hash() instead.
|
|
|
|
*/
|
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_del_by_data(Eina_Hash *hash, const void *data)
|
|
|
|
{
|
|
|
|
Eina_Hash_El *el;
|
|
|
|
Eina_Hash_Head *eh;
|
|
|
|
int key_hash;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
|
2008-12-05 22:58:56 -08:00
|
|
|
|
|
|
|
el = _eina_hash_find_by_data(hash, data, &key_hash, &eh);
|
|
|
|
if (!el) return EINA_FALSE;
|
|
|
|
if (el->tuple.data != data) return EINA_FALSE;
|
|
|
|
return _eina_hash_del_by_hash_el(hash, el, eh, key_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the entry identified by @p key and @p key_hash or @p data from the given
|
|
|
|
* hash table.
|
|
|
|
*
|
|
|
|
* If @p key is @c NULL, then @p data is used to find a match to
|
|
|
|
* remove.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table.
|
|
|
|
* @param key The key. Can be @c NULL.
|
|
|
|
* @param key_length Should be the length of @p key (don't forget to count '\\0' for string).
|
|
|
|
* @param key_hash The hash that always match the key. Ignored if @p key is @c NULL.
|
|
|
|
* @param data The data pointer to remove if @p key is @c NULL.
|
|
|
|
* Otherwise, not required and can be @c NULL.
|
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
|
|
|
*
|
|
|
|
* @note if you know you already have the key, use eina_hash_del_by_key_hash(),
|
|
|
|
* if you know you don't have the key, use eina_hash_del_by_data()
|
|
|
|
* directly.
|
|
|
|
*/
|
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_del_by_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash, const void *data)
|
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
2008-12-09 09:39:48 -08:00
|
|
|
if (key) return _eina_hash_del_by_key_hash(hash, key, key_length, key_hash, data);
|
2008-12-05 22:58:56 -08:00
|
|
|
else return eina_hash_del_by_data(hash, data);
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the entry identified by @p key or @p data from the given
|
|
|
|
* hash table.
|
|
|
|
*
|
|
|
|
* If @p key is @c NULL, then @p data is used to find a match to
|
|
|
|
* remove.
|
|
|
|
*
|
|
|
|
* @param hash The given hash table.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key The key. Can be @c NULL.
|
2008-07-30 05:46:55 -07:00
|
|
|
* @param data The data pointer to remove if @p key is @c NULL.
|
|
|
|
* Otherwise, not required and can be @c NULL.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @return Will return EINA_FALSE if an error occured, and EINA_TRUE if every
|
|
|
|
* thing goes fine.
|
2008-12-05 22:58:56 -08:00
|
|
|
*
|
|
|
|
* @note if you know you already have the key, use
|
|
|
|
* eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
|
|
|
|
* know you don't have the key, use eina_hash_del_by_data()
|
|
|
|
* directly.
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2008-08-06 08:46:57 -07:00
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_hash_del(Eina_Hash *hash, const void *key, const void *data)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
|
2008-12-09 09:39:48 -08:00
|
|
|
if (key) return _eina_hash_del_by_key(hash, key, data);
|
2008-12-05 22:58:56 -08:00
|
|
|
else return eina_hash_del_by_data(hash, data);
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a specific entry in the given hash table.
|
|
|
|
* @param hash The given hash table.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key The key of the entry to find.
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param key_length Should be the length of @p key (don't forget to count '\\0' for string).
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key_hash The hash that always match the key. Ignored if @p key is @c NULL.
|
2008-07-30 05:46:55 -07:00
|
|
|
* @return The data pointer for the stored entry, or @c NULL if not
|
|
|
|
* found.
|
|
|
|
*/
|
2008-08-06 08:46:57 -07:00
|
|
|
EAPI void *
|
|
|
|
eina_hash_find_by_hash(const Eina_Hash *hash, const void *key, int key_length, int key_hash)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Hash_Head *eh;
|
2008-08-06 08:46:57 -07:00
|
|
|
Eina_Hash_El *el;
|
2008-09-19 05:19:06 -07:00
|
|
|
Eina_Hash_Tuple tuple;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-29 04:41:46 -08:00
|
|
|
if (!hash) return NULL;
|
|
|
|
|
2008-12-09 06:59:30 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-09-19 05:19:06 -07:00
|
|
|
tuple.key = key;
|
|
|
|
tuple.key_length = key_length;
|
|
|
|
tuple.data = NULL;
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
|
2008-08-28 09:35:43 -07:00
|
|
|
if (el) return el->tuple.data;
|
2008-08-06 08:46:57 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a specific entry in the given hash table.
|
|
|
|
* @param hash The given hash table.
|
|
|
|
* @param key The key of the entry to find.
|
|
|
|
* @return The data pointer for the stored entry, or @c NULL if not
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
EAPI void *
|
|
|
|
eina_hash_find(const Eina_Hash *hash, const void *key)
|
|
|
|
{
|
|
|
|
int key_length;
|
|
|
|
int hash_num;
|
|
|
|
|
2008-12-29 04:41:46 -08:00
|
|
|
if (!hash) return NULL;
|
|
|
|
|
2008-12-09 06:24:08 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2009-06-18 05:08:52 -07:00
|
|
|
key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
|
2008-08-06 08:46:57 -07:00
|
|
|
hash_num = hash->key_hash_cb(key, key_length);
|
|
|
|
|
|
|
|
return eina_hash_find_by_hash(hash, key, key_length, hash_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies the entry pointer at the specified key and returns the old entry
|
|
|
|
* @param hash The given hash table.
|
|
|
|
* @param key The key of the entry to modify.
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param key_length Should be the length of @p key (don't forget to count '\\0' for string).
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key_hash The hash that always match the key. Ignored if @p key is @c NULL.
|
|
|
|
* @param data The data to replace the old entry, if it exists.
|
|
|
|
* @return The data pointer for the old stored entry, or @c NULL if not
|
|
|
|
* found. If an existing entry is not found, nothing is added to the
|
|
|
|
* hash.
|
|
|
|
*/
|
|
|
|
EAPI void *
|
|
|
|
eina_hash_modify_by_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash, const void *data)
|
|
|
|
{
|
2008-09-24 10:14:29 -07:00
|
|
|
Eina_Hash_Head *eh;
|
2008-08-06 08:46:57 -07:00
|
|
|
Eina_Hash_El *el;
|
|
|
|
void *old_data = NULL;
|
2008-09-19 05:19:06 -07:00
|
|
|
Eina_Hash_Tuple tuple;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(data, NULL);
|
|
|
|
|
2008-09-19 05:19:06 -07:00
|
|
|
tuple.key = key;
|
|
|
|
tuple.key_length = key_length;
|
|
|
|
tuple.data = NULL;
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
|
2008-08-06 08:46:57 -07:00
|
|
|
if (el)
|
|
|
|
{
|
2008-08-11 09:30:16 -07:00
|
|
|
old_data = el->tuple.data;
|
|
|
|
el->tuple.data = (void *) data;
|
2008-08-06 08:46:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return old_data;
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies the entry pointer at the specified key and returns the old entry
|
|
|
|
* @param hash The given hash table.
|
2008-08-06 08:46:57 -07:00
|
|
|
* @param key The key of the entry to modify.
|
2008-07-30 05:46:55 -07:00
|
|
|
* @param data The data to replace the old entry, if it exists.
|
|
|
|
* @return The data pointer for the old stored entry, or @c NULL if not
|
|
|
|
* found. If an existing entry is not found, nothing is added to the
|
|
|
|
* hash.
|
|
|
|
*/
|
2008-08-06 08:46:57 -07:00
|
|
|
EAPI void *
|
|
|
|
eina_hash_modify(Eina_Hash *hash, const void *key, const void *data)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-06 08:46:57 -07:00
|
|
|
int key_length;
|
|
|
|
int hash_num;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(data, NULL);
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2009-06-18 05:08:52 -07:00
|
|
|
key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
|
2008-08-06 08:46:57 -07:00
|
|
|
hash_num = hash->key_hash_cb(key, key_length);
|
|
|
|
|
|
|
|
return eina_hash_modify_by_hash(hash, key, key_length, hash_num, data);
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
|
2008-08-27 06:34:04 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Iterator *
|
|
|
|
*============================================================================*/
|
2008-07-30 05:46:55 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call a function on every member stored in the hash table
|
|
|
|
* @param hash The hash table whose members will be walked
|
|
|
|
* @param func The function to call on each parameter
|
|
|
|
* @param fdata The data pointer to pass to the function being called
|
|
|
|
*
|
|
|
|
* This function goes through every entry in the hash table @p hash and calls
|
|
|
|
* the function @p func on each member. The function should NOT modify the
|
|
|
|
* hash table contents if it returns 1. IF the hash table contents are
|
|
|
|
* modified by this function or the function wishes to stop processing it must
|
|
|
|
* return 0, otherwise return 1 to keep processing.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
|
|
|
* extern Eina_Hash *hash;
|
|
|
|
*
|
|
|
|
* Eina_Bool hash_fn(Eina_Hash *hash, const char *key, void *data, void *fdata)
|
|
|
|
* {
|
|
|
|
* printf("Func data: %s, Hash entry: %s / %p\n", fdata, key, data);
|
|
|
|
* return 1;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* int main(int argc, char **argv)
|
|
|
|
* {
|
|
|
|
* char *hash_fn_data;
|
|
|
|
*
|
|
|
|
* hash_fn_data = strdup("Hello World");
|
|
|
|
* eina_hash_foreach(hash, hash_fn, hash_fn_data);
|
|
|
|
* free(hash_fn_data);
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*/
|
2008-08-11 09:30:16 -07:00
|
|
|
EAPI void
|
|
|
|
eina_hash_foreach(const Eina_Hash *hash,
|
2008-12-05 18:21:17 -08:00
|
|
|
Eina_Hash_Foreach func,
|
2008-08-11 09:30:16 -07:00
|
|
|
const void *fdata)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-11 09:30:16 -07:00
|
|
|
Eina_Iterator *it;
|
2008-12-05 18:21:17 -08:00
|
|
|
Eina_Hash_Foreach_Data foreach;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(hash);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(func);
|
2008-12-07 05:28:29 -08:00
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
foreach.cb = func;
|
|
|
|
foreach.fdata = fdata;
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
it = eina_hash_iterator_tuple_new(hash);
|
2008-12-09 05:05:33 -08:00
|
|
|
if (!it) return;
|
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
eina_iterator_foreach(it, EINA_EACH(_eina_foreach_cb), &foreach);
|
|
|
|
eina_iterator_free(it);
|
|
|
|
}
|
2008-08-06 08:46:57 -07:00
|
|
|
|
2009-02-27 08:32:22 -08:00
|
|
|
/**
|
|
|
|
* @brief Returned a new iterator asociated to hash data.
|
|
|
|
*
|
|
|
|
* @param hash The hash.
|
|
|
|
* @return A new iterator.
|
|
|
|
*
|
|
|
|
* This function returns a newly allocated iterator associated to @p
|
|
|
|
* hash. If @p hash is not populated, this function still returns a
|
|
|
|
* valid iterator that will always return false on
|
|
|
|
* eina_iterator_next(), thus keeping API sane.
|
|
|
|
*
|
|
|
|
* If the memory can not be allocated, NULL is returned and
|
|
|
|
* #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @warning if the hash structure changes then the iterator becomes
|
|
|
|
* invalid! That is, if you add or remove items this iterator
|
|
|
|
* behavior is undefined and your program may crash!
|
|
|
|
*/
|
2008-08-11 09:30:16 -07:00
|
|
|
EAPI Eina_Iterator *
|
|
|
|
eina_hash_iterator_data_new(const Eina_Hash *hash)
|
|
|
|
{
|
|
|
|
Eina_Iterator_Hash *it;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
2009-02-27 08:32:22 -08:00
|
|
|
eina_error_set(0);
|
2008-08-11 09:30:16 -07:00
|
|
|
it = calloc(1, sizeof (Eina_Iterator_Hash));
|
2009-02-27 08:32:22 -08:00
|
|
|
if (!it) {
|
|
|
|
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-11 09:30:16 -07:00
|
|
|
|
|
|
|
it->hash = hash;
|
2008-08-11 10:22:36 -07:00
|
|
|
it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_data_get_content);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next);
|
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_hash_iterator_get_container);
|
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_hash_iterator_free);
|
|
|
|
|
2008-12-05 22:17:11 -08:00
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_SET(it, EINA_MAGIC_HASH_ITERATOR);
|
2008-12-05 22:17:11 -08:00
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
return &it->iterator;
|
|
|
|
}
|
|
|
|
|
2009-02-27 08:32:22 -08:00
|
|
|
/**
|
|
|
|
* @brief Returned a new iterator asociated to hash keys.
|
|
|
|
*
|
|
|
|
* @param hash The hash.
|
|
|
|
* @return A new iterator.
|
|
|
|
*
|
|
|
|
* This function returns a newly allocated iterator associated to @p
|
|
|
|
* hash. If @p hash is not populated, this function still returns a
|
|
|
|
* valid iterator that will always return false on
|
|
|
|
* eina_iterator_next(), thus keeping API sane.
|
|
|
|
*
|
|
|
|
* If the memory can not be allocated, NULL is returned and
|
|
|
|
* #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @warning if the hash structure changes then the iterator becomes
|
|
|
|
* invalid! That is, if you add or remove items this iterator
|
|
|
|
* behavior is undefined and your program may crash!
|
|
|
|
*/
|
2008-08-11 09:30:16 -07:00
|
|
|
EAPI Eina_Iterator *
|
|
|
|
eina_hash_iterator_key_new(const Eina_Hash *hash)
|
|
|
|
{
|
|
|
|
Eina_Iterator_Hash *it;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
2009-02-27 08:32:22 -08:00
|
|
|
eina_error_set(0);
|
2008-08-11 09:30:16 -07:00
|
|
|
it = calloc(1, sizeof (Eina_Iterator_Hash));
|
2009-02-27 08:32:22 -08:00
|
|
|
if (!it) {
|
|
|
|
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-11 09:30:16 -07:00
|
|
|
|
|
|
|
it->hash = hash;
|
2008-08-11 10:22:36 -07:00
|
|
|
it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_key_get_content);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next);
|
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_hash_iterator_get_container);
|
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_hash_iterator_free);
|
|
|
|
|
2008-12-05 22:17:11 -08:00
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_SET(it, EINA_MAGIC_HASH_ITERATOR);
|
2008-12-05 22:17:11 -08:00
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
return &it->iterator;
|
|
|
|
}
|
|
|
|
|
2009-02-27 08:32:22 -08:00
|
|
|
/**
|
|
|
|
* @brief Returned a new iterator asociated to hash keys and data.
|
|
|
|
*
|
|
|
|
* @param hash The hash.
|
|
|
|
* @return A new iterator.
|
|
|
|
*
|
|
|
|
* This function returns a newly allocated iterator associated to @p
|
|
|
|
* hash. If @p hash is not populated, this function still returns a
|
|
|
|
* valid iterator that will always return false on
|
|
|
|
* eina_iterator_next(), thus keeping API sane.
|
|
|
|
*
|
|
|
|
* If the memory can not be allocated, NULL is returned and
|
|
|
|
* #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @note iterator data will provide values as Eina_Hash_Tuple that should not
|
|
|
|
* be modified!
|
|
|
|
*
|
|
|
|
* @warning if the hash structure changes then the iterator becomes
|
|
|
|
* invalid! That is, if you add or remove items this iterator
|
|
|
|
* behavior is undefined and your program may crash!
|
|
|
|
*/
|
2008-08-11 09:30:16 -07:00
|
|
|
EAPI Eina_Iterator *
|
|
|
|
eina_hash_iterator_tuple_new(const Eina_Hash *hash)
|
|
|
|
{
|
|
|
|
Eina_Iterator_Hash *it;
|
|
|
|
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_CHECK_HASH(hash);
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
2009-02-27 08:32:22 -08:00
|
|
|
eina_error_set(0);
|
2008-08-11 09:30:16 -07:00
|
|
|
it = calloc(1, sizeof (Eina_Iterator_Hash));
|
2009-02-27 08:32:22 -08:00
|
|
|
if (!it) {
|
|
|
|
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-11 09:30:16 -07:00
|
|
|
|
|
|
|
it->hash = hash;
|
2008-08-11 10:22:36 -07:00
|
|
|
it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_tuple_get_content);
|
2008-08-11 09:30:16 -07:00
|
|
|
|
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next);
|
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_hash_iterator_get_container);
|
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_hash_iterator_free);
|
|
|
|
|
2008-12-05 22:17:11 -08:00
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
2008-12-07 05:28:29 -08:00
|
|
|
EINA_MAGIC_SET(it, EINA_MAGIC_HASH_ITERATOR);
|
2008-12-05 22:17:11 -08:00
|
|
|
|
2008-08-11 09:30:16 -07:00
|
|
|
return &it->iterator;
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
|
2008-08-06 08:46:57 -07:00
|
|
|
/* Common hash functions */
|
|
|
|
|
|
|
|
/* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html)
|
|
|
|
used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
|
2008-08-26 01:59:25 -07:00
|
|
|
EAPI int
|
2008-08-06 08:46:57 -07:00
|
|
|
eina_hash_superfast(const char *key, int len)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-06 08:46:57 -07:00
|
|
|
int hash = len, tmp;
|
|
|
|
int rem;
|
|
|
|
|
|
|
|
rem = len & 3;
|
|
|
|
len >>= 2;
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
for ( ;len > 0; len--)
|
|
|
|
{
|
|
|
|
hash += get16bits(key);
|
|
|
|
tmp = (get16bits(key + 2) << 11) ^ hash;
|
|
|
|
hash = (hash << 16) ^ tmp;
|
|
|
|
key += 2 * sizeof (uint16_t);
|
|
|
|
hash += hash >> 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle end cases */
|
|
|
|
switch (rem)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
hash += get16bits(key);
|
|
|
|
hash ^= hash << 16;
|
|
|
|
hash ^= key[sizeof (uint16_t)] << 18;
|
|
|
|
hash += hash >> 11;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
hash += get16bits(key);
|
|
|
|
hash ^= hash << 11;
|
|
|
|
hash += hash >> 17;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
hash += *key;
|
|
|
|
hash ^= hash << 10;
|
|
|
|
hash += hash >> 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force "avalanching" of final 127 bits */
|
|
|
|
hash ^= hash << 3;
|
|
|
|
hash += hash >> 5;
|
|
|
|
hash ^= hash << 4;
|
|
|
|
hash += hash >> 17;
|
|
|
|
hash ^= hash << 25;
|
|
|
|
hash += hash >> 6;
|
|
|
|
|
|
|
|
return hash;
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
|
2008-08-27 06:34:04 -07:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|