2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.20 1994/08/17 18:22:54 markc
11 * Moved the definitions of the << operator into the class declaration to
14 * Revision 1.19 1994/07/26 20:07:42 hollings
15 * added cast to ensure hash table pointers are positive.
17 * Revision 1.18 1994/07/11 23:00:57 jcargill
18 * Fixed bug where added two lists with (+=) operator could result in
19 * duplicate key entries
21 * Revision 1.17 1994/07/07 03:20:36 markc
22 * Added removeAll function to list class.
23 * Added machineType headers to specify pvm, cm5, ...
25 * Revision 1.16 1994/05/30 19:37:39 hollings
26 * added pragma for external g++ functions.
28 * Revision 1.15 1994/03/11 21:01:23 hollings
29 * Changed Boolean from int to char to match X11 convention.
31 * Revision 1.14 1994/02/25 00:25:57 hollings
32 * added tunable constants.
34 * Revision 1.13 1994/02/24 07:05:28 markc
35 * Man page for librpcUtil.a
36 * Extended list class to provide map function. rpcUtil supports internet domain
39 * Revision 1.12 1994/02/10 23:08:21 hollings
40 * Fixed list.h ++ function to work when a hash table has an element at
41 * slot zero in the table.
43 * Removed unused fields in hist class.
45 * Revision 1.11 1994/02/09 22:37:09 hollings
46 * Added print routines to list and hash table.
48 * Revision 1.10 1994/02/08 00:30:32 hollings
49 * Make libutil more compatable with ATT CC.
51 * Revision 1.9 1994/02/03 23:30:43 hollings
52 * changed listHash to a macro to work with g++ 2.5.2.
54 * Revision 1.8 1994/01/25 20:49:40 hollings
55 * First real version of utility library.
57 * Revision 1.7 1994/01/19 20:46:17 hollings
58 * guardef defn of true/false.
60 * Revision 1.6 1993/12/15 21:06:54 hollings
61 * removed destructors. Our current list semantics don't support auto
62 * destruction of list comonents since list elements can be shared between
65 * Revision 1.5 1993/12/13 20:11:14 hollings
66 * added destructor for List class.
68 * Revision 1.4 1993/10/19 15:31:52 hollings
69 * assorted small fixes.
71 * Revision 1.3 1993/08/02 22:46:37 hollings
72 * added remove which was missing.
74 * Revision 1.2 1993/07/01 17:02:36 hollings
77 * Revision 1.1 1993/05/07 20:21:15 hollings
80 * Revision 1.1 1993/03/19 22:51:05 hollings
100 #define ListHash(ptr, size) (((unsigned int)(ptr) % (unsigned int)(size)))
102 template <class Type> class List;
103 template <class Type> class StringList;
104 template <class Type> class HTable;
106 template <class Type> class ListItem {
107 friend class List<Type>;
108 friend class StringList<Type>;
112 ListItem<Type> *next;
115 template <class Type> class List {
117 List() { head = NULL; }
118 int empty() { return (head == NULL);}
119 friend ostream &operator<<(ostream &os, List<Type> &data) {
121 for (curr= data; *curr; curr++) {
127 void add(Type data, void *key);
129 Boolean addUnique(Type data);
130 Boolean addUnique(Type data, void *key) {
141 Type find(void *key);
142 Boolean remove(void *key);
146 ListItem<Type> *curr;
148 for (curr=head,c=0; curr; curr=curr->next) c++;
157 void operator +=(List<Type> mergee) {
158 ListItem<Type> *curr;
160 for (curr=mergee.head; curr; curr=curr->next) {
161 addUnique(curr->data, curr->key);
165 Type ret = (Type) NULL;
172 void map (void (*map_function)(const Type item)) {
173 const ListItem<Type> *temp_ptr = 0;
175 if (!map_function) return;
176 for (temp_ptr = head; temp_ptr && temp_ptr->data; temp_ptr = temp_ptr->next)
177 map_function (temp_ptr->data);
180 ListItem<Type> *head;
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> void List<Type>::removeAll()
207 ListItem<Type> *curr, *nx;
218 template <class Type> Boolean List<Type>::remove(void *key)
221 ListItem<Type> *curr;
223 for (curr=head, lag = NULL; curr; curr=curr->next) {
224 if (curr->key == key) {
232 lag->next = curr->next;
243 template <class Type> Type List<Type>::find(void *data)
245 ListItem<Type> *curr;
247 for (curr=head; curr; curr=curr->next) {
248 if (curr->key == data) {
255 template <class Type> class HTable {
258 // placing function def here makes gcc happy
259 friend ostream &operator<<(ostream &os,
260 HTable<Type> &data) {
264 for (i=0; i < data.tableSize; i++) {
266 os << *data.table[i];
271 HTable(Type data) { (void) HTable(); add(data, (void *) data); }
272 void add(Type data, void *key);
274 Boolean addUnique(Type data, void *key) {
285 Type find(void *key);
286 Boolean remove(void *key);
287 HTable<Type> operator =(HTable<Type> arg) {
289 tableSize = arg.tableSize;
291 // find the first item.
304 if (curr) return(curr);
305 for (currHid++; currHid < tableSize; currHid++) {
306 if (table[currHid]) {
307 currList = *table[currHid];
309 if (curr) return(curr);
318 for (i=0; i < tableSize; i++) {
320 total += table[i]->count();
334 template <class Type> HTable<Type>::HTable()
341 template <class Type> Type HTable<Type>::find(void *key)
345 if (!tableSize) return(NULL);
346 hid = ListHash(key, tableSize);
347 if (hid <0 || hid > tableSize) abort();
351 return(table[hid]->find(key));
355 template <class Type> void HTable<Type>::add(Type data, void *key)
361 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
363 hid = ListHash(key, tableSize);
364 if (hid <0 || hid > tableSize) abort();
366 table[hid] = new(List<Type>);
368 table[hid]->add(data, key);
372 template <class Type> Boolean HTable<Type>::remove(void *key)
376 hid = ListHash(key, tableSize);
377 if (hid <0 || hid > tableSize) abort();
379 return(table[hid]->remove(key));
384 template <class Type> class StringList: public List<Type> {
386 Type find(void *key);
389 template <class Type> Type StringList<Type>::find(void *data)
391 ListItem<Type> *curr;
393 for (curr=head; curr; curr=curr->next) {
394 if (!strcmp((char *) curr->key, (char *) data)) {