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
|
// -*-c++-*-
/* $Id: qhash.h,v 1.16 2001/07/18 03:02:17 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
*
*/
#ifndef _QHASH_H_
#define _QHASH_H_ 1
#include "ihash.h"
template<class T> struct qhash_lookup_return {
typedef T *type;
typedef const T *const_type;
static type ret (T *v) { return v; }
static const_type const_ret (T *v) { return v; }
};
template<class T> struct qhash_lookup_return<ref<T> > {
typedef ptr<T> type;
typedef ptr<const T> const_type;
static type ret (ref<T> *v) { if (v) return *v; return NULL; }
static const_type const_ret (ref<T> *v) { if (v) return *v; return NULL; }
};
template<class T> struct qhash_lookup_return<T &> {
typedef T *type;
typedef const T *const_type;
static type ret (T *v) { return v; }
static const_type const_ret (T *v) { return v; }
};
template<class K, class V> struct qhash_slot {
ihash_entry<qhash_slot> link;
const K key;
V value;
qhash_slot (const K &k, CREF (V) v) : key (k), value (v) {}
qhash_slot (const K &k, NCREF (V) v) : key (k), value (v) {}
};
template<class K, class V, class H = hashfn<K>, class E = equals<K>,
// XXX - We need to kludge this for both g++ and KCC
class R = qhash_lookup_return<V>,
ihash_entry<qhash_slot<K, V> > qhash_slot<K,V>::*kludge
= &qhash_slot<K,V>::link>
class qhash
: public ihash_core<qhash_slot<K, V>, kludge> {
typedef qhash_slot<K, V> slot;
typedef ihash_core<slot, kludge> core;
const E eq;
const H hash;
slot *getslot (const K &k) const {
slot *s;
for (s = lookup_val (hash (k)); s && !eq (s->key, k); s = next_val (s))
;
return s;
}
void delslot (slot *s) { core::remove (s); delete s; }
static void mkcb (callback<void, K, typename R::type>::ref cb, slot *s)
{ (*cb) (s->key, R::ret (&s->value)); }
static void mkcbr (callback<void, const K &, typename R::type>::ref cb,
slot *s)
{ (*cb) (s->key, R::ret (&s->value)); }
public:
qhash () {}
void clear () {
core::traverse (wrap (this, &qhash::delslot));
core::clear ();
}
~qhash () { clear (); }
#if 0
void traverse (callback <void, K, typename R::type>::ref cb)
{ core::traverse (wrap (mkcb, cb)); }
#endif
void traverse (callback <void, const K &, typename R::type>::ref cb)
{ core::traverse (wrap (mkcbr, cb)); }
void insert (const K &k) {
if (slot *s = getslot (k))
s->value = V ();
else
core::insert_val (New slot (k, V ()), hash (k));
}
void insert (const K &k, CREF (V) v) {
if (slot *s = getslot (k))
s->value = v;
else
core::insert_val (New slot (k, v), hash (k));
}
void insert (const K &k, NCREF (V) v) {
if (slot *s = getslot (k))
s->value = v;
else
core::insert_val (New slot (k, v), hash (k));
}
void remove (const K &k) {
if (slot *s = getslot (k))
delslot (s);
}
typename R::type operator[] (const K &k) {
if (slot *s = getslot (k))
return R::ret (&s->value);
else
return R::ret (NULL);
}
typename R::const_type operator[] (const K &k) const {
if (slot *s = getslot (k))
return R::const_ret (&s->value);
else
return R::const_ret (NULL);
}
};
template<class K> struct qhash_slot<K, void> {
ihash_entry<qhash_slot> link;
const K key;
qhash_slot (const K &k) : key (k) {}
};
template<class K, class H = hashfn<K>, class E = equals<K>,
// XXX - We need to kludge this for both g++ and KCC
ihash_entry<qhash_slot<K, void> > qhash_slot<K, void>::*kludge
= &qhash_slot<K, void>::link>
class bhash // <K, void, H, E, kludge>
: public ihash_core<qhash_slot<K, void>, kludge> {
typedef qhash_slot<K, void> slot;
typedef ihash_core<slot, kludge> core;
const E eq;
const H hash;
slot *getslot (const K &k) const {
slot *s;
for (s = lookup_val (hash (k)); s && !eq (s->key, k); s = next_val (s))
;
return s;
}
void delslot (slot *s) { core::remove (s); delete s; }
static void mkcb (callback<void, K>::ref cb, qhash_slot<K, void> *s)
{ (*cb) (s->key); }
static void mkcbr (callback<void, const K &>::ref cb, qhash_slot<K, void> *s)
{ (*cb) (s->key); }
public:
bhash () {}
void clear () { deleteall (); }
~bhash () { clear (); }
bool insert (const K &k) {
if (!getslot (k)) {
core::insert_val (New slot (k), hash (k));
return true;
}
return false;
}
void remove (const K &k) { if (slot *s = getslot (k)) delslot (s); }
bool operator[] (const K &k) const { return getslot (k); }
#if 0
void traverse (callback <void, K>::ref cb)
{ core::traverse (wrap (mkcb, cb)); }
#endif
void traverse (callback <void, const K &>::ref cb)
{ core::traverse (wrap (mkcbr, cb)); }
};
#endif /* !_QHASH_H_ */
|