2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.13 1994/02/24 07:05:28 markc
11 * Man page for librpcUtil.a
12 * Extended list class to provide map function. rpcUtil supports internet domain
15 * Revision 1.12 1994/02/10 23:08:21 hollings
16 * Fixed list.h ++ function to work when a hash table has an element at
17 * slot zero in the table.
19 * Removed unused fields in hist class.
21 * Revision 1.11 1994/02/09 22:37:09 hollings
22 * Added print routines to list and hash table.
24 * Revision 1.10 1994/02/08 00:30:32 hollings
25 * Make libutil more compatable with ATT CC.
27 * Revision 1.9 1994/02/03 23:30:43 hollings
28 * changed listHash to a macro to work with g++ 2.5.2.
30 * Revision 1.8 1994/01/25 20:49:40 hollings
31 * First real version of utility library.
33 * Revision 1.7 1994/01/19 20:46:17 hollings
34 * guardef defn of true/false.
36 * Revision 1.6 1993/12/15 21:06:54 hollings
37 * removed destructors. Our current list semantics don't support auto
38 * destruction of list comonents since list elements can be shared between
41 * Revision 1.5 1993/12/13 20:11:14 hollings
42 * added destructor for List class.
44 * Revision 1.4 1993/10/19 15:31:52 hollings
45 * assorted small fixes.
47 * Revision 1.3 1993/08/02 22:46:37 hollings
48 * added remove which was missing.
50 * Revision 1.2 1993/07/01 17:02:36 hollings
53 * Revision 1.1 1993/05/07 20:21:15 hollings
56 * Revision 1.1 1993/03/19 22:51:05 hollings
74 #define ListHash(ptr, size) (((int)(ptr) % (int)(size)))
76 template <class Type> class List;
77 template <class Type> class StringList;
79 template <class Type> class ListItem {
80 friend class List<Type>;
81 friend class StringList<Type>;
88 template <class Type> class List {
90 List() { head = NULL; current = NULL; }
91 int empty() { return (head == NULL);}
92 friend ostream &operator<<(ostream&, List<Type>&);
94 void add(Type data, void *key);
96 Boolean addUnique(Type data);
97 Boolean addUnique(Type data, void *key) {
108 Type find(void *key);
109 Boolean remove(void *key);
112 ListItem<Type> *curr;
114 for (curr=head,c=0; curr; curr=curr->next) c++;
123 void operator +=(List<Type> mergee) {
124 ListItem<Type> *curr;
126 for (curr=mergee.head; curr; curr=curr->next) {
127 add(curr->data, curr->key);
131 Type ret = (Type) NULL;
138 void map (void (*map_function)(const Type item)) {
139 const ListItem<Type> *temp_ptr = 0;
141 if (!map_function) return;
142 for (temp_ptr = head; temp_ptr && temp_ptr->data; temp_ptr = temp_ptr->next)
143 map_function (temp_ptr->data);
145 void setCurrent() { current = head;}
146 const Type getCurrent() {
147 if (current) return ( (const Type) current->data);
154 void advanceCurrent() { if (current) current = current->next;}
156 ListItem<Type> *head;
157 ListItem<Type> *current;
160 template <class Type> ostream &operator<<(ostream &os, List<Type> &data)
164 for (curr= data; *curr; curr++) {
171 // Warning this function should be replaced by the stream operator above, but
172 // g++ is broken and won't create it.
174 template <class Type> void List<Type>::print()
176 ListItem<Type> *curr;
178 for (curr=head; curr; curr=curr->next) {
179 cout << (curr->data) << endl;
183 template <class Type> void List<Type>::add(Type data, void *key)
187 ni = new(ListItem<Type>);
195 template <class Type> void List<Type>::add(Type data)
197 add(data, (void *) data);
200 template <class Type> Boolean List<Type>::addUnique(Type data)
202 return(addUnique(data, (void *) data));
205 template <class Type> Boolean List<Type>::remove(void *key)
208 ListItem<Type> *curr;
210 for (curr=head, lag = NULL; curr; curr=curr->next) {
211 if (curr->key == key) {
219 lag->next = curr->next;
223 // if the 'current' pointer is this element, advance it
233 template <class Type> Type List<Type>::find(void *data)
235 ListItem<Type> *curr;
237 for (curr=head; curr; curr=curr->next) {
238 if (curr->key == data) {
245 template <class Type> class HTable {
248 friend ostream &operator<<(ostream&, HTable<Type>&);
250 HTable(Type data) { (void) HTable(); add(data, (void *) data); }
251 void add(Type data, void *key);
253 Boolean addUnique(Type data, void *key) {
264 Type find(void *key);
265 Boolean remove(void *key);
266 Type operator =(HTable<Type> arg) {
268 tableSize = arg.tableSize;
270 // find the first item.
282 if (curr) return(curr);
283 for (currHid++; currHid < tableSize; currHid++) {
284 if (table[currHid]) {
285 currList = *table[currHid];
287 if (curr) return(curr);
296 for (i=0; i < tableSize; i++) {
298 total += table[i]->count();
310 template <class Type> ostream &operator<<(ostream &os, HTable<Type> &data)
315 for (i=0; i < data.tableSize; i++) {
317 os << *data.table[i];
325 // Warning this function should be replaced by the stream operator above, but
326 // g++ is broken and won't create it.
328 template <class Type> void HTable<Type>::print()
333 for (i=0; i < tableSize; i++) {
340 template <class Type> HTable<Type>::HTable()
347 template <class Type> Type HTable<Type>::find(void *key)
351 if (!tableSize) return(NULL);
352 hid = ListHash(key, tableSize);
356 return(table[hid]->find(key));
360 template <class Type> void HTable<Type>::add(Type data, void *key)
366 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
368 hid = ListHash(key, tableSize);
370 table[hid] = new(List<Type>);
372 table[hid]->add(data, key);
376 template <class Type> Boolean HTable<Type>::remove(void *key)
380 hid = ListHash(key, tableSize);
382 return(table[hid]->remove(key));
387 template <class Type> class StringList: public List<Type> {
389 Type find(void *key);
392 template <class Type> Type StringList<Type>::find(void *data)
394 ListItem<Type> *curr;
396 for (curr=head; curr; curr=curr->next) {
397 if (!strcmp((char *) curr->key, (char *) data)) {