2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.18 1994/07/11 23:00:57 jcargill
11 * Fixed bug where added two lists with (+=) operator could result in
12 * duplicate key entries
14 * Revision 1.17 1994/07/07 03:20:36 markc
15 * Added removeAll function to list class.
16 * Added machineType headers to specify pvm, cm5, ...
18 * Revision 1.16 1994/05/30 19:37:39 hollings
19 * added pragma for external g++ functions.
21 * Revision 1.15 1994/03/11 21:01:23 hollings
22 * Changed Boolean from int to char to match X11 convention.
24 * Revision 1.14 1994/02/25 00:25:57 hollings
25 * added tunable constants.
27 * Revision 1.13 1994/02/24 07:05:28 markc
28 * Man page for librpcUtil.a
29 * Extended list class to provide map function. rpcUtil supports internet domain
32 * Revision 1.12 1994/02/10 23:08:21 hollings
33 * Fixed list.h ++ function to work when a hash table has an element at
34 * slot zero in the table.
36 * Removed unused fields in hist class.
38 * Revision 1.11 1994/02/09 22:37:09 hollings
39 * Added print routines to list and hash table.
41 * Revision 1.10 1994/02/08 00:30:32 hollings
42 * Make libutil more compatable with ATT CC.
44 * Revision 1.9 1994/02/03 23:30:43 hollings
45 * changed listHash to a macro to work with g++ 2.5.2.
47 * Revision 1.8 1994/01/25 20:49:40 hollings
48 * First real version of utility library.
50 * Revision 1.7 1994/01/19 20:46:17 hollings
51 * guardef defn of true/false.
53 * Revision 1.6 1993/12/15 21:06:54 hollings
54 * removed destructors. Our current list semantics don't support auto
55 * destruction of list comonents since list elements can be shared between
58 * Revision 1.5 1993/12/13 20:11:14 hollings
59 * added destructor for List class.
61 * Revision 1.4 1993/10/19 15:31:52 hollings
62 * assorted small fixes.
64 * Revision 1.3 1993/08/02 22:46:37 hollings
65 * added remove which was missing.
67 * Revision 1.2 1993/07/01 17:02:36 hollings
70 * Revision 1.1 1993/05/07 20:21:15 hollings
73 * Revision 1.1 1993/03/19 22:51:05 hollings
93 #define ListHash(ptr, size) (((int)(ptr) % (int)(size)))
95 template <class Type> class List;
96 template <class Type> class StringList;
98 template <class Type> class ListItem {
99 friend class List<Type>;
100 friend class StringList<Type>;
104 ListItem<Type> *next;
107 template <class Type> class List {
109 List() { head = NULL; current = NULL; }
110 int empty() { return (head == NULL);}
111 friend ostream &operator<<(ostream&, List<Type>&);
113 void add(Type data, void *key);
115 Boolean addUnique(Type data);
116 Boolean addUnique(Type data, void *key) {
127 Type find(void *key);
128 Boolean remove(void *key);
129 void removeAll(int deleteItem=1);
132 ListItem<Type> *curr;
134 for (curr=head,c=0; curr; curr=curr->next) c++;
143 void operator +=(List<Type> mergee) {
144 ListItem<Type> *curr;
146 for (curr=mergee.head; curr; curr=curr->next) {
147 addUnique(curr->data, curr->key);
151 Type ret = (Type) NULL;
158 void map (void (*map_function)(const Type item)) {
159 const ListItem<Type> *temp_ptr = 0;
161 if (!map_function) return;
162 for (temp_ptr = head; temp_ptr && temp_ptr->data; temp_ptr = temp_ptr->next)
163 map_function (temp_ptr->data);
165 void setCurrent() { current = head;}
166 const Type getCurrent() {
167 if (current) return ( (const Type) current->data);
174 void advanceCurrent() { if (current) current = current->next;}
176 ListItem<Type> *head;
177 ListItem<Type> *current;
180 template <class Type> ostream &operator<<(ostream &os, List<Type> &data)
184 for (curr= data; *curr; curr++) {
191 // Warning this function should be replaced by the stream operator above, but
192 // g++ is broken and won't create it.
194 template <class Type> void List<Type>::print()
196 ListItem<Type> *curr;
198 for (curr=head; curr; curr=curr->next) {
199 cout << (curr->data) << endl;
203 template <class Type> void List<Type>::add(Type data, void *key)
207 ni = new(ListItem<Type>);
215 template <class Type> void List<Type>::add(Type data)
217 add(data, (void *) data);
220 template <class Type> Boolean List<Type>::addUnique(Type data)
222 return(addUnique(data, (void *) data));
225 template <class Type> void List<Type>::removeAll(int deleteItem)
227 ListItem<Type> *curr, *next;
242 template <class Type> Boolean List<Type>::remove(void *key)
245 ListItem<Type> *curr;
247 for (curr=head, lag = NULL; curr; curr=curr->next) {
248 if (curr->key == key) {
256 lag->next = curr->next;
260 // if the 'current' pointer is this element, advance it
270 template <class Type> Type List<Type>::find(void *data)
272 ListItem<Type> *curr;
274 for (curr=head; curr; curr=curr->next) {
275 if (curr->key == data) {
282 template <class Type> class HTable {
285 friend ostream &operator<<(ostream&, HTable<Type>&);
287 HTable(Type data) { (void) HTable(); add(data, (void *) data); }
288 void add(Type data, void *key);
290 Boolean addUnique(Type data, void *key) {
301 Type find(void *key);
302 Boolean remove(void *key);
303 HTable<Type> operator =(HTable<Type> arg) {
305 tableSize = arg.tableSize;
307 // find the first item.
320 if (curr) return(curr);
321 for (currHid++; currHid < tableSize; currHid++) {
322 if (table[currHid]) {
323 currList = *table[currHid];
325 if (curr) return(curr);
334 for (i=0; i < tableSize; i++) {
336 total += table[i]->count();
348 template <class Type> ostream &operator<<(ostream &os, HTable<Type> &data)
353 for (i=0; i < data.tableSize; i++) {
355 os << *data.table[i];
363 // Warning this function should be replaced by the stream operator above, but
364 // g++ is broken and won't create it.
366 template <class Type> void HTable<Type>::print()
371 for (i=0; i < tableSize; i++) {
378 template <class Type> HTable<Type>::HTable()
385 template <class Type> Type HTable<Type>::find(void *key)
389 if (!tableSize) return(NULL);
390 hid = ListHash(key, tableSize);
394 return(table[hid]->find(key));
398 template <class Type> void HTable<Type>::add(Type data, void *key)
404 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
406 hid = ListHash(key, tableSize);
408 table[hid] = new(List<Type>);
410 table[hid]->add(data, key);
414 template <class Type> Boolean HTable<Type>::remove(void *key)
418 hid = ListHash(key, tableSize);
420 return(table[hid]->remove(key));
425 template <class Type> class StringList: public List<Type> {
427 Type find(void *key);
430 template <class Type> Type StringList<Type>::find(void *data)
432 ListItem<Type> *curr;
434 for (curr=head; curr; curr=curr->next) {
435 if (!strcmp((char *) curr->key, (char *) data)) {