2 Copyright (C) 2005 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Hashtable::Hashtable()
25 for (int i = 0 ; i < MAX_BUCKETS ; i++)
32 name = "Unnamed Hashtable";
35 Hashtable::~Hashtable()
40 void Hashtable::put(char *key, void *data)
42 unsigned int hash = 0;
43 unsigned int entryBucket = 0;
45 for (unsigned int i = 0 ; i < strlen(key) ; i++)
47 hash = hash * MULTIPLIER + key[i];
50 entryBucket = hash % MAX_BUCKETS;
54 if (bucket[entryBucket] == NULL)
59 entry->previous = NULL;
62 bucket[entryBucket] = entry;
64 //printf("Added %s = (Hash = %d : Bucket = %d (1))\n", key, hash, entryBucket);
68 Entry *last = bucket[entryBucket];
76 if (last->hash == hash)
78 debug(("WARNING - POSSIBLE HASHCODE COLLISION!! - %s\n", key));
82 if (last->next != NULL)
95 entry->previous = last;
100 //printf("Added %s = (Hash = %d : Bucket = %d (%d))\n", key, hash, entryBucket, count);
104 void *Hashtable::get(char *key)
106 unsigned int hash = 0;
107 unsigned int entryBucket = 0;
109 for (unsigned int i = 0 ; i < strlen(key) ; i++)
111 hash = hash * MULTIPLIER + key[i];
114 entryBucket = hash % MAX_BUCKETS;
116 Entry *entry = bucket[entryBucket];
118 while (entry != NULL)
120 if (entry->hash == hash)
130 printf("WARNING - Hashtable::get('%s') - Returning NULL!\n", key);
136 void Hashtable::remove(char *key)
138 unsigned int hash = 0;
139 unsigned int entryBucket = 0;
141 for (unsigned int i = 0 ; i < strlen(key) ; i++)
143 hash = hash * MULTIPLIER + key[i];
146 entryBucket = hash % MAX_BUCKETS;
148 Entry *entry = bucket[entryBucket];
150 while (entry != NULL)
152 if (entry->hash == hash)
154 if (entry->previous != NULL)
156 entry->previous->next = entry->next;
159 if (entry->next != NULL)
161 entry->next->previous = entry->previous;
168 if (entry == bucket[entryBucket])
170 bucket[entryBucket] = NULL;
180 int Hashtable::getSize()
185 for (int i = 0 ; i < MAX_BUCKETS ; i++)
187 if (bucket[i] != NULL)
191 entry = bucket[i]->next;
193 while (entry != NULL)
205 List *Hashtable::toList()
212 for (int i = 0 ; i < MAX_BUCKETS ; i++)
214 if (bucket[i] != NULL)
216 ref = new Reference();
217 ref->object = (GameObject*)bucket[i]->data;
220 entry = bucket[i]->next;
222 while (entry != NULL)
224 ref = new Reference();
225 ref->object = (GameObject*)entry->data;
236 void Hashtable::printTable()
241 for (int i = 0 ; i < MAX_BUCKETS ; i++)
243 if (bucket[i] != NULL)
247 printf("%.3d = %d", i, bucket[i]->hash);
249 entry = bucket[i]->next;
251 while (entry != NULL)
255 printf(", %d", entry->hash);
264 printf("%d entries in table\n", count);
267 void Hashtable::clear()
270 Entry *entry2 = NULL;
274 for (int i = 0 ; i < MAX_BUCKETS ; i++)
276 for (entry = bucket[i] ; entry != NULL ; entry = entry2)
278 entry2 = entry->next;
287 if ((count > 0) && (nullWarning))
289 debug(("Removed %d items from %s Hashtable\n", count, name.getText()));