3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2014 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef GINAC_CONTAINER_H
24 #define GINAC_CONTAINER_H
29 #include "assertion.h"
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 {
46 container_storage() {}
47 container_storage(size_t n, const ex & e) : seq(n, e) {}
50 container_storage(In b, In e) : seq(b, e) {}
52 void reserve(size_t) {}
53 static void reserve(STLT &, size_t) {}
57 // disallow destruction of container through a container_storage*
59 ~container_storage() {}
63 inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
66 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
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 {
74 container_init(STLT & s) : stlt(s) {}
76 container_init<T, STLT> operator,(const T & x)
79 return container_init<T, STLT>(stlt);
82 // The following specializations produce much tighter code than the
85 container_init<T, STLT> operator,(int x)
88 return container_init<T, STLT>(stlt);
91 container_init<T, STLT> operator,(unsigned int x)
94 return container_init<T, STLT>(stlt);
97 container_init<T, STLT> operator,(long x)
100 return container_init<T, STLT>(stlt);
103 container_init<T, STLT> operator,(unsigned long x)
106 return container_init<T, STLT>(stlt);
109 container_init<T, STLT> operator,(double x)
112 return container_init<T, STLT>(stlt);
115 container_init<T, STLT> operator,(const symbol & x)
117 stlt.push_back(T(x));
118 return container_init<T, STLT>(stlt);
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)
131 typedef typename container_storage<C>::STLT STLT;
134 typedef typename STLT::const_iterator const_iterator;
135 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
139 static unsigned get_default_flags() { return 0; }
140 static char get_open_delim() { return '('; }
141 static char get_close_delim() { return ')'; }
145 container(STLT const & s, bool discardable = false)
147 setflag(get_default_flags());
150 this->seq.swap(const_cast<STLT &>(s));
155 explicit container(std::auto_ptr<STLT> vp)
157 setflag(get_default_flags());
161 container(exvector::const_iterator b, exvector::const_iterator e)
162 : container_storage<C>(b, e)
164 setflag(get_default_flags());
167 explicit container(const ex & p1)
168 : container_storage<C>(1, p1)
170 setflag(get_default_flags());
173 container(const ex & p1, const ex & p2)
175 setflag(get_default_flags());
176 this->reserve(this->seq, 2);
177 this->seq.push_back(p1); this->seq.push_back(p2);
180 container(const ex & p1, const ex & p2, const ex & p3)
182 setflag(get_default_flags());
183 this->reserve(this->seq, 3);
184 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
187 container(const ex & p1, const ex & p2, const ex & p3,
190 setflag(get_default_flags());
191 this->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);
196 container(const ex & p1, const ex & p2, const ex & p3,
197 const ex & p4, const ex & p5)
199 setflag(get_default_flags());
200 this->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);
205 container(const ex & p1, const ex & p2, const ex & p3,
206 const ex & p4, const ex & p5, const ex & p6)
208 setflag(get_default_flags());
209 this->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);
214 container(const ex & p1, const ex & p2, const ex & p3,
215 const ex & p4, const ex & p5, const ex & p6,
218 setflag(get_default_flags());
219 this->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);
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)
229 setflag(get_default_flags());
230 this->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);
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)
240 setflag(get_default_flags());
241 this->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);
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,
252 setflag(get_default_flags());
253 this->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);
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)
265 setflag(get_default_flags());
266 this->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);
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)
278 setflag(get_default_flags());
279 this->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);
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,
292 setflag(get_default_flags());
293 this->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);
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)
307 setflag(get_default_flags());
308 this->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);
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)
322 setflag(get_default_flags());
323 this->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);
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,
338 setflag(get_default_flags());
339 this->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);
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)
353 this->seq.push_back(x);
354 return container_init<ex, STLT>(this->seq);
357 // functions overriding virtual functions from base classes
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;
367 void read_archive(const archive_node &n, lst &sym_lst)
369 inherited::read_archive(n, sym_lst);
370 setflag(get_default_flags());
372 archive_node::archive_node_cit first = n.find_first("seq");
373 archive_node::archive_node_cit last = n.find_last("seq");
375 this->reserve(this->seq, last - first);
376 for (archive_node::archive_node_cit i=first; i<last; ++i) {
378 n.find_ex_by_loc(i, e, sym_lst);
379 this->seq.push_back(e);
383 /** Archive the object. */
384 void archive(archive_node &n) const
386 inherited::archive(n);
387 const_iterator i = this->seq.begin(), end = this->seq.end();
397 STLT *newcont = NULL;
398 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
400 newcont->push_back(i->conjugate());
403 ex x = i->conjugate();
404 if (are_ex_trivially_equal(x, *i)) {
408 this->reserve(*newcont, this->seq.size());
409 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
410 newcont->push_back(*j);
412 newcont->push_back(x);
415 ex result = thiscontainer(*newcont);
425 this->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);
436 this->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);
444 bool is_equal_same_type(const basic & other) const;
446 // new virtual functions which can be overridden by derived classes
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); }
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); }
456 virtual void printseq(const print_context & c, char openbracket, char delim,
457 char closebracket, unsigned this_precedence,
458 unsigned upper_precedence = 0) const;
460 // non-virtual functions in this class
462 void sort_(std::random_access_iterator_tag)
464 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
467 void sort_(std::input_iterator_tag)
469 this->seq.sort(ex_is_less());
474 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
475 this->seq.erase(p, this->seq.end());
479 container & prepend(const ex & b);
480 container & append(const ex & b);
481 container & remove_first();
482 container & remove_last();
483 container & remove_all();
485 container & unique();
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();}
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;
501 /** Default constructor */
502 template <template <class T, class = std::allocator<T> > class C>
503 container<C>::container()
505 setflag(get_default_flags());
509 template <template <class T, class = std::allocator<T> > class C>
510 void container<C>::do_print(const print_context & c, unsigned level) const
512 // always print brackets around seq, ignore upper_precedence
513 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
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
519 c.s << std::string(level, ' ') << class_name() << " @" << this
520 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
521 << ", nops=" << nops()
523 const_iterator i = this->seq.begin(), end = this->seq.end();
525 i->print(c, level + c.delta_indent);
528 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
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
534 printseq(c, '[', ',', ']', precedence(), precedence()+1);
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
541 printseq(c, '(', ',', ')', precedence(), precedence()+1);
544 template <template <class T, class = std::allocator<T> > class C>
545 ex container<C>::op(size_t i) const
547 GINAC_ASSERT(i < nops());
549 const_iterator it = this->seq.begin();
554 template <template <class T, class = std::allocator<T> > class C>
555 ex & container<C>::let_op(size_t i)
557 GINAC_ASSERT(i < nops());
559 ensure_if_modifiable();
560 typename STLT::iterator it = this->seq.begin();
565 template <template <class T, class = std::allocator<T> > class C>
566 ex container<C>::eval(int level) const
571 return thiscontainer(evalchildren(level));
574 template <template <class T, class = std::allocator<T> > class C>
575 ex container<C>::subs(const exmap & m, unsigned options) const
577 // After having subs'ed all children, this method subs'es one final
578 // level, but only if the intermediate result is a container! This is
579 // because if the intermediate result has eval'ed to a non-container a
580 // last level substitution would be wrong, as this example involving a
581 // function f and its inverse f^-1 shows:
582 // f(x).subs(x==f^-1(x))
583 // -> f(f^-1(x)) [subschildren]
584 // -> x [eval] /* must not subs(x==f^-1(x))! */
585 std::auto_ptr<STLT> vp = subschildren(m, options);
587 ex result(thiscontainer(vp));
588 if (is_a<container<C> >(result))
589 return ex_to<basic>(result).subs_one_level(m, options);
593 if (is_a<container<C> >(*this))
594 return subs_one_level(m, options);
600 /** Compare two containers of the same type. */
601 template <template <class T, class = std::allocator<T> > class C>
602 int container<C>::compare_same_type(const basic & other) const
604 GINAC_ASSERT(is_a<container>(other));
605 const container & o = static_cast<const container &>(other);
607 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
608 it2 = o.seq.begin(), it2end = o.seq.end();
610 while (it1 != it1end && it2 != it2end) {
611 int cmpval = it1->compare(*it2);
617 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
620 template <template <class T, class = std::allocator<T> > class C>
621 bool container<C>::is_equal_same_type(const basic & other) const
623 GINAC_ASSERT(is_a<container>(other));
624 const container & o = static_cast<const container &>(other);
626 if (this->seq.size() != o.seq.size())
629 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
630 while (it1 != it1end) {
631 if (!it1->is_equal(*it2))
639 /** Add element at front. */
640 template <template <class T, class = std::allocator<T> > class C>
641 container<C> & container<C>::prepend(const ex & b)
643 ensure_if_modifiable();
644 this->seq.push_front(b);
648 /** Add element at back. */
649 template <template <class T, class = std::allocator<T> > class C>
650 container<C> & container<C>::append(const ex & b)
652 ensure_if_modifiable();
653 this->seq.push_back(b);
657 /** Remove first element. */
658 template <template <class T, class = std::allocator<T> > class C>
659 container<C> & container<C>::remove_first()
661 ensure_if_modifiable();
662 this->seq.pop_front();
666 /** Remove last element. */
667 template <template <class T, class = std::allocator<T> > class C>
668 container<C> & container<C>::remove_last()
670 ensure_if_modifiable();
671 this->seq.pop_back();
675 /** Remove all elements. */
676 template <template <class T, class = std::allocator<T> > class C>
677 container<C> & container<C>::remove_all()
679 ensure_if_modifiable();
684 /** Sort elements. */
685 template <template <class T, class = std::allocator<T> > class C>
686 container<C> & container<C>::sort()
688 ensure_if_modifiable();
689 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
693 /** Specialization of container::unique_() for std::list. */
694 template<> inline void container<std::list>::unique_()
696 this->seq.unique(ex_is_equal());
699 /** Remove adjacent duplicate elements. */
700 template <template <class T, class = std::allocator<T> > class C>
701 container<C> & container<C>::unique()
703 ensure_if_modifiable();
708 /** Print sequence of contained elements. */
709 template <template <class T, class = std::allocator<T> > class C>
710 void container<C>::printseq(const print_context & c, char openbracket, char delim,
711 char closebracket, unsigned this_precedence,
712 unsigned upper_precedence) const
714 if (this_precedence <= upper_precedence)
717 if (!this->seq.empty()) {
718 const_iterator it = this->seq.begin(), itend = this->seq.end();
720 while (it != itend) {
721 it->print(c, this_precedence);
725 it->print(c, this_precedence);
728 if (this_precedence <= upper_precedence)
732 template <template <class T, class = std::allocator<T> > class C>
733 typename container<C>::STLT container<C>::evalchildren(int level) const
737 else if (level == -max_recursion_level)
738 throw std::runtime_error("max recursion level reached");
741 this->reserve(s, this->seq.size());
744 const_iterator it = this->seq.begin(), itend = this->seq.end();
745 while (it != itend) {
746 s.push_back(it->eval(level));
753 template <template <class T, class = std::allocator<T> > class C>
754 std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
756 // returns a NULL pointer if nothing had to be substituted
757 // returns a pointer to a newly created STLT otherwise
758 // (and relinquishes responsibility for the STLT)
760 const_iterator cit = this->seq.begin(), end = this->seq.end();
762 const ex & subsed_ex = cit->subs(m, options);
763 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
765 // copy first part of seq which hasn't changed
766 std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
767 this->reserve(*s, this->seq.size());
769 // insert changed element
770 s->push_back(subsed_ex);
775 s->push_back(cit->subs(m, options));
785 return std::auto_ptr<STLT>(0); // nothing has changed
790 #endif // ndef GINAC_CONTAINER_H