* eina: remove thread safe data type for now. They will be back !
[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  * @addtogroup Eina_Data_Types_Group Data Types
28  *
29  * @{
30  */
31
32 /**
33  * @addtogroup Eina_Containers_Group Containers
34  *
35  * @{
36  */
37
38 /**
39  * @defgroup Eina_Hash_Group Hash Table
40  *
41  * @{
42  */
43
44 typedef struct _Eina_Hash Eina_Hash;
45 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
46 struct _Eina_Hash_Tuple
47 {
48    const void *key;
49    void *data;
50    unsigned int key_length;
51 };
52
53 typedef unsigned int (*Eina_Key_Length)(const void *key);
54 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
55 typedef int (*Eina_Key_Cmp)(const void *key1, int key1_length, const void *key2, int key2_length);
56 #define EINA_KEY_CMP(Function) ((Eina_Key_Cmp)Function)
57 typedef int (*Eina_Key_Hash)(const void *key, int key_length);
58 #define EINA_KEY_HASH(Function) ((Eina_Key_Hash)Function)
59 typedef Eina_Bool (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
60
61 EAPI Eina_Hash *     eina_hash_new(Eina_Key_Length key_length_cb,
62                                    Eina_Key_Cmp key_cmp_cb,
63                                    Eina_Key_Hash key_hash_cb,
64                                    Eina_Free_Cb data_free_cb,
65                                    int buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
66 EAPI Eina_Hash *     eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
67 EAPI Eina_Hash *     eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
68 EAPI Eina_Hash *     eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
69 EAPI Eina_Hash *     eina_hash_int32_new(Eina_Free_Cb data_free_cb);
70 EAPI Eina_Hash *     eina_hash_int64_new(Eina_Free_Cb data_free_cb);
71 EAPI Eina_Hash *     eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
72 EAPI Eina_Hash *     eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
73 EAPI Eina_Hash *     eina_hash_threadsafe_new(Eina_Key_Length key_length_cb,
74                                               Eina_Key_Cmp key_cmp_cb,
75                                               Eina_Key_Hash key_hash_cb,
76                                               Eina_Free_Cb data_free_cb,
77                                               int buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
78 EAPI Eina_Hash *     eina_hash_threadsafe_string_djb2_new(Eina_Free_Cb data_free_cb);
79 EAPI Eina_Hash *     eina_hash_threadsafe_string_superfast_new(Eina_Free_Cb data_free_cb);
80 EAPI Eina_Hash *     eina_hash_threadsafe_string_small_new(Eina_Free_Cb data_free_cb);
81 EAPI Eina_Hash *     eina_hash_threadsafe_int32_new(Eina_Free_Cb data_free_cb);
82 EAPI Eina_Hash *     eina_hash_threadsafe_int64_new(Eina_Free_Cb data_free_cb);
83 EAPI Eina_Hash *     eina_hash_threadsafe_pointer_new(Eina_Free_Cb data_free_cb);
84 EAPI Eina_Hash *     eina_hash_threadsafe_stringshared_new(Eina_Free_Cb data_free_cb);
85 EAPI Eina_Bool       eina_hash_add(Eina_Hash *hash,
86                                    const void *key,
87                                    const void *data) EINA_ARG_NONNULL(1, 2, 3);
88 EAPI Eina_Bool       eina_hash_direct_add(Eina_Hash *hash,
89                                           const void *key,
90                                           const void *data) EINA_ARG_NONNULL(1, 2, 3);
91 EAPI Eina_Bool       eina_hash_del(Eina_Hash *hash,
92                                    const void *key,
93                                    const void *data) EINA_ARG_NONNULL(1);
94 EAPI void *          eina_hash_find(const Eina_Hash *hash,
95                                     const void *key) EINA_ARG_NONNULL(1, 2);
96 EAPI void *          eina_hash_modify(Eina_Hash *hash,
97                                       const void *key,
98                                       const void *data) EINA_ARG_NONNULL(1, 2, 3);
99 EAPI void *          eina_hash_set(Eina_Hash *hash,
100                                    const void *key,
101                                    const void *data) EINA_ARG_NONNULL(1, 2, 3);
102 EAPI Eina_Bool       eina_hash_move(Eina_Hash *hash,
103                                     const void *old_key,
104                                     const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
105 EAPI void            eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
106 EAPI void            eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
107 EAPI int             eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
108 EAPI Eina_Bool       eina_hash_add_by_hash(Eina_Hash *hash,
109                                            const void *key,
110                                            int key_length,
111                                            int key_hash,
112                                            const void *data) EINA_ARG_NONNULL(1, 2, 5);
113 EAPI Eina_Bool       eina_hash_direct_add_by_hash(Eina_Hash *hash,
114                                                   const void *key,
115                                                   int key_length,
116                                                   int key_hash,
117                                                   const void *data) EINA_ARG_NONNULL(1, 2, 5);
118 EAPI Eina_Bool       eina_hash_del_by_key_hash(Eina_Hash *hash,
119                                                const void *key,
120                                                int key_length,
121                                                int key_hash) EINA_ARG_NONNULL(1, 2);
122 EAPI Eina_Bool       eina_hash_del_by_key(Eina_Hash *hash,
123                                           const void *key) EINA_ARG_NONNULL(1, 2);
124 EAPI Eina_Bool       eina_hash_del_by_data(Eina_Hash *hash,
125                                            const void *data) EINA_ARG_NONNULL(1, 2);
126 EAPI Eina_Bool       eina_hash_del_by_hash(Eina_Hash *hash,
127                                            const void *key,
128                                            int key_length,
129                                            int key_hash,
130                                            const void *data) EINA_ARG_NONNULL(1);
131 EAPI void *          eina_hash_find_by_hash(const Eina_Hash *hash,
132                                             const void *key,
133                                             int key_length,
134                                             int key_hash) EINA_ARG_NONNULL(1, 2);
135 EAPI void *          eina_hash_modify_by_hash(Eina_Hash *hash,
136                                               const void *key,
137                                               int key_length,
138                                               int key_hash,
139                                               const void *data) EINA_ARG_NONNULL(1, 2, 5);
140 EAPI Eina_Iterator * eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
141 EAPI Eina_Iterator * eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
142 EAPI Eina_Iterator * eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
143 EAPI void            eina_hash_foreach(const Eina_Hash *hash,
144                                        Eina_Hash_Foreach cb,
145                                        const void *fdata) EINA_ARG_NONNULL(1, 2);
146 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
147 EAPI int             eina_hash_superfast(const char *key,
148                                          int len) EINA_ARG_NONNULL(1);
149 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
150 static inline int    eina_hash_djb2(const char *key,
151                                     int len) EINA_ARG_NONNULL(1);
152 static inline int    eina_hash_djb2_len(const char *key,
153                                         int *plen) EINA_ARG_NONNULL(1, 2);
154 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
155 static inline int    eina_hash_int32(const unsigned int *pkey,
156                                      int len) EINA_ARG_NONNULL(1);
157 static inline int    eina_hash_int64(const unsigned long int *pkey,
158                                      int len) EINA_ARG_NONNULL(1);
159
160 #include "eina_inline_hash.x"
161
162 /**
163  * @}
164  */
165
166 /**
167  * @}
168  */
169
170 /**
171  * @}
172  */
173
174 #endif /*EINA_HASH_H_*/