3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2020 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 auto range = n.find_property_range("seq", "seq");
216 this->reserve(this->seq, range.end - range.begin);
217 for (archive_node::archive_node_cit i=range.begin; i<range.end; ++i) {
219 n.find_ex_by_loc(i, e, sym_lst);
220 this->seq.emplace_back(e);
224 /** Archive the object. */
225 void archive(archive_node &n) const override
227 inherited::archive(n);
228 for (auto & i : this->seq) {
234 ex conjugate() const override
236 STLT *newcont = nullptr;
237 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
239 newcont->push_back(i->conjugate());
242 ex x = i->conjugate();
243 if (are_ex_trivially_equal(x, *i)) {
247 this->reserve(*newcont, this->seq.size());
248 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
249 newcont->push_back(*j);
251 newcont->push_back(x);
254 ex result = thiscontainer(*newcont);
261 ex real_part() const override
264 this->reserve(cont, nops());
265 const_iterator b = begin();
266 const_iterator e = end();
267 for(const_iterator i=b; i!=e; ++i)
268 cont.push_back(i->real_part());
269 return thiscontainer(cont);
272 ex imag_part() const override
275 this->reserve(cont, nops());
276 const_iterator b = begin();
277 const_iterator e = end();
278 for(const_iterator i=b; i!=e; ++i)
279 cont.push_back(i->imag_part());
280 return thiscontainer(cont);
283 bool is_equal_same_type(const basic & other) const override;
285 // new virtual functions which can be overridden by derived classes
287 /** Similar to duplicate(), but with a preset sequence. Must be
288 * overridden by derived classes. */
289 virtual ex thiscontainer(const STLT & v) const { return container(v); }
291 /** Similar to duplicate(), but with a preset sequence (which gets
292 * pilfered). Must be overridden by derived classes. */
293 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
295 virtual void printseq(const print_context & c, char openbracket, char delim,
296 char closebracket, unsigned this_precedence,
297 unsigned upper_precedence = 0) const;
299 // non-virtual functions in this class
301 void sort_(std::random_access_iterator_tag)
303 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
306 void sort_(std::input_iterator_tag)
308 this->seq.sort(ex_is_less());
313 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
314 this->seq.erase(p, this->seq.end());
318 container & prepend(const ex & b);
319 container & append(const ex & b);
320 container & remove_first();
321 container & remove_last();
322 container & remove_all();
324 container & unique();
326 const_iterator begin() const {return this->seq.begin();}
327 const_iterator end() const {return this->seq.end();}
328 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
329 const_reverse_iterator rend() const {return this->seq.rend();}
332 void do_print(const print_context & c, unsigned level) const;
333 void do_print_tree(const print_tree & c, unsigned level) const;
334 void do_print_python(const print_python & c, unsigned level) const;
335 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
336 STLT subschildren(const exmap & m, unsigned options = 0) const;
339 /** Default constructor */
340 template <template <class T, class = std::allocator<T>> class C>
341 container<C>::container()
343 setflag(get_default_flags());
346 /** Deprecatd constructors (prefer initializer list) */
347 template <template <class T, class = std::allocator<T>> class C>
348 container<C>::container(const ex & p1)
349 : container_storage<C>(1, p1) { setflag(get_default_flags()); }
351 template <template <class T, class = std::allocator<T>> class C>
352 container<C>::container(const ex & p1, const ex & p2)
353 : container_storage<C>{p1, p2} { setflag(get_default_flags()); }
355 template <template <class T, class = std::allocator<T>> class C>
356 container<C>::container(const ex & p1, const ex & p2, const ex & p3)
357 : container_storage<C>{p1, p2, p3} { setflag(get_default_flags()); }
359 template <template <class T, class = std::allocator<T>> class C>
360 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4)
361 : container_storage<C>{p1, p2, p3, p4} { setflag(get_default_flags()); }
363 template <template <class T, class = std::allocator<T>> class C>
364 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5)
365 : container_storage<C>{p1, p2, p3, p4, p5} { setflag(get_default_flags()); }
367 template <template <class T, class = std::allocator<T>> class C>
368 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6)
369 : container_storage<C>{p1, p2, p3, p4, p5, p6} { setflag(get_default_flags()); }
371 template <template <class T, class = std::allocator<T>> class C>
372 container<C>::container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7)
373 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7} { setflag(get_default_flags()); }
375 template <template <class T, class = std::allocator<T>> class C>
376 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)
377 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8} { setflag(get_default_flags()); }
379 template <template <class T, class = std::allocator<T>> class C>
380 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,
382 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9} { setflag(get_default_flags()); }
384 template <template <class T, class = std::allocator<T>> class C>
385 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,
386 const ex & p9, const ex & p10)
387 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10} { setflag(get_default_flags()); }
389 template <template <class T, class = std::allocator<T>> class C>
390 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,
391 const ex & p9, const ex & p10, const ex & p11)
392 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11} { setflag(get_default_flags()); }
394 template <template <class T, class = std::allocator<T>> class C>
395 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,
396 const ex & p9, const ex & p10, const ex & p11, const ex & p12)
397 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12} { setflag(get_default_flags()); }
399 template <template <class T, class = std::allocator<T>> class C>
400 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,
401 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13)
402 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13} { setflag(get_default_flags()); }
404 template <template <class T, class = std::allocator<T>> class C>
405 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,
406 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14)
407 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14} { setflag(get_default_flags()); }
409 template <template <class T, class = std::allocator<T>> class C>
410 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,
411 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15)
412 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15} { setflag(get_default_flags()); }
413 template <template <class T, class = std::allocator<T>> class C>
414 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,
415 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15, const ex & p16)
416 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16} { setflag(get_default_flags()); }
418 template <template <class T, class = std::allocator<T>> class C>
419 container_init<ex, typename container_storage<C>::STLT> container<C>::operator=(const ex & x)
421 this->seq.push_back(x);
422 return container_init<ex, typename container_storage<C>::STLT>(this->seq);
425 template <template <class T, class = std::allocator<T>> class C>
426 void container<C>::do_print(const print_context & c, unsigned level) const
428 // always print brackets around seq, ignore upper_precedence
429 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
432 template <template <class T, class = std::allocator<T>> class C>
433 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
435 c.s << std::string(level, ' ') << class_name() << " @" << this
436 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
437 << ", nops=" << nops()
439 const_iterator i = this->seq.begin(), end = this->seq.end();
441 i->print(c, level + c.delta_indent);
444 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
447 template <template <class T, class = std::allocator<T>> class C>
448 void container<C>::do_print_python(const print_python & c, unsigned level) const
450 printseq(c, '[', ',', ']', precedence(), precedence()+1);
453 template <template <class T, class = std::allocator<T>> class C>
454 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
457 printseq(c, '(', ',', ')', precedence(), precedence()+1);
460 template <template <class T, class = std::allocator<T>> class C>
461 ex container<C>::op(size_t i) const
463 GINAC_ASSERT(i < nops());
465 const_iterator it = this->seq.begin();
470 template <template <class T, class = std::allocator<T>> class C>
471 ex & container<C>::let_op(size_t i)
473 GINAC_ASSERT(i < nops());
475 ensure_if_modifiable();
476 typename STLT::iterator it = this->seq.begin();
481 template <template <class T, class = std::allocator<T>> class C>
482 ex container<C>::subs(const exmap & m, unsigned options) const
484 // After having subs'ed all children, this method subs'es one final
485 // level, but only if the intermediate result is a container! This is
486 // because if the intermediate result has eval'ed to a non-container a
487 // last level substitution would be wrong, as this example involving a
488 // function f and its inverse f^-1 shows:
489 // f(x).subs(x==f^-1(x))
490 // -> f(f^-1(x)) [subschildren]
491 // -> x [eval] /* must not subs(x==f^-1(x))! */
492 STLT subsed = subschildren(m, options);
493 if (!subsed.empty()) {
494 ex result(thiscontainer(subsed));
495 if (is_a<container<C>>(result))
496 return ex_to<basic>(result).subs_one_level(m, options);
500 if (is_a<container<C>>(*this))
501 return subs_one_level(m, options);
507 /** Compare two containers of the same type. */
508 template <template <class T, class = std::allocator<T>> class C>
509 int container<C>::compare_same_type(const basic & other) const
511 GINAC_ASSERT(is_a<container>(other));
512 const container & o = static_cast<const container &>(other);
514 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
515 it2 = o.seq.begin(), it2end = o.seq.end();
517 while (it1 != it1end && it2 != it2end) {
518 int cmpval = it1->compare(*it2);
524 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
527 template <template <class T, class = std::allocator<T>> class C>
528 bool container<C>::is_equal_same_type(const basic & other) const
530 GINAC_ASSERT(is_a<container>(other));
531 const container & o = static_cast<const container &>(other);
533 if (this->seq.size() != o.seq.size())
536 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
537 while (it1 != it1end) {
538 if (!it1->is_equal(*it2))
546 /** Add element at front. */
547 template <template <class T, class = std::allocator<T>> class C>
548 container<C> & container<C>::prepend(const ex & b)
550 ensure_if_modifiable();
551 this->seq.push_front(b);
555 /** Add element at back. */
556 template <template <class T, class = std::allocator<T>> class C>
557 container<C> & container<C>::append(const ex & b)
559 ensure_if_modifiable();
560 this->seq.push_back(b);
564 /** Remove first element. */
565 template <template <class T, class = std::allocator<T>> class C>
566 container<C> & container<C>::remove_first()
568 ensure_if_modifiable();
569 this->seq.pop_front();
573 /** Remove last element. */
574 template <template <class T, class = std::allocator<T>> class C>
575 container<C> & container<C>::remove_last()
577 ensure_if_modifiable();
578 this->seq.pop_back();
582 /** Remove all elements. */
583 template <template <class T, class = std::allocator<T>> class C>
584 container<C> & container<C>::remove_all()
586 ensure_if_modifiable();
591 /** Sort elements. */
592 template <template <class T, class = std::allocator<T>> class C>
593 container<C> & container<C>::sort()
595 ensure_if_modifiable();
596 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
600 /** Specialization of container::unique_() for std::list. */
601 template<> inline void container<std::list>::unique_()
603 this->seq.unique(ex_is_equal());
606 /** Remove adjacent duplicate elements. */
607 template <template <class T, class = std::allocator<T>> class C>
608 container<C> & container<C>::unique()
610 ensure_if_modifiable();
615 /** Print sequence of contained elements. */
616 template <template <class T, class = std::allocator<T>> class C>
617 void container<C>::printseq(const print_context & c, char openbracket, char delim,
618 char closebracket, unsigned this_precedence,
619 unsigned upper_precedence) const
621 if (this_precedence <= upper_precedence)
624 if (!this->seq.empty()) {
625 const_iterator it = this->seq.begin(), itend = this->seq.end();
627 while (it != itend) {
628 it->print(c, this_precedence);
632 it->print(c, this_precedence);
635 if (this_precedence <= upper_precedence)
639 template <template <class T, class = std::allocator<T>> class C>
640 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
642 // returns an empty container if nothing had to be substituted
643 // returns a STLT with substituted elements otherwise
645 const_iterator cit = this->seq.begin(), end = this->seq.end();
647 const ex & subsed_ex = cit->subs(m, options);
648 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
650 // copy first part of seq which hasn't changed
651 STLT s(this->seq.begin(), cit);
652 this->reserve(s, this->seq.size());
654 // insert changed element
655 s.push_back(subsed_ex);
660 s.push_back(cit->subs(m, options));
670 return STLT(); // nothing has changed
675 #endif // ndef GINAC_CONTAINER_H