2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.3 1993/08/02 22:46:37 hollings
11 * added remove which was missing.
13 * Revision 1.2 1993/07/01 17:02:36 hollings
16 * Revision 1.1 1993/05/07 20:21:15 hollings
19 * Revision 1.1 1993/03/19 22:51:05 hollings
33 inline static int ListHash(void *ptr, int size)
35 return(((int)(ptr) % (int)(size)));
38 template <class Type> class List;
40 template <class Type> class ListItem {
41 friend class List<Type>;
48 template <class Type> class List {
50 List() { head = NULL; }
51 void add(Type data, void *key);
52 void add(Type data) { add(data, (void *) data); }
53 Boolean addUnique(Type data) { return(addUnique(data, (void *) data)); }
54 Boolean addUnique(Type data, void *key) {
66 void remove(Type data);
71 for (curr=head,c=0; curr; curr=curr->next) c++;
80 void operator +=(List<Type> mergee) {
83 for (curr=mergee.head; curr; curr=curr->next) {
84 add(curr->data, curr->key);
88 Type ret = (Type) NULL;
100 template <class Type> void List<Type>::add(Type data, void *key)
104 ni = new(ListItem<Type>);
112 template <class Type> void List<Type>::remove(Type data)
115 ListItem<Type> *curr;
117 for (curr=head, lag = NULL; curr; curr=curr->next) {
118 if (curr->data == data) {
126 lag->next = curr->next;
136 template <class Type> Type List<Type>::find(void *data)
138 ListItem<Type> *curr;
140 for (curr=head; curr; curr=curr->next) {
141 if (curr->key == data) {
148 template <class Type> class HTable {
151 HTable(Type data) { HTable(); add(data, (void *) data); }
153 void add(Type data, void *key);
154 Boolean addUnique(Type data, void *key) {
165 Type find(void *key);
166 void remove(Type data);
171 if (curr) return(curr);
172 for (currHid++; currHid < tableSize; currHid++) {
173 if (table[currHid]) {
174 currList = *table[currHid];
176 if (curr) return(curr);
192 for (i=0; i < tableSize; i++) {
194 total += table[i]->count();
206 template <class Type> HTable<Type>::HTable()
213 template <class Type> Type HTable<Type>::find(void *key)
217 if (!tableSize) return(NULL);
218 hid = ListHash(key, tableSize);
222 return(table[hid]->find(key));
226 template <class Type> void HTable<Type>::add(Type data, void *key)
232 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
234 hid = ListHash(key, tableSize);
236 table[hid] = new(List<Type>);
238 table[hid]->add(data, key);
242 template <class Type> void HTable<Type>::remove(Type data)
246 for (hid = 0; hid < tableSize; hid++) {
248 table[hid]->remove(data);