lst(0) now works as expected
[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-2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
32 #include "ex.h"
33 #include "print.h"
34 #include "archive.h"
35 #include "assertion.h"
36
37 namespace GiNaC {
38
39
40 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
41 template <template <class> 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 /** Wrapper template for making GiNaC classes out of STL containers. */
70 template <template <class> class C>
71 class container : public basic, public container_storage<C> {
72         GINAC_DECLARE_REGISTERED_CLASS(container, basic)
73
74         typedef typename container_storage<C>::STLT STLT;
75
76 public:
77         typedef typename STLT::const_iterator const_iterator;
78         typedef typename STLT::const_reverse_iterator const_reverse_iterator;
79
80 protected:
81         // helpers
82         static unsigned get_tinfo() { return TINFO_fail; }
83         static char get_open_delim() { return '('; }
84         static char get_close_delim() { return ')'; }
85
86         // constructors
87 public:
88         container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
89         {
90                 if (discardable)
91                         this->seq.swap(const_cast<STLT &>(s));
92                 else
93                         this->seq = s;
94         }
95
96         explicit container(STLT * vp) : inherited(get_tinfo())
97         {
98                 if (vp == 0) {
99                         // lst(0) ends up here
100                         this->seq.push_back(0);
101                 } else {
102                         this->seq.swap(*vp);
103                         delete vp;
104                 }
105         }
106
107         container(exvector::const_iterator b, exvector::const_iterator e)
108          : inherited(get_tinfo()), container_storage<C>(b, e) {}
109
110         explicit container(const ex & p1)
111          : inherited(get_tinfo()), container_storage<C>(1, p1) {}
112
113         container(const ex & p1, const ex & p2) : inherited(get_tinfo())
114         {
115                 reserve(this->seq, 2);
116                 this->seq.push_back(p1); this->seq.push_back(p2);
117         }
118
119         container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
120         {
121                 reserve(this->seq, 3);
122                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
123         }
124
125         container(const ex & p1, const ex & p2, const ex & p3,
126                            const ex & p4) : inherited(get_tinfo())
127         {
128                 reserve(this->seq, 4);
129                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
130                 this->seq.push_back(p4);
131         }
132
133         container(const ex & p1, const ex & p2, const ex & p3,
134                   const ex & p4, const ex & p5) : inherited(get_tinfo())
135         {
136                 reserve(this->seq, 5);
137                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
138                 this->seq.push_back(p4); this->seq.push_back(p5);
139         }
140
141         container(const ex & p1, const ex & p2, const ex & p3,
142                   const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
143         {
144                 reserve(this->seq, 6);
145                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
146                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
147         }
148
149         container(const ex & p1, const ex & p2, const ex & p3,
150                   const ex & p4, const ex & p5, const ex & p6,
151                   const ex & p7) : inherited(get_tinfo())
152         {
153                 reserve(this->seq, 7);
154                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
155                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
156                 this->seq.push_back(p7);
157         }
158
159         container(const ex & p1, const ex & p2, const ex & p3,
160                   const ex & p4, const ex & p5, const ex & p6,
161                   const ex & p7, const ex & p8) : inherited(get_tinfo())
162         {
163                 reserve(this->seq, 8);
164                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
165                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
166                 this->seq.push_back(p7); this->seq.push_back(p8);
167         }
168
169         container(const ex & p1, const ex & p2, const ex & p3,
170                   const ex & p4, const ex & p5, const ex & p6,
171                   const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
172         {
173                 reserve(this->seq, 9);
174                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
175                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
176                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
177         }
178
179         container(const ex & p1, const ex & p2, const ex & p3,
180                   const ex & p4, const ex & p5, const ex & p6,
181                   const ex & p7, const ex & p8, const ex & p9,
182                   const ex & p10) : inherited(get_tinfo())
183         {
184                 reserve(this->seq, 10);
185                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
186                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
187                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
188                 this->seq.push_back(p10);
189         }
190
191         container(const ex & p1, const ex & p2, const ex & p3,
192                   const ex & p4, const ex & p5, const ex & p6,
193                   const ex & p7, const ex & p8, const ex & p9,
194                   const ex & p10, const ex & p11) : inherited(get_tinfo())
195         {
196                 reserve(this->seq, 11);
197                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
198                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
199                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
200                 this->seq.push_back(p10); this->seq.push_back(p11);
201         }
202
203         container(const ex & p1, const ex & p2, const ex & p3,
204                   const ex & p4, const ex & p5, const ex & p6,
205                   const ex & p7, const ex & p8, const ex & p9,
206                   const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
207         {
208                 reserve(this->seq, 12);
209                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
210                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
211                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
212                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
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, const ex & p8, const ex & p9,
218                   const ex & p10, const ex & p11, const ex & p12,
219                   const ex & p13) : inherited(get_tinfo())
220         {
221                 reserve(this->seq, 13);
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); this->seq.push_back(p8); this->seq.push_back(p9);
225                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
226                 this->seq.push_back(p13);
227         }
228
229         container(const ex & p1, const ex & p2, const ex & p3,
230                   const ex & p4, const ex & p5, const ex & p6,
231                   const ex & p7, const ex & p8, const ex & p9,
232                   const ex & p10, const ex & p11, const ex & p12,
233                   const ex & p13, const ex & p14) : inherited(get_tinfo())
234         {
235                 reserve(this->seq, 14);
236                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
237                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
238                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
239                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
240                 this->seq.push_back(p13); this->seq.push_back(p14);
241         }
242
243         container(const ex & p1, const ex & p2, const ex & p3,
244                   const ex & p4, const ex & p5, const ex & p6,
245                   const ex & p7, const ex & p8, const ex & p9,
246                   const ex & p10, const ex & p11, const ex & p12,
247                   const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
248         {
249                 reserve(this->seq, 15);
250                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
251                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
252                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
253                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
254                 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
255         }
256
257         container(const ex & p1, const ex & p2, const ex & p3,
258                   const ex & p4, const ex & p5, const ex & p6,
259                   const ex & p7, const ex & p8, const ex & p9,
260                   const ex & p10, const ex & p11, const ex & p12,
261                   const ex & p13, const ex & p14, const ex & p15,
262                   const ex & p16) : inherited(get_tinfo())
263         {
264                 reserve(this->seq, 16);
265                 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
266                 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
267                 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
268                 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
269                 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
270                 this->seq.push_back(p16);
271         }
272
273         // functions overriding virtual functions from base classes
274 public:
275         void print(const print_context & c, unsigned level = 0) const;
276         bool info(unsigned inf) const { return inherited::info(inf); }
277         unsigned precedence() const { return 10; }
278         size_t nops() const { return this->seq.size(); }
279         ex op(size_t i) const;
280         ex & let_op(size_t i);
281         ex eval(int level = 0) const;
282         ex subs(const exmap & m, unsigned options = 0) const;
283
284 protected:
285         bool is_equal_same_type(const basic & other) const;
286
287         // new virtual functions which can be overridden by derived classes
288 protected:
289         /** Similar to duplicate(), but with a preset sequence. Must be
290          *  overridden by derived classes. */
291         virtual ex thiscontainer(const STLT & v) const { return container(v); }
292
293         /** Similar to duplicate(), but with a preset sequence (which gets
294          *  deleted). Must be overridden by derived classes. */
295         virtual ex thiscontainer(STLT * vp) const { return container(vp); }
296
297         virtual void printseq(const print_context & c, char openbracket, char delim,
298                               char closebracket, unsigned this_precedence,
299                               unsigned upper_precedence = 0) const;
300
301         // non-virtual functions in this class
302 private:
303         void sort_(std::random_access_iterator_tag)
304         {
305                 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
306         }
307
308         void sort_(std::input_iterator_tag)
309         {
310                 this->seq.sort(ex_is_less());
311         }
312
313         void unique_()
314         {
315                 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
316                 this->seq.erase(p, this->seq.end());
317         }
318
319 public:
320         container & prepend(const ex & b);
321         container & append(const ex & b);
322         container & remove_first();
323         container & remove_last();
324         container & remove_all();
325         container & sort();
326         container & unique();
327
328         const_iterator begin() const {return this->seq.begin();}
329         const_iterator end() const {return this->seq.end();}
330         const_reverse_iterator rbegin() const {return this->seq.rbegin();}
331         const_reverse_iterator rend() const {return this->seq.rend();}
332
333 protected:
334         STLT evalchildren(int level) const;
335         STLT *subschildren(const exmap & m, unsigned options = 0) const;
336 };
337
338 /** Default constructor */
339 template <template <class> class C>
340 container<C>::container() : inherited(get_tinfo()) {}
341
342 /** Construct object from archive_node. */
343 template <template <class> class C>
344 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
345 {
346         for (unsigned int i=0; true; i++) {
347                 ex e;
348                 if (n.find_ex("seq", e, sym_lst, i))
349                         this->seq.push_back(e);
350                 else
351                         break;
352         }
353 }
354
355 /** Unarchive the object. */
356 template <template <class> class C>
357 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
358 {
359         return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
360 }
361
362 /** Archive the object. */
363 template <template <class> class C>
364 void container<C>::archive(archive_node &n) const
365 {
366         inherited::archive(n);
367         const_iterator i = this->seq.begin(), end = this->seq.end();
368         while (i != end) {
369                 n.add_ex("seq", *i);
370                 ++i;
371         }
372 }
373
374 template <template <class> class C>
375 void container<C>::print(const print_context & c, unsigned level) const
376 {
377         if (is_a<print_tree>(c)) {
378                 c.s << std::string(level, ' ') << class_name()
379                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
380                     << ", nops=" << nops()
381                     << std::endl;
382                 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
383                 const_iterator i = this->seq.begin(), end = this->seq.end();
384                 while (i != end) {
385                         i->print(c, level + delta_indent);
386                         ++i;
387                 }
388                 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
389         } else if (is_a<print_python>(c)) {
390                 printseq(c, '[', ',', ']', precedence(), precedence()+1);
391         } else if (is_a<print_python_repr>(c)) {
392                 c.s << class_name ();
393                 printseq(c, '(', ',', ')', precedence(), precedence()+1);
394         } else {
395                 // always print brackets around seq, ignore upper_precedence
396                 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
397         }
398 }
399
400 template <template <class> class C>
401 ex container<C>::op(size_t i) const
402 {
403         GINAC_ASSERT(i < nops());
404
405         const_iterator it = this->seq.begin();
406         advance(it, i);
407         return *it;
408 }
409
410 template <template <class> class C>
411 ex & container<C>::let_op(size_t i)
412 {
413         GINAC_ASSERT(i < nops());
414
415         ensure_if_modifiable();
416         typename STLT::iterator it = this->seq.begin();
417         advance(it, i);
418         return *it;
419 }
420
421 template <template <class> class C>
422 ex container<C>::eval(int level) const
423 {
424         if (level == 1)
425                 return hold();
426         else
427                 return thiscontainer(evalchildren(level));
428 }
429
430 template <template <class> class C>
431 ex container<C>::subs(const exmap & m, unsigned options) const
432 {
433         STLT *vp = subschildren(m, options);
434         if (vp)
435                 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
436         else
437                 return subs_one_level(m, options);
438 }
439
440 /** Compare two containers of the same type. */
441 template <template <class> class C>
442 int container<C>::compare_same_type(const basic & other) const
443 {
444         GINAC_ASSERT(is_a<container>(other));
445         const container & o = static_cast<const container &>(other);
446
447         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
448                        it2 = o.seq.begin(), it2end = o.seq.end();
449
450         while (it1 != it1end && it2 != it2end) {
451                 int cmpval = it1->compare(*it2);
452                 if (cmpval)
453                         return cmpval;
454                 ++it1; ++it2;
455         }
456
457         return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
458 }
459
460 template <template <class> class C>
461 bool container<C>::is_equal_same_type(const basic & other) const
462 {
463         GINAC_ASSERT(is_a<container>(other));
464         const container & o = static_cast<const container &>(other);
465
466         if (this->seq.size() != o.seq.size())
467                 return false;
468
469         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
470         while (it1 != it1end) {
471                 if (!it1->is_equal(*it2))
472                         return false;
473                 ++it1; ++it2;
474         }
475
476         return true;
477 }
478
479 /** Add element at front. */
480 template <template <class> class C>
481 container<C> & container<C>::prepend(const ex & b)
482 {
483         ensure_if_modifiable();
484         this->seq.push_front(b);
485         return *this;
486 }
487
488 /** Add element at back. */
489 template <template <class> class C>
490 container<C> & container<C>::append(const ex & b)
491 {
492         ensure_if_modifiable();
493         this->seq.push_back(b);
494         return *this;
495 }
496
497 /** Remove first element. */
498 template <template <class> class C>
499 container<C> & container<C>::remove_first()
500 {
501         ensure_if_modifiable();
502         this->seq.pop_front();
503         return *this;
504 }
505
506 /** Remove last element. */
507 template <template <class> class C>
508 container<C> & container<C>::remove_last()
509 {
510         ensure_if_modifiable();
511         this->seq.pop_back();
512         return *this;
513 }
514
515 /** Remove all elements. */
516 template <template <class> class C>
517 container<C> & container<C>::remove_all()
518 {
519         ensure_if_modifiable();
520         this->seq.clear();
521         return *this;
522 }
523
524 /** Sort elements. */
525 template <template <class> class C>
526 container<C> & container<C>::sort()
527 {
528         ensure_if_modifiable();
529         sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
530         return *this;
531 }
532
533 /** Specialization of container::unique_() for std::list. */
534 inline void container<std::list>::unique_()
535 {
536         this->seq.unique(ex_is_equal());
537 }
538
539 /** Remove adjacent duplicate elements. */
540 template <template <class> class C>
541 container<C> & container<C>::unique()
542 {
543         ensure_if_modifiable();
544         unique_();
545         return *this;
546 }
547
548 /** Print sequence of contained elements. */
549 template <template <class> class C>
550 void container<C>::printseq(const print_context & c, char openbracket, char delim,
551                             char closebracket, unsigned this_precedence,
552                             unsigned upper_precedence) const
553 {
554         if (this_precedence <= upper_precedence)
555                 c.s << openbracket;
556
557         if (!this->seq.empty()) {
558                 const_iterator it = this->seq.begin(), itend = this->seq.end();
559                 --itend;
560                 while (it != itend) {
561                         it->print(c, this_precedence);
562                         c.s << delim;
563                         ++it;
564                 }
565                 it->print(c, this_precedence);
566         }
567
568         if (this_precedence <= upper_precedence)
569                 c.s << closebracket;
570 }
571
572 template <template <class> class C>
573 typename container<C>::STLT container<C>::evalchildren(int level) const
574 {
575         if (level == 1)
576                 return this->seq;
577         else if (level == -max_recursion_level)
578                 throw std::runtime_error("max recursion level reached");
579
580         STLT s;
581         reserve(s, this->seq.size());
582
583         --level;
584         const_iterator it = this->seq.begin(), itend = this->seq.end();
585         while (it != itend) {
586                 s.push_back(it->eval(level));
587                 ++it;
588         }
589
590         return s;
591 }
592
593 template <template <class> class C>
594 typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigned options) const
595 {
596         // returns a NULL pointer if nothing had to be substituted
597         // returns a pointer to a newly created epvector otherwise
598         // (which has to be deleted somewhere else)
599
600         const_iterator cit = this->seq.begin(), end = this->seq.end();
601         while (cit != end) {
602                 const ex & subsed_ex = cit->subs(m, options);
603                 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
604
605                         // copy first part of seq which hasn't changed
606                         STLT *s = new STLT(this->seq.begin(), cit);
607                         reserve(*s, this->seq.size());
608
609                         // insert changed element
610                         s->push_back(subsed_ex);
611                         ++cit;
612
613                         // copy rest
614                         while (cit != end) {
615                                 s->push_back(cit->subs(m, options));
616                                 ++cit;
617                         }
618
619                         return s;
620                 }
621
622                 ++cit;
623         }
624         
625         return 0; // nothing has changed
626 }
627
628 } // namespace GiNaC
629
630 #endif // ndef __GINAC_CONTAINER_H__