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