]> www.ginac.de Git - ginac.git/blob - ginac/container.h
Add trivial shortcuts in expair plumbing of class add.
[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-2015 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(STLT && v)
156         {
157                 setflag(get_default_flags());
158                 this->seq.swap(v);
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                 for (auto & i : this->seq) {
388                         n.add_ex("seq", i);
389                 }
390         }
391
392 protected:
393         ex conjugate() const
394         {
395                 STLT *newcont = nullptr;
396                 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
397                         if (newcont) {
398                                 newcont->push_back(i->conjugate());
399                                 continue;
400                         }
401                         ex x = i->conjugate();
402                         if (are_ex_trivially_equal(x, *i)) {
403                                 continue;
404                         }
405                         newcont = new STLT;
406                         this->reserve(*newcont, this->seq.size());
407                         for (const_iterator j=this->seq.begin(); j!=i; ++j) {
408                                 newcont->push_back(*j);
409                         }
410                         newcont->push_back(x);
411                 }
412                 if (newcont) {
413                         ex result = thiscontainer(*newcont);
414                         delete newcont;
415                         return result;
416                 }
417                 return *this;
418         }
419
420         ex real_part() const
421         {
422                 STLT cont;
423                 this->reserve(cont, nops());
424                 const_iterator b = begin();
425                 const_iterator e = end();
426                 for(const_iterator i=b; i!=e; ++i)
427                         cont.push_back(i->real_part());
428                 return thiscontainer(cont);
429         }
430
431         ex imag_part() const
432         {
433                 STLT cont;
434                 this->reserve(cont, nops());
435                 const_iterator b = begin();
436                 const_iterator e = end();
437                 for(const_iterator i=b; i!=e; ++i)
438                         cont.push_back(i->imag_part());
439                 return thiscontainer(cont);
440         }
441
442         bool is_equal_same_type(const basic & other) const;
443
444         // new virtual functions which can be overridden by derived classes
445 protected:
446         /** Similar to duplicate(), but with a preset sequence. Must be
447          *  overridden by derived classes. */
448         virtual ex thiscontainer(const STLT & v) const { return container(v); }
449
450         /** Similar to duplicate(), but with a preset sequence (which gets
451          *  pilfered). Must be overridden by derived classes. */
452         virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
453
454         virtual void printseq(const print_context & c, char openbracket, char delim,
455                               char closebracket, unsigned this_precedence,
456                               unsigned upper_precedence = 0) const;
457
458         // non-virtual functions in this class
459 private:
460         void sort_(std::random_access_iterator_tag)
461         {
462                 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
463         }
464
465         void sort_(std::input_iterator_tag)
466         {
467                 this->seq.sort(ex_is_less());
468         }
469
470         void unique_()
471         {
472                 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
473                 this->seq.erase(p, this->seq.end());
474         }
475
476 public:
477         container & prepend(const ex & b);
478         container & append(const ex & b);
479         container & remove_first();
480         container & remove_last();
481         container & remove_all();
482         container & sort();
483         container & unique();
484
485         const_iterator begin() const {return this->seq.begin();}
486         const_iterator end() const {return this->seq.end();}
487         const_reverse_iterator rbegin() const {return this->seq.rbegin();}
488         const_reverse_iterator rend() const {return this->seq.rend();}
489
490 protected:
491         void do_print(const print_context & c, unsigned level) const;
492         void do_print_tree(const print_tree & c, unsigned level) const;
493         void do_print_python(const print_python & c, unsigned level) const;
494         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
495         STLT evalchildren(int level) const;
496         STLT subschildren(const exmap & m, unsigned options = 0) const;
497 };
498
499 /** Default constructor */
500 template <template <class T, class = std::allocator<T> > class C>
501 container<C>::container()
502 {
503         setflag(get_default_flags());
504 }
505
506
507 template <template <class T, class = std::allocator<T> > class C>
508 void container<C>::do_print(const print_context & c, unsigned level) const
509 {
510         // always print brackets around seq, ignore upper_precedence
511         printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
512 }
513
514 template <template <class T, class = std::allocator<T> > class C>
515 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
516 {
517         c.s << std::string(level, ' ') << class_name() << " @" << this
518             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
519             << ", nops=" << nops()
520             << std::endl;
521         const_iterator i = this->seq.begin(), end = this->seq.end();
522         while (i != end) {
523                 i->print(c, level + c.delta_indent);
524                 ++i;
525         }
526         c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
527 }
528
529 template <template <class T, class = std::allocator<T> > class C>
530 void container<C>::do_print_python(const print_python & c, unsigned level) const
531 {
532         printseq(c, '[', ',', ']', precedence(), precedence()+1);
533 }
534
535 template <template <class T, class = std::allocator<T> > class C>
536 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
537 {
538         c.s << class_name();
539         printseq(c, '(', ',', ')', precedence(), precedence()+1);
540 }
541
542 template <template <class T, class = std::allocator<T> > class C>
543 ex container<C>::op(size_t i) const
544 {
545         GINAC_ASSERT(i < nops());
546
547         const_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>::let_op(size_t i)
554 {
555         GINAC_ASSERT(i < nops());
556
557         ensure_if_modifiable();
558         typename STLT::iterator it = this->seq.begin();
559         advance(it, i);
560         return *it;
561 }
562
563 template <template <class T, class = std::allocator<T> > class C>
564 ex container<C>::eval(int level) const
565 {
566         if (level == 1)
567                 return hold();
568         else
569                 return thiscontainer(evalchildren(level));
570 }
571
572 template <template <class T, class = std::allocator<T> > class C>
573 ex container<C>::subs(const exmap & m, unsigned options) const
574 {
575         // After having subs'ed all children, this method subs'es one final
576         // level, but only if the intermediate result is a container! This is
577         // because if the intermediate result has eval'ed to a non-container a
578         // last level substitution would be wrong, as this example involving a
579         // function f and its inverse f^-1 shows:
580         // f(x).subs(x==f^-1(x))
581         //   -> f(f^-1(x))  [subschildren]
582         //   -> x           [eval]   /* must not subs(x==f^-1(x))! */
583         STLT subsed = subschildren(m, options);
584         if (!subsed.empty()) {
585                 ex result(thiscontainer(subsed));
586                 if (is_a<container<C> >(result))
587                         return ex_to<basic>(result).subs_one_level(m, options);
588                 else
589                         return result;
590         } else {
591                 if (is_a<container<C> >(*this))
592                         return subs_one_level(m, options);
593                 else
594                         return *this;
595         }
596 }
597
598 /** Compare two containers of the same type. */
599 template <template <class T, class = std::allocator<T> > class C>
600 int container<C>::compare_same_type(const basic & other) const
601 {
602         GINAC_ASSERT(is_a<container>(other));
603         const container & o = static_cast<const container &>(other);
604
605         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
606                        it2 = o.seq.begin(), it2end = o.seq.end();
607
608         while (it1 != it1end && it2 != it2end) {
609                 int cmpval = it1->compare(*it2);
610                 if (cmpval)
611                         return cmpval;
612                 ++it1; ++it2;
613         }
614
615         return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
616 }
617
618 template <template <class T, class = std::allocator<T> > class C>
619 bool container<C>::is_equal_same_type(const basic & other) const
620 {
621         GINAC_ASSERT(is_a<container>(other));
622         const container & o = static_cast<const container &>(other);
623
624         if (this->seq.size() != o.seq.size())
625                 return false;
626
627         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
628         while (it1 != it1end) {
629                 if (!it1->is_equal(*it2))
630                         return false;
631                 ++it1; ++it2;
632         }
633
634         return true;
635 }
636
637 /** Add element at front. */
638 template <template <class T, class = std::allocator<T> > class C>
639 container<C> & container<C>::prepend(const ex & b)
640 {
641         ensure_if_modifiable();
642         this->seq.push_front(b);
643         return *this;
644 }
645
646 /** Add element at back. */
647 template <template <class T, class = std::allocator<T> > class C>
648 container<C> & container<C>::append(const ex & b)
649 {
650         ensure_if_modifiable();
651         this->seq.push_back(b);
652         return *this;
653 }
654
655 /** Remove first element. */
656 template <template <class T, class = std::allocator<T> > class C>
657 container<C> & container<C>::remove_first()
658 {
659         ensure_if_modifiable();
660         this->seq.pop_front();
661         return *this;
662 }
663
664 /** Remove last element. */
665 template <template <class T, class = std::allocator<T> > class C>
666 container<C> & container<C>::remove_last()
667 {
668         ensure_if_modifiable();
669         this->seq.pop_back();
670         return *this;
671 }
672
673 /** Remove all elements. */
674 template <template <class T, class = std::allocator<T> > class C>
675 container<C> & container<C>::remove_all()
676 {
677         ensure_if_modifiable();
678         this->seq.clear();
679         return *this;
680 }
681
682 /** Sort elements. */
683 template <template <class T, class = std::allocator<T> > class C>
684 container<C> & container<C>::sort()
685 {
686         ensure_if_modifiable();
687         sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
688         return *this;
689 }
690
691 /** Specialization of container::unique_() for std::list. */
692 template<> inline void container<std::list>::unique_()
693 {
694         this->seq.unique(ex_is_equal());
695 }
696
697 /** Remove adjacent duplicate elements. */
698 template <template <class T, class = std::allocator<T> > class C>
699 container<C> & container<C>::unique()
700 {
701         ensure_if_modifiable();
702         unique_();
703         return *this;
704 }
705
706 /** Print sequence of contained elements. */
707 template <template <class T, class = std::allocator<T> > class C>
708 void container<C>::printseq(const print_context & c, char openbracket, char delim,
709                             char closebracket, unsigned this_precedence,
710                             unsigned upper_precedence) const
711 {
712         if (this_precedence <= upper_precedence)
713                 c.s << openbracket;
714
715         if (!this->seq.empty()) {
716                 const_iterator it = this->seq.begin(), itend = this->seq.end();
717                 --itend;
718                 while (it != itend) {
719                         it->print(c, this_precedence);
720                         c.s << delim;
721                         ++it;
722                 }
723                 it->print(c, this_precedence);
724         }
725
726         if (this_precedence <= upper_precedence)
727                 c.s << closebracket;
728 }
729
730 template <template <class T, class = std::allocator<T> > class C>
731 typename container<C>::STLT container<C>::evalchildren(int level) const
732 {
733         if (level == 1)
734                 return this->seq;
735         else if (level == -max_recursion_level)
736                 throw std::runtime_error("max recursion level reached");
737
738         STLT s;
739         this->reserve(s, this->seq.size());
740
741         --level;
742         const_iterator it = this->seq.begin(), itend = this->seq.end();
743         while (it != itend) {
744                 s.push_back(it->eval(level));
745                 ++it;
746         }
747
748         return s;
749 }
750
751 template <template <class T, class = std::allocator<T> > class C>
752 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
753 {
754         // returns an empty container if nothing had to be substituted
755         // returns a STLT with substituted elements otherwise
756
757         const_iterator cit = this->seq.begin(), end = this->seq.end();
758         while (cit != end) {
759                 const ex & subsed_ex = cit->subs(m, options);
760                 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
761
762                         // copy first part of seq which hasn't changed
763                         STLT s(this->seq.begin(), cit);
764                         this->reserve(s, this->seq.size());
765
766                         // insert changed element
767                         s.push_back(subsed_ex);
768                         ++cit;
769
770                         // copy rest
771                         while (cit != end) {
772                                 s.push_back(cit->subs(m, options));
773                                 ++cit;
774                         }
775
776                         return s;
777                 }
778
779                 ++cit;
780         }
781         
782         return STLT(); // nothing has changed
783 }
784
785 } // namespace GiNaC
786
787 #endif // ndef GINAC_CONTAINER_H