Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 290 lines (241 sloc) 12.904 kb
573b4602 » andigutmans
1999-04-07 Zend Library
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
2c5d4b8c » Sebastian Bergmann
2002-12-31 Bump year.
5 | Copyright (c) 1998-2003 Zend Technologies Ltd. (http://www.zend.com) |
573b4602 » andigutmans
1999-04-07 Zend Library
6 +----------------------------------------------------------------------+
d863d52a » Sebastian Bergmann
2001-12-11 Update headers.
7 | This source file is subject to version 2.00 of the Zend license, |
b5b11177 » zsuraski
1999-07-16 License update
8 | that is bundled with this package in the file LICENSE, and is |
9 | available at through the world-wide-web at |
d863d52a » Sebastian Bergmann
2001-12-11 Update headers.
10 | http://www.zend.com/license/2_00.txt. |
b5b11177 » zsuraski
1999-07-16 License update
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. |
573b4602 » andigutmans
1999-04-07 Zend Library
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 +----------------------------------------------------------------------+
18 */
19
333406bd » foobar
2003-02-01 - Added some missing CVS $Id$ tags, headers and footers.
20 /* $Id$ */
21
a6172174 » Sascha Schumann
2000-07-03 Replace macros which begin with an underscore through an appropiately
22 #ifndef ZEND_HASH_H
23 #define ZEND_HASH_H
573b4602 » andigutmans
1999-04-07 Zend Library
24
25 #include <sys/types.h>
90f43c78 » andigutmans
2002-06-08 - This should improve performance on Windows
26 #include "zend.h"
573b4602 » andigutmans
1999-04-07 Zend Library
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
2f8fa8cf » andigutmans
2001-07-10 - Merge faster hash implementation.
39 typedef ulong (*hash_func_t)(char *arKey, uint nKeyLength);
3bdddb49 » Sebastian Bergmann
2001-09-19 MFZE1
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);
62114c18 » zsuraski
2000-01-17 Destructors no longer return ints, the low level problem it was inten…
42 typedef void (*dtor_func_t)(void *pDest);
9f51f154 » zsuraski
2000-02-05 - Stop passing list/plist to internal functions
43 typedef void (*copy_ctor_func_t)(void *pElement);
e062dffe » zsuraski
2002-12-06 - Implement public/protected/private methods.
44 typedef void (*copy_ctor_param_func_t)(void *pElement, void *pParam);
62114c18 » zsuraski
2000-01-17 Destructors no longer return ints, the low level problem it was inten…
45
9b916e6a » Thies C. Arntzen
2000-05-24 rename hastable -> _hashtable to avoid clashes
46 struct _hashtable;
573b4602 » andigutmans
1999-04-07 Zend Library
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;
aa73f6b6 » andigutmans
2000-01-19 - Hopefully fix the hash problem.
56 struct bucket *pLast;
573b4602 » andigutmans
1999-04-07 Zend Library
57 char arKey[1]; /* Must be last element */
58 } Bucket;
59
9b916e6a » Thies C. Arntzen
2000-05-24 rename hastable -> _hashtable to avoid clashes
60 typedef struct _hashtable {
573b4602 » andigutmans
1999-04-07 Zend Library
61 uint nTableSize;
2f8fa8cf » andigutmans
2001-07-10 - Merge faster hash implementation.
62 uint nTableMask;
573b4602 » andigutmans
1999-04-07 Zend Library
63 uint nNumOfElements;
64 ulong nNextFreeElement;
65 Bucket *pInternalPointer; /* Used for element traversal */
66 Bucket *pListHead;
67 Bucket *pListTail;
68 Bucket **arBuckets;
62114c18 » zsuraski
2000-01-17 Destructors no longer return ints, the low level problem it was inten…
69 dtor_func_t pDestructor;
38cdb8e7 » zsuraski
2000-06-17 Avoid crashing with recursive applies - limit apply nest level to 3 (…
70 zend_bool persistent;
71 unsigned char nApplyCount;
1263932a » zsuraski
2000-07-11 Disable the hash_apply() protection on hashes that persist across req…
72 zend_bool bApplyProtection;
c93a1890 » Thies C. Arntzen
2000-01-15 if ZEND_DEBUG mode is on we'll now see warnings when a HashTable is a…
73 #if ZEND_DEBUG
74 int inconsistent;
75 #endif
573b4602 » andigutmans
1999-04-07 Zend Library
76 } HashTable;
77
471947b1 » zsuraski
2003-02-04 Reimplement PPP properties
78
79 typedef struct _zend_hash_key {
80 char *arKey;
81 uint nKeyLength;
82 ulong h;
83 } zend_hash_key;
84
85
45c12359 » zsuraski
2003-02-06 Fix prototype (may have caused stack corruption)
86 typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, void *source_data, zend_hash_key *hash_key, void *pParam);
471947b1 » zsuraski
2003-02-04 Reimplement PPP properties
87
a32c54bd » Andrei Zmievski
2000-03-13 Introduced a way to traverse hashes through external pointers.
88 typedef Bucket* HashPosition;
89
fd4650b5 » zsuraski
1999-06-07 Add zend_hash_get_current_key_type()
90 BEGIN_EXTERN_C()
91
573b4602 » andigutmans
1999-04-07 Zend Library
92 /* startup/shutdown */
91b0d033 » zsuraski
2002-09-17 Add tracking for hashtable allocation
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);
573b4602 » andigutmans
1999-04-07 Zend Library
95 ZEND_API void zend_hash_destroy(HashTable *ht);
96 ZEND_API void zend_hash_clean(HashTable *ht);
91b0d033 » zsuraski
2002-09-17 Add tracking for hashtable allocation
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)
573b4602 » andigutmans
1999-04-07 Zend Library
99
100 /* additions/updates/changes */
4f6c95d1 » zsuraski
2001-08-11 Whitespace
101 ZEND_API int zend_hash_add_or_update(HashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize, void **pDest, int flag);
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)
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)
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);
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)
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)
573b4602 » andigutmans
1999-04-07 Zend Library
112
113 ZEND_API int zend_hash_index_update_or_next_insert(HashTable *ht, ulong h, void *pData, uint nDataSize, void **pDest, int flag);
4f6c95d1 » zsuraski
2001-08-11 Whitespace
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)
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)
573b4602 » andigutmans
1999-04-07 Zend Library
118
755207a1 » zsuraski
2001-05-16 Implement zend_hash_add_empty_element() using the existing infrastruc…
119 ZEND_API int zend_hash_add_empty_element(HashTable *ht, char *arKey, uint nKeyLength);
120
c14baeec » zsuraski
2001-08-02 Avoid going over huge lists of functions, classes and constants.
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
746ce67b » andigutmans
1999-08-25 - Add hash_apply_with_arguments()
126
7deb44e3 » zsuraski
2001-08-02 Some cleanup
127 typedef int (*apply_func_t)(void *pDest TSRMLS_DC);
128 typedef int (*apply_func_arg_t)(void *pDest, void *argument TSRMLS_DC);
b51aa1f3 » Andrei Zmievski
2000-06-09 Made an alias for hash apply with arguments.
129 typedef int (*apply_func_args_t)(void *pDest, int num_args, va_list args, zend_hash_key *hash_key);
130
ee08b81a » zsuraski
2000-01-16 - Make zend_hash_apply() (and friends) reentrant and much, much quicker
131 ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
bf5bb842 » Sebastian Bergmann
2002-04-19 MFZE1: make sure the resource-list is always consistent during shutdo…
132 ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
d76cf1da » zsuraski
2001-07-31 More TSRMLS_FETCH work
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);
1c2893a1 » zsuraski
2001-07-15 Minor cleaning
135 ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
746ce67b » andigutmans
1999-08-25 - Add hash_apply_with_arguments()
136
c14baeec » zsuraski
2001-08-02 Avoid going over huge lists of functions, classes and constants.
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);
573b4602 » andigutmans
1999-04-07 Zend Library
144
145
146 /* Deletes */
147 ZEND_API int zend_hash_del_key_or_index(HashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag);
4f6c95d1 » zsuraski
2001-08-11 Whitespace
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)
573b4602 » andigutmans
1999-04-07 Zend Library
152
471947b1 » zsuraski
2003-02-04 Reimplement PPP properties
153 ZEND_API ulong zend_get_hash_value(char *arKey, uint nKeyLength);
573b4602 » andigutmans
1999-04-07 Zend Library
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);
376d6317 » zsuraski
2003-02-05 Add quick_exists()
162 ZEND_API int zend_hash_quick_exists(HashTable *ht, char *arKey, uint nKeyLength, ulong h);
573b4602 » andigutmans
1999-04-07 Zend Library
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 /* traversing */
ee286feb » Andrei Zmievski
2000-03-15 Make zend_hash_move_forward()/zenv_hash_move_backwards() a little sma…
167 ZEND_API int zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
168 ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
9b3df86e » zsuraski
2001-08-20 MFZE1
169 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);
a32c54bd » Andrei Zmievski
2000-03-13 Introduced a way to traverse hashes through external pointers.
170 ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
171 ZEND_API int zend_hash_get_current_data_ex(HashTable *ht, void **pData, HashPosition *pos);
172 ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
173 ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
174
175 #define zend_hash_move_forward(ht) \
176 zend_hash_move_forward_ex(ht, NULL)
177 #define zend_hash_move_backwards(ht) \
178 zend_hash_move_backwards_ex(ht, NULL)
36eaad25 » zsuraski
2000-12-22 Allow get_current_key() not to return the key itself, instead of a du…
179 #define zend_hash_get_current_key(ht, str_index, num_index, duplicate) \
180 zend_hash_get_current_key_ex(ht, str_index, NULL, num_index, duplicate, NULL)
a32c54bd » Andrei Zmievski
2000-03-13 Introduced a way to traverse hashes through external pointers.
181 #define zend_hash_get_current_key_type(ht) \
182 zend_hash_get_current_key_type_ex(ht, NULL)
183 #define zend_hash_get_current_data(ht, pData) \
184 zend_hash_get_current_data_ex(ht, pData, NULL)
185 #define zend_hash_internal_pointer_reset(ht) \
186 zend_hash_internal_pointer_reset_ex(ht, NULL)
187 #define zend_hash_internal_pointer_end(ht) \
188 zend_hash_internal_pointer_end_ex(ht, NULL)
573b4602 » andigutmans
1999-04-07 Zend Library
189
190 /* Copying, merging and sorting */
9f51f154 » zsuraski
2000-02-05 - Stop passing list/plist to internal functions
191 ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size);
192 ZEND_API void zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite);
471947b1 » zsuraski
2003-02-04 Reimplement PPP properties
193 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);
3bdddb49 » Sebastian Bergmann
2001-09-19 MFZE1
194 ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC);
8ce8324e » zsuraski
2001-07-30 More TSRMLS_FETCH annihilation
195 ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
3bdddb49 » Sebastian Bergmann
2001-09-19 MFZE1
196 ZEND_API int zend_hash_minmax(HashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC);
573b4602 » andigutmans
1999-04-07 Zend Library
197
198 ZEND_API int zend_hash_num_elements(HashTable *ht);
199
3964c92f » Andrei Zmievski
1999-11-04 Made zend_hash_rehash() callable from outside.
200 ZEND_API int zend_hash_rehash(HashTable *ht);
573b4602 » andigutmans
1999-04-07 Zend Library
201
1186d51c » andigutmans
2002-06-08 - Add a loop unrolled version of the hash function and a bit of an
202 /*
203 * DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
204 *
205 * This is Daniel J. Bernstein's popular `times 33' hash function as
206 * posted by him years ago on comp.lang.c. It basically uses a function
207 * like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
208 * known hash functions for strings. Because it is both computed very
209 * fast and distributes very well.
210 *
211 * The magic of number 33, i.e. why it works better than many other
212 * constants, prime or not, has never been adequately explained by
213 * anyone. So I try an explanation: if one experimentally tests all
214 * multipliers between 1 and 256 (as RSE did now) one detects that even
215 * numbers are not useable at all. The remaining 128 odd numbers
216 * (except for the number 1) work more or less all equally well. They
217 * all distribute in an acceptable way and this way fill a hash table
218 * with an average percent of approx. 86%.
219 *
220 * If one compares the Chi^2 values of the variants, the number 33 not
221 * even has the best value. But the number 33 and a few other equally
222 * good numbers like 17, 31, 63, 127 and 129 have nevertheless a great
223 * advantage to the remaining numbers in the large set of possible
224 * multipliers: their multiply operation can be replaced by a faster
225 * operation based on just one shift plus either a single addition
226 * or subtraction operation. And because a hash function has to both
227 * distribute good _and_ has to be very fast to compute, those few
228 * numbers should be preferred and seems to be the reason why Daniel J.
229 * Bernstein also preferred it.
230 *
231 *
232 * -- Ralf S. Engelschall <rse@engelschall.com>
233 */
234
b98acd61 » andigutmans
2001-07-11 - Move inline_zend_hash_func() to header file
235 static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength)
236 {
1186d51c » andigutmans
2002-06-08 - Add a loop unrolled version of the hash function and a bit of an
237 register ulong hash = 5381;
238
239 /* variant with the hash unrolled eight times */
240 for (; nKeyLength >= 8; nKeyLength -= 8) {
241 hash = ((hash << 5) + hash) + *arKey++;
242 hash = ((hash << 5) + hash) + *arKey++;
243 hash = ((hash << 5) + hash) + *arKey++;
244 hash = ((hash << 5) + hash) + *arKey++;
245 hash = ((hash << 5) + hash) + *arKey++;
246 hash = ((hash << 5) + hash) + *arKey++;
247 hash = ((hash << 5) + hash) + *arKey++;
248 hash = ((hash << 5) + hash) + *arKey++;
249 }
250 switch (nKeyLength) {
251 case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
252 case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
253 case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
254 case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
255 case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
256 case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
257 case 1: hash = ((hash << 5) + hash) + *arKey++; break;
90f43c78 » andigutmans
2002-06-08 - This should improve performance on Windows
258 case 0: break;
259 EMPTY_SWITCH_DEFAULT_CASE()
b98acd61 » andigutmans
2001-07-11 - Move inline_zend_hash_func() to header file
260 }
1186d51c » andigutmans
2002-06-08 - Add a loop unrolled version of the hash function and a bit of an
261 return hash;
b98acd61 » andigutmans
2001-07-11 - Move inline_zend_hash_func() to header file
262 }
263
1186d51c » andigutmans
2002-06-08 - Add a loop unrolled version of the hash function and a bit of an
264
6cf5e7f9 » smalyshev
2001-07-11 No hashpjw anymore, but we have zend_hash_func
265 ZEND_API ulong zend_hash_func(char *arKey, uint nKeyLength);
573b4602 » andigutmans
1999-04-07 Zend Library
266
93536507 » Stig S. Bakken
1999-12-01 Fix warnings surfacing in maintainer-mode.
267 #if ZEND_DEBUG
573b4602 » andigutmans
1999-04-07 Zend Library
268 /* debug */
269 void zend_hash_display_pListTail(HashTable *ht);
270 void zend_hash_display(HashTable *ht);
271 #endif
272
fd4650b5 » zsuraski
1999-06-07 Add zend_hash_get_current_key_type()
273 END_EXTERN_C()
274
0a14a32c » zsuraski
2000-04-25 Add useful macros
275 #define ZEND_INIT_SYMTABLE(ht) \
276 ZEND_INIT_SYMTABLE_EX(ht, 2, 0)
277
278 #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent) \
279 zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
280
281
a6172174 » Sascha Schumann
2000-07-03 Replace macros which begin with an underscore through an appropiately
282 #endif /* ZEND_HASH_H */
333406bd » foobar
2003-02-01 - Added some missing CVS $Id$ tags, headers and footers.
283
284 /*
285 * Local variables:
286 * tab-width: 4
287 * c-basic-offset: 4
288 * indent-tabs-mode: t
289 * End:
290 */
Something went wrong with that request. Please try again.