Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 379 lines (315 sloc) 16.478 kb
573b460 @andigutmans Zend Library
andigutmans authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
4223aa4 MFH: Bump year.
Sebastian Bergmann authored
5 | Copyright (c) 1998-2007 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 |
f68c7ff updating license information in the headers.
James Cox authored
9 | available through the world-wide-web at the following url: |
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
333406b - Added some missing CVS $Id$ tags, headers and footers.
foobar authored
20 /* $Id$ */
21
a617217 Replace macros which begin with an underscore through an appropiately
Sascha Schumann authored
22 #ifndef ZEND_HASH_H
23 #define ZEND_HASH_H
573b460 @andigutmans Zend Library
andigutmans authored
24
25 #include <sys/types.h>
90f43c7 @andigutmans - This should improve performance on Windows
andigutmans authored
26 #include "zend.h"
573b460 @andigutmans Zend Library
andigutmans authored
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
2f8fa8c @andigutmans - Merge faster hash implementation.
andigutmans authored
39 typedef ulong (*hash_func_t)(char *arKey, uint nKeyLength);
3bdddb4 MFZE1
Sebastian Bergmann authored
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);
62114c1 @zsuraski Destructors no longer return ints, the low level problem it was intended...
zsuraski authored
42 typedef void (*dtor_func_t)(void *pDest);
9f51f15 @zsuraski - Stop passing list/plist to internal functions
zsuraski authored
43 typedef void (*copy_ctor_func_t)(void *pElement);
e062dff @zsuraski - Implement public/protected/private methods.
zsuraski authored
44 typedef void (*copy_ctor_param_func_t)(void *pElement, void *pParam);
62114c1 @zsuraski Destructors no longer return ints, the low level problem it was intended...
zsuraski authored
45
9b916e6 rename hastable -> _hashtable to avoid clashes
Thies C. Arntzen authored
46 struct _hashtable;
573b460 @andigutmans Zend Library
andigutmans authored
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;
aa73f6b @andigutmans - Hopefully fix the hash problem.
andigutmans authored
56 struct bucket *pLast;
573b460 @andigutmans Zend Library
andigutmans authored
57 char arKey[1]; /* Must be last element */
58 } Bucket;
59
9b916e6 rename hastable -> _hashtable to avoid clashes
Thies C. Arntzen authored
60 typedef struct _hashtable {
573b460 @andigutmans Zend Library
andigutmans authored
61 uint nTableSize;
2f8fa8c @andigutmans - Merge faster hash implementation.
andigutmans authored
62 uint nTableMask;
573b460 @andigutmans Zend Library
andigutmans authored
63 uint nNumOfElements;
64 ulong nNextFreeElement;
65 Bucket *pInternalPointer; /* Used for element traversal */
66 Bucket *pListHead;
67 Bucket *pListTail;
68 Bucket **arBuckets;
62114c1 @zsuraski Destructors no longer return ints, the low level problem it was intended...
zsuraski authored
69 dtor_func_t pDestructor;
38cdb8e @zsuraski Avoid crashing with recursive applies - limit apply nest level to 3 (I'm...
zsuraski authored
70 zend_bool persistent;
71 unsigned char nApplyCount;
1263932 @zsuraski Disable the hash_apply() protection on hashes that persist across reques...
zsuraski authored
72 zend_bool bApplyProtection;
c93a189 if ZEND_DEBUG mode is on we'll now see warnings when a HashTable is acc...
Thies C. Arntzen authored
73 #if ZEND_DEBUG
74 int inconsistent;
75 #endif
573b460 @andigutmans Zend Library
andigutmans authored
76 } HashTable;
77
471947b @zsuraski Reimplement PPP properties
zsuraski authored
78
79 typedef struct _zend_hash_key {
80 char *arKey;
81 uint nKeyLength;
82 ulong h;
83 } zend_hash_key;
84
85
45c1235 @zsuraski Fix prototype (may have caused stack corruption)
zsuraski authored
86 typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, void *source_data, zend_hash_key *hash_key, void *pParam);
471947b @zsuraski Reimplement PPP properties
zsuraski authored
87
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
88 typedef Bucket* HashPosition;
89
fd4650b @zsuraski Add zend_hash_get_current_key_type()
zsuraski authored
90 BEGIN_EXTERN_C()
91
573b460 @andigutmans Zend Library
andigutmans authored
92 /* startup/shutdown */
91b0d03 @zsuraski Add tracking for hashtable allocation
zsuraski authored
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);
573b460 @andigutmans Zend Library
andigutmans authored
95 ZEND_API void zend_hash_destroy(HashTable *ht);
96 ZEND_API void zend_hash_clean(HashTable *ht);
91b0d03 @zsuraski Add tracking for hashtable allocation
zsuraski authored
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)
573b460 @andigutmans Zend Library
andigutmans authored
99
100 /* additions/updates/changes */
0dba58c @zsuraski Improve tracking
zsuraski authored
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);
4f6c95d @zsuraski Whitespace
zsuraski authored
102 #define zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest) \
0dba58c @zsuraski Improve tracking
zsuraski authored
103 _zend_hash_add_or_update(ht, arKey, nKeyLength, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)
4f6c95d @zsuraski Whitespace
zsuraski authored
104 #define zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest) \
0dba58c @zsuraski Improve tracking
zsuraski authored
105 _zend_hash_add_or_update(ht, arKey, nKeyLength, pData, nDataSize, pDest, HASH_ADD ZEND_FILE_LINE_CC)
4f6c95d @zsuraski Whitespace
zsuraski authored
106
0dba58c @zsuraski Improve tracking
zsuraski authored
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);
4f6c95d @zsuraski Whitespace
zsuraski authored
108 #define zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) \
0dba58c @zsuraski Improve tracking
zsuraski authored
109 _zend_hash_quick_add_or_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)
4f6c95d @zsuraski Whitespace
zsuraski authored
110 #define zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) \
0dba58c @zsuraski Improve tracking
zsuraski authored
111 _zend_hash_quick_add_or_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest, HASH_ADD ZEND_FILE_LINE_CC)
573b460 @andigutmans Zend Library
andigutmans authored
112
0dba58c @zsuraski Improve tracking
zsuraski authored
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);
4f6c95d @zsuraski Whitespace
zsuraski authored
114 #define zend_hash_index_update(ht, h, pData, nDataSize, pDest) \
0dba58c @zsuraski Improve tracking
zsuraski authored
115 _zend_hash_index_update_or_next_insert(ht, h, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)
4f6c95d @zsuraski Whitespace
zsuraski authored
116 #define zend_hash_next_index_insert(ht, pData, nDataSize, pDest) \
0dba58c @zsuraski Improve tracking
zsuraski authored
117 _zend_hash_index_update_or_next_insert(ht, 0, pData, nDataSize, pDest, HASH_NEXT_INSERT ZEND_FILE_LINE_CC)
573b460 @andigutmans Zend Library
andigutmans authored
118
755207a @zsuraski Implement zend_hash_add_empty_element() using the existing infrastructur...
zsuraski authored
119 ZEND_API int zend_hash_add_empty_element(HashTable *ht, char *arKey, uint nKeyLength);
120
c14baee @zsuraski Avoid going over huge lists of functions, classes and constants.
zsuraski authored
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
746ce67 @andigutmans - Add hash_apply_with_arguments()
andigutmans authored
126
7deb44e @zsuraski Some cleanup
zsuraski authored
127 typedef int (*apply_func_t)(void *pDest TSRMLS_DC);
128 typedef int (*apply_func_arg_t)(void *pDest, void *argument TSRMLS_DC);
b51aa1f Made an alias for hash apply with arguments.
Andrei Zmievski authored
129 typedef int (*apply_func_args_t)(void *pDest, int num_args, va_list args, zend_hash_key *hash_key);
130
ee08b81 @zsuraski - Make zend_hash_apply() (and friends) reentrant and much, much quicker
zsuraski authored
131 ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
bf5bb84 MFZE1: make sure the resource-list is always consistent during shutdown ...
Sebastian Bergmann authored
132 ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
d76cf1d @zsuraski More TSRMLS_FETCH work
zsuraski authored
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);
1c2893a @zsuraski Minor cleaning
zsuraski authored
135 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
136
c14baee @zsuraski Avoid going over huge lists of functions, classes and constants.
zsuraski authored
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);
573b460 @andigutmans Zend Library
andigutmans authored
144
145
146 /* Deletes */
147 ZEND_API int zend_hash_del_key_or_index(HashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag);
4f6c95d @zsuraski Whitespace
zsuraski authored
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)
573b460 @andigutmans Zend Library
andigutmans authored
152
471947b @zsuraski Reimplement PPP properties
zsuraski authored
153 ZEND_API ulong zend_get_hash_value(char *arKey, uint nKeyLength);
573b460 @andigutmans Zend Library
andigutmans authored
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);
376d631 @zsuraski Add quick_exists()
zsuraski authored
162 ZEND_API int zend_hash_quick_exists(HashTable *ht, char *arKey, uint nKeyLength, ulong h);
573b460 @andigutmans Zend Library
andigutmans authored
163 ZEND_API int zend_hash_index_exists(HashTable *ht, ulong h);
164 ZEND_API ulong zend_hash_next_free_element(HashTable *ht);
165
cf90932 @zsuraski Improve infrastructure of numeric handling of elements in symbol tables.
zsuraski authored
166
573b460 @andigutmans Zend Library
andigutmans authored
167 /* traversing */
184118d Make it a macro
Marcus Boerger authored
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)
ee286fe Make zend_hash_move_forward()/zenv_hash_move_backwards() a little smarte...
Andrei Zmievski authored
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);
9b3df86 @zsuraski MFZE1
zsuraski authored
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);
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
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);
d9c1a38 Fixed bug #28072 (static array with some constant keys will be incorrect...
Dmitry Stogov authored
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);
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
178
e470e22 - Fixed bug #35106 (nested foreach fails when array variable has a refer...
Dmitry Stogov authored
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
892c1fb Add missing function to ease implementations
Marcus Boerger authored
187 #define zend_hash_has_more_elements(ht) \
188 zend_hash_has_more_elements_ex(ht, NULL)
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
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)
36eaad2 @zsuraski Allow get_current_key() not to return the key itself, instead of a dupli...
zsuraski authored
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)
a32c54b Introduced a way to traverse hashes through external pointers.
Andrei Zmievski authored
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)
d9c1a38 Fixed bug #28072 (static array with some constant keys will be incorrect...
Dmitry Stogov authored
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)
573b460 @andigutmans Zend Library
andigutmans authored
205
206 /* Copying, merging and sorting */
9f51f15 @zsuraski - Stop passing list/plist to internal functions
zsuraski authored
207 ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size);
0dba58c @zsuraski Improve tracking
zsuraski authored
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);
471947b @zsuraski Reimplement PPP properties
zsuraski authored
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);
3bdddb4 MFZE1
Sebastian Bergmann authored
210 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
211 ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
3bdddb4 MFZE1
Sebastian Bergmann authored
212 ZEND_API int zend_hash_minmax(HashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC);
573b460 @andigutmans Zend Library
andigutmans authored
213
0dba58c @zsuraski Improve tracking
zsuraski authored
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
573b460 @andigutmans Zend Library
andigutmans authored
217 ZEND_API int zend_hash_num_elements(HashTable *ht);
218
3964c92 Made zend_hash_rehash() callable from outside.
Andrei Zmievski authored
219 ZEND_API int zend_hash_rehash(HashTable *ht);
573b460 @andigutmans Zend Library
andigutmans authored
220
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
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
b98acd6 @andigutmans - Move inline_zend_hash_func() to header file
andigutmans authored
254 static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength)
255 {
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
256 register ulong hash = 5381;
257
258 /* variant with the hash unrolled eight times */
259 for (; nKeyLength >= 8; nKeyLength -= 8) {
260 hash = ((hash << 5) + hash) + *arKey++;
261 hash = ((hash << 5) + hash) + *arKey++;
262 hash = ((hash << 5) + hash) + *arKey++;
263 hash = ((hash << 5) + hash) + *arKey++;
264 hash = ((hash << 5) + hash) + *arKey++;
265 hash = ((hash << 5) + hash) + *arKey++;
266 hash = ((hash << 5) + hash) + *arKey++;
267 hash = ((hash << 5) + hash) + *arKey++;
268 }
269 switch (nKeyLength) {
270 case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
271 case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
272 case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
273 case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
274 case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
275 case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
276 case 1: hash = ((hash << 5) + hash) + *arKey++; break;
90f43c7 @andigutmans - This should improve performance on Windows
andigutmans authored
277 case 0: break;
278 EMPTY_SWITCH_DEFAULT_CASE()
b98acd6 @andigutmans - Move inline_zend_hash_func() to header file
andigutmans authored
279 }
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
280 return hash;
b98acd6 @andigutmans - Move inline_zend_hash_func() to header file
andigutmans authored
281 }
282
1186d51 @andigutmans - Add a loop unrolled version of the hash function and a bit of an
andigutmans authored
283
6cf5e7f @smalyshev No hashpjw anymore, but we have zend_hash_func
smalyshev authored
284 ZEND_API ulong zend_hash_func(char *arKey, uint nKeyLength);
573b460 @andigutmans Zend Library
andigutmans authored
285
9353650 Fix warnings surfacing in maintainer-mode.
Stig S. Bakken authored
286 #if ZEND_DEBUG
573b460 @andigutmans Zend Library
andigutmans authored
287 /* debug */
288 void zend_hash_display_pListTail(HashTable *ht);
289 void zend_hash_display(HashTable *ht);
290 #endif
291
fd4650b @zsuraski Add zend_hash_get_current_key_type()
zsuraski authored
292 END_EXTERN_C()
293
0a14a32 @zsuraski Add useful macros
zsuraski authored
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
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
301 #define HANDLE_NUMERIC(key, length, func) { \
302 register char *tmp=key; \
303 \
f77f977 Bugfix 21918
Marcus Boerger authored
304 if (*tmp=='-') { \
305 tmp++; \
306 } \
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
307 if ((*tmp>='0' && *tmp<='9')) do { /* possibly a numeric index */ \
f77f977 Bugfix 21918
Marcus Boerger authored
308 char *end=key+length-1; \
664078c changed ulong to long to avoid compiler warnings (comparison between sig...
Georg Richter authored
309 long idx; \
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
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 */ \
f77f977 Bugfix 21918
Marcus Boerger authored
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 } \
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
331 } \
332 } \
333 } while (0); \
334 }
335
336
cf90932 @zsuraski Improve infrastructure of numeric handling of elements in symbol tables.
zsuraski authored
337 static inline int zend_symtable_update(HashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize, void **pDest) \
338 {
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
339 HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_update(ht, idx, pData, nDataSize, pDest));
340 return zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest);
cf90932 @zsuraski Improve infrastructure of numeric handling of elements in symbol tables.
zsuraski authored
341 }
342
343
344 static inline int zend_symtable_del(HashTable *ht, char *arKey, uint nKeyLength)
345 {
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
346 HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_del(ht, idx))
347 return zend_hash_del(ht, arKey, nKeyLength);
cf90932 @zsuraski Improve infrastructure of numeric handling of elements in symbol tables.
zsuraski authored
348 }
349
350
351 static inline int zend_symtable_find(HashTable *ht, char *arKey, uint nKeyLength, void **pData)
352 {
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
353 HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_find(ht, idx, pData));
354 return zend_hash_find(ht, arKey, nKeyLength, pData);
cf90932 @zsuraski Improve infrastructure of numeric handling of elements in symbol tables.
zsuraski authored
355 }
356
357
358 static inline int zend_symtable_exists(HashTable *ht, char *arKey, uint nKeyLength)
359 {
e89977f @zsuraski Go back to ZE1-like code
zsuraski authored
360 HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_index_exists(ht, idx));
361 return zend_hash_exists(ht, arKey, nKeyLength);
cf90932 @zsuraski Improve infrastructure of numeric handling of elements in symbol tables.
zsuraski authored
362 }
363
9ee0707 Fixed bug #35509 (string constant as array key has different behavior in...
Dmitry Stogov authored
364 static inline int zend_symtable_update_current_key(HashTable *ht, char *arKey, uint nKeyLength)
365 {
366 HANDLE_NUMERIC(arKey, nKeyLength, zend_hash_update_current_key(ht, HASH_KEY_IS_LONG, NULL, 0, idx));
367 return zend_hash_update_current_key(ht, HASH_KEY_IS_STRING, arKey, nKeyLength, 0);
368 }
369
a617217 Replace macros which begin with an underscore through an appropiately
Sascha Schumann authored
370 #endif /* ZEND_HASH_H */
333406b - Added some missing CVS $Id$ tags, headers and footers.
foobar authored
371
372 /*
373 * Local variables:
374 * tab-width: 4
375 * c-basic-offset: 4
376 * indent-tabs-mode: t
377 * End:
378 */
Something went wrong with that request. Please try again.