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