3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2019 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"
41 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
42 template <template <class T, class = std::allocator<T>> class C>
43 class container_storage {
47 container_storage() {}
48 container_storage(size_t n, const ex & e) : seq(n, e) {}
49 container_storage(std::initializer_list<ex> il) : seq(il) {}
52 container_storage(In b, In e) : seq(b, e) {}
54 void reserve(size_t) {}
55 static void reserve(STLT &, size_t) {}
59 // disallow destruction of container through a container_storage*
61 ~container_storage() {}
65 inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
68 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
71 /** Helper template to allow initialization of containers via an overloaded
72 * comma operator (idea stolen from Blitz++). */
73 template <typename T, typename STLT>
74 class container_init {
76 container_init(STLT & s) : stlt(s) {}
78 container_init<T, STLT> operator,(const T & x)
81 return container_init<T, STLT>(stlt);
84 // The following specializations produce much tighter code than the
87 container_init<T, STLT> operator,(int x)
90 return container_init<T, STLT>(stlt);
93 container_init<T, STLT> operator,(unsigned int x)
96 return container_init<T, STLT>(stlt);
99 container_init<T, STLT> operator,(long x)
102 return container_init<T, STLT>(stlt);
105 container_init<T, STLT> operator,(unsigned long x)
108 return container_init<T, STLT>(stlt);
111 container_init<T, STLT> operator,(double x)
114 return container_init<T, STLT>(stlt);
117 container_init<T, STLT> operator,(const symbol & x)
119 stlt.push_back(T(x));
120 return container_init<T, STLT>(stlt);
128 /** Wrapper template for making GiNaC classes out of STL containers. */
129 template <template <class T, class = std::allocator<T>> class C>
130 class container : public basic, public container_storage<C> {
131 GINAC_DECLARE_REGISTERED_CLASS(container, basic)
133 typedef typename container_storage<C>::STLT STLT;
136 typedef typename STLT::const_iterator const_iterator;
137 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
141 static unsigned get_default_flags() { return 0; }
142 static char get_open_delim() { return '('; }
143 static char get_close_delim() { return ')'; }
147 container(STLT const & s)
149 setflag(get_default_flags());
153 explicit container(STLT && v)
155 setflag(get_default_flags());
159 container(exvector::const_iterator b, exvector::const_iterator e)
160 : container_storage<C>(b, e)
162 setflag(get_default_flags());
165 container(std::initializer_list<ex> il)
166 : container_storage<C>(il)
168 setflag(get_default_flags());
171 explicit container(const ex & p1) attribute_deprecated;
172 container(const ex & p1, const ex & p2) attribute_deprecated;
173 container(const ex & p1, const ex & p2, const ex & p3) attribute_deprecated;
174 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4) attribute_deprecated;
175 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5) attribute_deprecated;
176 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6) attribute_deprecated;
177 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7) attribute_deprecated;
178 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8) attribute_deprecated;
179 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
180 const ex & p9) attribute_deprecated;
181 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
182 const ex & p9, const ex & p10) attribute_deprecated;
183 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
184 const ex & p9, const ex & p10, const ex & p11) attribute_deprecated;
185 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
186 const ex & p9, const ex & p10, const ex & p11, const ex & p12) attribute_deprecated;
187 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
188 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13) attribute_deprecated;
189 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
190 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14) attribute_deprecated;
191 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
192 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15) attribute_deprecated;
193 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
194 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15, const ex & p16) attribute_deprecated;
196 // First step of initialization of container with a comma-separated
197 // sequence of expressions. Subsequent steps are handled by
198 // container_init<>::operator,().
199 container_init<ex, STLT> operator=(const ex & x) attribute_deprecated;
201 // functions overriding virtual functions from base classes
203 bool info(unsigned inf) const override { return inherited::info(inf); }
204 unsigned precedence() const override { return 10; }
205 size_t nops() const override { return this->seq.size(); }
206 ex op(size_t i) const override;
207 ex & let_op(size_t i) override;
208 ex subs(const exmap & m, unsigned options = 0) const override;
210 void read_archive(const archive_node &n, lst &sym_lst) override
212 inherited::read_archive(n, sym_lst);
213 setflag(get_default_flags());
215 archive_node::archive_node_cit first = n.find_first("seq");
216 archive_node::archive_node_cit last = n.find_last("seq");
218 this->reserve(this->seq, last - first);
219 for (archive_node::archive_node_cit i=first; i<last; ++i) {
221 n.find_ex_by_loc(i, e, sym_lst);
222 this->seq.push_back(e);
226 /** Archive the object. */
227 void archive(archive_node &n) const override
229 inherited::archive(n);
230 for (auto & i : this->seq) {
236 ex conjugate() const override
238 STLT *newcont = nullptr;
239 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
241 newcont->push_back(i->conjugate());
244 ex x = i->conjugate();
245 if (are_ex_trivially_equal(x, *i)) {
249 this->reserve(*newcont, this->seq.size());
250 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
251 newcont->push_back(*j);
253 newcont->push_back(x);
256 ex result = thiscontainer(*newcont);
263 ex real_part() const override
266 this->reserve(cont, nops());
267 const_iterator b = begin();
268 const_iterator e = end();
269 for(const_iterator i=b; i!=e; ++i)
270 cont.push_back(i->real_part());
271 return thiscontainer(cont);
274 ex imag_part() const override
277 this->reserve(cont, nops());
278 const_iterator b = begin();
279 const_iterator e = end();
280 for(const_iterator i=b; i!=e; ++i)
281 cont.push_back(i->imag_part());
282 return thiscontainer(cont);
285 bool is_equal_same_type(const basic & other) const override;
287 // new virtual functions which can be overridden by derived classes
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); }
293 /** Similar to duplicate(), but with a preset sequence (which gets
294 * pilfered). Must be overridden by derived classes. */
295 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
297 virtual void printseq(const print_context & c, char openbracket, char delim,
298 char closebracket, unsigned this_precedence,
299 unsigned upper_precedence = 0) const;
301 // non-virtual functions in this class
303 void sort_(std::random_access_iterator_tag)
305 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
308 void sort_(std::input_iterator_tag)
310 this->seq.sort(ex_is_less());
315 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
316 this->seq.erase(p, this->seq.end());
320 container & prepend(const ex & b);
321 container & append(const ex & b);
322 container & remove_first();
323 container & remove_last();
324 container & remove_all();
326 container & unique();
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();}
334 void do_print(const print_context & c, unsigned level) const;
335 void do_print_tree(const print_tree & c, unsigned level) const;
336 void do_print_python(const print_python & c, unsigned level) const;
337 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
338 STLT subschildren(const exmap & m, unsigned options = 0) const;
341 /** Default constructor */
342 template <template <class T, class = std::allocator<T>> class C>
343 container<C>::container()
345 setflag(get_default_flags());
348 /** Deprecatd constructors (prefer initializer list) */
349 template <template <class T, class = std::allocator<T>> class C>
350 container<C>::container(const ex & p1)
351 : container_storage<C>(1, p1) { setflag(get_default_flags()); }
353 template <template <class T, class = std::allocator<T>> class C>
354 container<C>::container(const ex & p1, const ex & p2)
355 : container_storage<C>{p1, p2} { setflag(get_default_flags()); }
357 template <template <class T, class = std::allocator<T>> class C>
358 container<C>::container(const ex & p1, const ex & p2, const ex & p3)
359 : container_storage<C>{p1, p2, p3} { setflag(get_default_flags()); }
361 template <template <class T, class = std::allocator<T>> class C>
362 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4)
363 : container_storage<C>{p1, p2, p3, p4} { setflag(get_default_flags()); }
365 template <template <class T, class = std::allocator<T>> class C>
366 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5)
367 : container_storage<C>{p1, p2, p3, p4, p5} { setflag(get_default_flags()); }
369 template <template <class T, class = std::allocator<T>> class C>
370 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6)
371 : container_storage<C>{p1, p2, p3, p4, p5, p6} { setflag(get_default_flags()); }
373 template <template <class T, class = std::allocator<T>> class C>
374 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7)
375 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7} { setflag(get_default_flags()); }
377 template <template <class T, class = std::allocator<T>> class C>
378 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8)
379 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8} { setflag(get_default_flags()); }
381 template <template <class T, class = std::allocator<T>> class C>
382 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
384 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9} { setflag(get_default_flags()); }
386 template <template <class T, class = std::allocator<T>> class C>
387 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
388 const ex & p9, const ex & p10)
389 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10} { setflag(get_default_flags()); }
391 template <template <class T, class = std::allocator<T>> class C>
392 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
393 const ex & p9, const ex & p10, const ex & p11)
394 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11} { setflag(get_default_flags()); }
396 template <template <class T, class = std::allocator<T>> class C>
397 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
398 const ex & p9, const ex & p10, const ex & p11, const ex & p12)
399 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12} { setflag(get_default_flags()); }
401 template <template <class T, class = std::allocator<T>> class C>
402 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
403 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13)
404 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13} { setflag(get_default_flags()); }
406 template <template <class T, class = std::allocator<T>> class C>
407 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
408 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14)
409 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14} { setflag(get_default_flags()); }
411 template <template <class T, class = std::allocator<T>> class C>
412 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
413 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15)
414 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15} { setflag(get_default_flags()); }
415 template <template <class T, class = std::allocator<T>> class C>
416 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7, const ex & p8,
417 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15, const ex & p16)
418 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16} { setflag(get_default_flags()); }
420 template <template <class T, class = std::allocator<T>> class C>
421 container_init<ex, typename container_storage<C>::STLT> container<C>::operator=(const ex & x)
423 this->seq.push_back(x);
424 return container_init<ex, typename container_storage<C>::STLT>(this->seq);
427 template <template <class T, class = std::allocator<T>> class C>
428 void container<C>::do_print(const print_context & c, unsigned level) const
430 // always print brackets around seq, ignore upper_precedence
431 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
434 template <template <class T, class = std::allocator<T>> class C>
435 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
437 c.s << std::string(level, ' ') << class_name() << " @" << this
438 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
439 << ", nops=" << nops()
441 const_iterator i = this->seq.begin(), end = this->seq.end();
443 i->print(c, level + c.delta_indent);
446 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
449 template <template <class T, class = std::allocator<T>> class C>
450 void container<C>::do_print_python(const print_python & c, unsigned level) const
452 printseq(c, '[', ',', ']', precedence(), precedence()+1);
455 template <template <class T, class = std::allocator<T>> class C>
456 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
459 printseq(c, '(', ',', ')', precedence(), precedence()+1);
462 template <template <class T, class = std::allocator<T>> class C>
463 ex container<C>::op(size_t i) const
465 GINAC_ASSERT(i < nops());
467 const_iterator it = this->seq.begin();
472 template <template <class T, class = std::allocator<T>> class C>
473 ex & container<C>::let_op(size_t i)
475 GINAC_ASSERT(i < nops());
477 ensure_if_modifiable();
478 typename STLT::iterator it = this->seq.begin();
483 template <template <class T, class = std::allocator<T>> class C>
484 ex container<C>::subs(const exmap & m, unsigned options) const
486 // After having subs'ed all children, this method subs'es one final
487 // level, but only if the intermediate result is a container! This is
488 // because if the intermediate result has eval'ed to a non-container a
489 // last level substitution would be wrong, as this example involving a
490 // function f and its inverse f^-1 shows:
491 // f(x).subs(x==f^-1(x))
492 // -> f(f^-1(x)) [subschildren]
493 // -> x [eval] /* must not subs(x==f^-1(x))! */
494 STLT subsed = subschildren(m, options);
495 if (!subsed.empty()) {
496 ex result(thiscontainer(subsed));
497 if (is_a<container<C>>(result))
498 return ex_to<basic>(result).subs_one_level(m, options);
502 if (is_a<container<C>>(*this))
503 return subs_one_level(m, options);
509 /** Compare two containers of the same type. */
510 template <template <class T, class = std::allocator<T>> class C>
511 int container<C>::compare_same_type(const basic & other) const
513 GINAC_ASSERT(is_a<container>(other));
514 const container & o = static_cast<const container &>(other);
516 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
517 it2 = o.seq.begin(), it2end = o.seq.end();
519 while (it1 != it1end && it2 != it2end) {
520 int cmpval = it1->compare(*it2);
526 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
529 template <template <class T, class = std::allocator<T>> class C>
530 bool container<C>::is_equal_same_type(const basic & other) const
532 GINAC_ASSERT(is_a<container>(other));
533 const container & o = static_cast<const container &>(other);
535 if (this->seq.size() != o.seq.size())
538 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
539 while (it1 != it1end) {
540 if (!it1->is_equal(*it2))
548 /** Add element at front. */
549 template <template <class T, class = std::allocator<T>> class C>
550 container<C> & container<C>::prepend(const ex & b)
552 ensure_if_modifiable();
553 this->seq.push_front(b);
557 /** Add element at back. */
558 template <template <class T, class = std::allocator<T>> class C>
559 container<C> & container<C>::append(const ex & b)
561 ensure_if_modifiable();
562 this->seq.push_back(b);
566 /** Remove first element. */
567 template <template <class T, class = std::allocator<T>> class C>
568 container<C> & container<C>::remove_first()
570 ensure_if_modifiable();
571 this->seq.pop_front();
575 /** Remove last element. */
576 template <template <class T, class = std::allocator<T>> class C>
577 container<C> & container<C>::remove_last()
579 ensure_if_modifiable();
580 this->seq.pop_back();
584 /** Remove all elements. */
585 template <template <class T, class = std::allocator<T>> class C>
586 container<C> & container<C>::remove_all()
588 ensure_if_modifiable();
593 /** Sort elements. */
594 template <template <class T, class = std::allocator<T>> class C>
595 container<C> & container<C>::sort()
597 ensure_if_modifiable();
598 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
602 /** Specialization of container::unique_() for std::list. */
603 template<> inline void container<std::list>::unique_()
605 this->seq.unique(ex_is_equal());
608 /** Remove adjacent duplicate elements. */
609 template <template <class T, class = std::allocator<T>> class C>
610 container<C> & container<C>::unique()
612 ensure_if_modifiable();
617 /** Print sequence of contained elements. */
618 template <template <class T, class = std::allocator<T>> class C>
619 void container<C>::printseq(const print_context & c, char openbracket, char delim,
620 char closebracket, unsigned this_precedence,
621 unsigned upper_precedence) const
623 if (this_precedence <= upper_precedence)
626 if (!this->seq.empty()) {
627 const_iterator it = this->seq.begin(), itend = this->seq.end();
629 while (it != itend) {
630 it->print(c, this_precedence);
634 it->print(c, this_precedence);
637 if (this_precedence <= upper_precedence)
641 template <template <class T, class = std::allocator<T>> class C>
642 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
644 // returns an empty container if nothing had to be substituted
645 // returns a STLT with substituted elements otherwise
647 const_iterator cit = this->seq.begin(), end = this->seq.end();
649 const ex & subsed_ex = cit->subs(m, options);
650 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
652 // copy first part of seq which hasn't changed
653 STLT s(this->seq.begin(), cit);
654 this->reserve(s, this->seq.size());
656 // insert changed element
657 s.push_back(subsed_ex);
662 s.push_back(cit->subs(m, options));
672 return STLT(); // nothing has changed
677 #endif // ndef GINAC_CONTAINER_H