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