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