1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
// -*-c++-*-
/* $Id: ihash.h,v 1.11 1999/11/08 17:46:51 dm Exp $ */
/*
*
* Copyright (C) 1998 David Mazieres (dm@uun.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
/* Template implementation of intrusive hash table. Note that while
* externally this code is type safe, interally it makes use of void *
* pointers and byte offsets of field structures. This permits a
* single implementation of the _ihash_grow() function. */
#ifndef _IHASH_H_
#define _IHASH_H_ 1
#ifdef DMALLOC
#define IHASH_DEBUG 1
#include <typeinfo>
#endif /* DMALLOC */
#include "callback.h"
#include "keyfunc.h"
template<class T> class ihash_entry;
template<class T, ihash_entry<T> T::*field> class ihash_core;
struct _ihash_table;
extern void _ihash_grow (_ihash_table *, const size_t);
struct _ihash_entry {
void *next;
void **pprev;
hash_t val;
};
/* The template is for consistency accross all interfaces. We don't
* actually need it. */
template<class T>
#ifndef NO_TEMPLATE_FRIENDS
class
#else /* NO_TEMPLATE_FRIENDS */
struct
#endif /* NO_TEMPLATE_FRIENDS */
ihash_entry : _ihash_entry {
#ifndef NO_TEMPLATE_FRIENDS
template<class U, ihash_entry<U> U::*field> friend class ihash_core;
// template<ihash_entry<T> T::*field> friend class ihash_core<T, field>;
#endif /* NO_TEMPLATE_FRIENDS */
};
struct _ihash_table {
size_t buckets;
size_t entries;
void **tab;
};
template<class T, ihash_entry<T> T::*field>
class ihash_core {
_ihash_table t;
protected:
void init () {
t.buckets = t.entries = 0;
t.tab = NULL;
_ihash_grow (&t, (size_t) &(((T *) 0)->*field));
}
ihash_core () { init (); }
bool present (T *elm) {
for (T *e = lookup_val ((elm->*field).val); e; e = next_val (e))
if (e == elm)
return true;
return false;
}
void _check () {
#ifdef IHASH_DEBUG
size_t s = 0;
for (size_t n = 0; n < t.buckets; n++)
for (T *e = (T *) t.tab[n], *ne; e; e = ne) {
ne = (T *) (e->*field).next;
assert (n == (e->*field).val % t.buckets);
assert (ne != e);
s++;
}
assert (s == t.entries);
#endif
}
void insert_val (T *elm, hash_t hval) {
#ifdef IHASH_DEBUG
if (present (elm))
panic ("ihash_core(%s)::insert_val: element already in hash table\n",
typeid (ihash_core).name ());
#endif /* IHASH_DEBUG */
_check ();
if (++t.entries >= t.buckets)
_ihash_grow (&t, (size_t) (_ihash_entry *) &(((T *) 0)->*field));
(elm->*field).val = hval;
size_t bn = hval % t.buckets;
T *np;
if ((np = (T *) t.tab[bn]))
(np->*field).pprev = &(elm->*field).next;
(elm->*field).next = np;
(elm->*field).pprev = &t.tab[bn];
t.tab[bn] = elm;
_check ();
}
T *lookup_val (hash_t hval) const {
T *elm;
for (elm = (T *) t.tab[hval % t.buckets];
elm && (elm->*field).val != hval;
elm = (T *) (elm->*field).next)
;
return elm;
}
static T *next_val (T *elm) {
hash_t hval = (elm->*field).val;
while ((elm = (T *) (elm->*field).next) && (elm->*field).val != hval)
;
return elm;
}
public:
void clear () { delete[] t.tab; init (); }
~ihash_core () { delete[] t.tab; }
void deleteall () {
for (size_t i = 0; i < t.buckets; i++)
for (T *n = (T *) t.tab[i], *nn; n; n = nn) {
nn = (T *) (n->*field).next;
delete n;
}
clear ();
}
size_t size () const { return t.entries; }
void remove (T *elm) {
#ifdef IHASH_DEBUG
if (!present (elm))
panic ("ihash_core(%s)::remove: element not in hash table\n",
typeid (ihash_core).name ());
#endif /* IHASH_DEBUG */
_check ();
t.entries--;
if ((elm->*field).next)
(((T *) (elm->*field).next)->*field).pprev = (elm->*field).pprev;
*(elm->*field).pprev = (elm->*field).next;
}
T *first () const {
if (t.entries)
for (size_t i = 0; i < t.buckets; i++)
if (t.tab[i])
return (T *) t.tab[i];
return NULL;
}
T *next (const T *n) const {
if ((n->*field).next)
return (T *) (n->*field).next;
for (size_t i = (n->*field).val % t.buckets; ++i < t.buckets;)
if (t.tab[i])
return (T *) t.tab[i];
return NULL;
}
void traverse (callback<void, T *>::ref cb) {
for (size_t i = 0; i < t.buckets; i++)
for (T *n = (T *) t.tab[i], *nn; n; n = nn) {
nn = (T *) (n->*field).next;
(*cb) (n);
}
}
void traverse (void (T::*fn) ()) {
for (size_t i = 0; i < t.buckets; i++)
for (T *n = (T *) t.tab[i], *nn; n; n = nn) {
nn = (T *) (n->*field).next;
(n->*fn) ();
}
}
private:
/* No copying */
ihash_core (const ihash_core &);
ihash_core &operator = (const ihash_core &);
};
template<class K, class V, K V::*key,
ihash_entry<V> V::*field, class H = hashfn<K>, class E = equals<K> >
class ihash
: public ihash_core<V, field>
{
const E eq;
const H hash;
public:
ihash () {}
ihash (const E &e, const H &h) : eq (e), hash (h) {}
void insert (V *elm) { insert_val (elm, hash (elm->*key)); }
#if 0
template<class T> V *operator[] (const T &k) const {
#else
V *operator[] (const K &k) const {
#endif
V *v;
for (v = lookup_val (hash (k));
v && !eq (k, v->*key);
v = next_val (v))
;
return v;
}
V *nextkeq (V *v) {
const K &k = v->*key;
while ((v = next_val (v)) && !eq (k, v->*key))
;
return v;
};
};
template<class K1, class K2, class V, K1 V::*key1, K2 V::*key2,
ihash_entry<V> V::*field, class H = hash2fn<K1, K2>,
class E1 = equals<K1>, class E2 = equals<K2> >
class ihash2
: public ihash_core<V, field>
{
const E1 eq1;
const E2 eq2;
const H hash;
public:
ihash2 () {}
ihash2 (const E1 &e1, const E2 &e2, const H &h)
: eq1 (e1), eq2 (e2), hash (h) {}
void insert (V *elm)
{ insert_val (elm, hash (elm->*key1, elm->*key2)); }
V *operator() (const K1 &k1, const K2 &k2) const {
V *v;
for (v = lookup_val (hash (k1, k2));
v && !(eq1 (k1, v->*key1) && eq2 (k2, v->*key2));
v = next_val (v))
;
return v;
}
V *nextkeq (V *v) {
const K1 &k1 = v->*key1;
const K1 &k2 = v->*key2;
while ((v = next_val (v))
&& !(eq1 (k1, v->*key1) && eq2 (k2, v->*key2)))
;
return v;
};
};
template<class V, ihash_entry<V> V::*field,
class H = hashfn<V>, class E = equals<V> >
class shash
: public ihash_core<V, field>
{
const E eq;
const H hash;
public:
shash () {}
shash (const E &e, const H &h) : eq (e), hash (h) {}
void insert (V *elm) { insert_val (elm, hash (*elm)); }
V *operator[] (const V &k) const {
V *v;
for (v = lookup_val (hash (k));
v && !eq (k, *v);
v = next_val (v))
;
return v;
}
V *nextkeq (V *v) {
const V &k = *v;
while ((v = next_val (v)) && !eq (k, *v))
;
return v;
};
};
#endif /* !_IHASH_H_ */
|