4 // GROUPS passed templates nested-classes
6 // The SetLS template test
8 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
11 #pragma implementation "ListS.h"
12 #pragma implementation "SetLS.h"
18 # 1 "../../templates/SetLS.h" 1
24 // A Set Template - implemented with an ListS
26 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
42 # 37 "../../templates/SetLS.h"
45 # 1 "../../templates/ListS.h" 1
51 // A List Template - providing a singly linked capability
53 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
67 # 1 "/projects/gnu-cygnus/gnu-cygnus-14/mips/lib/gcc-lib/decstation/cygnus-reno-1/g++-include/bool.h" 1 3
68 // Defining XTRUE and XFALSE is usually a Bad Idea,
69 // because you will probably be inconsistent with anyone
70 // else who had the same clever idea.
71 // Therefore: DON'T USE THIS FILE.
81 # 23 "../../templates/ListS.h" 2
83 # 37 "../../templates/ListS.h"
87 // g++ reno-1 is not yet capable of creating templates with nested
88 // classes which instantiate the template arguments.
94 ListS_link(const T& i, ListS_link<T> *n = 0): item(i), next(n)
100 // For now, errors are raised by ::abort() because exceptions
101 // are not well implemented in cxx or at all in CC 3.0.1
107 ListS(const ListS<T>&);
110 void operator=(const ListS<T>&);
112 unsigned length() const
115 void prepend(const T& item);
116 void append(const T& item);
119 const T& head() const
121 return head_link->item; }
124 return head_link->item; }
125 void head(T& fill) const
127 fill = head_link->item; }
129 { remove_head_filling(0); }
130 void remove_head(T& fill)
131 { remove_head_filling(&fill); }
133 const T& tail() const
135 return tail_link->item; }
138 return tail_link->item; }
139 void tail(T& fill) const
141 fill = tail_link->item; }
145 Vix(): owner(0), index(0)
148 // These are friend functions so that v == x is the same as x == v
149 friend int operator==(void *v, const Vix& x)
150 { return v == x.index; }
151 friend int operator==(const Vix& x, void *v)
152 { return v == x.index; }
153 friend int operator!=(void *v, const Vix& x)
154 { return v != x.index; }
155 friend int operator!=(const Vix& x, void *v)
156 { return v != x.index; }
157 friend int operator==(const Vix& x1, const Vix& x2)
158 { return x1.owner == x2.owner && x1.index == x2.index; }
159 friend int operator!=(const Vix& x1, const Vix& x2)
160 { return x1.owner != x2.owner || x1.index != x2.index; }
162 friend class ListS<T>;
165 Vix(const ListS<T> *o, ListS_link<T> *i): owner(o), index(i)
172 const ListS<T> *owner;
174 ListS_link<T> *index;
181 { return Vix(this, head_link); }
182 void next(Vix& x) const
185 x.index = x.index->next; }
186 T& operator()(const Vix& x)
188 return x.index->item; }
189 const T& operator()(const Vix& x) const
191 return x.index->item; }
193 # 154 "../../templates/ListS.h"
198 ListS_link<T> *head_link; // 0 for a zero-length list
199 ListS_link<T> *tail_link; // 0 for a zero-length list
206 // fill may be 0 (then don't fill)
207 void remove_head_filling(T *fill);
209 void ensure_1() const
210 { if (0 == head_link)
212 void check(const Vix& x) const
213 { if (this != x.owner)
227 ListS<T>::ListS(const ListS<T>& other):
232 for (Vix x=other.first(); 0 != x; other.next(x))
244 ListS<T>::operator=(const ListS<T>& other)
247 for (Vix x=other.first(); 0 != x; other.next(x))
253 ListS<T>::prepend(const T& item)
256 head_link = new ListS_link<T>(item, head_link);
261 tail_link = head_link;
267 ListS<T>::append(const T& item)
270 ListS_link<T> *new_link = new ListS_link<T>(item);
274 if (0 == tail_link) {
275 head_link = new_link;
276 tail_link = new_link;
278 tail_link->next = new_link;
279 tail_link = tail_link->next;
289 ListS_link<T> *next, *l;
293 for (l=head_link; 0 != l; l=next) {
305 ListS<T>::remove_head_filling(T* fill)
306 // fill may be 0 in which case don't assign into it
310 ListS_link<T> *ohead = head_link;
316 head_link = ohead->next;
324 # 40 "../../templates/SetLS.h" 2
327 # 62 "../../templates/SetLS.h"
334 void add(const T& item);
335 // There is no remove(const T& item) for this set
336 bool contains(const T& item) const;
338 unsigned length() const
339 { return list.length(); }
346 Vix(): owner(0), vix()
349 // These are friend functions so that v == x is the same as x == v
350 friend int operator==(void *v, const Vix& x)
351 { return v == x.vix; }
352 friend int operator==(const Vix& x, void *v)
353 { return v == x.vix; }
354 friend int operator!=(void *v, const Vix& x)
355 { return v != x.vix; }
356 friend int operator!=(const Vix& x, void *v)
357 { return v != x.vix; }
358 friend int operator==(const Vix& x1, const Vix& x2)
359 { return x1.owner == x2.owner && x1.vix == x2.vix; }
360 friend int operator!=(const Vix& x1, const Vix& x2)
361 { return x1.owner != x2.owner || x1.vix != x2.vix; }
363 friend class SetLS<T>;
365 Vix(const SetLS<T> *o, const typename ListS<T>::Vix& x): owner(o), vix(x)
368 const SetLS<T> *owner;
369 typename ListS<T>::Vix vix;
374 { return Vix(this, list.first()); }
375 void next(Vix& x) const
378 const T& operator()(const Vix& x) const
380 return list(x.vix); }
381 // There is item no remove(const Vix&) for this set
386 void check(const Vix& x) const
387 { if (this != x.owner)
403 SetLS<T>::add(const T& item)
405 if ( ! contains(item) ) {
416 SetLS<T>::contains(const T& item) const
418 for (Vix x=first(); 0 != x; next(x)) {
419 if (operator()(x) == item)
430 // In (most versions of) g++ 2.X, this use of typedefs has the effect
431 // of causing the instantiation of the templates, thereby testing the
438 test(int v): value(v)
441 void print(ostream& out) const
444 friend int operator==(const test& a, const test& b);
450 operator==(const test& a, const test& b)
452 return a.value == b.value;
456 operator<<(ostream& o, const test& t)
462 typedef SetLS<test> SLS;
465 operator<<(ostream& o, const SLS& s)
467 o << "set of " << s.length() << " = {";
471 for (first=XTRUE, x=s.first(); 0 != x; s.next(x), first=XFALSE) {
487 const unsigned SIZE = 20;
491 // SetLS(const SetLS<T>&)
495 // Fill sls with some interesting values
496 for (unsigned i=0; i<SIZE; i++) {
505 // void operator=(const SetLS<T>&);
510 // bool contains(const T& item) const
512 for (unsigned i=0; i<SIZE; i++) {
515 int contains = sls.contains(t);
522 if (sls.length() != 0)
529 // void next(Vix& x) const
530 // T& operator()(const Vix& x)
531 // const T& operator()(const Vix& x) const
534 for (cx=csls.first(); 0 != cx; sls.next(cx)) {
535 if ( ! sls.contains(csls(cx)) )
539 cout << "gsls:\t" << gsls << '\n';
540 cout << "gcsls:\t" << gcsls << '\n';
541 cout << "sls:\t" << sls << '\n';
542 cout << "csls:\t" << csls << '\n';
545 // Dummy function so it'll run
548 cout << "PASS" << endl;
551 template class ListS<test>;