3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2015 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(STLT && v)
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 for (auto & i : this->seq) {
395 STLT *newcont = nullptr;
396 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
398 newcont->push_back(i->conjugate());
401 ex x = i->conjugate();
402 if (are_ex_trivially_equal(x, *i)) {
406 this->reserve(*newcont, this->seq.size());
407 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
408 newcont->push_back(*j);
410 newcont->push_back(x);
413 ex result = thiscontainer(*newcont);
423 this->reserve(cont, nops());
424 const_iterator b = begin();
425 const_iterator e = end();
426 for(const_iterator i=b; i!=e; ++i)
427 cont.push_back(i->real_part());
428 return thiscontainer(cont);
434 this->reserve(cont, nops());
435 const_iterator b = begin();
436 const_iterator e = end();
437 for(const_iterator i=b; i!=e; ++i)
438 cont.push_back(i->imag_part());
439 return thiscontainer(cont);
442 bool is_equal_same_type(const basic & other) const;
444 // new virtual functions which can be overridden by derived classes
446 /** Similar to duplicate(), but with a preset sequence. Must be
447 * overridden by derived classes. */
448 virtual ex thiscontainer(const STLT & v) const { return container(v); }
450 /** Similar to duplicate(), but with a preset sequence (which gets
451 * pilfered). Must be overridden by derived classes. */
452 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
454 virtual void printseq(const print_context & c, char openbracket, char delim,
455 char closebracket, unsigned this_precedence,
456 unsigned upper_precedence = 0) const;
458 // non-virtual functions in this class
460 void sort_(std::random_access_iterator_tag)
462 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
465 void sort_(std::input_iterator_tag)
467 this->seq.sort(ex_is_less());
472 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
473 this->seq.erase(p, this->seq.end());
477 container & prepend(const ex & b);
478 container & append(const ex & b);
479 container & remove_first();
480 container & remove_last();
481 container & remove_all();
483 container & unique();
485 const_iterator begin() const {return this->seq.begin();}
486 const_iterator end() const {return this->seq.end();}
487 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
488 const_reverse_iterator rend() const {return this->seq.rend();}
491 void do_print(const print_context & c, unsigned level) const;
492 void do_print_tree(const print_tree & c, unsigned level) const;
493 void do_print_python(const print_python & c, unsigned level) const;
494 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
495 STLT evalchildren(int level) const;
496 STLT subschildren(const exmap & m, unsigned options = 0) const;
499 /** Default constructor */
500 template <template <class T, class = std::allocator<T> > class C>
501 container<C>::container()
503 setflag(get_default_flags());
507 template <template <class T, class = std::allocator<T> > class C>
508 void container<C>::do_print(const print_context & c, unsigned level) const
510 // always print brackets around seq, ignore upper_precedence
511 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
514 template <template <class T, class = std::allocator<T> > class C>
515 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
517 c.s << std::string(level, ' ') << class_name() << " @" << this
518 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
519 << ", nops=" << nops()
521 const_iterator i = this->seq.begin(), end = this->seq.end();
523 i->print(c, level + c.delta_indent);
526 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
529 template <template <class T, class = std::allocator<T> > class C>
530 void container<C>::do_print_python(const print_python & c, unsigned level) const
532 printseq(c, '[', ',', ']', precedence(), precedence()+1);
535 template <template <class T, class = std::allocator<T> > class C>
536 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
539 printseq(c, '(', ',', ')', precedence(), precedence()+1);
542 template <template <class T, class = std::allocator<T> > class C>
543 ex container<C>::op(size_t i) const
545 GINAC_ASSERT(i < nops());
547 const_iterator it = this->seq.begin();
552 template <template <class T, class = std::allocator<T> > class C>
553 ex & container<C>::let_op(size_t i)
555 GINAC_ASSERT(i < nops());
557 ensure_if_modifiable();
558 typename STLT::iterator it = this->seq.begin();
563 template <template <class T, class = std::allocator<T> > class C>
564 ex container<C>::eval(int level) const
569 return thiscontainer(evalchildren(level));
572 template <template <class T, class = std::allocator<T> > class C>
573 ex container<C>::subs(const exmap & m, unsigned options) const
575 // After having subs'ed all children, this method subs'es one final
576 // level, but only if the intermediate result is a container! This is
577 // because if the intermediate result has eval'ed to a non-container a
578 // last level substitution would be wrong, as this example involving a
579 // function f and its inverse f^-1 shows:
580 // f(x).subs(x==f^-1(x))
581 // -> f(f^-1(x)) [subschildren]
582 // -> x [eval] /* must not subs(x==f^-1(x))! */
583 STLT subsed = subschildren(m, options);
584 if (!subsed.empty()) {
585 ex result(thiscontainer(subsed));
586 if (is_a<container<C> >(result))
587 return ex_to<basic>(result).subs_one_level(m, options);
591 if (is_a<container<C> >(*this))
592 return subs_one_level(m, options);
598 /** Compare two containers of the same type. */
599 template <template <class T, class = std::allocator<T> > class C>
600 int container<C>::compare_same_type(const basic & other) const
602 GINAC_ASSERT(is_a<container>(other));
603 const container & o = static_cast<const container &>(other);
605 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
606 it2 = o.seq.begin(), it2end = o.seq.end();
608 while (it1 != it1end && it2 != it2end) {
609 int cmpval = it1->compare(*it2);
615 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
618 template <template <class T, class = std::allocator<T> > class C>
619 bool container<C>::is_equal_same_type(const basic & other) const
621 GINAC_ASSERT(is_a<container>(other));
622 const container & o = static_cast<const container &>(other);
624 if (this->seq.size() != o.seq.size())
627 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
628 while (it1 != it1end) {
629 if (!it1->is_equal(*it2))
637 /** Add element at front. */
638 template <template <class T, class = std::allocator<T> > class C>
639 container<C> & container<C>::prepend(const ex & b)
641 ensure_if_modifiable();
642 this->seq.push_front(b);
646 /** Add element at back. */
647 template <template <class T, class = std::allocator<T> > class C>
648 container<C> & container<C>::append(const ex & b)
650 ensure_if_modifiable();
651 this->seq.push_back(b);
655 /** Remove first element. */
656 template <template <class T, class = std::allocator<T> > class C>
657 container<C> & container<C>::remove_first()
659 ensure_if_modifiable();
660 this->seq.pop_front();
664 /** Remove last element. */
665 template <template <class T, class = std::allocator<T> > class C>
666 container<C> & container<C>::remove_last()
668 ensure_if_modifiable();
669 this->seq.pop_back();
673 /** Remove all elements. */
674 template <template <class T, class = std::allocator<T> > class C>
675 container<C> & container<C>::remove_all()
677 ensure_if_modifiable();
682 /** Sort elements. */
683 template <template <class T, class = std::allocator<T> > class C>
684 container<C> & container<C>::sort()
686 ensure_if_modifiable();
687 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
691 /** Specialization of container::unique_() for std::list. */
692 template<> inline void container<std::list>::unique_()
694 this->seq.unique(ex_is_equal());
697 /** Remove adjacent duplicate elements. */
698 template <template <class T, class = std::allocator<T> > class C>
699 container<C> & container<C>::unique()
701 ensure_if_modifiable();
706 /** Print sequence of contained elements. */
707 template <template <class T, class = std::allocator<T> > class C>
708 void container<C>::printseq(const print_context & c, char openbracket, char delim,
709 char closebracket, unsigned this_precedence,
710 unsigned upper_precedence) const
712 if (this_precedence <= upper_precedence)
715 if (!this->seq.empty()) {
716 const_iterator it = this->seq.begin(), itend = this->seq.end();
718 while (it != itend) {
719 it->print(c, this_precedence);
723 it->print(c, this_precedence);
726 if (this_precedence <= upper_precedence)
730 template <template <class T, class = std::allocator<T> > class C>
731 typename container<C>::STLT container<C>::evalchildren(int level) const
735 else if (level == -max_recursion_level)
736 throw std::runtime_error("max recursion level reached");
739 this->reserve(s, this->seq.size());
742 const_iterator it = this->seq.begin(), itend = this->seq.end();
743 while (it != itend) {
744 s.push_back(it->eval(level));
751 template <template <class T, class = std::allocator<T> > class C>
752 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
754 // returns an empty container if nothing had to be substituted
755 // returns a STLT with substituted elements otherwise
757 const_iterator cit = this->seq.begin(), end = this->seq.end();
759 const ex & subsed_ex = cit->subs(m, options);
760 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
762 // copy first part of seq which hasn't changed
763 STLT s(this->seq.begin(), cit);
764 this->reserve(s, this->seq.size());
766 // insert changed element
767 s.push_back(subsed_ex);
772 s.push_back(cit->subs(m, options));
782 return STLT(); // nothing has changed
787 #endif // ndef GINAC_CONTAINER_H