Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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