2 // GROUPS passed templates nested-classes
3 // Special g++ Options:
5 // The SetLS template test
7 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
11 #pragma implementation "ListS.h"
12 #pragma implementation "SetLS.h"
17 # 1 "../../templates/SetLS.h" 1
23 // A Set Template - implemented with an ListS
25 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
41 # 37 "../../templates/SetLS.h"
44 # 1 "../../templates/ListS.h" 1
50 // A List Template - providing a singly linked capability
52 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
66 # 1 "/projects/gnu-cygnus/gnu-cygnus-14/mips/lib/gcc-lib/decstation/cygnus-reno-1/g++-include/bool.h" 1 3
67 // Defining XTRUE and XFALSE is usually a Bad Idea,
68 // because you will probably be inconsistent with anyone
69 // else who had the same clever idea.
70 // Therefore: DON'T USE THIS FILE.
80 # 23 "../../templates/ListS.h" 2
82 # 37 "../../templates/ListS.h"
86 // g++ reno-1 is not yet capable of creating templates with nested
87 // classes which instantiate the template arguments.
93 ListS_link(const T& i, ListS_link<T> *n = 0): item(i), next(n)
99 // For now, errors are raised by ::abort() because exceptions
100 // are not well implemented in cxx or at all in CC 3.0.1
106 ListS(const ListS<T>&);
109 void operator=(const ListS<T>&);
111 unsigned length() const
114 void prepend(const T& item);
115 void append(const T& item);
118 const T& head() const
120 return head_link->item; }
123 return head_link->item; }
124 void head(T& fill) const
126 fill = head_link->item; }
128 { remove_head_filling(0); }
129 void remove_head(T& fill)
130 { remove_head_filling(&fill); }
132 const T& tail() const
134 return tail_link->item; }
137 return tail_link->item; }
138 void tail(T& fill) const
140 fill = tail_link->item; }
144 Vix(): owner(0), index(0)
147 // These are friend functions so that v == x is the same as x == v
148 friend int operator==(void *v, const Vix& x)
149 { return v == x.index; }
150 friend int operator==(const Vix& x, void *v)
151 { return v == x.index; }
152 friend int operator!=(void *v, const Vix& x)
153 { return v != x.index; }
154 friend int operator!=(const Vix& x, void *v)
155 { return v != x.index; }
156 friend int operator==(const Vix& x1, const Vix& x2)
157 { return x1.owner == x2.owner && x1.index == x2.index; }
158 friend int operator!=(const Vix& x1, const Vix& x2)
159 { return x1.owner != x2.owner || x1.index != x2.index; }
161 friend class ListS<T>;
164 Vix(const ListS<T> *o, ListS_link<T> *i): owner(o), index(i)
171 const ListS<T> *owner;
173 ListS_link<T> *index;
180 { return Vix(this, head_link); }
181 void next(Vix& x) const
184 x.index = x.index->next; }
185 T& operator()(const Vix& x)
187 return x.index->item; }
188 const T& operator()(const Vix& x) const
190 return x.index->item; }
192 # 154 "../../templates/ListS.h"
197 ListS_link<T> *head_link; // 0 for a zero-length list
198 ListS_link<T> *tail_link; // 0 for a zero-length list
205 // fill may be 0 (then don't fill)
206 void remove_head_filling(T *fill);
208 void ensure_1() const
209 { if (0 == head_link)
211 void check(const Vix& x) const
212 { if (this != x.owner)
226 ListS<T>::ListS(const ListS<T>& other):
231 for (Vix x=other.first(); 0 != x; other.next(x))
243 ListS<T>::operator=(const ListS<T>& other)
246 for (Vix x=other.first(); 0 != x; other.next(x))
252 ListS<T>::prepend(const T& item)
255 head_link = new ListS_link<T>(item, head_link);
260 tail_link = head_link;
266 ListS<T>::append(const T& item)
269 ListS_link<T> *new_link = new ListS_link<T>(item);
273 if (0 == tail_link) {
274 head_link = new_link;
275 tail_link = new_link;
277 tail_link->next = new_link;
278 tail_link = tail_link->next;
288 ListS_link<T> *next, *l;
292 for (l=head_link; 0 != l; l=next) {
304 ListS<T>::remove_head_filling(T* fill)
305 // fill may be 0 in which case don't assign into it
309 ListS_link<T> *ohead = head_link;
315 head_link = ohead->next;
323 # 40 "../../templates/SetLS.h" 2
326 # 62 "../../templates/SetLS.h"
333 void add(const T& item);
334 // There is no remove(const T& item) for this set
335 bool contains(const T& item) const;
337 unsigned length() const
338 { return list.length(); }
345 Vix(): owner(0), vix()
348 // These are friend functions so that v == x is the same as x == v
349 friend int operator==(void *v, const Vix& x)
350 { return v == x.vix; }
351 friend int operator==(const Vix& x, void *v)
352 { return v == x.vix; }
353 friend int operator!=(void *v, const Vix& x)
354 { return v != x.vix; }
355 friend int operator!=(const Vix& x, void *v)
356 { return v != x.vix; }
357 friend int operator==(const Vix& x1, const Vix& x2)
358 { return x1.owner == x2.owner && x1.vix == x2.vix; }
359 friend int operator!=(const Vix& x1, const Vix& x2)
360 { return x1.owner != x2.owner || x1.vix != x2.vix; }
362 friend class SetLS<T>;
364 Vix(const SetLS<T> *o, const ListS<T>::Vix& x): owner(o), vix(x)
367 const SetLS<T> *owner;
373 { return Vix(this, list.first()); }
374 void next(Vix& x) const
377 const T& operator()(const Vix& x) const
379 return list(x.vix); }
380 // There is item no remove(const Vix&) for this set
385 void check(const Vix& x) const
386 { if (this != x.owner)
402 SetLS<T>::add(const T& item)
404 if ( ! contains(item) ) {
415 SetLS<T>::contains(const T& item) const
417 for (Vix x=first(); 0 != x; next(x)) {
418 if (operator()(x) == item)
429 // In (most versions of) g++ 2.X, this use of typedefs has the effect
430 // of causing the instantiation of the templates, thereby testing the
437 test(int v): value(v)
440 void print(ostream& out) const
443 friend int operator==(const test& a, const test& b);
449 operator==(const test& a, const test& b)
451 return a.value == b.value;
455 operator<<(ostream& o, const test& t)
461 typedef SetLS<test> SLS;
464 operator<<(ostream& o, const SLS& s)
466 o << "set of " << s.length() << " = {";
470 for (first=XTRUE, x=s.first(); 0 != x; s.next(x), first=XFALSE) {
486 const unsigned SIZE = 20;
490 // SetLS(const SetLS<T>&)
494 // Fill sls with some interesting values
495 for (unsigned i=0; i<SIZE; i++) {
504 // void operator=(const SetLS<T>&);
509 // bool contains(const T& item) const
511 for (unsigned i=0; i<SIZE; i++) {
514 int contains = sls.contains(t);
521 if (sls.length() != 0)
528 // void next(Vix& x) const
529 // T& operator()(const Vix& x)
530 // const T& operator()(const Vix& x) const
533 for (cx=csls.first(); 0 != cx; sls.next(cx)) {
534 if ( ! sls.contains(csls(cx)) )
538 cout << "gsls:\t" << gsls << '\n';
539 cout << "gcsls:\t" << gcsls << '\n';
540 cout << "sls:\t" << sls << '\n';
541 cout << "csls:\t" << csls << '\n';
544 // Dummy function so it'll run
547 cout << "PASS" << endl;
550 template class ListS<test>;