Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / testsuite / g++.old-deja / g++.jason / 2371.C
blobc4263d6a4fb5fcef0f3231e178ab5226e491dcd8
1 // { dg-do run  }
2 // { dg-options "" }
3 # 1 "SetLS.cc"
4 // GROUPS passed templates nested-classes
5 //
6 // The SetLS template test
7 //
8 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
9 //
11 #pragma implementation "ListS.h"
12 #pragma implementation "SetLS.h"
14 #include <cstdlib>
15 #include <iostream>
16 using namespace std;
18 # 1 "../../templates/SetLS.h" 1
19 // -*- C++ -*-
24 // A Set Template - implemented with an ListS
26 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
33 #pragma interface
39 #define XTRUE true
40 #define XFALSE false
42 # 37 "../../templates/SetLS.h"
45 # 1 "../../templates/ListS.h" 1
46 // -*- C++ -*-
51 // A List Template - providing a singly linked capability
53 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
60 #pragma interface
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.
89 template<class T>
90 struct ListS_link {
91     T item;
92     ListS_link<T> *next;
94     ListS_link(const T& i, ListS_link<T> *n = 0): item(i), next(n)
95         { }
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
103 template<class T>
104 class ListS {
105 public:
106     ListS();
107     ListS(const ListS<T>&);
108     ~ListS();
110     void operator=(const ListS<T>&);
111     
112     unsigned length() const
113         { return count; }
114     
115     void prepend(const T& item);
116     void append(const T& item);
117     void clear();
119     const T& head() const
120         { ensure_1();
121           return head_link->item; }
122     T& head()
123         { ensure_1();
124           return head_link->item; }
125     void head(T& fill) const
126         { ensure_1();
127           fill = head_link->item; }
128     void remove_head()
129         { remove_head_filling(0); }
130     void remove_head(T& fill)
131         { remove_head_filling(&fill); }
133     const T& tail() const
134         { ensure_1();
135           return tail_link->item; }
136     T& tail()
137         { ensure_1();
138           return tail_link->item; }
139     void tail(T& fill) const
140         { ensure_1();
141           fill = tail_link->item; }
143     class Vix {
144     public:
145         Vix(): owner(0), index(0)
146             { }
147         
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; }
161     private:
162         friend class ListS<T>;
163         
165         Vix(const ListS<T> *o, ListS_link<T> *i): owner(o), index(i)
166             { }
171         
172         const ListS<T> *owner;
174         ListS_link<T> *index;
178     };
179     
180     Vix first() const
181         { return Vix(this, head_link); }
182     void next(Vix& x) const
183         { check(x);
184           if (x.index != 0)
185               x.index = x.index->next; }
186     T& operator()(const Vix& x)
187         { check(x);
188           return x.index->item; }
189     const T& operator()(const Vix& x) const
190         { check(x);
191           return x.index->item; }
192 protected:
193 # 154 "../../templates/ListS.h"
196     unsigned count;
198     ListS_link<T> *head_link;   // 0 for a zero-length list
199     ListS_link<T> *tail_link;   // 0 for a zero-length list
205 private:
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)
211               ::abort(); }
212     void check(const Vix& x) const
213         { if (this != x.owner)
214               ::abort();
215           if (0 == x.index)
216               ::abort(); }
219 template<class T>
220 ListS<T>::ListS():
221 count(0),
222 head_link(0),
223 tail_link(0)
224 { }
226 template<class T>
227 ListS<T>::ListS(const ListS<T>& other):
228 count(0),
229 head_link(0),
230 tail_link(0)
232     for (Vix x=other.first(); 0 != x; other.next(x))
233         append(other(x));
236 template<class T>
237 ListS<T>::~ListS()
239     clear();
242 template<class T>
243 void
244 ListS<T>::operator=(const ListS<T>& other)
246     clear();
247     for (Vix x=other.first(); 0 != x; other.next(x))
248         append(other(x));
251 template<class T>
252 void
253 ListS<T>::prepend(const T& item)
256     head_link = new ListS_link<T>(item, head_link);
260     if (0 == tail_link)
261         tail_link = head_link;
262     count++;
265 template<class T>
266 void
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;
277     } else {
278         tail_link->next = new_link;
279         tail_link = tail_link->next;
280     }
281     count++;
284 template<class T>
285 void
286 ListS<T>::clear()
289     ListS_link<T> *next, *l;
293     for (l=head_link; 0 != l; l=next) {
294         next = l->next;
295         delete l;
296     }
298     count = 0;
299     head_link = 0;
300     tail_link = 0;
303 template<class T>
304 void
305 ListS<T>::remove_head_filling(T* fill)
306 // fill may be 0 in which case don't assign into it
308     ensure_1();
310     ListS_link<T> *ohead = head_link;
314     if (0 != fill)
315         *fill = ohead->item;
316     head_link = ohead->next;
317     if (0 == head_link)
318         tail_link = 0;
319     count--;
320     delete ohead;
324 # 40 "../../templates/SetLS.h" 2
327 # 62 "../../templates/SetLS.h"
329 template<class T>
330 class SetLS {
331 public:
332     SetLS();
333     
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(); }
341     void clear()
342         { list.clear(); }
344     class Vix {
345     public:
346         Vix(): owner(0), vix()
347             { }
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; }
362     private:
363         friend class SetLS<T>;
365         Vix(const SetLS<T> *o, const typename ListS<T>::Vix& x): owner(o), vix(x)
366             { }
368         const SetLS<T> *owner;
369         typename ListS<T>::Vix vix;
370     };
371     friend class Vix;
372     
373     Vix first() const
374         { return Vix(this, list.first()); }
375     void next(Vix& x) const
376         { check(x);
377           list.next(x.vix); }
378     const T& operator()(const Vix& x) const
379         { check(x);
380           return list(x.vix); }
381     // There is item no remove(const Vix&) for this set
382 protected:
383     ListS<T> list;
385 private:
386     void check(const Vix& x) const
387         { if (this != x.owner)
388               ::abort(); }
392 template<class T>
393 SetLS<T>::SetLS():
397 list()
399 { }
401 template<class T>
402 void
403 SetLS<T>::add(const T& item)
405     if ( ! contains(item) ) {
409         list.append(item);
411     }
414 template<class T>
415 bool
416 SetLS<T>::contains(const T& item) const
418     for (Vix x=first(); 0 != x; next(x)) {
419         if (operator()(x) == item)
420             return XTRUE;
421     }
422     return XFALSE;
426 # 17 "SetLS.cc" 2
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
432 // templates
434 class test {
435 public:
436     test(): value(0)
437         { }
438     test(int v): value(v)
439         { }
441     void print(ostream& out) const
442         { out << value; }
444     friend int operator==(const test& a, const test& b);
445 private:
446     int value;
450 operator==(const test& a, const test& b)
452     return a.value == b.value;
455 ostream&
456 operator<<(ostream& o, const test& t)
458     t.print(o);
459     return o;
462 typedef SetLS<test> SLS;
464 static ostream&
465 operator<<(ostream& o, const SLS& s)
467     o << "set of " << s.length() << " = {";
469     bool first;
470     SetLS<test>::Vix x;
471     for (first=XTRUE, x=s.first(); 0 != x; s.next(x), first=XFALSE) {
472         if ( ! first )
473             o << ',';
474         o << ' ';
475         s(x).print(o);
476     }
477     o << '}';
479     return o;
482 SLS gsls;
483 const SLS gcsls;
485 void foo()
487     const unsigned SIZE = 20;
489     //
490     // SetLS()
491     // SetLS(const SetLS<T>&)
492     // 
493     SLS sls;
494     {
495         // Fill sls with some interesting values
496         for (unsigned i=0; i<SIZE; i++) {
497             test t = i;
498             sls.add(t);
499         }
500     }
502     const SLS csls(sls);
504     //
505     // void operator=(const SetLS<T>&);
506     //
507     sls = csls;
509     //
510     // bool contains(const T& item) const
511     //
512     for (unsigned i=0; i<SIZE; i++) {
513         test t = i;
515         int contains = sls.contains(t);
516     }
518     //
519     // void clear()
520     //
521     sls.clear();
522     if (sls.length() != 0)
523         ::abort();
525     sls = csls;
527     //
528     // Vix first() const
529     // void next(Vix& x) const
530     // T& operator()(const Vix& x)
531     // const T& operator()(const Vix& x) const
532     //
533     SetLS<test>::Vix cx;
534     for (cx=csls.first(); 0 != cx; sls.next(cx)) {
535         if ( ! sls.contains(csls(cx)) )
536             ::abort();
537     }
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
546 int main()
548   cout << "PASS" << endl;
551 template class ListS<test>;