Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 276 lines (231 sloc) 12.597 kB
573b460 @andigutmans Zend Library
andigutmans authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
62dc854 Happy New Year.
Sebastian Bergmann authored
5 | Copyright (c) 1998-2002 Zend Technologies Ltd. (http://www.zend.com) |
573b460 @andigutmans Zend Library
andigutmans authored
6 +----------------------------------------------------------------------+
d863d52 Update headers.
Sebastian Bergmann authored
7 | This source file is subject to version 2.00 of the Zend license, |
b5b1117 @zsuraski License update
zsuraski authored
8 | that is bundled with this package in the file LICENSE, and is |
9 | available at through the world-wide-web at |
d863d52 Update headers.
Sebastian Bergmann authored
10 | http://www.zend.com/license/2_00.txt. |
b5b1117 @zsuraski License update
zsuraski authored
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
573b460 @andigutmans Zend Library
andigutmans authored
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 +----------------------------------------------------------------------+
18 */
19
a617217 Replace macros which begin with an underscore through an appropiately
Sascha Schumann authored
20 #ifndef ZEND_HASH_H
21 #define ZEND_HASH_H
573b460 @andigutmans Zend Library
andigutmans authored
22
23 #include <sys/types.h>
90f43c7 @andigutmans - This should improve performance on Windows
andigutmans authored
24 #include "zend.h"
573b460 @andigutmans Zend Library
andigutmans authored
25
26 #define HASH_KEY_IS_STRING 1
27 #define HASH_KEY_IS_LONG 2
28 #define HASH_KEY_NON_EXISTANT 3
29
30 #define HASH_UPDATE (1<<0)
31 #define HASH_ADD (1<<1)
32 #define HASH_NEXT_INSERT (1<<2)
33
34 #define HASH_DEL_KEY 0
35 #define HASH_DEL_INDEX 1
36
2f8fa8c @andigutmans - Merge faster hash implementation.
andigutmans authored
37 typedef ulong (*hash_func_t)(char *arKey, uint nKeyLength);
3bdddb4 MFZE1
Sebastian Bergmann authored
38 typedef int (*compare_func_t)(const void *, const void * TSRMLS_DC);
39 typedef void (*sort_func_t)(void *, size_t, register size_t, compare_func_t TSRMLS_DC);
62114c1 @zsuraski Destructors no longer return ints, the low level problem it was inten…
zsuraski authored
40 typedef void (*dtor_func_t)(void *pDest);
9f51f15 @zsuraski - Stop passing list/plist to internal functions
zsuraski authored
41 typedef void (*copy_ctor_func_t)(void *pElement);
e062dff @zsuraski - Implement public/protected/private methods.
zsuraski authored
42 typedef void (*copy_ctor_param_func_t)(void *pElement, void *pParam);
62114c1 @zsuraski Destructors no longer return ints, the low level problem it was inten…
zsuraski authored
43
9b916e6 rename hastable -> _hashtable to avoid clashes
Thies C. Arntzen authored
44 struct _hashtable;
573b460 @andigutmans Zend Library
andigutmans authored
45
46 typedef struct bucket {
47 ulong h; /* Used for numeric indexing */
48 uint nKeyLength;
49 void *pData;
50 void *pDataPtr;
51 struct bucket *pListNext;
52 struct bucket *pListLast;
53 struct bucket *pNext;
aa73f6b @andigutmans - Hopefully fix the hash problem.
andigutmans authored
54 struct bucket *pLast;
573b460 @andigutmans Zend Library
andigutmans authored
55 char arKey[1]; /* Must be last element */
56 } Bucket;
57
9b916e6 rename hastable -> _hashtable to avoid clashes
Thies C. Arntzen authored
58 typedef struct _hashtable {
573b460 @andigutmans Zend Library
andigutmans authored
59 uint nTableSize;
2f8fa8c @andigutmans - Merge faster hash implementation.
andigutmans authored
60 uint nTableMask;
573b460 @andigutmans Zend Library
andigutmans authored
61 uint nNumOfElements;
62 ulong nNextFreeElement;
63 Bucket *pInternalPointer; /* Used for element traversal */
64 Bucket *pListHead;
65 Bucket *pListTail;
66 Bucket **arBuckets;
62114c1 @zsuraski Destructors no longer return ints, the low level problem it was inten…
zsuraski authored
67 dtor_func_t pDestructor;
38cdb8e @zsuraski Avoid crashing with recursive applies - limit apply nest level to 3 (…
zsuraski authored
68 zend_bool persistent;
69 unsigned char nApplyCount;
1263932 @zsuraski Disable the hash_apply() protection on hashes that persist across req…
zsuraski authored
70 zend_bool bApplyProtection;
c93a189 if ZEND_DEBUG mode is on we'll now see warnings when a HashTable is …
Thies C. Arntzen authored
71 #if ZEND_DEBUG
72 int inconsistent;
73 #endif
573b460 @andigutmans Zend Library
andigutmans authored
74 } HashTable;
75
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
76 typedef Bucket* HashPosition;
77
fd4650b @zsuraski Add zend_hash_get_current_key_type()
zsuraski authored
78 BEGIN_EXTERN_C()
79
573b460 @andigutmans Zend Library
andigutmans authored
80 /* startup/shutdown */
91b0d03 @zsuraski Add tracking for hashtable allocation
zsuraski authored
81 ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC);
82 ZEND_API int _zend_hash_init_ex(HashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC);
573b460 @andigutmans Zend Library
andigutmans authored
83 ZEND_API void zend_hash_destroy(HashTable *ht);
84 ZEND_API void zend_hash_clean(HashTable *ht);
91b0d03 @zsuraski Add tracking for hashtable allocation
zsuraski authored
85 #define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent) _zend_hash_init((ht), (nSize), (pHashFunction), (pDestructor), (persistent) ZEND_FILE_LINE_CC)
86 #define zend_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection) _zend_hash_init_ex((ht), (nSize), (pHashFunction), (pDestructor), (persistent), (bApplyProtection) ZEND_FILE_LINE_CC)
573b460 @andigutmans Zend Library
andigutmans authored
87
88 /* additions/updates/changes */
4f6c95d @zsuraski Whitespace
zsuraski authored
89 ZEND_API int zend_hash_add_or_update(HashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize, void **pDest, int flag);
90 #define zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest) \
91 zend_hash_add_or_update(ht, arKey, nKeyLength, pData, nDataSize, pDest, HASH_UPDATE)
92 #define zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest) \
93 zend_hash_add_or_update(ht, arKey, nKeyLength, pData, nDataSize, pDest, HASH_ADD)
94
95 ZEND_API int zend_hash_quick_add_or_update(HashTable *ht, char *arKey, uint nKeyLength, ulong h, void *pData, uint nDataSize, void **pDest, int flag);
96 #define zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) \
97 zend_hash_quick_add_or_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest, HASH_UPDATE)
98 #define zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) \
99 zend_hash_quick_add_or_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest, HASH_ADD)
573b460 @andigutmans Zend Library
andigutmans authored
100
101 ZEND_API int zend_hash_index_update_or_next_insert(HashTable *ht, ulong h, void *pData, uint nDataSize, void **pDest, int flag);
4f6c95d @zsuraski Whitespace
zsuraski authored
102 #define zend_hash_index_update(ht, h, pData, nDataSize, pDest) \
103 zend_hash_index_update_or_next_insert(ht, h, pData, nDataSize, pDest, HASH_UPDATE)
104 #define zend_hash_next_index_insert(ht, pData, nDataSize, pDest) \
105 zend_hash_index_update_or_next_insert(ht, 0, pData, nDataSize, pDest, HASH_NEXT_INSERT)
573b460 @andigutmans Zend Library
andigutmans authored
106
755207a @zsuraski Implement zend_hash_add_empty_element() using the existing infrastruc…
zsuraski authored
107 ZEND_API int zend_hash_add_empty_element(HashTable *ht, char *arKey, uint nKeyLength);
108
c14baee @zsuraski Avoid going over huge lists of functions, classes and constants.
zsuraski authored
109
110 #define ZEND_HASH_APPLY_KEEP 0
111 #define ZEND_HASH_APPLY_REMOVE 1<<0
112 #define ZEND_HASH_APPLY_STOP 1<<1
113
746ce67 @andigutmans - Add hash_apply_with_arguments()
andigutmans authored
114 typedef struct _zend_hash_key {
115 char *arKey;
116 uint nKeyLength;
117 ulong h;
118 } zend_hash_key;
119
120
7deb44e @zsuraski Some cleanup
zsuraski authored
121 typedef int (*apply_func_t)(void *pDest TSRMLS_DC);
122 typedef int (*apply_func_arg_t)(void *pDest, void *argument TSRMLS_DC);
b51aa1f Made an alias for hash apply with arguments.
Andrei Zmievski authored
123 typedef int (*apply_func_args_t)(void *pDest, int num_args, va_list args, zend_hash_key *hash_key);
124
ee08b81 @zsuraski - Make zend_hash_apply() (and friends) reentrant and much, much quicker
zsuraski authored
125 ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
bf5bb84 MFZE1: make sure the resource-list is always consistent during shutdo…
Sebastian Bergmann authored
126 ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
d76cf1d @zsuraski More TSRMLS_FETCH work
zsuraski authored
127 ZEND_API void zend_hash_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
128 ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void * TSRMLS_DC);
1c2893a @zsuraski Minor cleaning
zsuraski authored
129 ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
746ce67 @andigutmans - Add hash_apply_with_arguments()
andigutmans authored
130
c14baee @zsuraski Avoid going over huge lists of functions, classes and constants.
zsuraski authored
131 /* This function should be used with special care (in other words,
132 * it should usually not be used). When used with the ZEND_HASH_APPLY_STOP
133 * return value, it assumes things about the order of the elements in the hash.
134 * Also, it does not provide the same kind of reentrancy protection that
135 * the standard apply functions do.
136 */
137 ZEND_API void zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
573b460 @andigutmans Zend Library
andigutmans authored
138
139
140 /* Deletes */
141 ZEND_API int zend_hash_del_key_or_index(HashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag);
4f6c95d @zsuraski Whitespace
zsuraski authored
142 #define zend_hash_del(ht, arKey, nKeyLength) \
143 zend_hash_del_key_or_index(ht, arKey, nKeyLength, 0, HASH_DEL_KEY)
144 #define zend_hash_index_del(ht, h) \
145 zend_hash_del_key_or_index(ht, NULL, 0, h, HASH_DEL_INDEX)
573b460 @andigutmans Zend Library
andigutmans authored
146
147 ZEND_API ulong zend_get_hash_value(HashTable *ht, char *arKey, uint nKeyLength);
148
149 /* Data retreival */
150 ZEND_API int zend_hash_find(HashTable *ht, char *arKey, uint nKeyLength, void **pData);
151 ZEND_API int zend_hash_quick_find(HashTable *ht, char *arKey, uint nKeyLength, ulong h, void **pData);
152 ZEND_API int zend_hash_index_find(HashTable *ht, ulong h, void **pData);
153
154 /* Misc */
155 ZEND_API int zend_hash_exists(HashTable *ht, char *arKey, uint nKeyLength);
156 ZEND_API int zend_hash_index_exists(HashTable *ht, ulong h);
157 ZEND_API ulong zend_hash_next_free_element(HashTable *ht);
158
159 /* traversing */
ee286fe Make zend_hash_move_forward()/zenv_hash_move_backwards() a little sma…
Andrei Zmievski authored
160 ZEND_API int zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
161 ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
9b3df86 @zsuraski MFZE1
zsuraski authored
162 ZEND_API int zend_hash_get_current_key_ex(HashTable *ht, char **str_index, uint *str_length, ulong *num_index, zend_bool duplicate, HashPosition *pos);
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
163 ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
164 ZEND_API int zend_hash_get_current_data_ex(HashTable *ht, void **pData, HashPosition *pos);
165 ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
166 ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
167
168 #define zend_hash_move_forward(ht) \
169 zend_hash_move_forward_ex(ht, NULL)
170 #define zend_hash_move_backwards(ht) \
171 zend_hash_move_backwards_ex(ht, NULL)
36eaad2 @zsuraski Allow get_current_key() not to return the key itself, instead of a du…
zsuraski authored
172 #define zend_hash_get_current_key(ht, str_index, num_index, duplicate) \
173 zend_hash_get_current_key_ex(ht, str_index, NULL, num_index, duplicate, NULL)
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
174 #define zend_hash_get_current_key_type(ht) \
175 zend_hash_get_current_key_type_ex(ht, NULL)
176 #define zend_hash_get_current_data(ht, pData) \
177 zend_hash_get_current_data_ex(ht, pData, NULL)
178 #define zend_hash_internal_pointer_reset(ht) \
179 zend_hash_internal_pointer_reset_ex(ht, NULL)
180 #define zend_hash_internal_pointer_end(ht) \
181 zend_hash_internal_pointer_end_ex(ht, NULL)
573b460 @andigutmans Zend Library
andigutmans authored
182
183 /* Copying, merging and sorting */
9f51f15 @zsuraski - Stop passing list/plist to internal functions
zsuraski authored
184 ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size);
185 ZEND_API void zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite);
e988715 @andigutmans Don't use 'new' symbol
andigutmans authored
186 ZEND_API void zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, uint size, zend_bool (*pReplaceOrig)(void *orig, void *p_new));
3bdddb4 MFZE1
Sebastian Bergmann authored
187 ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC);
8ce8324 @zsuraski More TSRMLS_FETCH annihilation
zsuraski authored
188 ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
3bdddb4 MFZE1
Sebastian Bergmann authored
189 ZEND_API int zend_hash_minmax(HashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC);
573b460 @andigutmans Zend Library
andigutmans authored
190
191 ZEND_API int zend_hash_num_elements(HashTable *ht);
192
3964c92 Made zend_hash_rehash() callable from outside.
Andrei Zmievski authored
193 ZEND_API int zend_hash_rehash(HashTable *ht);
573b460 @andigutmans Zend Library
andigutmans authored
194
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
195 /*
196 * DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
197 *
198 * This is Daniel J. Bernstein's popular `times 33' hash function as
199 * posted by him years ago on comp.lang.c. It basically uses a function
200 * like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
201 * known hash functions for strings. Because it is both computed very
202 * fast and distributes very well.
203 *
204 * The magic of number 33, i.e. why it works better than many other
205 * constants, prime or not, has never been adequately explained by
206 * anyone. So I try an explanation: if one experimentally tests all
207 * multipliers between 1 and 256 (as RSE did now) one detects that even
208 * numbers are not useable at all. The remaining 128 odd numbers
209 * (except for the number 1) work more or less all equally well. They
210 * all distribute in an acceptable way and this way fill a hash table
211 * with an average percent of approx. 86%.
212 *
213 * If one compares the Chi^2 values of the variants, the number 33 not
214 * even has the best value. But the number 33 and a few other equally
215 * good numbers like 17, 31, 63, 127 and 129 have nevertheless a great
216 * advantage to the remaining numbers in the large set of possible
217 * multipliers: their multiply operation can be replaced by a faster
218 * operation based on just one shift plus either a single addition
219 * or subtraction operation. And because a hash function has to both
220 * distribute good _and_ has to be very fast to compute, those few
221 * numbers should be preferred and seems to be the reason why Daniel J.
222 * Bernstein also preferred it.
223 *
224 *
225 * -- Ralf S. Engelschall <rse@engelschall.com>
226 */
227
b98acd6 @andigutmans - Move inline_zend_hash_func() to header file
andigutmans authored
228 static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength)
229 {
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
230 register ulong hash = 5381;
231
232 /* variant with the hash unrolled eight times */
233 for (; nKeyLength >= 8; nKeyLength -= 8) {
234 hash = ((hash << 5) + hash) + *arKey++;
235 hash = ((hash << 5) + hash) + *arKey++;
236 hash = ((hash << 5) + hash) + *arKey++;
237 hash = ((hash << 5) + hash) + *arKey++;
238 hash = ((hash << 5) + hash) + *arKey++;
239 hash = ((hash << 5) + hash) + *arKey++;
240 hash = ((hash << 5) + hash) + *arKey++;
241 hash = ((hash << 5) + hash) + *arKey++;
242 }
243 switch (nKeyLength) {
244 case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
245 case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
246 case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
247 case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
248 case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
249 case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
250 case 1: hash = ((hash << 5) + hash) + *arKey++; break;
90f43c7 @andigutmans - This should improve performance on Windows
andigutmans authored
251 case 0: break;
252 EMPTY_SWITCH_DEFAULT_CASE()
b98acd6 @andigutmans - Move inline_zend_hash_func() to header file
andigutmans authored
253 }
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
254 return hash;
b98acd6 @andigutmans - Move inline_zend_hash_func() to header file
andigutmans authored
255 }
256
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
257
6cf5e7f @smalyshev No hashpjw anymore, but we have zend_hash_func
smalyshev authored
258 ZEND_API ulong zend_hash_func(char *arKey, uint nKeyLength);
573b460 @andigutmans Zend Library
andigutmans authored
259
9353650 Fix warnings surfacing in maintainer-mode.
Stig S. Bakken authored
260 #if ZEND_DEBUG
573b460 @andigutmans Zend Library
andigutmans authored
261 /* debug */
262 void zend_hash_display_pListTail(HashTable *ht);
263 void zend_hash_display(HashTable *ht);
264 #endif
265
fd4650b @zsuraski Add zend_hash_get_current_key_type()
zsuraski authored
266 END_EXTERN_C()
267
0a14a32 @zsuraski Add useful macros
zsuraski authored
268 #define ZEND_INIT_SYMTABLE(ht) \
269 ZEND_INIT_SYMTABLE_EX(ht, 2, 0)
270
271 #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent) \
272 zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
273
274
a617217 Replace macros which begin with an underscore through an appropiately
Sascha Schumann authored
275 #endif /* ZEND_HASH_H */
Something went wrong with that request. Please try again.