40 static int hash_code (
char *
key) {
43 while (*p) { code = (code << 1) ^ *p; p++; }
50 static int hash_key_equals (
char * key1,
char * key2) {
52 if (key1 == key2)
return 0;
56 if (*p1 != *p2)
return -1;
59 if (!*p1 && !*p2)
return 0;
65 static unsigned hash_key_length (
char * key) {
73 template <
class type_t>
77 for (n = size, buckets = 1; n != 1; n >>= 1)
86 equals = hash_key_equals;
87 keylen = hash_key_length;
95 template <
class type_t>
97 for (
int n = 0;
n < buckets;
n++) {
98 if (table[
n])
delete table[
n];
105 template <
class type_t>
107 for (
int n = 0;
n < buckets;
n++) {
108 if (table[
n])
delete table[
n];
121 template <
class type_t>
129 template <
class type_t>
140 for (n = buckets / 2; n < buckets; n++) { table[
n] = NULL; }
144 for (n = 0; n < buckets / 2; n++){
146 for (e = 0; bucket && e < bucket->size; e++) {
150 if ((next = table[loc]) == NULL) {
154 next->
add (bucket->entry[e]);
155 if (next->size == 1) fill++;
158 if (bucket->size == 0) fill--;
167 for (n = buckets; n < buckets * 2; n++) {
169 if (bucket && bucket->size) {
170 for (e = 0; e < bucket->size; e++) {
172 if ((next = table[loc]) == NULL) {
175 next->
add (bucket->entry[e]);
176 if (next->size == 1) fill++;
191 template <
class type_t>
193 int code = this->code (key);
198 for (
int e = 0;
e < bucket->size;
e++) {
199 if (bucket->entry[
e]->code == code) {
200 if (equals (bucket->entry[
e]->key, key) == 0) {
201 type_t * old = bucket->entry[
e]->value;
202 bucket->entry[
e]->value = value;
215 entry->key = (
char *) malloc (keylen (key));
216 memcpy (entry->key, key, keylen (key));
217 entry->value = value;
224 if (bucket->size == 1) {
236 template <
class type_t>
239 int code = this->code (key);
242 for (
int n = 0;
n < bucket->size;
n++) {
243 if (bucket->entry[
n]->code == code) {
244 if (equals (bucket->entry[
n]->key, key) == 0) {
245 value = bucket->entry[
n]->value;
247 if (bucket->size == 0) {
264 template <
class type_t>
266 int code = this->code (key);
269 for (
int n = 0;
n < bucket->size;
n++) {
270 if (bucket->entry[
n]->code == code)
271 if (equals (bucket->entry[
n]->key, key) == 0)
272 return bucket->entry[
n]->value;
279 template <
class type_t>
289 template <
class type_t>
294 _first = _last = _current = NULL;
298 template <
class type_t>
303 template <
class type_t>
309 template <
class type_t>
311 for (
int n = 0;
n < _hash->buckets;
n++) {
313 if (bucket && bucket->size) {
316 _current = _first = bucket->entry[_entry];
317 return _current->key;
320 _current = _first = NULL;
325 template <
class type_t>
327 for (
int n = _hash->buckets - 1;
n >= 0;
n--) {
329 if (bucket && bucket->size) {
331 _entry = bucket->size - 1;
332 _current = _last = bucket->entry[_entry];
333 return _current->key;
336 _current = _last = NULL;
341 template <
class type_t>
344 if (bucket && _entry < bucket->size - 1) {
346 _current = bucket->entry[_entry];
347 return _current->key;
349 for (
int n = _bucket + 1;
n < _hash->buckets;
n++) {
350 bucket = _hash->table[
n];
351 if (bucket && bucket->size) {
354 _current = bucket->entry[_entry];
355 return _current->key;
363 template <
class type_t>
366 if (bucket && _entry > 0) {
368 _current = bucket->entry[_entry];
369 return _current->key;
371 for (
int n = _bucket - 1;
n >= 0 ;
n--) {
372 bucket = _hash->table[
n];
373 if (bucket && bucket->size) {
375 _entry = bucket->size - 1;
376 _current = bucket->entry[_entry];
377 return _current->key;
385 template <
class type_t>
387 return _current ? _current->key : NULL;
391 template <
class type_t>
397 template <
class type_t>
399 return _current ? _current->value : NULL;
403 template <
class type_t>
405 return _first ? _first->key : NULL;
409 template <
class type_t>
411 return _last ? _last->key : NULL;