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