2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Small, efficient, type-safe hash table class. See wvhashtable.h.
7 #include "wvhashtable.h"
10 // we do not accept the _numslots value directly. Instead, we find the
11 // next number of slots which is >= _numslots and one less then a power
12 // of 2. This usually results in a fairly good hash table size.
13 WvHashTableBase::WvHashTableBase(unsigned _numslots
)
16 while ((_numslots
>>= 1) != 0)
18 numslots
= (1 << slides
) - 1;
22 // never returns NULL. If the object is not found, the 'previous' link
23 // is the last one in the list.
24 WvLink
*WvHashTableBase::prevlink(WvListBase
*wvslots
, const void *data
,
27 WvListBase::IterBase
i(wvslots
[hash
% numslots
]);
31 for (prev
= i
.cur(); prev
->next
; prev
= i
.next())
33 if (compare(data
, prev
->next
->data
))
40 void *WvHashTableBase::genfind(WvListBase
*wvslots
, const void *data
,
43 WvLink
*prev
= prevlink(wvslots
, data
, hash
);
45 return prev
->next
->data
;
51 size_t WvHashTableBase::count() const
55 for (unsigned i
= 0; i
< numslots
; i
++)
56 count
+= wvslots
[i
].count();
61 bool WvHashTableBase::isempty() const
63 for (unsigned i
= 0; i
< numslots
; i
++)
64 if (! wvslots
[i
].isempty())
70 WvLink
*WvHashTableBase::IterBase::next()
72 // In the best case, we can just look at the next item in the bucket.
77 // Keep local copies of information, so we don't have to poke into the
79 WvLink
*_link
= NULL
; // we would have returned if link were non-NULL
80 WvListBase
*begin
= tbl
->wvslots
;
81 WvListBase
*cur
= begin
+ tblindex
;
82 WvListBase
*end
= begin
+ tbl
->numslots
- 1;
84 // We'll go from the current bucket to the last bucket, in hopes that
85 // one of them will contain something.
89 _link
= cur
->head
.next
;
94 tblindex
= cur
- begin
; // Compute the tblindex.
95 link
= _link
; // Save the link