2 * Copyright 1993 Jeff Hollingsworth. All rights reserved.
10 * Revision 1.16 1994/05/30 19:37:39 hollings
11 * added pragma for external g++ functions.
13 * Revision 1.15 1994/03/11 21:01:23 hollings
14 * Changed Boolean from int to char to match X11 convention.
16 * Revision 1.14 1994/02/25 00:25:57 hollings
17 * added tunable constants.
19 * Revision 1.13 1994/02/24 07:05:28 markc
20 * Man page for librpcUtil.a
21 * Extended list class to provide map function. rpcUtil supports internet domain
24 * Revision 1.12 1994/02/10 23:08:21 hollings
25 * Fixed list.h ++ function to work when a hash table has an element at
26 * slot zero in the table.
28 * Removed unused fields in hist class.
30 * Revision 1.11 1994/02/09 22:37:09 hollings
31 * Added print routines to list and hash table.
33 * Revision 1.10 1994/02/08 00:30:32 hollings
34 * Make libutil more compatable with ATT CC.
36 * Revision 1.9 1994/02/03 23:30:43 hollings
37 * changed listHash to a macro to work with g++ 2.5.2.
39 * Revision 1.8 1994/01/25 20:49:40 hollings
40 * First real version of utility library.
42 * Revision 1.7 1994/01/19 20:46:17 hollings
43 * guardef defn of true/false.
45 * Revision 1.6 1993/12/15 21:06:54 hollings
46 * removed destructors. Our current list semantics don't support auto
47 * destruction of list comonents since list elements can be shared between
50 * Revision 1.5 1993/12/13 20:11:14 hollings
51 * added destructor for List class.
53 * Revision 1.4 1993/10/19 15:31:52 hollings
54 * assorted small fixes.
56 * Revision 1.3 1993/08/02 22:46:37 hollings
57 * added remove which was missing.
59 * Revision 1.2 1993/07/01 17:02:36 hollings
62 * Revision 1.1 1993/05/07 20:21:15 hollings
65 * Revision 1.1 1993/03/19 22:51:05 hollings
85 #define ListHash(ptr, size) (((int)(ptr) % (int)(size)))
87 template <class Type> class List;
88 template <class Type> class StringList;
90 template <class Type> class ListItem {
91 friend class List<Type>;
92 friend class StringList<Type>;
99 template <class Type> class List {
101 List() { head = NULL; current = NULL; }
102 int empty() { return (head == NULL);}
103 friend ostream &operator<<(ostream&, List<Type>&);
105 void add(Type data, void *key);
107 Boolean addUnique(Type data);
108 Boolean addUnique(Type data, void *key) {
119 Type find(void *key);
120 Boolean remove(void *key);
123 ListItem<Type> *curr;
125 for (curr=head,c=0; curr; curr=curr->next) c++;
134 void operator +=(List<Type> mergee) {
135 ListItem<Type> *curr;
137 for (curr=mergee.head; curr; curr=curr->next) {
138 add(curr->data, curr->key);
142 Type ret = (Type) NULL;
149 void map (void (*map_function)(const Type item)) {
150 const ListItem<Type> *temp_ptr = 0;
152 if (!map_function) return;
153 for (temp_ptr = head; temp_ptr && temp_ptr->data; temp_ptr = temp_ptr->next)
154 map_function (temp_ptr->data);
156 void setCurrent() { current = head;}
157 const Type getCurrent() {
158 if (current) return ( (const Type) current->data);
165 void advanceCurrent() { if (current) current = current->next;}
167 ListItem<Type> *head;
168 ListItem<Type> *current;
171 template <class Type> ostream &operator<<(ostream &os, List<Type> &data)
175 for (curr= data; *curr; curr++) {
182 // Warning this function should be replaced by the stream operator above, but
183 // g++ is broken and won't create it.
185 template <class Type> void List<Type>::print()
187 ListItem<Type> *curr;
189 for (curr=head; curr; curr=curr->next) {
190 cout << (curr->data) << endl;
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> Boolean List<Type>::remove(void *key)
219 ListItem<Type> *curr;
221 for (curr=head, lag = NULL; curr; curr=curr->next) {
222 if (curr->key == key) {
230 lag->next = curr->next;
234 // if the 'current' pointer is this element, advance it
244 template <class Type> Type List<Type>::find(void *data)
246 ListItem<Type> *curr;
248 for (curr=head; curr; curr=curr->next) {
249 if (curr->key == data) {
256 template <class Type> class HTable {
259 friend ostream &operator<<(ostream&, HTable<Type>&);
261 HTable(Type data) { (void) HTable(); add(data, (void *) data); }
262 void add(Type data, void *key);
264 Boolean addUnique(Type data, void *key) {
275 Type find(void *key);
276 Boolean remove(void *key);
277 HTable<Type> operator =(HTable<Type> arg) {
279 tableSize = arg.tableSize;
281 // find the first item.
294 if (curr) return(curr);
295 for (currHid++; currHid < tableSize; currHid++) {
296 if (table[currHid]) {
297 currList = *table[currHid];
299 if (curr) return(curr);
308 for (i=0; i < tableSize; i++) {
310 total += table[i]->count();
322 template <class Type> ostream &operator<<(ostream &os, HTable<Type> &data)
327 for (i=0; i < data.tableSize; i++) {
329 os << *data.table[i];
337 // Warning this function should be replaced by the stream operator above, but
338 // g++ is broken and won't create it.
340 template <class Type> void HTable<Type>::print()
345 for (i=0; i < tableSize; i++) {
352 template <class Type> HTable<Type>::HTable()
359 template <class Type> Type HTable<Type>::find(void *key)
363 if (!tableSize) return(NULL);
364 hid = ListHash(key, tableSize);
368 return(table[hid]->find(key));
372 template <class Type> void HTable<Type>::add(Type data, void *key)
378 table = (List<Type>**) calloc(tableSize, sizeof(List<Type>*));
380 hid = ListHash(key, tableSize);
382 table[hid] = new(List<Type>);
384 table[hid]->add(data, key);
388 template <class Type> Boolean HTable<Type>::remove(void *key)
392 hid = ListHash(key, tableSize);
394 return(table[hid]->remove(key));
399 template <class Type> class StringList: public List<Type> {
401 Type find(void *key);
404 template <class Type> Type StringList<Type>::find(void *data)
406 ListItem<Type> *curr;
408 for (curr=head; curr; curr=curr->next) {
409 if (!strcmp((char *) curr->key, (char *) data)) {