]> www.ginac.de Git - ginac.git/blob - ginac/container.h
Synced to HEAD:
[ginac.git] / ginac / container.h
1 /** @file container.h
2  *
3  *  Wrapper template for making GiNaC classes out of STL containers. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef __GINAC_CONTAINER_H__
24 #define __GINAC_CONTAINER_H__
25
26 #include <iterator>
27 #include <stdexcept>
28 #include <algorithm>
29 #include <vector>
30 #include <list>
31 #include <memory>
32
33 #include "ex.h"
34 #include "print.h"
35 #include "archive.h"
36 #include "assertion.h"
37
38 namespace GiNaC {
39
40
41 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
42 template <template <class T, class = std::allocator<T> > class C>
43 class container_storage {
44 protected:
45         typedef C<ex> STLT;
46
47         container_storage() {}
48         container_storage(size_t n, const ex & e) : seq(n, e) {}
49
50         template <class In>
51         container_storage(In b, In e) : seq(b, e) {}
52
53         void reserve(size_t) {}
54         static void reserve(STLT &, size_t) {}
55
56         STLT seq;
57
58         // disallow destruction of container through a container_storage*
59 protected:
60         ~container_storage() {}
61 };
62
63 template <>
64 inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
65
66 template <>
67 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
68
69
70 /** Helper template to allow initialization of containers via an overloaded
71  *  comma operator (idea stolen from Blitz++). */
72 template <typename T, typename STLT>
73 class container_init {
74 public:
75         container_init(STLT & s) : stlt(s) {}
76
77         container_init<T, STLT> operator,(const T & x)
78         {
79                 stlt.push_back(x);
80                 return container_init<T, STLT>(stlt);
81         }
82
83         // The following specializations produce much tighter code than the
84         // general case above
85
86         container_init<T, STLT> operator,(int x)
87         {
88                 stlt.push_back(x);
89                 return container_init<T, STLT>(stlt);
90         }
91
92         container_init<T, STLT> operator,(unsigned int x)
93         {
94                 stlt.push_back(x);
95                 return container_init<T, STLT>(stlt);
96         }
97
98         container_init<T, STLT> operator,(long x)
99         {
100                 stlt.push_back(x);
101                 return container_init<T, STLT>(stlt);
102         }
103
104         container_init<T, STLT> operator,(unsigned long x)
105         {
106                 stlt.push_back(x);
107                 return container_init<T, STLT>(stlt);
108         }
109
110         container_init<T, STLT> operator,(double x)
111         {
112                 stlt.push_back(x);
113                 return container_init<T, STLT>(stlt);
114         }
115
116         container_init<T, STLT> operator,(const symbol & x)
117         {
118                 stlt.push_back(T(x));
119                 return container_init<T, STLT>(stlt);
120         }
121
122 private:
123         container_init();
124         STLT & stlt;
125 };
126
127 /** Wrapper template for making GiNaC classes out of STL containers. */
128 template <template <class T, class = std::allocator<T> > class C>
129 class container : public basic, public container_storage<C> {
130         GINAC_DECLARE_REGISTERED_CLASS(container, basic)
131
132 protected:
133         typedef typename container_storage<C>::STLT STLT;
134
135 public:
136         typedef typename STLT::const_iterator const_iterator;
137         typedef typename STLT::const_reverse_iterator const_reverse_iterator;
138
139 protected:
140         // helpers
141         static unsigned get_tinfo() { return TINFO_fail; }
142         static unsigned get_default_flags() { return 0; }
143         static char get_open_delim() { return '('; }
144         static char get_close_delim() { return ')'; }
145
146         // constructors
147 public:
148         container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
149         {
150                 setflag(get_default_flags());
151
152                 if (discardable)
153                         this->seq.swap(const_cast<STLT &>(s));
154                 else
155                         this->seq = s;
156         }
157
158         explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
159         {
160                 setflag(get_default_flags());
161                 this->seq.swap(*vp);
162         }
163
164         container(exvector::const_iterator b, exvector::const_iterator e)
165          : inherited(get_tinfo()), container_storage<C>(b, e)
166         {
167                 setflag(get_default_flags());
168         }
169
170         explicit container(const ex & p1)
171          : inherited(get_tinfo()), container_storage<C>(1, p1)
172         {
173                 setflag(get_default_flags());
174         }
175
176         container(const ex & p1, const ex & p2) : inherited(get_tinfo())
177         {
178                 setflag(get_default_flags());
179                 reserve(this->seq, 2);
180                 this->seq.push_back(p1); this->seq.push_back(p2);
181         }
182
183         container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
184         {
185                 setflag(get_default_flags());
186                 reserve(this->seq, 3);
187                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
188         }
189
190         container(const ex & p1, const ex & p2, const ex & p3,
191                   const ex & p4) : inherited(get_tinfo())
192         {
193                 setflag(get_default_flags());
194                 reserve(this->seq, 4);
195                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
196                 this->seq.push_back(p4);
197         }
198
199         container(const ex & p1, const ex & p2, const ex & p3,
200                   const ex & p4, const ex & p5) : inherited(get_tinfo())
201         {
202                 setflag(get_default_flags());
203                 reserve(this->seq, 5);
204                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
205                 this->seq.push_back(p4); this->seq.push_back(p5);
206         }
207
208         container(const ex & p1, const ex & p2, const ex & p3,
209                   const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
210         {
211                 setflag(get_default_flags());
212                 reserve(this->seq, 6);
213                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
214                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
215         }
216
217         container(const ex & p1, const ex & p2, const ex & p3,
218                   const ex & p4, const ex & p5, const ex & p6,
219                   const ex & p7) : inherited(get_tinfo())
220         {
221                 setflag(get_default_flags());
222                 reserve(this->seq, 7);
223                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
224                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
225                 this->seq.push_back(p7);
226         }
227
228         container(const ex & p1, const ex & p2, const ex & p3,
229                   const ex & p4, const ex & p5, const ex & p6,
230                   const ex & p7, const ex & p8) : inherited(get_tinfo())
231         {
232                 setflag(get_default_flags());
233                 reserve(this->seq, 8);
234                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
235                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
236                 this->seq.push_back(p7); this->seq.push_back(p8);
237         }
238
239         container(const ex & p1, const ex & p2, const ex & p3,
240                   const ex & p4, const ex & p5, const ex & p6,
241                   const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
242         {
243                 setflag(get_default_flags());
244                 reserve(this->seq, 9);
245                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
246                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
247                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
248         }
249
250         container(const ex & p1, const ex & p2, const ex & p3,
251                   const ex & p4, const ex & p5, const ex & p6,
252                   const ex & p7, const ex & p8, const ex & p9,
253                   const ex & p10) : inherited(get_tinfo())
254         {
255                 setflag(get_default_flags());
256                 reserve(this->seq, 10);
257                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
258                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
259                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
260                 this->seq.push_back(p10);
261         }
262
263         container(const ex & p1, const ex & p2, const ex & p3,
264                   const ex & p4, const ex & p5, const ex & p6,
265                   const ex & p7, const ex & p8, const ex & p9,
266                   const ex & p10, const ex & p11) : inherited(get_tinfo())
267         {
268                 setflag(get_default_flags());
269                 reserve(this->seq, 11);
270                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
271                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
272                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
273                 this->seq.push_back(p10); this->seq.push_back(p11);
274         }
275
276         container(const ex & p1, const ex & p2, const ex & p3,
277                   const ex & p4, const ex & p5, const ex & p6,
278                   const ex & p7, const ex & p8, const ex & p9,
279                   const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
280         {
281                 setflag(get_default_flags());
282                 reserve(this->seq, 12);
283                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
284                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
285                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
286                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
287         }
288
289         container(const ex & p1, const ex & p2, const ex & p3,
290                   const ex & p4, const ex & p5, const ex & p6,
291                   const ex & p7, const ex & p8, const ex & p9,
292                   const ex & p10, const ex & p11, const ex & p12,
293                   const ex & p13) : inherited(get_tinfo())
294         {
295                 setflag(get_default_flags());
296                 reserve(this->seq, 13);
297                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
298                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
299                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
300                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
301                 this->seq.push_back(p13);
302         }
303
304         container(const ex & p1, const ex & p2, const ex & p3,
305                   const ex & p4, const ex & p5, const ex & p6,
306                   const ex & p7, const ex & p8, const ex & p9,
307                   const ex & p10, const ex & p11, const ex & p12,
308                   const ex & p13, const ex & p14) : inherited(get_tinfo())
309         {
310                 setflag(get_default_flags());
311                 reserve(this->seq, 14);
312                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
313                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
314                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
315                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
316                 this->seq.push_back(p13); this->seq.push_back(p14);
317         }
318
319         container(const ex & p1, const ex & p2, const ex & p3,
320                   const ex & p4, const ex & p5, const ex & p6,
321                   const ex & p7, const ex & p8, const ex & p9,
322                   const ex & p10, const ex & p11, const ex & p12,
323                   const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
324         {
325                 setflag(get_default_flags());
326                 reserve(this->seq, 15);
327                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
328                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
329                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
330                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
331                 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
332         }
333
334         container(const ex & p1, const ex & p2, const ex & p3,
335                   const ex & p4, const ex & p5, const ex & p6,
336                   const ex & p7, const ex & p8, const ex & p9,
337                   const ex & p10, const ex & p11, const ex & p12,
338                   const ex & p13, const ex & p14, const ex & p15,
339                   const ex & p16) : inherited(get_tinfo())
340         {
341                 setflag(get_default_flags());
342                 reserve(this->seq, 16);
343                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
344                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
345                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
346                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
347                 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
348                 this->seq.push_back(p16);
349         }
350
351         // First step of initialization of container with a comma-separated
352         // sequence of expressions. Subsequent steps are handled by
353         // container_init<>::operator,().
354         container_init<ex, STLT> operator=(const ex & x)
355         {
356                 this->seq.push_back(x);
357                 return container_init<ex, STLT>(this->seq);
358         }
359
360         // functions overriding virtual functions from base classes
361 public:
362         bool info(unsigned inf) const { return inherited::info(inf); }
363         unsigned precedence() const { return 10; }
364         size_t nops() const { return this->seq.size(); }
365         ex op(size_t i) const;
366         ex & let_op(size_t i);
367         ex eval(int level = 0) const;
368         ex subs(const exmap & m, unsigned options = 0) const;
369
370 protected:
371         ex conjugate() const
372         {
373                 STLT *newcont = NULL;
374                 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
375                         if (newcont) {
376                                 newcont->push_back(i->conjugate());
377                                 continue;
378                         }
379                         ex x = i->conjugate();
380                         if (are_ex_trivially_equal(x, *i)) {
381                                 continue;
382                         }
383                         newcont = new STLT;
384                         reserve(*newcont, this->seq.size());
385                         for (const_iterator j=this->seq.begin(); j!=i; ++j) {
386                                 newcont->push_back(*j);
387                         }
388                         newcont->push_back(x);
389                 }
390                 if (newcont) {
391                         ex result = thiscontainer(*newcont);
392                         delete newcont;
393                         return result;
394                 }
395                 return *this;
396         }
397
398         bool is_equal_same_type(const basic & other) const;
399
400         // new virtual functions which can be overridden by derived classes
401 protected:
402         /** Similar to duplicate(), but with a preset sequence. Must be
403          *  overridden by derived classes. */
404         virtual ex thiscontainer(const STLT & v) const { return container(v); }
405
406         /** Similar to duplicate(), but with a preset sequence (which gets
407          *  deleted). Must be overridden by derived classes. */
408         virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
409
410         virtual void printseq(const print_context & c, char openbracket, char delim,
411                               char closebracket, unsigned this_precedence,
412                               unsigned upper_precedence = 0) const;
413
414         // non-virtual functions in this class
415 private:
416         void sort_(std::random_access_iterator_tag)
417         {
418                 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
419         }
420
421         void sort_(std::input_iterator_tag)
422         {
423                 this->seq.sort(ex_is_less());
424         }
425
426         void unique_()
427         {
428                 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
429                 this->seq.erase(p, this->seq.end());
430         }
431
432 public:
433         container & prepend(const ex & b);
434         container & append(const ex & b);
435         container & remove_first();
436         container & remove_last();
437         container & remove_all();
438         container & sort();
439         container & unique();
440
441         const_iterator begin() const {return this->seq.begin();}
442         const_iterator end() const {return this->seq.end();}
443         const_reverse_iterator rbegin() const {return this->seq.rbegin();}
444         const_reverse_iterator rend() const {return this->seq.rend();}
445
446 protected:
447         void do_print(const print_context & c, unsigned level) const;
448         void do_print_tree(const print_tree & c, unsigned level) const;
449         void do_print_python(const print_python & c, unsigned level) const;
450         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
451         STLT evalchildren(int level) const;
452         std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
453 };
454
455 /** Default constructor */
456 template <template <class T, class = std::allocator<T> > class C>
457 container<C>::container() : inherited(get_tinfo())
458 {
459         setflag(get_default_flags());
460 }
461
462 /** Construct object from archive_node. */
463 template <template <class T, class = std::allocator<T> > class C>
464 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
465 {
466         setflag(get_default_flags());
467
468         for (unsigned int i=0; true; i++) {
469                 ex e;
470                 if (n.find_ex("seq", e, sym_lst, i))
471                         this->seq.push_back(e);
472                 else
473                         break;
474         }
475 }
476
477 /** Unarchive the object. */
478 template <template <class T, class = std::allocator<T> > class C>
479 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
480 {
481         return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
482 }
483
484 /** Archive the object. */
485 template <template <class T, class = std::allocator<T> > class C>
486 void container<C>::archive(archive_node &n) const
487 {
488         inherited::archive(n);
489         const_iterator i = this->seq.begin(), end = this->seq.end();
490         while (i != end) {
491                 n.add_ex("seq", *i);
492                 ++i;
493         }
494 }
495
496 template <template <class T, class = std::allocator<T> > class C>
497 void container<C>::do_print(const print_context & c, unsigned level) const
498 {
499         // always print brackets around seq, ignore upper_precedence
500         printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
501 }
502
503 template <template <class T, class = std::allocator<T> > class C>
504 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
505 {
506         c.s << std::string(level, ' ') << class_name() << " @" << this
507             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
508             << ", nops=" << nops()
509             << std::endl;
510         const_iterator i = this->seq.begin(), end = this->seq.end();
511         while (i != end) {
512                 i->print(c, level + c.delta_indent);
513                 ++i;
514         }
515         c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
516 }
517
518 template <template <class T, class = std::allocator<T> > class C>
519 void container<C>::do_print_python(const print_python & c, unsigned level) const
520 {
521         printseq(c, '[', ',', ']', precedence(), precedence()+1);
522 }
523
524 template <template <class T, class = std::allocator<T> > class C>
525 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
526 {
527         c.s << class_name();
528         printseq(c, '(', ',', ')', precedence(), precedence()+1);
529 }
530
531 template <template <class T, class = std::allocator<T> > class C>
532 ex container<C>::op(size_t i) const
533 {
534         GINAC_ASSERT(i < nops());
535
536         const_iterator it = this->seq.begin();
537         advance(it, i);
538         return *it;
539 }
540
541 template <template <class T, class = std::allocator<T> > class C>
542 ex & container<C>::let_op(size_t i)
543 {
544         GINAC_ASSERT(i < nops());
545
546         ensure_if_modifiable();
547         typename STLT::iterator it = this->seq.begin();
548         advance(it, i);
549         return *it;
550 }
551
552 template <template <class T, class = std::allocator<T> > class C>
553 ex container<C>::eval(int level) const
554 {
555         if (level == 1)
556                 return hold();
557         else
558                 return thiscontainer(evalchildren(level));
559 }
560
561 template <template <class T, class = std::allocator<T> > class C>
562 ex container<C>::subs(const exmap & m, unsigned options) const
563 {
564         std::auto_ptr<STLT> vp = subschildren(m, options);
565         if (vp.get())
566                 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
567         else
568                 return subs_one_level(m, options);
569 }
570
571 /** Compare two containers of the same type. */
572 template <template <class T, class = std::allocator<T> > class C>
573 int container<C>::compare_same_type(const basic & other) const
574 {
575         GINAC_ASSERT(is_a<container>(other));
576         const container & o = static_cast<const container &>(other);
577
578         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
579                        it2 = o.seq.begin(), it2end = o.seq.end();
580
581         while (it1 != it1end && it2 != it2end) {
582                 int cmpval = it1->compare(*it2);
583                 if (cmpval)
584                         return cmpval;
585                 ++it1; ++it2;
586         }
587
588         return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
589 }
590
591 template <template <class T, class = std::allocator<T> > class C>
592 bool container<C>::is_equal_same_type(const basic & other) const
593 {
594         GINAC_ASSERT(is_a<container>(other));
595         const container & o = static_cast<const container &>(other);
596
597         if (this->seq.size() != o.seq.size())
598                 return false;
599
600         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
601         while (it1 != it1end) {
602                 if (!it1->is_equal(*it2))
603                         return false;
604                 ++it1; ++it2;
605         }
606
607         return true;
608 }
609
610 /** Add element at front. */
611 template <template <class T, class = std::allocator<T> > class C>
612 container<C> & container<C>::prepend(const ex & b)
613 {
614         ensure_if_modifiable();
615         this->seq.push_front(b);
616         return *this;
617 }
618
619 /** Add element at back. */
620 template <template <class T, class = std::allocator<T> > class C>
621 container<C> & container<C>::append(const ex & b)
622 {
623         ensure_if_modifiable();
624         this->seq.push_back(b);
625         return *this;
626 }
627
628 /** Remove first element. */
629 template <template <class T, class = std::allocator<T> > class C>
630 container<C> & container<C>::remove_first()
631 {
632         ensure_if_modifiable();
633         this->seq.pop_front();
634         return *this;
635 }
636
637 /** Remove last element. */
638 template <template <class T, class = std::allocator<T> > class C>
639 container<C> & container<C>::remove_last()
640 {
641         ensure_if_modifiable();
642         this->seq.pop_back();
643         return *this;
644 }
645
646 /** Remove all elements. */
647 template <template <class T, class = std::allocator<T> > class C>
648 container<C> & container<C>::remove_all()
649 {
650         ensure_if_modifiable();
651         this->seq.clear();
652         return *this;
653 }
654
655 /** Sort elements. */
656 template <template <class T, class = std::allocator<T> > class C>
657 container<C> & container<C>::sort()
658 {
659         ensure_if_modifiable();
660         sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
661         return *this;
662 }
663
664 /** Specialization of container::unique_() for std::list. */
665 template<> inline void container<std::list>::unique_()
666 {
667         this->seq.unique(ex_is_equal());
668 }
669
670 /** Remove adjacent duplicate elements. */
671 template <template <class T, class = std::allocator<T> > class C>
672 container<C> & container<C>::unique()
673 {
674         ensure_if_modifiable();
675         unique_();
676         return *this;
677 }
678
679 /** Print sequence of contained elements. */
680 template <template <class T, class = std::allocator<T> > class C>
681 void container<C>::printseq(const print_context & c, char openbracket, char delim,
682                             char closebracket, unsigned this_precedence,
683                             unsigned upper_precedence) const
684 {
685         if (this_precedence <= upper_precedence)
686                 c.s << openbracket;
687
688         if (!this->seq.empty()) {
689                 const_iterator it = this->seq.begin(), itend = this->seq.end();
690                 --itend;
691                 while (it != itend) {
692                         it->print(c, this_precedence);
693                         c.s << delim;
694                         ++it;
695                 }
696                 it->print(c, this_precedence);
697         }
698
699         if (this_precedence <= upper_precedence)
700                 c.s << closebracket;
701 }
702
703 template <template <class T, class = std::allocator<T> > class C>
704 typename container<C>::STLT container<C>::evalchildren(int level) const
705 {
706         if (level == 1)
707                 return this->seq;
708         else if (level == -max_recursion_level)
709                 throw std::runtime_error("max recursion level reached");
710
711         STLT s;
712         reserve(s, this->seq.size());
713
714         --level;
715         const_iterator it = this->seq.begin(), itend = this->seq.end();
716         while (it != itend) {
717                 s.push_back(it->eval(level));
718                 ++it;
719         }
720
721         return s;
722 }
723
724 template <template <class T, class = std::allocator<T> > class C>
725 std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
726 {
727         // returns a NULL pointer if nothing had to be substituted
728         // returns a pointer to a newly created STLT otherwise
729         // (and relinquishes responsibility for the STLT)
730
731         const_iterator cit = this->seq.begin(), end = this->seq.end();
732         while (cit != end) {
733                 const ex & subsed_ex = cit->subs(m, options);
734                 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
735
736                         // copy first part of seq which hasn't changed
737                         std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
738                         reserve(*s, this->seq.size());
739
740                         // insert changed element
741                         s->push_back(subsed_ex);
742                         ++cit;
743
744                         // copy rest
745                         while (cit != end) {
746                                 s->push_back(cit->subs(m, options));
747                                 ++cit;
748                         }
749
750                         return s;
751                 }
752
753                 ++cit;
754         }
755         
756         return std::auto_ptr<STLT>(0); // nothing has changed
757 }
758
759 } // namespace GiNaC
760
761 #endif // ndef __GINAC_CONTAINER_H__