2 * Routines to provide a memory-efficient hashtable.
4 * Copyright (C) 2007-2009 Wayne Davison
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, visit the http://fsf.org website.
22 #define HASH_LOAD_LIMIT(size) ((size)*3/4)
24 struct hashtable *hashtable_create(int size, int key64)
27 struct hashtable *tbl;
28 int node_size = key64 ? sizeof (struct ht_int64_node)
29 : sizeof (struct ht_int32_node);
31 /* Pick a power of 2 that can hold the requested size. */
32 if (size & (size-1) || size < 16) {
38 if (!(tbl = new(struct hashtable))
39 || !(tbl->nodes = new_array0(char, size * node_size)))
40 out_of_memory("hashtable_create");
43 tbl->node_size = node_size;
44 tbl->key64 = (short)key64;
46 if (DEBUG_GTE(HASH, 1)) {
49 snprintf(buf, sizeof buf, "req: %d, ", req);
52 rprintf(FINFO, "[%s] created hashtable %lx (%ssize: %d, keys: %d-bit)\n",
53 who_am_i(), (long)tbl, buf, size, key64 ? 64 : 32);
59 void hashtable_destroy(struct hashtable *tbl)
61 if (DEBUG_GTE(HASH, 1)) {
62 rprintf(FINFO, "[%s] destroyed hashtable %lx (size: %d, keys: %d-bit)\n",
63 who_am_i(), (long)tbl, tbl->size, tbl->key64 ? 64 : 32);
69 /* This returns the node for the indicated key, either newly created or
70 * already existing. Returns NULL if not allocating and not found. */
71 void *hashtable_find(struct hashtable *tbl, int64 key, int allocate_if_missing)
73 int key64 = tbl->key64;
74 struct ht_int32_node *node;
77 if (allocate_if_missing && tbl->entries > HASH_LOAD_LIMIT(tbl->size)) {
78 void *old_nodes = tbl->nodes;
79 int size = tbl->size * 2;
82 if (!(tbl->nodes = new_array0(char, size * tbl->node_size)))
83 out_of_memory("hashtable_node");
87 if (DEBUG_GTE(HASH, 1)) {
88 rprintf(FINFO, "[%s] growing hashtable %lx (size: %d, keys: %d-bit)\n",
89 who_am_i(), (long)tbl, size, key64 ? 64 : 32);
92 for (i = size / 2; i-- > 0; ) {
93 struct ht_int32_node *move_node = HT_NODE(tbl, old_nodes, i);
94 int64 move_key = HT_KEY(move_node, key64);
97 node = hashtable_find(tbl, move_key, 1);
98 node->data = move_node->data;
105 /* Based on Jenkins One-at-a-time hash. */
106 uchar buf[4], *keyp = buf;
110 for (ndx = 0, i = 0; i < 4; i++) {
119 /* Based on Jenkins hashword() from lookup3.c. */
122 /* Set up the internal state */
123 a = b = c = 0xdeadbeef + (8 << 2);
125 #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
126 #if SIZEOF_INT64 >= 8
127 b += (uint32)(key >> 32);
130 c ^= b; c -= rot(b, 14);
131 a ^= c; a -= rot(c, 11);
132 b ^= a; b -= rot(a, 25);
133 c ^= b; c -= rot(b, 16);
134 a ^= c; a -= rot(c, 4);
135 b ^= a; b -= rot(a, 14);
136 c ^= b; c -= rot(b, 24);
141 /* If it already exists, return the node. If we're not
142 * allocating, return NULL if the key is not found. */
146 ndx &= tbl->size - 1;
147 node = HT_NODE(tbl, tbl->nodes, ndx);
148 nkey = HT_KEY(node, key64);
153 if (!allocate_if_missing)
160 /* Take over this empty spot and then return the node. */
162 ((struct ht_int64_node*)node)->key = key;
164 node->key = (int32)key;