2
0

dict.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Hash table 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-2010, 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. #ifndef __DICT_H
  36. #define __DICT_H
  37. #define DICT_OK 0
  38. #define DICT_ERR 1
  39. /* Unused arguments generate annoying warnings... */
  40. #define DICT_NOTUSED(V) ((void) V)
  41. typedef struct dictEntry {
  42. void *key;
  43. void *val;
  44. struct dictEntry *next;
  45. } dictEntry;
  46. typedef struct dictType {
  47. unsigned int (*hashFunction)(const void *key);
  48. void *(*keyDup)(void *privdata, const void *key);
  49. void *(*valDup)(void *privdata, const void *obj);
  50. int (*keyCompare)(void *privdata, const void *key1, const void *key2);
  51. void (*keyDestructor)(void *privdata, void *key);
  52. void (*valDestructor)(void *privdata, void *obj);
  53. } dictType;
  54. typedef struct dict {
  55. dictEntry **table;
  56. dictType *type;
  57. unsigned long size;
  58. unsigned long sizemask;
  59. unsigned long used;
  60. void *privdata;
  61. } dict;
  62. typedef struct dictIterator {
  63. dict *ht;
  64. int index;
  65. dictEntry *entry, *nextEntry;
  66. } dictIterator;
  67. /* This is the initial size of every hash table */
  68. #define DICT_HT_INITIAL_SIZE 4
  69. /* ------------------------------- Macros ------------------------------------*/
  70. #define dictFreeEntryVal(ht, entry) \
  71. if ((ht)->type->valDestructor) \
  72. (ht)->type->valDestructor((ht)->privdata, (entry)->val)
  73. #define dictSetHashVal(ht, entry, _val_) do { \
  74. if ((ht)->type->valDup) \
  75. entry->val = (ht)->type->valDup((ht)->privdata, _val_); \
  76. else \
  77. entry->val = (_val_); \
  78. } while(0)
  79. #define dictFreeEntryKey(ht, entry) \
  80. if ((ht)->type->keyDestructor) \
  81. (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
  82. #define dictSetHashKey(ht, entry, _key_) do { \
  83. if ((ht)->type->keyDup) \
  84. entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
  85. else \
  86. entry->key = (_key_); \
  87. } while(0)
  88. #define dictCompareHashKeys(ht, key1, key2) \
  89. (((ht)->type->keyCompare) ? \
  90. (ht)->type->keyCompare((ht)->privdata, key1, key2) : \
  91. (key1) == (key2))
  92. #define dictHashKey(ht, key) (ht)->type->hashFunction(key)
  93. #define dictGetEntryKey(he) ((he)->key)
  94. #define dictGetEntryVal(he) ((he)->val)
  95. #define dictSlots(ht) ((ht)->size)
  96. #define dictSize(ht) ((ht)->used)
  97. /* API */
  98. static unsigned int dictGenHashFunction(const unsigned char *buf, int len);
  99. static dict *dictCreate(dictType *type, void *privDataPtr);
  100. static int dictExpand(dict *ht, unsigned long size);
  101. static int dictAdd(dict *ht, void *key, void *val);
  102. static int dictReplace(dict *ht, void *key, void *val);
  103. static int dictDelete(dict *ht, const void *key);
  104. static void dictRelease(dict *ht);
  105. static dictEntry * dictFind(dict *ht, const void *key);
  106. static dictIterator *dictGetIterator(dict *ht);
  107. static dictEntry *dictNext(dictIterator *iter);
  108. static void dictReleaseIterator(dictIterator *iter);
  109. #endif /* __DICT_H */