2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.21 1994/09/22 03:17:22 markc
11 * added postfix ++ operator
13 * Revision 1.20 1994/08/17 18:22:54 markc
14 * Moved the definitions of the << operator into the class declaration to
17 * Revision 1.19 1994/07/26 20:07:42 hollings
18 * added cast to ensure hash table pointers are positive.
20 * Revision 1.18 1994/07/11 23:00:57 jcargill
21 * Fixed bug where added two lists with (+=) operator could result in
22 * duplicate key entries
24 * Revision 1.17 1994/07/07 03:20:36 markc
25 * Added removeAll function to list class.
26 * Added machineType headers to specify pvm, cm5, ...
28 * Revision 1.16 1994/05/30 19:37:39 hollings
29 * added pragma for external g++ functions.
31 * Revision 1.15 1994/03/11 21:01:23 hollings
32 * Changed Boolean from int to char to match X11 convention.
34 * Revision 1.14 1994/02/25 00:25:57 hollings
35 * added tunable constants.
37 * Revision 1.13 1994/02/24 07:05:28 markc
38 * Man page for librpcUtil.a
39 * Extended list class to provide map function. rpcUtil supports internet domain
42 * Revision 1.12 1994/02/10 23:08:21 hollings
43 * Fixed list.h ++ function to work when a hash table has an element at
44 * slot zero in the table.
46 * Removed unused fields in hist class.
48 * Revision 1.11 1994/02/09 22:37:09 hollings
49 * Added print routines to list and hash table.
51 * Revision 1.10 1994/02/08 00:30:32 hollings
52 * Make libutil more compatable with ATT CC.
54 * Revision 1.9 1994/02/03 23:30:43 hollings
55 * changed listHash to a macro to work with g++ 2.5.2.
57 * Revision 1.8 1994/01/25 20:49:40 hollings
58 * First real version of utility library.
60 * Revision 1.7 1994/01/19 20:46:17 hollings
61 * guardef defn of true/false.
63 * Revision 1.6 1993/12/15 21:06:54 hollings
64 * removed destructors. Our current list semantics don't support auto
65 * destruction of list comonents since list elements can be shared between
68 * Revision 1.5 1993/12/13 20:11:14 hollings
69 * added destructor for List class.
71 * Revision 1.4 1993/10/19 15:31:52 hollings
72 * assorted small fixes.
74 * Revision 1.3 1993/08/02 22:46:37 hollings
75 * added remove which was missing.
77 * Revision 1.2 1993/07/01 17:02:36 hollings
80 * Revision 1.1 1993/05/07 20:21:15 hollings
83 * Revision 1.1 1993/03/19 22:51:05 hollings
102 #define ListHash(ptr, size) (((unsigned int)(ptr) % (unsigned int)(size)))
104 template <class Type> class List;
105 template <class Type> class StringList;
106 template <class Type> class HTable;
108 template <class Type> class ListItem {
109 friend class List<Type>;
110 friend class StringList<Type>;
114 ListItem<Type> *next;
117 template <class Type> class List {
119 List() { head = NULL; }
120 int empty() { return (head == NULL);}
121 friend ostream &operator<<(ostream &os, List<Type> &data) {
123 for (curr= data; *curr; ++curr) {
128 void add(Type data, void *key);
130 Boolean addUnique(Type data);
131 Boolean addUnique(Type data, void *key) {
142 Type find(void *key);
143 Boolean remove(void *key);
147 ListItem<Type> *curr;
149 for (curr=head,c=0; curr; curr=curr->next) c++;
158 void operator +=(List<Type> mergee) {
159 ListItem<Type> *curr;
161 for (curr=mergee.head; curr; curr=curr->next) {
162 addUnique(curr->data, curr->key);
165 // postfix - the beauty of c++
166 Type operator ++(int i) {
167 Type ret = (Type) NULL;
176 Type ret = (Type) NULL;
183 void map (void (*map_function)(const Type item)) {
184 const ListItem<Type> *temp_ptr = 0;
186 if (!map_function) return;
187 for (temp_ptr = head; temp_ptr && temp_ptr->data; temp_ptr = temp_ptr->next)
188 map_function (temp_ptr->data);
191 ListItem<Type> *head;
194 template <class Type> void List<Type>::add(Type data, void *key)
198 ni = new(ListItem<Type>);
206 template <class Type> void List<Type>::add(Type data)
208 add(data, (void *) data);
211 template <class Type> Boolean List<Type>::addUnique(Type data)
213 return(addUnique(data, (void *) data));
216 template <class Type> void List<Type>::removeAll()
218 ListItem<Type> *curr, *nx;
229 template <class Type> Boolean List<Type>::remove(void *key)
232 ListItem<Type> *curr;
234 for (curr=head, lag = NULL; curr; curr=curr->next) {
235 if (curr->key == key) {
243 lag->next = curr->next;
254 template <class Type> Type List<Type>::find(void *data)
256 ListItem<Type> *curr;
258 for (curr=head; curr; curr=curr->next) {
259 if (curr->key == data) {
266 template <class Type> class HTable {
269 // placing function def here makes gcc happy
270 friend ostream &operator<<(ostream &os,
271 HTable<Type> &data) {
275 for (i=0; i < data.tableSize; i++) {
277 os << *data.table[i];
282 HTable(Type data) { (void) HTable(); add(data, (void *) data); }
283 void add(Type data, void *key);
285 Boolean addUnique(Type data, void *key) {
296 Type find(void *key);
297 Boolean remove(void *key);
298 HTable<Type> operator =(HTable<Type> arg) {
300 tableSize = arg.tableSize;
302 // find the first item.
311 Type operator ++(int i) {
316 if (curr) return(curr);
317 for (currHid++; currHid < tableSize; currHid++) {
318 if (table[currHid]) {
319 currList = *table[currHid];
321 if (curr) return(curr);
332 if (curr) return(curr);
333 for (currHid++; currHid < tableSize; currHid++) {
334 if (table[currHid]) {
335 currList = *table[currHid];
337 if (curr) return(curr);
346 for (i=0; i < tableSize; i++) {
348 total += table[i]->count();
362 template <class Type> HTable<Type>::HTable()
369 template <class Type> Type HTable<Type>::find(void *key)
373 if (!tableSize) return(NULL);
374 hid = ListHash(key, tableSize);
375 if (hid <0 || hid > tableSize) abort();
379 return(table[hid]->find(key));
383 template <class Type> void HTable<Type>::add(Type data, void *key)
389 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
391 hid = ListHash(key, tableSize);
392 if (hid <0 || hid > tableSize) abort();
394 table[hid] = new(List<Type>);
396 table[hid]->add(data, key);
400 template <class Type> Boolean HTable<Type>::remove(void *key)
404 hid = ListHash(key, tableSize);
405 if (hid <0 || hid > tableSize) abort();
407 return(table[hid]->remove(key));
412 template <class Type> class StringList: public List<Type> {
414 Type find(void *key);
417 template <class Type> Type StringList<Type>::find(void *data)
419 ListItem<Type> *curr;
421 for (curr=head; curr; curr=curr->next) {
422 if (!strcmp((char *) curr->key, (char *) data)) {