Add small bucket support.
[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 int eina_hash_init(void);
64 EAPI int eina_hash_shutdown(void);
65
66 EAPI Eina_Hash * eina_hash_new(Eina_Key_Length key_length_cb,
67                                Eina_Key_Cmp key_cmp_cb,
68                                Eina_Key_Hash key_hash_cb,
69                                Eina_Free_Cb data_free_cb,
70                                int buckets_power_size);
71 EAPI Eina_Hash * eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
72 EAPI Eina_Hash * eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
73 EAPI Eina_Hash * eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
74 EAPI Eina_Hash * eina_hash_int32_new(Eina_Free_Cb data_free_cb);
75 EAPI Eina_Hash * eina_hash_int64_new(Eina_Free_Cb data_free_cb);
76 EAPI Eina_Hash * eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
77
78 EAPI Eina_Bool   eina_hash_add(Eina_Hash *hash, const void *key, const void *data);
79 EAPI Eina_Bool   eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data);
80 EAPI Eina_Bool   eina_hash_del(Eina_Hash *hash, const void *key, const void *data);
81 EAPI void      * eina_hash_find(const Eina_Hash *hash, const void *key);
82 EAPI void      * eina_hash_modify(Eina_Hash *hash, const void *key, const void *data);
83 EAPI void        eina_hash_free(Eina_Hash *hash);
84 EAPI int         eina_hash_population(const Eina_Hash *hash);
85
86 EAPI Eina_Bool   eina_hash_add_by_hash(Eina_Hash *hash,
87                                        const void *key, int key_length, int key_hash,
88                                        const void *data);
89 EAPI Eina_Bool   eina_hash_direct_add_by_hash(Eina_Hash *hash,
90                                               const void *key, int key_length, int key_hash,
91                                               const void *data);
92
93 EAPI Eina_Bool   eina_hash_del_by_key_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash);
94
95 EAPI Eina_Bool   eina_hash_del_by_key(Eina_Hash *hash, const void *key);
96 EAPI Eina_Bool   eina_hash_del_by_data(Eina_Hash *hash, const void *data);
97
98 EAPI Eina_Bool   eina_hash_del_by_hash(Eina_Hash *hash,
99                                        const void *key, int key_length, int key_hash,
100                                        const void *data);
101 EAPI void      * eina_hash_find_by_hash(const Eina_Hash *hash,
102                                         const void *key, int key_length, int key_hash);
103 EAPI void      * eina_hash_modify_by_hash(Eina_Hash *hash,
104                                           const void *key, int key_length, int key_hash,
105                                           const void *data);
106
107 EAPI Eina_Iterator * eina_hash_iterator_key_new(const Eina_Hash *hash);
108 EAPI Eina_Iterator * eina_hash_iterator_data_new(const Eina_Hash *hash);
109 EAPI Eina_Iterator * eina_hash_iterator_tuple_new(const Eina_Hash *hash);
110
111 typedef Eina_Bool (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
112 EAPI void        eina_hash_foreach(const Eina_Hash *hash,
113                                    Eina_Hash_Foreach cb,
114                                    const void *fdata);
115
116 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function
117    used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
118 EAPI int eina_hash_superfast(const char *key, int len);
119
120 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
121 static inline int eina_hash_djb2(const char *key, int len);
122
123 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
124 static inline int eina_hash_int32(unsigned int *pkey, int len);
125 static inline int eina_hash_int64(unsigned long int *pkey, int len);
126
127 #include "eina_inline_hash.x"
128
129 /**
130  * @}
131  */
132
133 /**
134  * @}
135  */
136
137 /**
138  * @}
139  */
140
141 #endif /*EINA_HASH_H_*/