1 /* Hash Tables Implementation. 2 * 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... :) 7 * 8 * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions are met: 13 * 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. 22 * 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. 34 */ 35 36 #include <stdint.h> 37 38 #ifndef __DICT_H 39 #define __DICT_H 40 41 #define DICT_OK 0 42 #define DICT_ERR 1 43 44 /* Unused arguments generate annoying warnings... */ 45 #define DICT_NOTUSED(V) ((void) V) 46 47 typedef struct dictEntry { 48 void *key; 49 union { 50 void *val; 51 uint64_t u64; 52 int64_t s64; 53 double d; 54 } v; 55 struct dictEntry *next; 56 } dictEntry; 57 58 typedef struct dictType { 59 unsigned int (*hashFunction)(const void *key); 60 void *(*keyDup)(void *privdata, const void *key); 61 void *(*valDup)(void *privdata, const void *obj); 62 int (*keyCompare)(void *privdata, const void *key1, const void *key2); 63 void (*keyDestructor)(void *privdata, void *key); 64 void (*valDestructor)(void *privdata, void *obj); 65 } dictType; 66 67 /* This is our hash table structure. Every dictionary has two of this as we 68 * implement incremental rehashing, for the old to the new table. */ 69 typedef struct dictht { 70 dictEntry **table; 71 unsigned long size; 72 unsigned long sizemask; 73 unsigned long used; 74 } dictht; 75 76 typedef struct dict { 77 dictType *type; 78 void *privdata; 79 dictht ht[2]; 80 long rehashidx; /* rehashing not in progress if rehashidx == -1 */ 81 int iterators; /* number of iterators currently running */ 82 } dict; 83 84 /* If safe is set to 1 this is a safe iterator, that means, you can call 85 * dictAdd, dictFind, and other functions against the dictionary even while 86 * iterating. Otherwise it is a non safe iterator, and only dictNext() 87 * should be called while iterating. */ 88 typedef struct dictIterator { 89 dict *d; 90 long index; 91 int table, safe; 92 dictEntry *entry, *nextEntry; 93 /* unsafe iterator fingerprint for misuse detection. */ 94 long long fingerprint; 95 } dictIterator; 96 97 typedef void (dictScanFunction)(void *privdata, const dictEntry *de); 98 99 /* This is the initial size of every hash table */ 100 #define DICT_HT_INITIAL_SIZE 4 101 102 /* ------------------------------- Macros ------------------------------------*/ 103 #define dictFreeVal(d, entry) \ 104 if ((d)->type->valDestructor) \ 105 (d)->type->valDestructor((d)->privdata, (entry)->v.val) 106 107 #define dictSetVal(d, entry, _val_) do { \ 108 if ((d)->type->valDup) \ 109 entry->v.val = (d)->type->valDup((d)->privdata, _val_); \ 110 else \ 111 entry->v.val = (_val_); \ 112 } while(0) 113 114 #define dictSetSignedIntegerVal(entry, _val_) \ 115 do { entry->v.s64 = _val_; } while(0) 116 117 #define dictSetUnsignedIntegerVal(entry, _val_) \ 118 do { entry->v.u64 = _val_; } while(0) 119 120 #define dictSetDoubleVal(entry, _val_) \ 121 do { entry->v.d = _val_; } while(0) 122 123 #define dictFreeKey(d, entry) \ 124 if ((d)->type->keyDestructor) \ 125 (d)->type->keyDestructor((d)->privdata, (entry)->key) 126 127 #define dictSetKey(d, entry, _key_) do { \ 128 if ((d)->type->keyDup) \ 129 entry->key = (d)->type->keyDup((d)->privdata, _key_); \ 130 else \ 131 entry->key = (_key_); \ 132 } while(0) 133 134 #define dictCompareKeys(d, key1, key2) \ 135 (((d)->type->keyCompare) ? \ 136 (d)->type->keyCompare((d)->privdata, key1, key2) : \ 137 (key1) == (key2)) 138 139 #define dictHashKey(d, key) (d)->type->hashFunction(key) 140 #define dictGetKey(he) ((he)->key) 141 #define dictGetVal(he) ((he)->v.val) 142 #define dictGetSignedIntegerVal(he) ((he)->v.s64) 143 #define dictGetUnsignedIntegerVal(he) ((he)->v.u64) 144 #define dictGetDoubleVal(he) ((he)->v.d) 145 #define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size) 146 #define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used) 147 #define dictIsRehashing(d) ((d)->rehashidx != -1) 148 149 /* API */ 150 dict *dictCreate(dictType *type, void *privDataPtr); 151 int dictExpand(dict *d, unsigned long size); 152 int dictAdd(dict *d, void *key, void *val); 153 dictEntry *dictAddRaw(dict *d, void *key); 154 int dictReplace(dict *d, void *key, void *val); 155 dictEntry *dictReplaceRaw(dict *d, void *key); 156 int dictDelete(dict *d, const void *key); 157 int dictDeleteNoFree(dict *d, const void *key); 158 void dictRelease(dict *d); 159 dictEntry * dictFind(dict *d, const void *key); 160 void *dictFetchValue(dict *d, const void *key); 161 int dictResize(dict *d); 162 dictIterator *dictGetIterator(dict *d); 163 dictIterator *dictGetSafeIterator(dict *d); 164 dictEntry *dictNext(dictIterator *iter); 165 void dictReleaseIterator(dictIterator *iter); 166 dictEntry *dictGetRandomKey(dict *d); 167 unsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count); 168 void dictGetStats(char *buf, size_t bufsize, dict *d); 169 unsigned int dictGenHashFunction(const void *key, int len); 170 unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len); 171 void dictEmpty(dict *d, void(callback)(void*)); 172 void dictEnableResize(void); 173 void dictDisableResize(void); 174 int dictRehash(dict *d, int n); 175 int dictRehashMilliseconds(dict *d, int ms); 176 void dictSetHashFunctionSeed(unsigned int initval); 177 unsigned int dictGetHashFunctionSeed(void); 178 unsigned long dictScan(dict *d, unsigned long v, dictScanFunction *fn, void *privdata); 179 180 /* Hash table types */ 181 extern dictType dictTypeHeapStringCopyKey; 182 extern dictType dictTypeHeapStrings; 183 extern dictType dictTypeHeapStringCopyKeyValue; 184 185 #endif /* __DICT_H */ 186