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)
147 setflag(get_default_flags());
151 explicit container(STLT && v)
153 setflag(get_default_flags());
157 container(exvector::const_iterator b, exvector::const_iterator e)
158 : container_storage<C>(b, e)
160 setflag(get_default_flags());
163 explicit container(const ex & p1)
164 : container_storage<C>(1, p1)
166 setflag(get_default_flags());
169 container(const ex & p1, const ex & p2)
171 setflag(get_default_flags());
172 this->reserve(this->seq, 2);
173 this->seq.push_back(p1); this->seq.push_back(p2);
176 container(const ex & p1, const ex & p2, const ex & p3)
178 setflag(get_default_flags());
179 this->reserve(this->seq, 3);
180 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
183 container(const ex & p1, const ex & p2, const ex & p3,
186 setflag(get_default_flags());
187 this->reserve(this->seq, 4);
188 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
189 this->seq.push_back(p4);
192 container(const ex & p1, const ex & p2, const ex & p3,
193 const ex & p4, const ex & p5)
195 setflag(get_default_flags());
196 this->reserve(this->seq, 5);
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);
201 container(const ex & p1, const ex & p2, const ex & p3,
202 const ex & p4, const ex & p5, const ex & p6)
204 setflag(get_default_flags());
205 this->reserve(this->seq, 6);
206 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
207 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
210 container(const ex & p1, const ex & p2, const ex & p3,
211 const ex & p4, const ex & p5, const ex & p6,
214 setflag(get_default_flags());
215 this->reserve(this->seq, 7);
216 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
217 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
218 this->seq.push_back(p7);
221 container(const ex & p1, const ex & p2, const ex & p3,
222 const ex & p4, const ex & p5, const ex & p6,
223 const ex & p7, const ex & p8)
225 setflag(get_default_flags());
226 this->reserve(this->seq, 8);
227 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
228 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
229 this->seq.push_back(p7); this->seq.push_back(p8);
232 container(const ex & p1, const ex & p2, const ex & p3,
233 const ex & p4, const ex & p5, const ex & p6,
234 const ex & p7, const ex & p8, const ex & p9)
236 setflag(get_default_flags());
237 this->reserve(this->seq, 9);
238 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
239 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
240 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
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,
248 setflag(get_default_flags());
249 this->reserve(this->seq, 10);
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);
256 container(const ex & p1, const ex & p2, const ex & p3,
257 const ex & p4, const ex & p5, const ex & p6,
258 const ex & p7, const ex & p8, const ex & p9,
259 const ex & p10, const ex & p11)
261 setflag(get_default_flags());
262 this->reserve(this->seq, 11);
263 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
264 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
265 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
266 this->seq.push_back(p10); this->seq.push_back(p11);
269 container(const ex & p1, const ex & p2, const ex & p3,
270 const ex & p4, const ex & p5, const ex & p6,
271 const ex & p7, const ex & p8, const ex & p9,
272 const ex & p10, const ex & p11, const ex & p12)
274 setflag(get_default_flags());
275 this->reserve(this->seq, 12);
276 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
277 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
278 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
279 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
282 container(const ex & p1, const ex & p2, const ex & p3,
283 const ex & p4, const ex & p5, const ex & p6,
284 const ex & p7, const ex & p8, const ex & p9,
285 const ex & p10, const ex & p11, const ex & p12,
288 setflag(get_default_flags());
289 this->reserve(this->seq, 13);
290 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
291 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
292 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
293 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
294 this->seq.push_back(p13);
297 container(const ex & p1, const ex & p2, const ex & p3,
298 const ex & p4, const ex & p5, const ex & p6,
299 const ex & p7, const ex & p8, const ex & p9,
300 const ex & p10, const ex & p11, const ex & p12,
301 const ex & p13, const ex & p14)
303 setflag(get_default_flags());
304 this->reserve(this->seq, 14);
305 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
306 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
307 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
308 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
309 this->seq.push_back(p13); this->seq.push_back(p14);
312 container(const ex & p1, const ex & p2, const ex & p3,
313 const ex & p4, const ex & p5, const ex & p6,
314 const ex & p7, const ex & p8, const ex & p9,
315 const ex & p10, const ex & p11, const ex & p12,
316 const ex & p13, const ex & p14, const ex & p15)
318 setflag(get_default_flags());
319 this->reserve(this->seq, 15);
320 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
321 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
322 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
323 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
324 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
327 container(const ex & p1, const ex & p2, const ex & p3,
328 const ex & p4, const ex & p5, const ex & p6,
329 const ex & p7, const ex & p8, const ex & p9,
330 const ex & p10, const ex & p11, const ex & p12,
331 const ex & p13, const ex & p14, const ex & p15,
334 setflag(get_default_flags());
335 this->reserve(this->seq, 16);
336 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
337 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
338 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
339 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
340 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
341 this->seq.push_back(p16);
344 // First step of initialization of container with a comma-separated
345 // sequence of expressions. Subsequent steps are handled by
346 // container_init<>::operator,().
347 container_init<ex, STLT> operator=(const ex & x)
349 this->seq.push_back(x);
350 return container_init<ex, STLT>(this->seq);
353 // functions overriding virtual functions from base classes
355 bool info(unsigned inf) const { return inherited::info(inf); }
356 unsigned precedence() const { return 10; }
357 size_t nops() const { return this->seq.size(); }
358 ex op(size_t i) const;
359 ex & let_op(size_t i);
360 ex eval(int level = 0) const;
361 ex subs(const exmap & m, unsigned options = 0) const;
363 void read_archive(const archive_node &n, lst &sym_lst)
365 inherited::read_archive(n, sym_lst);
366 setflag(get_default_flags());
368 archive_node::archive_node_cit first = n.find_first("seq");
369 archive_node::archive_node_cit last = n.find_last("seq");
371 this->reserve(this->seq, last - first);
372 for (archive_node::archive_node_cit i=first; i<last; ++i) {
374 n.find_ex_by_loc(i, e, sym_lst);
375 this->seq.push_back(e);
379 /** Archive the object. */
380 void archive(archive_node &n) const
382 inherited::archive(n);
383 for (auto & i : this->seq) {
391 STLT *newcont = nullptr;
392 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
394 newcont->push_back(i->conjugate());
397 ex x = i->conjugate();
398 if (are_ex_trivially_equal(x, *i)) {
402 this->reserve(*newcont, this->seq.size());
403 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
404 newcont->push_back(*j);
406 newcont->push_back(x);
409 ex result = thiscontainer(*newcont);
419 this->reserve(cont, nops());
420 const_iterator b = begin();
421 const_iterator e = end();
422 for(const_iterator i=b; i!=e; ++i)
423 cont.push_back(i->real_part());
424 return thiscontainer(cont);
430 this->reserve(cont, nops());
431 const_iterator b = begin();
432 const_iterator e = end();
433 for(const_iterator i=b; i!=e; ++i)
434 cont.push_back(i->imag_part());
435 return thiscontainer(cont);
438 bool is_equal_same_type(const basic & other) const;
440 // new virtual functions which can be overridden by derived classes
442 /** Similar to duplicate(), but with a preset sequence. Must be
443 * overridden by derived classes. */
444 virtual ex thiscontainer(const STLT & v) const { return container(v); }
446 /** Similar to duplicate(), but with a preset sequence (which gets
447 * pilfered). Must be overridden by derived classes. */
448 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
450 virtual void printseq(const print_context & c, char openbracket, char delim,
451 char closebracket, unsigned this_precedence,
452 unsigned upper_precedence = 0) const;
454 // non-virtual functions in this class
456 void sort_(std::random_access_iterator_tag)
458 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
461 void sort_(std::input_iterator_tag)
463 this->seq.sort(ex_is_less());
468 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
469 this->seq.erase(p, this->seq.end());
473 container & prepend(const ex & b);
474 container & append(const ex & b);
475 container & remove_first();
476 container & remove_last();
477 container & remove_all();
479 container & unique();
481 const_iterator begin() const {return this->seq.begin();}
482 const_iterator end() const {return this->seq.end();}
483 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
484 const_reverse_iterator rend() const {return this->seq.rend();}
487 void do_print(const print_context & c, unsigned level) const;
488 void do_print_tree(const print_tree & c, unsigned level) const;
489 void do_print_python(const print_python & c, unsigned level) const;
490 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
491 STLT evalchildren(int level) const;
492 STLT subschildren(const exmap & m, unsigned options = 0) const;
495 /** Default constructor */
496 template <template <class T, class = std::allocator<T>> class C>
497 container<C>::container()
499 setflag(get_default_flags());
503 template <template <class T, class = std::allocator<T>> class C>
504 void container<C>::do_print(const print_context & c, unsigned level) const
506 // always print brackets around seq, ignore upper_precedence
507 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
510 template <template <class T, class = std::allocator<T>> class C>
511 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
513 c.s << std::string(level, ' ') << class_name() << " @" << this
514 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
515 << ", nops=" << nops()
517 const_iterator i = this->seq.begin(), end = this->seq.end();
519 i->print(c, level + c.delta_indent);
522 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
525 template <template <class T, class = std::allocator<T>> class C>
526 void container<C>::do_print_python(const print_python & c, unsigned level) const
528 printseq(c, '[', ',', ']', precedence(), precedence()+1);
531 template <template <class T, class = std::allocator<T>> class C>
532 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
535 printseq(c, '(', ',', ')', precedence(), precedence()+1);
538 template <template <class T, class = std::allocator<T>> class C>
539 ex container<C>::op(size_t i) const
541 GINAC_ASSERT(i < nops());
543 const_iterator it = this->seq.begin();
548 template <template <class T, class = std::allocator<T>> class C>
549 ex & container<C>::let_op(size_t i)
551 GINAC_ASSERT(i < nops());
553 ensure_if_modifiable();
554 typename STLT::iterator it = this->seq.begin();
559 template <template <class T, class = std::allocator<T>> class C>
560 ex container<C>::eval(int level) const
565 return thiscontainer(evalchildren(level));
568 template <template <class T, class = std::allocator<T>> class C>
569 ex container<C>::subs(const exmap & m, unsigned options) const
571 // After having subs'ed all children, this method subs'es one final
572 // level, but only if the intermediate result is a container! This is
573 // because if the intermediate result has eval'ed to a non-container a
574 // last level substitution would be wrong, as this example involving a
575 // function f and its inverse f^-1 shows:
576 // f(x).subs(x==f^-1(x))
577 // -> f(f^-1(x)) [subschildren]
578 // -> x [eval] /* must not subs(x==f^-1(x))! */
579 STLT subsed = subschildren(m, options);
580 if (!subsed.empty()) {
581 ex result(thiscontainer(subsed));
582 if (is_a<container<C>>(result))
583 return ex_to<basic>(result).subs_one_level(m, options);
587 if (is_a<container<C>>(*this))
588 return subs_one_level(m, options);
594 /** Compare two containers of the same type. */
595 template <template <class T, class = std::allocator<T>> class C>
596 int container<C>::compare_same_type(const basic & other) const
598 GINAC_ASSERT(is_a<container>(other));
599 const container & o = static_cast<const container &>(other);
601 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
602 it2 = o.seq.begin(), it2end = o.seq.end();
604 while (it1 != it1end && it2 != it2end) {
605 int cmpval = it1->compare(*it2);
611 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
614 template <template <class T, class = std::allocator<T>> class C>
615 bool container<C>::is_equal_same_type(const basic & other) const
617 GINAC_ASSERT(is_a<container>(other));
618 const container & o = static_cast<const container &>(other);
620 if (this->seq.size() != o.seq.size())
623 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
624 while (it1 != it1end) {
625 if (!it1->is_equal(*it2))
633 /** Add element at front. */
634 template <template <class T, class = std::allocator<T>> class C>
635 container<C> & container<C>::prepend(const ex & b)
637 ensure_if_modifiable();
638 this->seq.push_front(b);
642 /** Add element at back. */
643 template <template <class T, class = std::allocator<T>> class C>
644 container<C> & container<C>::append(const ex & b)
646 ensure_if_modifiable();
647 this->seq.push_back(b);
651 /** Remove first element. */
652 template <template <class T, class = std::allocator<T>> class C>
653 container<C> & container<C>::remove_first()
655 ensure_if_modifiable();
656 this->seq.pop_front();
660 /** Remove last element. */
661 template <template <class T, class = std::allocator<T>> class C>
662 container<C> & container<C>::remove_last()
664 ensure_if_modifiable();
665 this->seq.pop_back();
669 /** Remove all elements. */
670 template <template <class T, class = std::allocator<T>> class C>
671 container<C> & container<C>::remove_all()
673 ensure_if_modifiable();
678 /** Sort elements. */
679 template <template <class T, class = std::allocator<T>> class C>
680 container<C> & container<C>::sort()
682 ensure_if_modifiable();
683 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
687 /** Specialization of container::unique_() for std::list. */
688 template<> inline void container<std::list>::unique_()
690 this->seq.unique(ex_is_equal());
693 /** Remove adjacent duplicate elements. */
694 template <template <class T, class = std::allocator<T>> class C>
695 container<C> & container<C>::unique()
697 ensure_if_modifiable();
702 /** Print sequence of contained elements. */
703 template <template <class T, class = std::allocator<T>> class C>
704 void container<C>::printseq(const print_context & c, char openbracket, char delim,
705 char closebracket, unsigned this_precedence,
706 unsigned upper_precedence) const
708 if (this_precedence <= upper_precedence)
711 if (!this->seq.empty()) {
712 const_iterator it = this->seq.begin(), itend = this->seq.end();
714 while (it != itend) {
715 it->print(c, this_precedence);
719 it->print(c, this_precedence);
722 if (this_precedence <= upper_precedence)
726 template <template <class T, class = std::allocator<T>> class C>
727 typename container<C>::STLT container<C>::evalchildren(int level) const
731 else if (level == -max_recursion_level)
732 throw std::runtime_error("max recursion level reached");
735 this->reserve(s, this->seq.size());
738 const_iterator it = this->seq.begin(), itend = this->seq.end();
739 while (it != itend) {
740 s.push_back(it->eval(level));
747 template <template <class T, class = std::allocator<T>> class C>
748 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
750 // returns an empty container if nothing had to be substituted
751 // returns a STLT with substituted elements otherwise
753 const_iterator cit = this->seq.begin(), end = this->seq.end();
755 const ex & subsed_ex = cit->subs(m, options);
756 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
758 // copy first part of seq which hasn't changed
759 STLT s(this->seq.begin(), cit);
760 this->reserve(s, this->seq.size());
762 // insert changed element
763 s.push_back(subsed_ex);
768 s.push_back(cit->subs(m, options));
778 return STLT(); // nothing has changed
783 #endif // ndef GINAC_CONTAINER_H