140 typedef uintptr_t SmHashFunc(
const Key & key);
141 typedef void SmHashApplyFunc(
const Key & key,
const Type & obj,
void *
closure);
151 this->commonConstructor(
from.size,
from.loadfactor);
152 this->operator=(
from);
158 from.apply(SmHash::copy_data,
this);
166 delete [] this->buckets;
172 for (
i = 0;
i < this->size;
i++ ) {
173 while ( this->buckets[
i] ) {
175 this->buckets[
i] =
entry->next;
183 SbBool put(
const Key & key,
const Type & obj)
185 unsigned int i = this->getIndex(key);
188 if (
entry->key == key ) {
202 entry->next = this->buckets[
i];
205 if ( this->elements++ >= this->threshold ) {
206 this->resize((
unsigned int) smhash_geq_prime_number(this->size + 1));
211 SbBool get(
const Key & key, Type & obj)
const
214 unsigned int i = this->getIndex(key);
217 if (
entry->key == key ) {
226 SbBool remove(
const Key & key)
228 unsigned int i = this->getIndex(key);
232 if (
entry->key == key ) {
235 this->buckets[
i] = next;
249 void apply(SmHashApplyFunc * func,
void *
closure)
const
253 for (
i = 0;
i < this->size;
i++ ) {
254 elem = this->buckets[
i];
264 this->apply(SmHash::add_to_list, &
l);
267 unsigned int getNumElements(
void)
const {
return this->elements; }
269 void setHashFunc(SmHashFunc * func) { this->hashfunc = func; }
272 static uintptr_t default_hash_func(
const Key & key) {
276 unsigned int getIndex(
const Key & key)
const {
277 unsigned int idx = this->hashfunc(key);
278 return (idx % this->size);
281 void resize(
unsigned int newsize) {
283 if (this->size >=
newsize)
return;
285 unsigned int oldsize = this->size;
290 this->threshold = (
unsigned int) (
newsize * this->loadfactor);
312 unsigned int s = smhash_geq_prime_number(
sizearg);
320 this->hashfunc = default_hash_func;
327 for (
i = 0;
i < this->size;
i++ ) {
328 if ( this->buckets[
i] ) {
339 buckets = this->size;
340 elements = this->elements;
344 static void copy_data(
const Key & key,
const Type & obj,
void *
closure)
347 thisp->put(key, obj);
350 static void add_to_list(
const Key & key,
const Type & obj,
void *
closure)
358 unsigned int elements;
359 unsigned int threshold;
362 SmHashFunc * hashfunc;