8 #ifndef __WVHASHTABLE_H
9 #define __WVHASHTABLE_H
12 #include "wvlinklist.h"
13 #include "wvtypetraits.h"
101 void *genfind(
WvListBase *slots,
const void *data,
unsigned hash)
const;
105 virtual bool compare(
const void *key,
const void *elem)
const = 0;
114 size_t count()
const;
133 tblindex(other.tblindex), link(other.link)
136 { tblindex = 0; link = &tbl->wvslots[0].head; }
141 {
return link->data; }
148 return link->get_autofree();
156 link->set_autofree(autofree);
166 template <
class>
class Comparator =
OpEqComp
173 typedef Comparator<K> MyComparator;
175 unsigned hash(
const T *data)
176 {
return WvHash(*Accessor::get_key(data)); }
178 virtual bool compare(
const void *key,
const void *elem)
const
179 {
return MyComparator::compare((
const K *)key,
180 Accessor::get_key((
const T *)elem)); }
189 { wvslots =
new WvList<T>[numslots]; setup(); }
197 void add(T *data,
bool autofree)
198 { sl()[hash(data) % numslots].append(data, autofree); }
200 WvLink *getlink(
const K &key)
201 {
return prevlink(wvslots, &key, WvHash(key))->next; }
203 T *operator[] (
const K &key)
const
204 {
return (T *)genfind(wvslots, &key, WvHash(key)); }
213 return l->get_autofree();
229 l->set_autofree(autofree);
237 void remove(
const T *data)
239 unsigned h = hash(data);
240 WvLink *l = prevlink(wvslots, Accessor::get_key(data), h);
241 if (l && l->next) sl()[h % numslots].unlink_after(l);
258 {
return (T *)link->data; }
271 #define DeclareWvDict2(_classname_, _type_, _ftype_, _field_) \
272 __WvDict_base(_classname_, _type_, _ftype_, &obj->_field_)
274 #define DeclareWvDict(_type_, _ftype_, _field_) \
275 DeclareWvDict2(_type_##Dict, _type_, _ftype_, _field_)
277 #define DeclareWvTable2(_classname_, _type_) \
278 __WvDict_base(_classname_, _type_, _type_, obj)
280 #define DeclareWvTable(_type_) \
281 DeclareWvTable2(_type_##Table, _type_)
284 #define __WvDict_base(_classname_, _type_, _ftype_, _field_) \
285 template <class T, class K> \
286 struct _classname_##Accessor \
288 static const K *get_key(const T *obj) \
289 { return _field_; } \
292 typedef WvHashTable<_type_, _ftype_, \
293 _classname_##Accessor<_type_, _ftype_> > _classname_
295 #endif // __WVHASHTABLE_H