2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.1 1993/05/07 20:21:15 hollings
13 * Revision 1.1 1993/03/19 22:51:05 hollings
25 inline static int ListHash(void *ptr, int size)
27 return(((int)(ptr) % (int)(size)));
32 template <class Type> class ListItem {
33 friend class List<Type>;
40 template <class Type> class List {
42 List() { head = NULL; }
43 void add(Type data, void *key);
44 void add(Type data) { add(data, (void *) data); }
45 Boolean addUnique(Type data) { return(addUnique(data, (void *) data)); }
46 Boolean addUnique(Type data, void *key) {
58 void remove(Type data);
63 for (curr=head,c=0; curr; curr=curr->next) c++;
72 void operator +=(List<Type> mergee) {
75 for (curr=mergee.head; curr; curr=curr->next) {
76 add(curr->data, curr->key);
80 Type ret = (Type) NULL;
92 template <class Type> void List<Type>::add(Type data, void *key)
96 ni = new(ListItem<Type>);
104 template <class Type> void List<Type>::remove(Type data)
107 ListItem<Type> *curr;
109 for (curr=head, lag = NULL; curr; curr=curr->next) {
110 if (curr->data == data) {
118 lag->next = curr->next;
128 template <class Type> Type List<Type>::find(void *data)
130 ListItem<Type> *curr;
132 for (curr=head; curr; curr=curr->next) {
133 if (curr->key == data) {
140 template <class Type> class HTable {
143 HTable(Type data) { HTable(); add(data, (void *) data); }
145 void add(Type data, void *key);
146 Boolean addUnique(Type data, void *key) {
157 Type find(void *key);
158 // void remove(Type data);
163 if (curr) return(curr);
164 for (currHid++; currHid < tableSize; currHid++) {
165 if (table[currHid]) {
166 currList = *table[currHid];
168 if (curr) return(curr);
184 for (i=0; i < tableSize; i++) {
186 total += table[i]->count();
198 template <class Type> HTable<Type>::HTable()
205 template <class Type> Type HTable<Type>::find(void *key)
209 if (!tableSize) return(NULL);
210 hid = ListHash(key, tableSize);
214 return(table[hid]->find(key));
218 template <class Type> void HTable<Type>::add(Type data, void *key)
224 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
226 hid = ListHash(key, tableSize);
228 table[hid] = new(List<Type>);
230 table[hid]->add(data, key);