1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2007 Zend Technologies Ltd. (http://www.zend.com) |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 2.00 of the Zend license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.zend.com/license/2_00.txt. |
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. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Andi Gutmans <andi@zend.com> |
16 : | Zeev Suraski <zeev@zend.com> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: zend_hash.h,v 1.78.2.2.2.2 2007/01/10 15:58:07 dmitry Exp $ */
21 :
22 : #ifndef ZEND_HASH_H
23 : #define ZEND_HASH_H
24 :
25 : #include <sys/types.h>
26 : #include "zend.h"
27 :
28 : #define HASH_KEY_IS_STRING 1
29 : #define HASH_KEY_IS_LONG 2
30 : #define HASH_KEY_NON_EXISTANT 3
31 :
32 : #define HASH_UPDATE (1<<0)
33 : #define HASH_ADD (1<<1)
34 : #define HASH_NEXT_INSERT (1<<2)
35 :
36 : #define HASH_DEL_KEY 0
37 : #define HASH_DEL_INDEX 1
38 :
39 : typedef ulong (*hash_func_t)(char *arKey, uint nKeyLength);
40 : typedef int (*compare_func_t)(const void *, const void * TSRMLS_DC);
41 : typedef void (*sort_func_t)(void *, size_t, register size_t, compare_func_t TSRMLS_DC);
42 : typedef void (*dtor_func_t)(void *pDest);
43 : typedef void (*copy_ctor_func_t)(void *pElement);
44 : typedef void (*copy_ctor_param_func_t)(void *pElement, void *pParam);
45 :
46 : struct _hashtable;
47 :
48 : typedef struct bucket {
49 : ulong h; /* Used for numeric indexing */
50 : uint nKeyLength;
51 : void *pData;
52 : void *pDataPtr;
53 : struct bucket *pListNext;
54 : struct bucket *pListLast;
55 : struct bucket *pNext;
56 : struct bucket *pLast;
57 : char arKey[1]; /* Must be last element */
58 : } Bucket;
59 :
60 : typedef struct _hashtable {
61 : uint nTableSize;
62 : uint nTableMask;
63 : uint nNumOfElements;
64 : ulong nNextFreeElement;
65 : Bucket *pInternalPointer; /* Used for element traversal */
66 : Bucket *pListHead;
67 : Bucket *pListTail;
68 : Bucket **arBuckets;
69 : dtor_func_t pDestructor;
70 : zend_bool persistent;
71 : unsigned char nApplyCount;
72 : zend_bool bApplyProtection;
73 : #if ZEND_DEBUG
74 : int inconsistent;
75 : #endif
76 : } HashTable;
77 :
78 :
79 : typedef struct _zend_hash_key {
80 : char *arKey;
81 : uint nKeyLength;
82 : ulong h;
83 : } zend_hash_key;
84 :
85 :
86 : typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, void *source_data, zend_hash_key *hash_key, void *pParam);
87 :
88 : typedef Bucket* HashPosition;
89 :
90 : BEGIN_EXTERN_C()
91 :
92 : /* startup/shutdown */
93 : 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);
94 : 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);
95 : ZEND_API void zend_hash_destroy(HashTable *ht);
96 : ZEND_API void zend_hash_clean(HashTable *ht);
97 : #define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent) _zend_hash_init((ht), (nSize), (pHashFunction), (pDestructor), (persistent) ZEND_FILE_LINE_CC)
98 : #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)
99 :
100 : /* additions/updates/changes */
101 : ZEND_API int _zend_hash_add_or_update(HashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize, void **pDest, int flag ZEND_FILE_LINE_DC);
102 : #define zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest) \
103 : _zend_hash_add_or_update(ht, arKey, nKeyLength, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)
104 : #define zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest) \
105 : _zend_hash_add_or_update(ht, arKey, nKeyLength, pData, nDataSize, pDest, HASH_ADD ZEND_FILE_LINE_CC)
106 :
107 : 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 ZEND_FILE_LINE_DC);
108 : #define zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) \
109 : _zend_hash_quick_add_or_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)
110 : #define zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) \
111 : _zend_hash_quick_add_or_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest, HASH_ADD ZEND_FILE_LINE_CC)
112 :
113 : ZEND_API int _zend_hash_index_update_or_next_insert(HashTable *ht, ulong h, void *pData, uint nDataSize, void **pDest, int flag ZEND_FILE_LINE_DC);
114 : #define zend_hash_index_update(ht, h, pData, nDataSize, pDest) \
115 : _zend_hash_index_update_or_next_insert(ht, h, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)
116 : #define zend_hash_next_index_insert(ht, pData, nDataSize, pDest) \
117 : _zend_hash_index_update_or_next_insert(ht, 0, pData, nDataSize, pDest, HASH_NEXT_INSERT ZEND_FILE_LINE_CC)
118 :
119 : ZEND_API int zend_hash_add_empty_element(HashTable *ht, char *arKey, uint nKeyLength);
120 :
121 :
122 : #define ZEND_HASH_APPLY_KEEP 0
123 : #define ZEND_HASH_APPLY_REMOVE 1<<0
124 : #define ZEND_HASH_APPLY_STOP 1<<1
125 :
126 :
127 : typedef int (*apply_func_t)(void *pDest TSRMLS_DC);
128 : typedef int (*apply_func_arg_t)(void *pDest, void *argument TSRMLS_DC);
129 : typedef int (*apply_func_args_t)(void *pDest, int num_args, va_list args, zend_hash_key *hash_key);
130 :
131 : ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
132 : ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
133 : ZEND_API void zend_hash_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
134 : ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void * TSRMLS_DC);
135 : ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
136 :
137 : /* This function should be used with special care (in other words,
138 : * it should usually not be used). When used with the ZEND_HASH_APPLY_STOP
139 : * return value, it assumes things about the order of the elements in the hash.
140 : * Also, it does not provide the same kind of reentrancy protection that
141 : * the standard apply functions do.
142 : */
143 : ZEND_API void zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
144 :
145 :
146 : /* Deletes */
147 : ZEND_API int zend_hash_del_key_or_index(HashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag);
148 : #define zend_hash_del(ht, arKey, nKeyLength) \
149 : zend_hash_del_key_or_index(ht, arKey, nKeyLength, 0, HASH_DEL_KEY)
150 : #define zend_hash_index_del(ht, h) \
151 : zend_hash_del_key_or_index(ht, NULL, 0, h, HASH_DEL_INDEX)
152 :
153 : ZEND_API ulong zend_get_hash_value(char *arKey, uint nKeyLength);
154 :
155 : /* Data retreival */
156 : ZEND_API int zend_hash_find(HashTable *ht, char *arKey, uint nKeyLength, void **pData);
157 : ZEND_API int zend_hash_quick_find(HashTable *ht, char *arKey, uint nKeyLength, ulong h, void **pData);
158 : ZEND_API int zend_hash_index_find(HashTable *ht, ulong h, void **pData);
159 :
160 : /* Misc */
161 : ZEND_API int zend_hash_exists(HashTable *ht, char *arKey, uint nKeyLength);
162 : ZEND_API int zend_hash_quick_exists(HashTable *ht, char *arKey, uint nKeyLength, ulong h);
163 : ZEND_API int zend_hash_index_exists(HashTable *ht, ulong h);
164 : ZEND_API ulong zend_hash_next_free_element(HashTable *ht);
165 :
166 :
167 : /* traversing */
168 : #define zend_hash_has_more_elements_ex(ht, pos) \
169 : (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTANT ? FAILURE : SUCCESS)
170 : ZEND_API int zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
171 : ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
172 : 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);
173 : ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
174 : ZEND_API int zend_hash_get_current_data_ex(HashTable *ht, void **pData, HashPosition *pos);
175 : ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
176 : ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
177 : ZEND_API int zend_hash_update_current_key_ex(HashTable *ht, int key_type, char *str_index, uint str_length, ulong num_index, HashPosition *pos);
178 :
179 : typedef struct _HashPointer {
180 : HashPosition pos;
181 : ulong h;
182 : } HashPointer;
183 :
184 : ZEND_API int zend_hash_get_pointer(HashTable *ht, HashPointer *ptr);
185 : ZEND_API int zend_hash_set_pointer(HashTable *ht, const HashPointer *ptr);
186 :
187 : #define zend_hash_has_more_elements(ht) \
188 : zend_hash_has_more_elements_ex(ht, NULL)
189 : #define zend_hash_move_forward(ht) \
190 : zend_hash_move_forward_ex(ht, NULL)
191 : #define zend_hash_move_backwards(ht) \
192 : zend_hash_move_backwards_ex(ht, NULL)
193 : #define zend_hash_get_current_key(ht, str_index, num_index, duplicate) \
194 : zend_hash_get_current_key_ex(ht, str_index, NULL, num_index, duplicate, NULL)
195 : #define zend_hash_get_current_key_type(ht) \
196 : zend_hash_get_current_key_type_ex(ht, NULL)
197 : #define zend_hash_get_current_data(ht, pData) \
198 : zend_hash_get_current_data_ex(ht, pData, NULL)
199 : #define zend_hash_internal_pointer_reset(ht) \
200 : zend_hash_internal_pointer_reset_ex(ht, NULL)
201 : #define zend_hash_internal_pointer_end(ht) \
202 : zend_hash_internal_pointer_end_ex(ht, NULL)
203 : #define zend_hash_update_current_key(ht, key_type, str_index, str_length, num_index) \
204 : zend_hash_update_current_key_ex(ht, key_type, str_index, str_length, num_index, NULL)
205 :
206 : /* Copying, merging and sorting */
207 : ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size);
208 : ZEND_API void _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite ZEND_FILE_LINE_DC);
209 : ZEND_API void zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, uint size, merge_checker_func_t pMergeSource, void *pParam);
210 : ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC);
211 : ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
212 : ZEND_API int zend_hash_minmax(HashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC);
213 :
214 : #define zend_hash_merge(target, source, pCopyConstructor, tmp, size, overwrite) \
215 : _zend_hash_merge(target, source, pCopyConstructor, tmp, size, overwrite ZEND_FILE_LINE_CC)
216 :
217 : ZEND_API int zend_hash_num_elements(HashTable *ht);
218 :
219 : ZEND_API int zend_hash_rehash(HashTable *ht);
220 :
221 : /*
222 : * DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
223 : *
224 : * This is Daniel J. Bernstein's popular `times 33' hash function as
225 : * posted by him years ago on comp.lang.c. It basically uses a function
226 : * like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
227 : * known hash functions for strings. Because it is both computed very
228 : * fast and distributes very well.
229 : *
230 : * The magic of number 33, i.e. why it works better than many other
231 : * constants, prime or not, has never been adequately explained by
232 : * anyone. So I try an explanation: if one experimentally tests all
233 : * multipliers between 1 and 256 (as RSE did now) one detects that even
234 : * numbers are not useable at all. The remaining 128 odd numbers
235 : * (except for the number 1) work more or less all equally well. They
236 : * all distribute in an acceptable way and this way fill a hash table
237 : * with an average percent of approx. 86%.
238 : *
239 : * If one compares the Chi^2 values of the variants, the number 33 not
240 : * even has the best value. But the number 33 and a few other equally
241 : * good numbers like 17, 31, 63, 127 and 129 have nevertheless a great
242 : * advantage to the remaining numbers in the large set of possible
243 : * multipliers: their multiply operation can be replaced by a faster
244 : * operation based on just one shift plus either a single addition
245 : * or subtraction operation. And because a hash function has to both
246 : * distribute good _and_ has to be very fast to compute, those few
247 : * numbers should be preferred and seems to be the reason why Daniel J.
248 : * Bernstein also preferred it.
249 : *
250 : *
251 : * -- Ralf S. Engelschall <rse@engelschall.com>
252 : */
253 :
254 : static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength)
255 978110 : {
256 978110 : register ulong hash = 5381;
257 :
258 : /* variant with the hash unrolled eight times */
259 2155311 : for (; nKeyLength >= 8; nKeyLength -= 8) {
260 1177201 : hash = ((hash << 5) + hash) + *arKey++;
261 1177201 : hash = ((hash << 5) + hash) + *arKey++;
262 1177201 : hash = ((hash << 5) + hash) + *arKey++;
263 1177201 : hash = ((hash << 5) + hash) + *arKey++;
264 1177201 : hash = ((hash << 5) + hash) + *arKey++;
265 1177201 : hash = ((hash << 5) + hash) + *arKey++;
266 1177201 : hash = ((hash << 5) + hash) + *arKey++;
267 1177201 : hash = ((hash << 5) + hash) + *arKey++;
268 : }
269 978110 : switch (nKeyLength) {
270 124929 : case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
271 247842 : case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
272 381603 : case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
273 507310 : case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
274 618917 : case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
275 736401 : case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
276 850884 : case 1: hash = ((hash << 5) + hash) + *arKey++; break;
277 : case 0: break;
278 : EMPTY_SWITCH_DEFAULT_CASE()
279 : }
280 978110 : return hash;
281 : }
282 :
283 :
284 : ZEND_API ulong zend_hash_func(char *arKey, uint nKeyLength);
285 :
286 : #if ZEND_DEBUG
287 : /* debug */
288 : void zend_hash_display_pListTail(HashTable *ht);
289 : void zend_hash_display(HashTable *ht);
290 : #endif
291 :
292 : END_EXTERN_C()
293 :
294 : #define ZEND_INIT_SYMTABLE(ht) \
295 : ZEND_INIT_SYMTABLE_EX(ht, 2, 0)
296 :
297 : #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent) \
298 : zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
299 :
300 :
301 : #define HANDLE_NUMERIC(key, length, func) { \
302 : register char *tmp=key; \
303 : \
304 : if (*tmp=='-') { \
305 : tmp++; \
306 : } \
307 : if ((*tmp>='0' && *tmp<='9')) do { /* possibly a numeric index */ \
308 : char *end=key+length-1; \
309 : long idx; \
310 : \
311 : if (*tmp++=='0' && length>2) { /* don't accept numbers with leading zeros */ \
312 : break; \
313 : } \
314 : while (tmp<end) { \
315 : if (!(*tmp>='0' && *tmp<='9')) { \
316 : break; \
317 : } \
318 : tmp++; \
319 : } \
320 : if (tmp==end && *tmp=='\0') { /* a numeric index */ \
321 : if (*key=='-') { \
322 : idx = strtol(key, NULL, 10); \
323 : if (idx!=LONG_MIN) { \
324 : return func; \
325 : } \
326 : } else { \
327 : idx = strtol(key, NULL, 10); \
328 : if (idx!=LONG_MAX) { \
329 : return func; \
330 : } \
331 : } \
332 : } \
333 : } while (0); \
334 : }
335 :
336 :
337 30012 : static inline int zend_symtable_update(HashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize, void **pDest) \
338 : {
339 30012 : HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_update(ht, idx, pData, nDataSize, pDest));
340 30004 : return zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest);
341 : }
342 :
343 :
344 : static inline int zend_symtable_del(HashTable *ht, char *arKey, uint nKeyLength)
345 0 : {
346 0 : HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_del(ht, idx))
347 0 : return zend_hash_del(ht, arKey, nKeyLength);
348 : }
349 :
350 :
351 : static inline int zend_symtable_find(HashTable *ht, char *arKey, uint nKeyLength, void **pData)
352 13278 : {
353 13278 : HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_find(ht, idx, pData));
354 13277 : return zend_hash_find(ht, arKey, nKeyLength, pData);
355 : }
356 :
357 :
358 : static inline int zend_symtable_exists(HashTable *ht, char *arKey, uint nKeyLength)
359 758 : {
360 758 : HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_exists(ht, idx));
361 758 : return zend_hash_exists(ht, arKey, nKeyLength);
362 : }
363 :
364 : static inline int zend_symtable_update_current_key(HashTable *ht, char *arKey, uint nKeyLength)
365 0 : {
366 0 : HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_update_current_key(ht, HASH_KEY_IS_LONG, NULL, 0, idx));
367 0 : return zend_hash_update_current_key(ht, HASH_KEY_IS_STRING, arKey, nKeyLength, 0);
368 : }
369 :
370 : #endif /* ZEND_HASH_H */
371 :
372 : /*
373 : * Local variables:
374 : * tab-width: 4
375 : * c-basic-offset: 4
376 : * indent-tabs-mode: t
377 : * End:
378 : */
|