uncrustify eina.
[profile/ivi/eina.git] / src / include / eina_hash.h
1
2 /* EINA - EFL data type library
3  * Copyright (C) 2002-2008 Carsten Haitzler, Gustavo Sverzut Barbieri,
4  *                         Vincent Torri, Jorge Luis Zapata Muga, Cedric Bail
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library;
18  * if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef EINA_HASH_H_
22 #define EINA_HASH_H_
23
24 #include "eina_types.h"
25 #include "eina_iterator.h"
26
27 /**
28  * @addtogroup Eina_Data_Types_Group Data Types
29  *
30  * @{
31  */
32
33 /**
34  * @addtogroup Eina_Containers_Group Containers
35  *
36  * @{
37  */
38
39 /**
40  * @defgroup Eina_Hash_Group Hash Table
41  *
42  * @{
43  */
44
45 typedef struct _Eina_Hash Eina_Hash;
46 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
47 struct _Eina_Hash_Tuple
48 {
49    const void *key;
50    void *data;
51
52    unsigned int key_length;
53 };
54
55 typedef unsigned int (*Eina_Key_Length)(const void *key);
56 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
57 typedef int (*Eina_Key_Cmp)(const void *key1, int key1_length,
58                             const void *key2, int key2_length);
59 #define EINA_KEY_CMP(Function) ((Eina_Key_Cmp)Function)
60 typedef int (*Eina_Key_Hash)(const void *key, int key_length);
61 #define EINA_KEY_HASH(Function) ((Eina_Key_Hash)Function)
62
63 EAPI Eina_Hash *
64 eina_hash_new(Eina_Key_Length key_length_cb,
65               Eina_Key_Cmp
66               key_cmp_cb,
67               Eina_Key_Hash
68               key_hash_cb,
69               Eina_Free_Cb
70               data_free_cb,
71               int
72               buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT
73 EINA_ARG_NONNULL(2, 3);
74 EAPI Eina_Hash *
75 eina_hash_string_djb2_new(
76    Eina_Free_Cb data_free_cb);
77 EAPI Eina_Hash *
78 eina_hash_string_superfast_new(
79    Eina_Free_Cb data_free_cb);
80 EAPI Eina_Hash *
81 eina_hash_string_small_new(
82    Eina_Free_Cb data_free_cb);
83 EAPI Eina_Hash *
84 eina_hash_int32_new(
85    Eina_Free_Cb data_free_cb);
86 EAPI Eina_Hash *
87 eina_hash_int64_new(
88    Eina_Free_Cb data_free_cb);
89 EAPI Eina_Hash *
90 eina_hash_pointer_new(
91    Eina_Free_Cb data_free_cb);
92 EAPI Eina_Hash *
93 eina_hash_stringshared_new(
94    Eina_Free_Cb data_free_cb);
95
96 EAPI Eina_Bool
97 eina_hash_add(
98    Eina_Hash *hash,
99    const void *key,
100    const void *data) EINA_ARG_NONNULL(1, 2, 3);
101 EAPI Eina_Bool
102 eina_hash_direct_add(
103    Eina_Hash *hash,
104    const void *key,
105    const void *data) EINA_ARG_NONNULL(1, 2, 3);
106 EAPI Eina_Bool
107 eina_hash_del(
108    Eina_Hash *hash,
109    const void *key,
110    const void *data) EINA_ARG_NONNULL(1);
111 EAPI void *
112 eina_hash_find(
113    const Eina_Hash *hash,
114    const void *key) EINA_ARG_NONNULL(1, 2);
115 EAPI void *
116 eina_hash_modify(
117    Eina_Hash *hash,
118    const void *key,
119    const void *data) EINA_ARG_NONNULL(1, 2, 3);
120 EAPI void *
121 eina_hash_set(
122    Eina_Hash *hash,
123    const void *key,
124    const void *data) EINA_ARG_NONNULL(1, 2, 3);
125 EAPI void
126 eina_hash_free(
127    Eina_Hash *hash) EINA_ARG_NONNULL(1);
128 EAPI void
129 eina_hash_free_buckets(
130    Eina_Hash *hash) EINA_ARG_NONNULL(1);
131 EAPI int
132 eina_hash_population(
133    const Eina_Hash *hash) EINA_ARG_NONNULL(1);
134
135 EAPI Eina_Bool
136 eina_hash_add_by_hash(
137    Eina_Hash *hash,
138    const
139    void *key,
140    int key_length,
141    int key_hash,
142    const
143    void *data)  EINA_ARG_NONNULL(1, 2, 5);
144 EAPI Eina_Bool
145 eina_hash_direct_add_by_hash(Eina_Hash *hash,
146                              const
147                              void *key,
148                              int key_length,
149                              int key_hash,
150                              const
151                              void *data) EINA_ARG_NONNULL(1, 2, 5);
152
153 EAPI Eina_Bool
154 eina_hash_del_by_key_hash(Eina_Hash *hash,
155                           const void *key,
156                           int key_length,
157                           int key_hash) EINA_ARG_NONNULL(1, 2);
158
159 EAPI Eina_Bool
160 eina_hash_del_by_key(Eina_Hash *hash, const void *key) EINA_ARG_NONNULL(1, 2);
161 EAPI Eina_Bool
162 eina_hash_del_by_data(Eina_Hash *hash, const void *data) EINA_ARG_NONNULL(1, 2);
163
164 EAPI Eina_Bool
165 eina_hash_del_by_hash(Eina_Hash *hash,
166                       const
167                       void *key,
168                       int key_length,
169                       int key_hash,
170                       const
171                       void *data) EINA_ARG_NONNULL(1);
172 EAPI void *
173 eina_hash_find_by_hash(const Eina_Hash *hash,
174                        const
175                        void *key,
176                        int key_length,
177                        int key_hash) EINA_ARG_NONNULL(1, 2);
178 EAPI void *
179 eina_hash_modify_by_hash(Eina_Hash *hash,
180                          const
181                          void *key,
182                          int key_length,
183                          int key_hash,
184                          const
185                          void *data) EINA_ARG_NONNULL(1, 2, 5);
186
187 EAPI Eina_Iterator *
188                                       eina_hash_iterator_key_new(
189    const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1)
190 EINA_WARN_UNUSED_RESULT;
191 EAPI Eina_Iterator *
192                                       eina_hash_iterator_data_new(
193    const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(
194    1) EINA_WARN_UNUSED_RESULT;
195 EAPI Eina_Iterator *
196                                       eina_hash_iterator_tuple_new(
197    const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(
198    1) EINA_WARN_UNUSED_RESULT;
199
200 typedef Eina_Bool (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key,
201                                        void *data, void *fdata);
202 EAPI void
203 eina_hash_foreach(const Eina_Hash *hash,
204                   Eina_Hash_Foreach
205                   cb,
206                   const
207                   void *fdata) EINA_ARG_NONNULL(1, 2);
208
209 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function
210    used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
211 EAPI int
212 eina_hash_superfast(const char *key, int len) EINA_ARG_NONNULL(1);
213
214 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
215 static inline int
216 eina_hash_djb2(const char *key, int len) EINA_ARG_NONNULL(1);
217 static inline int
218 eina_hash_djb2_len(const char *key, int *plen) EINA_ARG_NONNULL(1, 2);
219
220 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
221 static inline int
222 eina_hash_int32(const unsigned int *pkey, int len) EINA_ARG_NONNULL(1);
223 static inline int
224 eina_hash_int64(const unsigned long int *pkey, int len) EINA_ARG_NONNULL(1);
225
226 #include "eina_inline_hash.x"
227
228 /**
229  * @}
230  */
231
232 /**
233  * @}
234  */
235
236 /**
237  * @}
238  */
239
240 #endif /*EINA_HASH_H_*/