]> www.ginac.de Git - ginac.git/blob - ginac/container.h
7092e3b06405e07d3521e1dc4ff76a06fe12aa0c
[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-2008 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef __GINAC_CONTAINER_H__
24 #define __GINAC_CONTAINER_H__
25
26 #include <iterator>
27 #include <stdexcept>
28 #include <algorithm>
29 #include <vector>
30 #include <list>
31 #include <memory>
32
33 #include "ex.h"
34 #include "print.h"
35 #include "archive.h"
36 #include "assertion.h"
37
38 namespace GiNaC {
39
40
41 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
42 template <template <class T, class = std::allocator<T> > class C>
43 class container_storage {
44 protected:
45         typedef C<ex> STLT;
46
47         container_storage() {}
48         container_storage(size_t n, const ex & e) : seq(n, e) {}
49
50         template <class In>
51         container_storage(In b, In e) : seq(b, e) {}
52
53         void reserve(size_t) {}
54         static void reserve(STLT &, size_t) {}
55
56         STLT seq;
57
58         // disallow destruction of container through a container_storage*
59 protected:
60         ~container_storage() {}
61 };
62
63 template <>
64 inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
65
66 template <>
67 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
68
69
70 /** Helper template to allow initialization of containers via an overloaded
71  *  comma operator (idea stolen from Blitz++). */
72 template <typename T, typename STLT>
73 class container_init {
74 public:
75         container_init(STLT & s) : stlt(s) {}
76
77         container_init<T, STLT> operator,(const T & x)
78         {
79                 stlt.push_back(x);
80                 return container_init<T, STLT>(stlt);
81         }
82
83         // The following specializations produce much tighter code than the
84         // general case above
85
86         container_init<T, STLT> operator,(int x)
87         {
88                 stlt.push_back(x);
89                 return container_init<T, STLT>(stlt);
90         }
91
92         container_init<T, STLT> operator,(unsigned int x)
93         {
94                 stlt.push_back(x);
95                 return container_init<T, STLT>(stlt);
96         }
97
98         container_init<T, STLT> operator,(long x)
99         {
100                 stlt.push_back(x);
101                 return container_init<T, STLT>(stlt);
102         }
103
104         container_init<T, STLT> operator,(unsigned long x)
105         {
106                 stlt.push_back(x);
107                 return container_init<T, STLT>(stlt);
108         }
109
110         container_init<T, STLT> operator,(double x)
111         {
112                 stlt.push_back(x);
113                 return container_init<T, STLT>(stlt);
114         }
115
116         container_init<T, STLT> operator,(const symbol & x)
117         {
118                 stlt.push_back(T(x));
119                 return container_init<T, STLT>(stlt);
120         }
121
122 private:
123         container_init();
124         STLT & stlt;
125 };
126
127 /** Wrapper template for making GiNaC classes out of STL containers. */
128 template <template <class T, class = std::allocator<T> > class C>
129 class container : public basic, public container_storage<C> {
130         GINAC_DECLARE_REGISTERED_CLASS(container, basic)
131
132 protected:
133         typedef typename container_storage<C>::STLT STLT;
134
135 public:
136         typedef typename STLT::const_iterator const_iterator;
137         typedef typename STLT::const_reverse_iterator const_reverse_iterator;
138
139 protected:
140         // helpers
141         static unsigned get_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, bool discardable = false)
148         {
149                 setflag(get_default_flags());
150
151                 if (discardable)
152                         this->seq.swap(const_cast<STLT &>(s));
153                 else
154                         this->seq = s;
155         }
156
157         explicit container(std::auto_ptr<STLT> vp)
158         {
159                 setflag(get_default_flags());
160                 this->seq.swap(*vp);
161         }
162
163         container(exvector::const_iterator b, exvector::const_iterator e)
164          : container_storage<C>(b, e)
165         {
166                 setflag(get_default_flags());
167         }
168
169         explicit container(const ex & p1)
170          : container_storage<C>(1, p1)
171         {
172                 setflag(get_default_flags());
173         }
174
175         container(const ex & p1, const ex & p2)
176         {
177                 setflag(get_default_flags());
178                 reserve(this->seq, 2);
179                 this->seq.push_back(p1); this->seq.push_back(p2);
180         }
181
182         container(const ex & p1, const ex & p2, const ex & p3)
183         {
184                 setflag(get_default_flags());
185                 reserve(this->seq, 3);
186                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
187         }
188
189         container(const ex & p1, const ex & p2, const ex & p3,
190                   const ex & p4)
191         {
192                 setflag(get_default_flags());
193                 reserve(this->seq, 4);
194                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
195                 this->seq.push_back(p4);
196         }
197
198         container(const ex & p1, const ex & p2, const ex & p3,
199                   const ex & p4, const ex & p5)
200         {
201                 setflag(get_default_flags());
202                 reserve(this->seq, 5);
203                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
204                 this->seq.push_back(p4); this->seq.push_back(p5);
205         }
206
207         container(const ex & p1, const ex & p2, const ex & p3,
208                   const ex & p4, const ex & p5, const ex & p6)
209         {
210                 setflag(get_default_flags());
211                 reserve(this->seq, 6);
212                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
213                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
214         }
215
216         container(const ex & p1, const ex & p2, const ex & p3,
217                   const ex & p4, const ex & p5, const ex & p6,
218                   const ex & p7)
219         {
220                 setflag(get_default_flags());
221                 reserve(this->seq, 7);
222                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
223                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
224                 this->seq.push_back(p7);
225         }
226
227         container(const ex & p1, const ex & p2, const ex & p3,
228                   const ex & p4, const ex & p5, const ex & p6,
229                   const ex & p7, const ex & p8)
230         {
231                 setflag(get_default_flags());
232                 reserve(this->seq, 8);
233                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
234                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
235                 this->seq.push_back(p7); this->seq.push_back(p8);
236         }
237
238         container(const ex & p1, const ex & p2, const ex & p3,
239                   const ex & p4, const ex & p5, const ex & p6,
240                   const ex & p7, const ex & p8, const ex & p9)
241         {
242                 setflag(get_default_flags());
243                 reserve(this->seq, 9);
244                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
245                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
246                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
247         }
248
249         container(const ex & p1, const ex & p2, const ex & p3,
250                   const ex & p4, const ex & p5, const ex & p6,
251                   const ex & p7, const ex & p8, const ex & p9,
252                   const ex & p10)
253         {
254                 setflag(get_default_flags());
255                 reserve(this->seq, 10);
256                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
257                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
258                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
259                 this->seq.push_back(p10);
260         }
261
262         container(const ex & p1, const ex & p2, const ex & p3,
263                   const ex & p4, const ex & p5, const ex & p6,
264                   const ex & p7, const ex & p8, const ex & p9,
265                   const ex & p10, const ex & p11)
266         {
267                 setflag(get_default_flags());
268                 reserve(this->seq, 11);
269                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
270                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
271                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
272                 this->seq.push_back(p10); this->seq.push_back(p11);
273         }
274
275         container(const ex & p1, const ex & p2, const ex & p3,
276                   const ex & p4, const ex & p5, const ex & p6,
277                   const ex & p7, const ex & p8, const ex & p9,
278                   const ex & p10, const ex & p11, const ex & p12)
279         {
280                 setflag(get_default_flags());
281                 reserve(this->seq, 12);
282                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
283                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
284                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
285                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
286         }
287
288         container(const ex & p1, const ex & p2, const ex & p3,
289                   const ex & p4, const ex & p5, const ex & p6,
290                   const ex & p7, const ex & p8, const ex & p9,
291                   const ex & p10, const ex & p11, const ex & p12,
292                   const ex & p13)
293         {
294                 setflag(get_default_flags());
295                 reserve(this->seq, 13);
296                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
297                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
298                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
299                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
300                 this->seq.push_back(p13);
301         }
302
303         container(const ex & p1, const ex & p2, const ex & p3,
304                   const ex & p4, const ex & p5, const ex & p6,
305                   const ex & p7, const ex & p8, const ex & p9,
306                   const ex & p10, const ex & p11, const ex & p12,
307                   const ex & p13, const ex & p14)
308         {
309                 setflag(get_default_flags());
310                 reserve(this->seq, 14);
311                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
312                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
313                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
314                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
315                 this->seq.push_back(p13); this->seq.push_back(p14);
316         }
317
318         container(const ex & p1, const ex & p2, const ex & p3,
319                   const ex & p4, const ex & p5, const ex & p6,
320                   const ex & p7, const ex & p8, const ex & p9,
321                   const ex & p10, const ex & p11, const ex & p12,
322                   const ex & p13, const ex & p14, const ex & p15)
323         {
324                 setflag(get_default_flags());
325                 reserve(this->seq, 15);
326                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
327                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
328                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
329                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
330                 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
331         }
332
333         container(const ex & p1, const ex & p2, const ex & p3,
334                   const ex & p4, const ex & p5, const ex & p6,
335                   const ex & p7, const ex & p8, const ex & p9,
336                   const ex & p10, const ex & p11, const ex & p12,
337                   const ex & p13, const ex & p14, const ex & p15,
338                   const ex & p16)
339         {
340                 setflag(get_default_flags());
341                 reserve(this->seq, 16);
342                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
343                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
344                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
345                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
346                 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
347                 this->seq.push_back(p16);
348         }
349
350         // First step of initialization of container with a comma-separated
351         // sequence of expressions. Subsequent steps are handled by
352         // container_init<>::operator,().
353         container_init<ex, STLT> operator=(const ex & x)
354         {
355                 this->seq.push_back(x);
356                 return container_init<ex, STLT>(this->seq);
357         }
358
359         // functions overriding virtual functions from base classes
360 public:
361         bool info(unsigned inf) const { return inherited::info(inf); }
362         unsigned precedence() const { return 10; }
363         size_t nops() const { return this->seq.size(); }
364         ex op(size_t i) const;
365         ex & let_op(size_t i);
366         ex eval(int level = 0) const;
367         ex subs(const exmap & m, unsigned options = 0) const;
368
369 protected:
370         ex conjugate() const
371         {
372                 STLT *newcont = NULL;
373                 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
374                         if (newcont) {
375                                 newcont->push_back(i->conjugate());
376                                 continue;
377                         }
378                         ex x = i->conjugate();
379                         if (are_ex_trivially_equal(x, *i)) {
380                                 continue;
381                         }
382                         newcont = new STLT;
383                         reserve(*newcont, this->seq.size());
384                         for (const_iterator j=this->seq.begin(); j!=i; ++j) {
385                                 newcont->push_back(*j);
386                         }
387                         newcont->push_back(x);
388                 }
389                 if (newcont) {
390                         ex result = thiscontainer(*newcont);
391                         delete newcont;
392                         return result;
393                 }
394                 return *this;
395         }
396
397         ex real_part() const
398         {
399                 STLT cont;
400                 reserve(cont, nops());
401                 const_iterator b = begin();
402                 const_iterator e = end();
403                 for(const_iterator i=b; i!=e; ++i)
404                         cont.push_back(i->real_part());
405                 return thiscontainer(cont);
406         }
407
408         ex imag_part() const
409         {
410                 STLT cont;
411                 reserve(cont, nops());
412                 const_iterator b = begin();
413                 const_iterator e = end();
414                 for(const_iterator i=b; i!=e; ++i)
415                         cont.push_back(i->imag_part());
416                 return thiscontainer(cont);
417         }
418
419         bool is_equal_same_type(const basic & other) const;
420
421         // new virtual functions which can be overridden by derived classes
422 protected:
423         /** Similar to duplicate(), but with a preset sequence. Must be
424          *  overridden by derived classes. */
425         virtual ex thiscontainer(const STLT & v) const { return container(v); }
426
427         /** Similar to duplicate(), but with a preset sequence (which gets
428          *  deleted). Must be overridden by derived classes. */
429         virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
430
431         virtual void printseq(const print_context & c, char openbracket, char delim,
432                               char closebracket, unsigned this_precedence,
433                               unsigned upper_precedence = 0) const;
434
435         // non-virtual functions in this class
436 private:
437         void sort_(std::random_access_iterator_tag)
438         {
439                 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
440         }
441
442         void sort_(std::input_iterator_tag)
443         {
444                 this->seq.sort(ex_is_less());
445         }
446
447         void unique_()
448         {
449                 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
450                 this->seq.erase(p, this->seq.end());
451         }
452
453 public:
454         container & prepend(const ex & b);
455         container & append(const ex & b);
456         container & remove_first();
457         container & remove_last();
458         container & remove_all();
459         container & sort();
460         container & unique();
461
462         const_iterator begin() const {return this->seq.begin();}
463         const_iterator end() const {return this->seq.end();}
464         const_reverse_iterator rbegin() const {return this->seq.rbegin();}
465         const_reverse_iterator rend() const {return this->seq.rend();}
466
467 protected:
468         void do_print(const print_context & c, unsigned level) const;
469         void do_print_tree(const print_tree & c, unsigned level) const;
470         void do_print_python(const print_python & c, unsigned level) const;
471         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
472         STLT evalchildren(int level) const;
473         std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
474 };
475
476 /** Default constructor */
477 template <template <class T, class = std::allocator<T> > class C>
478 container<C>::container()
479 {
480         setflag(get_default_flags());
481 }
482
483 /** Construct object from archive_node. */
484 template <template <class T, class = std::allocator<T> > class C>
485 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
486 {
487         setflag(get_default_flags());
488
489         archive_node::archive_node_cit first = n.find_first("seq");
490         archive_node::archive_node_cit last = n.find_last("seq");
491         ++last;
492         reserve(this->seq, last - first);
493         for (archive_node::archive_node_cit i=first; i<last; ++i) {
494                 ex e;
495                 n.find_ex_by_loc(i, e, sym_lst);
496                 this->seq.push_back(e);
497         }
498 }
499
500 /** Unarchive the object. */
501 template <template <class T, class = std::allocator<T> > class C>
502 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
503 {
504         return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
505 }
506
507 /** Archive the object. */
508 template <template <class T, class = std::allocator<T> > class C>
509 void container<C>::archive(archive_node &n) const
510 {
511         inherited::archive(n);
512         const_iterator i = this->seq.begin(), end = this->seq.end();
513         while (i != end) {
514                 n.add_ex("seq", *i);
515                 ++i;
516         }
517 }
518
519 template <template <class T, class = std::allocator<T> > class C>
520 void container<C>::do_print(const print_context & c, unsigned level) const
521 {
522         // always print brackets around seq, ignore upper_precedence
523         printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
524 }
525
526 template <template <class T, class = std::allocator<T> > class C>
527 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
528 {
529         c.s << std::string(level, ' ') << class_name() << " @" << this
530             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
531             << ", nops=" << nops()
532             << std::endl;
533         const_iterator i = this->seq.begin(), end = this->seq.end();
534         while (i != end) {
535                 i->print(c, level + c.delta_indent);
536                 ++i;
537         }
538         c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
539 }
540
541 template <template <class T, class = std::allocator<T> > class C>
542 void container<C>::do_print_python(const print_python & c, unsigned level) const
543 {
544         printseq(c, '[', ',', ']', precedence(), precedence()+1);
545 }
546
547 template <template <class T, class = std::allocator<T> > class C>
548 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
549 {
550         c.s << class_name();
551         printseq(c, '(', ',', ')', precedence(), precedence()+1);
552 }
553
554 template <template <class T, class = std::allocator<T> > class C>
555 ex container<C>::op(size_t i) const
556 {
557         GINAC_ASSERT(i < nops());
558
559         const_iterator it = this->seq.begin();
560         advance(it, i);
561         return *it;
562 }
563
564 template <template <class T, class = std::allocator<T> > class C>
565 ex & container<C>::let_op(size_t i)
566 {
567         GINAC_ASSERT(i < nops());
568
569         ensure_if_modifiable();
570         typename STLT::iterator it = this->seq.begin();
571         advance(it, i);
572         return *it;
573 }
574
575 template <template <class T, class = std::allocator<T> > class C>
576 ex container<C>::eval(int level) const
577 {
578         if (level == 1)
579                 return hold();
580         else
581                 return thiscontainer(evalchildren(level));
582 }
583
584 template <template <class T, class = std::allocator<T> > class C>
585 ex container<C>::subs(const exmap & m, unsigned options) const
586 {
587         std::auto_ptr<STLT> vp = subschildren(m, options);
588         if (vp.get())
589                 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
590         else
591                 return subs_one_level(m, options);
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         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 std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
749 {
750         // returns a NULL pointer if nothing had to be substituted
751         // returns a pointer to a newly created STLT otherwise
752         // (and relinquishes responsibility for the STLT)
753
754         const_iterator cit = this->seq.begin(), end = this->seq.end();
755         while (cit != end) {
756                 const ex & subsed_ex = cit->subs(m, options);
757                 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
758
759                         // copy first part of seq which hasn't changed
760                         std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
761                         reserve(*s, this->seq.size());
762
763                         // insert changed element
764                         s->push_back(subsed_ex);
765                         ++cit;
766
767                         // copy rest
768                         while (cit != end) {
769                                 s->push_back(cit->subs(m, options));
770                                 ++cit;
771                         }
772
773                         return s;
774                 }
775
776                 ++cit;
777         }
778         
779         return std::auto_ptr<STLT>(0); // nothing has changed
780 }
781
782 } // namespace GiNaC
783
784 #endif // ndef __GINAC_CONTAINER_H__