1 /* Hash Tables Implementation.
3 * This file implements in memory hash tables with insert/del/replace/find/
4 * get-random-element operations. Hash tables will auto resize if needed
5 * tables of power of two in size are used, collisions are handled by
6 * chaining. See the source code for more information... :)
8 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the name of Redis nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
48 /* ---------------------------- Utility funcitons --------------------------- */
50 static void _dictPanic(const char *fmt
, ...)
55 fprintf(stderr
, "\nDICT LIBRARY PANIC: ");
56 vfprintf(stderr
, fmt
, ap
);
57 fprintf(stderr
, "\n\n");
61 /* ------------------------- Heap Management Wrappers------------------------ */
63 static void *_dictAlloc(size_t size
)
65 void *p
= zmalloc(size
);
67 _dictPanic("Out of memory");
71 static void _dictFree(void *ptr
) {
75 /* -------------------------- private prototypes ---------------------------- */
77 static int _dictExpandIfNeeded(dict
*ht
);
78 static unsigned long _dictNextPower(unsigned long size
);
79 static int _dictKeyIndex(dict
*ht
, const void *key
);
80 static int _dictInit(dict
*ht
, dictType
*type
, void *privDataPtr
);
82 /* -------------------------- hash functions -------------------------------- */
84 /* Thomas Wang's 32 bit Mix Function */
85 unsigned int dictIntHashFunction(unsigned int key
)
96 /* Identity hash function for integer keys */
97 unsigned int dictIdentityHashFunction(unsigned int key
)
102 /* Generic hash function (a popular one from Bernstein).
103 * I tested a few and this was the best. */
104 unsigned int dictGenHashFunction(const unsigned char *buf
, int len
) {
105 unsigned int hash
= 5381;
108 hash
= ((hash
<< 5) + hash
) + (*buf
++); /* hash * 33 + c */
112 /* ----------------------------- API implementation ------------------------- */
114 /* Reset an hashtable already initialized with ht_init().
115 * NOTE: This function should only called by ht_destroy(). */
116 static void _dictReset(dict
*ht
)
124 /* Create a new hash table */
125 dict
*dictCreate(dictType
*type
,
128 dict
*ht
= _dictAlloc(sizeof(*ht
));
130 _dictInit(ht
,type
,privDataPtr
);
134 /* Initialize the hash table */
135 int _dictInit(dict
*ht
, dictType
*type
,
140 ht
->privdata
= privDataPtr
;
144 /* Resize the table to the minimal size that contains all the elements,
145 * but with the invariant of a USER/BUCKETS ration near to <= 1 */
146 int dictResize(dict
*ht
)
148 int minimal
= ht
->used
;
150 if (minimal
< DICT_HT_INITIAL_SIZE
)
151 minimal
= DICT_HT_INITIAL_SIZE
;
152 return dictExpand(ht
, minimal
);
155 /* Expand or create the hashtable */
156 int dictExpand(dict
*ht
, unsigned long size
)
158 dict n
; /* the new hashtable */
159 unsigned long realsize
= _dictNextPower(size
), i
;
161 /* the size is invalid if it is smaller than the number of
162 * elements already inside the hashtable */
166 _dictInit(&n
, ht
->type
, ht
->privdata
);
168 n
.sizemask
= realsize
-1;
169 n
.table
= _dictAlloc(realsize
*sizeof(dictEntry
*));
171 /* Initialize all the pointers to NULL */
172 memset(n
.table
, 0, realsize
*sizeof(dictEntry
*));
174 /* Copy all the elements from the old to the new table:
175 * note that if the old hash table is empty ht->size is zero,
176 * so dictExpand just creates an hash table. */
178 for (i
= 0; i
< ht
->size
&& ht
->used
> 0; i
++) {
179 dictEntry
*he
, *nextHe
;
181 if (ht
->table
[i
] == NULL
) continue;
183 /* For each hash entry on this slot... */
189 /* Get the new element index */
190 h
= dictHashKey(ht
, he
->key
) & n
.sizemask
;
191 he
->next
= n
.table
[h
];
194 /* Pass to the next element */
198 assert(ht
->used
== 0);
199 _dictFree(ht
->table
);
201 /* Remap the new hashtable in the old */
206 /* Add an element to the target hash table */
207 int dictAdd(dict
*ht
, void *key
, void *val
)
212 /* Get the index of the new element, or -1 if
213 * the element already exists. */
214 if ((index
= _dictKeyIndex(ht
, key
)) == -1)
217 /* Allocates the memory and stores key */
218 entry
= _dictAlloc(sizeof(*entry
));
219 entry
->next
= ht
->table
[index
];
220 ht
->table
[index
] = entry
;
222 /* Set the hash entry fields. */
223 dictSetHashKey(ht
, entry
, key
);
224 dictSetHashVal(ht
, entry
, val
);
229 /* Add an element, discarding the old if the key already exists.
230 * Return 1 if the key was added from scratch, 0 if there was already an
231 * element with such key and dictReplace() just performed a value update
233 int dictReplace(dict
*ht
, void *key
, void *val
)
237 /* Try to add the element. If the key
238 * does not exists dictAdd will suceed. */
239 if (dictAdd(ht
, key
, val
) == DICT_OK
)
241 /* It already exists, get the entry */
242 entry
= dictFind(ht
, key
);
243 /* Free the old value and set the new one */
244 dictFreeEntryVal(ht
, entry
);
245 dictSetHashVal(ht
, entry
, val
);
249 /* Search and remove an element */
250 static int dictGenericDelete(dict
*ht
, const void *key
, int nofree
)
253 dictEntry
*he
, *prevHe
;
257 h
= dictHashKey(ht
, key
) & ht
->sizemask
;
262 if (dictCompareHashKeys(ht
, key
, he
->key
)) {
263 /* Unlink the element from the list */
265 prevHe
->next
= he
->next
;
267 ht
->table
[h
] = he
->next
;
269 dictFreeEntryKey(ht
, he
);
270 dictFreeEntryVal(ht
, he
);
279 return DICT_ERR
; /* not found */
282 int dictDelete(dict
*ht
, const void *key
) {
283 return dictGenericDelete(ht
,key
,0);
286 int dictDeleteNoFree(dict
*ht
, const void *key
) {
287 return dictGenericDelete(ht
,key
,1);
290 /* Destroy an entire hash table */
291 int _dictClear(dict
*ht
)
295 /* Free all the elements */
296 for (i
= 0; i
< ht
->size
&& ht
->used
> 0; i
++) {
297 dictEntry
*he
, *nextHe
;
299 if ((he
= ht
->table
[i
]) == NULL
) continue;
302 dictFreeEntryKey(ht
, he
);
303 dictFreeEntryVal(ht
, he
);
309 /* Free the table and the allocated cache structure */
310 _dictFree(ht
->table
);
311 /* Re-initialize the table */
313 return DICT_OK
; /* never fails */
316 /* Clear & Release the hash table */
317 void dictRelease(dict
*ht
)
323 dictEntry
*dictFind(dict
*ht
, const void *key
)
328 if (ht
->size
== 0) return NULL
;
329 h
= dictHashKey(ht
, key
) & ht
->sizemask
;
332 if (dictCompareHashKeys(ht
, key
, he
->key
))
339 dictIterator
*dictGetIterator(dict
*ht
)
341 dictIterator
*iter
= _dictAlloc(sizeof(*iter
));
346 iter
->nextEntry
= NULL
;
350 dictEntry
*dictNext(dictIterator
*iter
)
353 if (iter
->entry
== NULL
) {
356 (signed)iter
->ht
->size
) break;
357 iter
->entry
= iter
->ht
->table
[iter
->index
];
359 iter
->entry
= iter
->nextEntry
;
362 /* We need to save the 'next' here, the iterator user
363 * may delete the entry we are returning. */
364 iter
->nextEntry
= iter
->entry
->next
;
371 void dictReleaseIterator(dictIterator
*iter
)
376 /* Return a random entry from the hash table. Useful to
377 * implement randomized algorithms */
378 dictEntry
*dictGetRandomKey(dict
*ht
)
382 int listlen
, listele
;
384 if (ht
->used
== 0) return NULL
;
386 h
= random() & ht
->sizemask
;
390 /* Now we found a non empty bucket, but it is a linked
391 * list and we need to get a random element from the list.
392 * The only sane way to do so is to count the element and
393 * select a random index. */
399 listele
= random() % listlen
;
401 while(listele
--) he
= he
->next
;
405 /* ------------------------- private functions ------------------------------ */
407 /* Expand the hash table if needed */
408 static int _dictExpandIfNeeded(dict
*ht
)
410 /* If the hash table is empty expand it to the intial size,
411 * if the table is "full" dobule its size. */
413 return dictExpand(ht
, DICT_HT_INITIAL_SIZE
);
414 if (ht
->used
== ht
->size
)
415 return dictExpand(ht
, ht
->size
*2);
419 /* Our hash table capability is a power of two */
420 static unsigned long _dictNextPower(unsigned long size
)
422 unsigned long i
= DICT_HT_INITIAL_SIZE
;
424 if (size
>= LONG_MAX
) return LONG_MAX
;
432 /* Returns the index of a free slot that can be populated with
433 * an hash entry for the given 'key'.
434 * If the key already exists, -1 is returned. */
435 static int _dictKeyIndex(dict
*ht
, const void *key
)
440 /* Expand the hashtable if needed */
441 if (_dictExpandIfNeeded(ht
) == DICT_ERR
)
443 /* Compute the key hash value */
444 h
= dictHashKey(ht
, key
) & ht
->sizemask
;
445 /* Search if this slot does not already contain the given key */
448 if (dictCompareHashKeys(ht
, key
, he
->key
))
455 void dictEmpty(dict
*ht
) {
459 #define DICT_STATS_VECTLEN 50
460 void dictPrintStats(dict
*ht
) {
461 unsigned long i
, slots
= 0, chainlen
, maxchainlen
= 0;
462 unsigned long totchainlen
= 0;
463 unsigned long clvector
[DICT_STATS_VECTLEN
];
466 printf("No stats available for empty dictionaries\n");
470 for (i
= 0; i
< DICT_STATS_VECTLEN
; i
++) clvector
[i
] = 0;
471 for (i
= 0; i
< ht
->size
; i
++) {
474 if (ht
->table
[i
] == NULL
) {
479 /* For each hash entry on this slot... */
486 clvector
[(chainlen
< DICT_STATS_VECTLEN
) ? chainlen
: (DICT_STATS_VECTLEN
-1)]++;
487 if (chainlen
> maxchainlen
) maxchainlen
= chainlen
;
488 totchainlen
+= chainlen
;
490 printf("Hash table stats:\n");
491 printf(" table size: %ld\n", ht
->size
);
492 printf(" number of elements: %ld\n", ht
->used
);
493 printf(" different slots: %ld\n", slots
);
494 printf(" max chain length: %ld\n", maxchainlen
);
495 printf(" avg chain length (counted): %.02f\n", (float)totchainlen
/slots
);
496 printf(" avg chain length (computed): %.02f\n", (float)ht
->used
/slots
);
497 printf(" Chain length distribution:\n");
498 for (i
= 0; i
< DICT_STATS_VECTLEN
-1; i
++) {
499 if (clvector
[i
] == 0) continue;
500 printf(" %s%ld: %ld (%.02f%%)\n",(i
== DICT_STATS_VECTLEN
-1)?">= ":"", i
, clvector
[i
], ((float)clvector
[i
]/ht
->size
)*100);
504 /* ----------------------- StringCopy Hash Table Type ------------------------*/
506 static unsigned int _dictStringCopyHTHashFunction(const void *key
)
508 return dictGenHashFunction(key
, strlen(key
));
511 static void *_dictStringCopyHTKeyDup(void *privdata
, const void *key
)
513 int len
= strlen(key
);
514 char *copy
= _dictAlloc(len
+1);
515 DICT_NOTUSED(privdata
);
517 memcpy(copy
, key
, len
);
522 static void *_dictStringKeyValCopyHTValDup(void *privdata
, const void *val
)
524 int len
= strlen(val
);
525 char *copy
= _dictAlloc(len
+1);
526 DICT_NOTUSED(privdata
);
528 memcpy(copy
, val
, len
);
533 static int _dictStringCopyHTKeyCompare(void *privdata
, const void *key1
,
536 DICT_NOTUSED(privdata
);
538 return strcmp(key1
, key2
) == 0;
541 static void _dictStringCopyHTKeyDestructor(void *privdata
, void *key
)
543 DICT_NOTUSED(privdata
);
545 _dictFree((void*)key
); /* ATTENTION: const cast */
548 static void _dictStringKeyValCopyHTValDestructor(void *privdata
, void *val
)
550 DICT_NOTUSED(privdata
);
552 _dictFree((void*)val
); /* ATTENTION: const cast */
555 dictType dictTypeHeapStringCopyKey
= {
556 _dictStringCopyHTHashFunction
, /* hash function */
557 _dictStringCopyHTKeyDup
, /* key dup */
559 _dictStringCopyHTKeyCompare
, /* key compare */
560 _dictStringCopyHTKeyDestructor
, /* key destructor */
561 NULL
/* val destructor */
564 /* This is like StringCopy but does not auto-duplicate the key.
565 * It's used for intepreter's shared strings. */
566 dictType dictTypeHeapStrings
= {
567 _dictStringCopyHTHashFunction
, /* hash function */
570 _dictStringCopyHTKeyCompare
, /* key compare */
571 _dictStringCopyHTKeyDestructor
, /* key destructor */
572 NULL
/* val destructor */
575 /* This is like StringCopy but also automatically handle dynamic
576 * allocated C strings as values. */
577 dictType dictTypeHeapStringCopyKeyValue
= {
578 _dictStringCopyHTHashFunction
, /* hash function */
579 _dictStringCopyHTKeyDup
, /* key dup */
580 _dictStringKeyValCopyHTValDup
, /* val dup */
581 _dictStringCopyHTKeyCompare
, /* key compare */
582 _dictStringCopyHTKeyDestructor
, /* key destructor */
583 _dictStringKeyValCopyHTValDestructor
, /* val destructor */