]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/hash.cxx
1 /*******************************************************************************
2 * Simplified Wrapper and Interface Generator (SWIG)
4 * Author : David Beazley
6 * Department of Computer Science
7 * University of Chicago
10 * beazley@cs.uchicago.edu
12 * Please read the file LICENSE for the copyright and terms by which SWIG
13 * can be used and distributed.
14 *******************************************************************************/
18 /*******************************************************************************
23 * A very simple Hash table class. Could probably make it more elegant with
24 * templates, but templates are pure evil...
25 *******************************************************************************/
29 // -----------------------------------------------------------------------------
32 // Constructor. Creates a new hash table.
36 // Output : New Hash object.
38 // Side Effects : None
39 // -----------------------------------------------------------------------------
44 hashtable
= new Node
*[hashsize
];
45 for (i
= 0; i
< hashsize
; i
++) {
52 // -----------------------------------------------------------------------------
61 // Side Effects : Total destruction.
62 // -----------------------------------------------------------------------------
68 for (i
= 0; i
< hashsize
; i
++) {
81 // -----------------------------------------------------------------------------
82 // int Hash::h1(const char *key)
86 // Inputs : ASCII character string.
88 // Output : Hash table index.
90 // Side Effects : None
91 // -----------------------------------------------------------------------------
93 int Hash::h1(const char *key
) {
99 h
= (128*h
+ *c
) % hashsize
;
105 // -----------------------------------------------------------------------------
106 // int Hash::add(const char *k, void *obj)
108 // Adds a new object to the hash table.
112 // obj = Pointer to an object
114 // Output : 0 on success, -1 if item is already in hash table.
117 // Makes a new hash table entry.
118 // -----------------------------------------------------------------------------
120 int Hash::add(const char *k
, void *obj
) {
125 hv
= h1(k
); // Get hash value
129 if (strcmp(n
->key
,k
) == 0) return -1; // Already in hash table
134 // Safe to add this to the table
136 n
= new Node(k
,obj
,0);
137 if (prev
) prev
->next
= n
;
138 else hashtable
[hv
] = n
;
142 // -----------------------------------------------------------------------------
143 // int Hash::add(const char *k, void *obj, void (*d)(void *))
145 // Adds a new object to the hash table. Allows additional specification of
146 // a callback function for object deletion.
150 // obj = Object pointer
151 // d = Deletion function
153 // Output : 0 on success, -1 if item is already in hash table.
156 // Adds an entry to the hash table
157 // -----------------------------------------------------------------------------
159 int Hash::add(const char *k
, void *obj
, void (*d
)(void *)) {
164 hv
= h1(k
); // Get hash value
168 if (strcmp(n
->key
,k
) == 0) return -1; // Already in hash table
173 // Safe to add this to the table
175 n
= new Node(k
,obj
,d
);
176 if (prev
) prev
->next
= n
;
177 else hashtable
[hv
] = n
;
181 // -----------------------------------------------------------------------------
182 // void *Hash::lookup(const char *k)
184 // Looks up a value in the hash table. Returns a pointer to the object if found.
186 // Inputs : k = key value
188 // Output : Pointer to object or NULL if not found.
190 // Side Effects : None
191 // -----------------------------------------------------------------------------
193 void *Hash::lookup(const char *k
) {
197 hv
= h1(k
); // Get hash value
201 if (strcmp(n
->key
,k
) == 0) return n
->object
;
208 // -----------------------------------------------------------------------------
209 // void Hash::remove(const char *k)
211 // Removes an item from the hash table. Does nothing if item isn't in the
212 // hash table to begin with.
214 // Inputs : k = Key value
218 // Side Effects : Deletes item from hash table.
219 // -----------------------------------------------------------------------------
221 void Hash::remove(const char *k
) {
226 hv
= h1(k
); // Get hash value
230 if (strcmp(n
->key
,k
) == 0) {
231 // Found it, kill the thing
233 prev
->next
= n
->next
;
235 hashtable
[hv
] = n
->next
;
245 // -----------------------------------------------------------------------------
246 // void *Hash::first()
248 // Gets the first item from the hash table or NULL if empty.
252 // Output : First object in hash table or NULL if hash table is empty.
254 // Side Effects : Resets an internal iterator variable on the hash table.
255 // -----------------------------------------------------------------------------
257 void *Hash::first() {
261 while (!hashtable
[index
] && (index
< hashsize
))
264 if (index
>= hashsize
) return 0;
265 current
= hashtable
[index
];
266 return current
->object
;
270 // -----------------------------------------------------------------------------
271 // char *Hash::firstkey()
273 // Gets the first key from the hash table or NULL if empty.
277 // Output : First key in hash table or NULL if hash table is empty.
279 // Side Effects : Resets an internal iterator variable on the hash table.
280 // -----------------------------------------------------------------------------
282 char *Hash::firstkey() {
286 while ((index
< hashsize
) && (!hashtable
[index
]))
289 if (index
>= hashsize
) return 0;
290 current
= hashtable
[index
];
294 // -----------------------------------------------------------------------------
295 // void *Hash::next()
297 // Returns the next item in the hash table or NULL if there are no more entries.
298 // A call to first() should generally be made before using this function.
302 // Output : Pointer to next object or NULL if there are no more objects.
304 // Side Effects : Updates an iterator variable private to the hash table.
305 // -----------------------------------------------------------------------------
308 if (index
< 0) return first();
310 // Try to move to the next entry
312 current
= current
->next
;
315 return current
->object
;
318 while ((index
< hashsize
) && (!hashtable
[index
]))
320 if (index
>= hashsize
) return 0;
321 current
= hashtable
[index
];
322 return current
->object
;
327 // -----------------------------------------------------------------------------
328 // char *Hash::nextkey()
330 // Returns the next key in the hash table or NULL if there are no more entries.
331 // A call to firstkey() should generally be made before using this function.
335 // Output : Pointer to next key or NULL if there are no more objects.
337 // Side Effects : Updates an iterator variable private to the hash table.
338 // -----------------------------------------------------------------------------
340 char *Hash::nextkey() {
341 if (index
< 0) return firstkey();
343 // Try to move to the next entry
345 current
= current
->next
;
351 while (!hashtable
[index
] && (index
< hashsize
))
353 if (index
>= hashsize
) return 0;
354 current
= hashtable
[index
];