2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.9 1994/02/03 23:30:43 hollings
11 * changed listHash to a macro to work with g++ 2.5.2.
13 * Revision 1.8 1994/01/25 20:49:40 hollings
14 * First real version of utility library.
16 * Revision 1.7 1994/01/19 20:46:17 hollings
17 * guardef defn of true/false.
19 * Revision 1.6 1993/12/15 21:06:54 hollings
20 * removed destructors. Our current list semantics don't support auto
21 * destruction of list comonents since list elements can be shared between
24 * Revision 1.5 1993/12/13 20:11:14 hollings
25 * added destructor for List class.
27 * Revision 1.4 1993/10/19 15:31:52 hollings
28 * assorted small fixes.
30 * Revision 1.3 1993/08/02 22:46:37 hollings
31 * added remove which was missing.
33 * Revision 1.2 1993/07/01 17:02:36 hollings
36 * Revision 1.1 1993/05/07 20:21:15 hollings
39 * Revision 1.1 1993/03/19 22:51:05 hollings
56 #define ListHash(ptr, size) (((int)(ptr) % (int)(size)))
58 template <class Type> class List;
60 template <class Type> class ListItem {
61 friend class List<Type>;
62 friend class StringList<Type>;
69 template <class Type> class List {
71 List() { head = NULL; }
72 void add(Type data, void *key);
73 void add(Type data) { add(data, (void *) data); }
74 Boolean addUnique(Type data) { return(addUnique(data, (void *) data)); }
75 Boolean addUnique(Type data, void *key) {
87 Boolean remove(void *key);
92 for (curr=head,c=0; curr; curr=curr->next) c++;
101 void operator +=(List<Type> mergee) {
102 ListItem<Type> *curr;
104 for (curr=mergee.head; curr; curr=curr->next) {
105 add(curr->data, curr->key);
109 Type ret = (Type) NULL;
117 ListItem<Type> *head;
120 template <class Type> void List<Type>::add(Type data, void *key)
124 ni = new(ListItem<Type>);
132 template <class Type> Boolean List<Type>::remove(void *key)
135 ListItem<Type> *curr;
137 for (curr=head, lag = NULL; curr; curr=curr->next) {
138 if (curr->key == key) {
146 lag->next = curr->next;
157 template <class Type> Type List<Type>::find(void *data)
159 ListItem<Type> *curr;
161 for (curr=head; curr; curr=curr->next) {
162 if (curr->key == data) {
169 template <class Type> class HTable {
172 HTable(Type data) { HTable(); add(data, (void *) data); }
174 void add(Type data, void *key);
175 Boolean addUnique(Type data, void *key) {
186 Type find(void *key);
187 Boolean remove(void *key);
192 if (curr) return(curr);
193 for (currHid++; currHid < tableSize; currHid++) {
194 if (table[currHid]) {
195 currList = *table[currHid];
197 if (curr) return(curr);
213 for (i=0; i < tableSize; i++) {
215 total += table[i]->count();
227 template <class Type> HTable<Type>::HTable()
234 template <class Type> Type HTable<Type>::find(void *key)
238 if (!tableSize) return(NULL);
239 hid = ListHash(key, tableSize);
243 return(table[hid]->find(key));
247 template <class Type> void HTable<Type>::add(Type data, void *key)
253 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
255 hid = ListHash(key, tableSize);
257 table[hid] = new(List<Type>);
259 table[hid]->add(data, key);
263 template <class Type> Boolean HTable<Type>::remove(void *key)
267 hid = ListHash(key, tableSize);
269 return(table[hid]->remove(key));
274 template <class Type> class StringList: public List<Type> {
276 Type find(void *key);
279 template <class Type> Type StringList<Type>::find(void *data)
281 ListItem<Type> *curr;
283 for (curr=head; curr; curr=curr->next) {
284 if (!strcmp((char *) curr->key, (char *) data)) {