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(); }
195 { shutdown(); deletev sl(); }
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_
bool get_autofree() const
Returns the state of autofree for the current element.
void set_autofree(bool autofree)
Sets the state of autofree for the current element.
A small, efficient, type-safe hash table (also known as dictionary) container class.
size_t count() const
Returns the number of elements in the hash table.
bool isempty() const
Returns true if the hash table is empty.
void set_autofree(const K &key, bool autofree)
Sets the state of autofree for the element associated with key.
bool get_autofree(const K &key) const
Returns the state of autofree for the element associated with key.
WvHashTable(unsigned _numslots)
Creates a hash table.
WvLink is one element of a WvList<T>.
A linked list container class.