c6a72e276d9226d76c06fe492a9879aa664248e3
[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         bool info(unsigned inf) const { return inherited::info(inf); }
276         unsigned precedence() const { return 10; }
277         size_t nops() const { return this->seq.size(); }
278         ex op(size_t i) const;
279         ex & let_op(size_t i);
280         ex eval(int level = 0) const;
281         ex subs(const exmap & m, unsigned options = 0) const;
282
283 protected:
284         bool is_equal_same_type(const basic & other) const;
285
286         // new virtual functions which can be overridden by derived classes
287 protected:
288         /** Similar to duplicate(), but with a preset sequence. Must be
289          *  overridden by derived classes. */
290         virtual ex thiscontainer(const STLT & v) const { return container(v); }
291
292         /** Similar to duplicate(), but with a preset sequence (which gets
293          *  deleted). Must be overridden by derived classes. */
294         virtual ex thiscontainer(STLT * vp) const { return container(vp); }
295
296         virtual void printseq(const print_context & c, char openbracket, char delim,
297                               char closebracket, unsigned this_precedence,
298                               unsigned upper_precedence = 0) const;
299
300         // non-virtual functions in this class
301 private:
302         void sort_(std::random_access_iterator_tag)
303         {
304                 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
305         }
306
307         void sort_(std::input_iterator_tag)
308         {
309                 this->seq.sort(ex_is_less());
310         }
311
312         void unique_()
313         {
314                 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
315                 this->seq.erase(p, this->seq.end());
316         }
317
318 public:
319         container & prepend(const ex & b);
320         container & append(const ex & b);
321         container & remove_first();
322         container & remove_last();
323         container & remove_all();
324         container & sort();
325         container & unique();
326
327         const_iterator begin() const {return this->seq.begin();}
328         const_iterator end() const {return this->seq.end();}
329         const_reverse_iterator rbegin() const {return this->seq.rbegin();}
330         const_reverse_iterator rend() const {return this->seq.rend();}
331
332 protected:
333         void do_print(const print_context & c, unsigned level) const;
334         void do_print_tree(const print_tree & c, unsigned level) const;
335         void do_print_python(const print_python & c, unsigned level) const;
336         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
337         STLT evalchildren(int level) const;
338         STLT *subschildren(const exmap & m, unsigned options = 0) const;
339 };
340
341 /** Default constructor */
342 template <template <class> class C>
343 container<C>::container() : inherited(get_tinfo()) {}
344
345 /** Construct object from archive_node. */
346 template <template <class> class C>
347 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
348 {
349         for (unsigned int i=0; true; i++) {
350                 ex e;
351                 if (n.find_ex("seq", e, sym_lst, i))
352                         this->seq.push_back(e);
353                 else
354                         break;
355         }
356 }
357
358 /** Unarchive the object. */
359 template <template <class> class C>
360 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
361 {
362         return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
363 }
364
365 /** Archive the object. */
366 template <template <class> class C>
367 void container<C>::archive(archive_node &n) const
368 {
369         inherited::archive(n);
370         const_iterator i = this->seq.begin(), end = this->seq.end();
371         while (i != end) {
372                 n.add_ex("seq", *i);
373                 ++i;
374         }
375 }
376
377 template <template <class> class C>
378 void container<C>::do_print(const print_context & c, unsigned level) const
379 {
380         // always print brackets around seq, ignore upper_precedence
381         printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
382 }
383
384 template <template <class> class C>
385 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
386 {
387         c.s << std::string(level, ' ') << class_name()
388             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
389             << ", nops=" << nops()
390             << std::endl;
391         const_iterator i = this->seq.begin(), end = this->seq.end();
392         while (i != end) {
393                 i->print(c, level + c.delta_indent);
394                 ++i;
395         }
396         c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
397 }
398
399 template <template <class> class C>
400 void container<C>::do_print_python(const print_python & c, unsigned level) const
401 {
402         printseq(c, '[', ',', ']', precedence(), precedence()+1);
403 }
404
405 template <template <class> class C>
406 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
407 {
408         c.s << class_name();
409         printseq(c, '(', ',', ')', precedence(), precedence()+1);
410 }
411
412 template <template <class> class C>
413 ex container<C>::op(size_t i) const
414 {
415         GINAC_ASSERT(i < nops());
416
417         const_iterator it = this->seq.begin();
418         advance(it, i);
419         return *it;
420 }
421
422 template <template <class> class C>
423 ex & container<C>::let_op(size_t i)
424 {
425         GINAC_ASSERT(i < nops());
426
427         ensure_if_modifiable();
428         typename STLT::iterator it = this->seq.begin();
429         advance(it, i);
430         return *it;
431 }
432
433 template <template <class> class C>
434 ex container<C>::eval(int level) const
435 {
436         if (level == 1)
437                 return hold();
438         else
439                 return thiscontainer(evalchildren(level));
440 }
441
442 template <template <class> class C>
443 ex container<C>::subs(const exmap & m, unsigned options) const
444 {
445         STLT *vp = subschildren(m, options);
446         if (vp)
447                 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
448         else
449                 return subs_one_level(m, options);
450 }
451
452 /** Compare two containers of the same type. */
453 template <template <class> class C>
454 int container<C>::compare_same_type(const basic & other) const
455 {
456         GINAC_ASSERT(is_a<container>(other));
457         const container & o = static_cast<const container &>(other);
458
459         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
460                        it2 = o.seq.begin(), it2end = o.seq.end();
461
462         while (it1 != it1end && it2 != it2end) {
463                 int cmpval = it1->compare(*it2);
464                 if (cmpval)
465                         return cmpval;
466                 ++it1; ++it2;
467         }
468
469         return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
470 }
471
472 template <template <class> class C>
473 bool container<C>::is_equal_same_type(const basic & other) const
474 {
475         GINAC_ASSERT(is_a<container>(other));
476         const container & o = static_cast<const container &>(other);
477
478         if (this->seq.size() != o.seq.size())
479                 return false;
480
481         const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
482         while (it1 != it1end) {
483                 if (!it1->is_equal(*it2))
484                         return false;
485                 ++it1; ++it2;
486         }
487
488         return true;
489 }
490
491 /** Add element at front. */
492 template <template <class> class C>
493 container<C> & container<C>::prepend(const ex & b)
494 {
495         ensure_if_modifiable();
496         this->seq.push_front(b);
497         return *this;
498 }
499
500 /** Add element at back. */
501 template <template <class> class C>
502 container<C> & container<C>::append(const ex & b)
503 {
504         ensure_if_modifiable();
505         this->seq.push_back(b);
506         return *this;
507 }
508
509 /** Remove first element. */
510 template <template <class> class C>
511 container<C> & container<C>::remove_first()
512 {
513         ensure_if_modifiable();
514         this->seq.pop_front();
515         return *this;
516 }
517
518 /** Remove last element. */
519 template <template <class> class C>
520 container<C> & container<C>::remove_last()
521 {
522         ensure_if_modifiable();
523         this->seq.pop_back();
524         return *this;
525 }
526
527 /** Remove all elements. */
528 template <template <class> class C>
529 container<C> & container<C>::remove_all()
530 {
531         ensure_if_modifiable();
532         this->seq.clear();
533         return *this;
534 }
535
536 /** Sort elements. */
537 template <template <class> class C>
538 container<C> & container<C>::sort()
539 {
540         ensure_if_modifiable();
541         sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
542         return *this;
543 }
544
545 /** Specialization of container::unique_() for std::list. */
546 inline void container<std::list>::unique_()
547 {
548         this->seq.unique(ex_is_equal());
549 }
550
551 /** Remove adjacent duplicate elements. */
552 template <template <class> class C>
553 container<C> & container<C>::unique()
554 {
555         ensure_if_modifiable();
556         unique_();
557         return *this;
558 }
559
560 /** Print sequence of contained elements. */
561 template <template <class> class C>
562 void container<C>::printseq(const print_context & c, char openbracket, char delim,
563                             char closebracket, unsigned this_precedence,
564                             unsigned upper_precedence) const
565 {
566         if (this_precedence <= upper_precedence)
567                 c.s << openbracket;
568
569         if (!this->seq.empty()) {
570                 const_iterator it = this->seq.begin(), itend = this->seq.end();
571                 --itend;
572                 while (it != itend) {
573                         it->print(c, this_precedence);
574                         c.s << delim;
575                         ++it;
576                 }
577                 it->print(c, this_precedence);
578         }
579
580         if (this_precedence <= upper_precedence)
581                 c.s << closebracket;
582 }
583
584 template <template <class> class C>
585 typename container<C>::STLT container<C>::evalchildren(int level) const
586 {
587         if (level == 1)
588                 return this->seq;
589         else if (level == -max_recursion_level)
590                 throw std::runtime_error("max recursion level reached");
591
592         STLT s;
593         reserve(s, this->seq.size());
594
595         --level;
596         const_iterator it = this->seq.begin(), itend = this->seq.end();
597         while (it != itend) {
598                 s.push_back(it->eval(level));
599                 ++it;
600         }
601
602         return s;
603 }
604
605 template <template <class> class C>
606 typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigned options) const
607 {
608         // returns a NULL pointer if nothing had to be substituted
609         // returns a pointer to a newly created epvector otherwise
610         // (which has to be deleted somewhere else)
611
612         const_iterator cit = this->seq.begin(), end = this->seq.end();
613         while (cit != end) {
614                 const ex & subsed_ex = cit->subs(m, options);
615                 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
616
617                         // copy first part of seq which hasn't changed
618                         STLT *s = new STLT(this->seq.begin(), cit);
619                         reserve(*s, this->seq.size());
620
621                         // insert changed element
622                         s->push_back(subsed_ex);
623                         ++cit;
624
625                         // copy rest
626                         while (cit != end) {
627                                 s->push_back(cit->subs(m, options));
628                                 ++cit;
629                         }
630
631                         return s;
632                 }
633
634                 ++cit;
635         }
636         
637         return 0; // nothing has changed
638 }
639
640 } // namespace GiNaC
641
642 #endif // ndef __GINAC_CONTAINER_H__