Add first try for an accessor and iterator API, comment welcome (lack inlist and...
[profile/ivi/eina.git] / src / include / eina_hash.h
1 /* EINA - EFL data type library
2  * Copyright (C) 2002-2008 Carsten Haitzler, Gustavo Sverzut Barbieri,
3  *                         Vincent Torri, Jorge Luis Zapata Muga, Cedric Bail
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library;
17  * if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef EINA_HASH_H_
21 #define EINA_HASH_H_
22
23 #include "eina_types.h"
24 #include "eina_iterator.h"
25
26 /**
27  *
28  */
29 typedef struct _Eina_Hash Eina_Hash;
30 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
31 struct _Eina_Hash_Tuple
32 {
33    const void *key;
34    void *data;
35
36    unsigned int key_length;
37 };
38
39 typedef unsigned int (*Eina_Key_Length)(const void *key);
40 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
41 typedef int (*Eina_Key_Cmp)(const void *key1, int key1_length,
42                             const void *key2, int key2_length);
43 #define EINA_KEY_CMP(Function) ((Eina_Key_Cmp)Function)
44 typedef int (*Eina_Key_Hash)(const void *key, int key_length);
45 #define EINA_KEY_HASH(Function) ((Eina_Key_Hash)Function)
46
47 EAPI int eina_hash_init(void);
48 EAPI int eina_hash_shutdown(void);
49
50 EAPI Eina_Hash * eina_hash_new(Eina_Key_Length key_length_cb,
51                                Eina_Key_Cmp key_cmp_cb,
52                                Eina_Key_Hash key_hash_cb);
53 EAPI Eina_Hash * eina_hash_string_djb2_new(void);
54 EAPI Eina_Hash * eina_hash_string_superfast_new(void);
55
56 EAPI Eina_Bool   eina_hash_add(Eina_Hash *hash, const void *key, const void *data);
57 EAPI Eina_Bool   eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data);
58 EAPI Eina_Bool   eina_hash_del(Eina_Hash *hash, const void *key, const void *data);
59 EAPI void      * eina_hash_find(const Eina_Hash *hash, const void *key);
60 EAPI void      * eina_hash_modify(Eina_Hash *hash, const void *key, const void *data);
61 EAPI void        eina_hash_free(Eina_Hash *hash);
62 EAPI int         eina_hash_population(const Eina_Hash *hash);
63
64 EAPI Eina_Bool   eina_hash_add_by_hash(Eina_Hash *hash,
65                                        const void *key, int key_length, int key_hash,
66                                        const void *data);
67 EAPI Eina_Bool   eina_hash_direct_add_by_hash(Eina_Hash *hash,
68                                               const void *key, int key_length, int key_hash,
69                                               const void *data);
70 EAPI Eina_Bool   eina_hash_del_by_hash(Eina_Hash *hash,
71                                        const void *key, int key_length, int key_hash,
72                                        const void *data);
73 EAPI void      * eina_hash_find_by_hash(const Eina_Hash *hash,
74                                         const void *key, int key_length, int key_hash);
75 EAPI void      * eina_hash_modify_by_hash(Eina_Hash *hash,
76                                           const void *key, int key_length, int key_hash,
77                                           const void *data);
78
79 EAPI Eina_Iterator * eina_hash_iterator_key_new(const Eina_Hash *hash);
80 EAPI Eina_Iterator * eina_hash_iterator_data_new(const Eina_Hash *hash);
81 EAPI Eina_Iterator * eina_hash_iterator_tuple_new(const Eina_Hash *hash);
82
83 typedef Eina_Bool (*Eina_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
84 EAPI void        eina_hash_foreach(const Eina_Hash *hash,
85                                    Eina_Foreach cb,
86                                    const void *fdata);
87
88 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function
89    used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
90 EAPI int eina_hash_superfast(const char *key, int len);
91
92 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
93 static inline int eina_hash_djb2(const char *key, int len);
94
95 #include "eina_inline_hash.x"
96
97 #endif /*EINA_HASH_H_*/