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"
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) deprecated;
172 container(const ex & p1, const ex & p2) deprecated;
173 container(const ex & p1, const ex & p2, const ex & p3) deprecated;
174 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4) deprecated;
175 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5) deprecated;
176 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6) deprecated;
177 container(const ex & p1, const ex & p2, const ex & p3, const ex & p4, const ex & p5, const ex & p6, const ex & p7) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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 eval(int level = 0) const override;
209 ex subs(const exmap & m, unsigned options = 0) const override;
211 void read_archive(const archive_node &n, lst &sym_lst) override
213 inherited::read_archive(n, sym_lst);
214 setflag(get_default_flags());
216 archive_node::archive_node_cit first = n.find_first("seq");
217 archive_node::archive_node_cit last = n.find_last("seq");
219 this->reserve(this->seq, last - first);
220 for (archive_node::archive_node_cit i=first; i<last; ++i) {
222 n.find_ex_by_loc(i, e, sym_lst);
223 this->seq.push_back(e);
227 /** Archive the object. */
228 void archive(archive_node &n) const override
230 inherited::archive(n);
231 for (auto & i : this->seq) {
237 ex conjugate() const override
239 STLT *newcont = nullptr;
240 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
242 newcont->push_back(i->conjugate());
245 ex x = i->conjugate();
246 if (are_ex_trivially_equal(x, *i)) {
250 this->reserve(*newcont, this->seq.size());
251 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
252 newcont->push_back(*j);
254 newcont->push_back(x);
257 ex result = thiscontainer(*newcont);
264 ex real_part() const override
267 this->reserve(cont, nops());
268 const_iterator b = begin();
269 const_iterator e = end();
270 for(const_iterator i=b; i!=e; ++i)
271 cont.push_back(i->real_part());
272 return thiscontainer(cont);
275 ex imag_part() const override
278 this->reserve(cont, nops());
279 const_iterator b = begin();
280 const_iterator e = end();
281 for(const_iterator i=b; i!=e; ++i)
282 cont.push_back(i->imag_part());
283 return thiscontainer(cont);
286 bool is_equal_same_type(const basic & other) const override;
288 // new virtual functions which can be overridden by derived classes
290 /** Similar to duplicate(), but with a preset sequence. Must be
291 * overridden by derived classes. */
292 virtual ex thiscontainer(const STLT & v) const { return container(v); }
294 /** Similar to duplicate(), but with a preset sequence (which gets
295 * pilfered). Must be overridden by derived classes. */
296 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
298 virtual void printseq(const print_context & c, char openbracket, char delim,
299 char closebracket, unsigned this_precedence,
300 unsigned upper_precedence = 0) const;
302 // non-virtual functions in this class
304 void sort_(std::random_access_iterator_tag)
306 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
309 void sort_(std::input_iterator_tag)
311 this->seq.sort(ex_is_less());
316 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
317 this->seq.erase(p, this->seq.end());
321 container & prepend(const ex & b);
322 container & append(const ex & b);
323 container & remove_first();
324 container & remove_last();
325 container & remove_all();
327 container & unique();
329 const_iterator begin() const {return this->seq.begin();}
330 const_iterator end() const {return this->seq.end();}
331 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
332 const_reverse_iterator rend() const {return this->seq.rend();}
335 void do_print(const print_context & c, unsigned level) const;
336 void do_print_tree(const print_tree & c, unsigned level) const;
337 void do_print_python(const print_python & c, unsigned level) const;
338 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
339 STLT evalchildren(int level) const;
340 STLT subschildren(const exmap & m, unsigned options = 0) const;
343 /** Default constructor */
344 template <template <class T, class = std::allocator<T>> class C>
345 container<C>::container()
347 setflag(get_default_flags());
350 /** Deprecatd constructors (prefer initializer list) */
351 template <template <class T, class = std::allocator<T>> class C>
352 container<C>::container(const ex & p1)
353 : container_storage<C>(1, p1) { 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)
357 : container_storage<C>{p1, p2} { 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)
361 : container_storage<C>{p1, p2, p3} { 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)
365 : container_storage<C>{p1, p2, p3, p4} { 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)
369 : container_storage<C>{p1, p2, p3, p4, p5} { 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)
373 : container_storage<C>{p1, p2, p3, p4, p5, p6} { 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)
377 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7} { 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)
381 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8} { setflag(get_default_flags()); }
383 template <template <class T, class = std::allocator<T>> class C>
384 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 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9} { setflag(get_default_flags()); }
388 template <template <class T, class = std::allocator<T>> class C>
389 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,
390 const ex & p9, const ex & p10)
391 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10} { setflag(get_default_flags()); }
393 template <template <class T, class = std::allocator<T>> class C>
394 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,
395 const ex & p9, const ex & p10, const ex & p11)
396 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11} { setflag(get_default_flags()); }
398 template <template <class T, class = std::allocator<T>> class C>
399 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,
400 const ex & p9, const ex & p10, const ex & p11, const ex & p12)
401 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12} { setflag(get_default_flags()); }
403 template <template <class T, class = std::allocator<T>> class C>
404 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,
405 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13)
406 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13} { setflag(get_default_flags()); }
408 template <template <class T, class = std::allocator<T>> class C>
409 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,
410 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14)
411 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14} { 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)
416 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15} { setflag(get_default_flags()); }
417 template <template <class T, class = std::allocator<T>> class C>
418 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,
419 const ex & p9, const ex & p10, const ex & p11, const ex & p12, const ex & p13, const ex & p14, const ex & p15, const ex & p16)
420 : container_storage<C>{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16} { setflag(get_default_flags()); }
422 template <template <class T, class = std::allocator<T>> class C>
423 container_init<ex, typename container_storage<C>::STLT> container<C>::operator=(const ex & x)
425 this->seq.push_back(x);
426 return container_init<ex, typename container_storage<C>::STLT>(this->seq);
429 template <template <class T, class = std::allocator<T>> class C>
430 void container<C>::do_print(const print_context & c, unsigned level) const
432 // always print brackets around seq, ignore upper_precedence
433 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
436 template <template <class T, class = std::allocator<T>> class C>
437 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
439 c.s << std::string(level, ' ') << class_name() << " @" << this
440 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
441 << ", nops=" << nops()
443 const_iterator i = this->seq.begin(), end = this->seq.end();
445 i->print(c, level + c.delta_indent);
448 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
451 template <template <class T, class = std::allocator<T>> class C>
452 void container<C>::do_print_python(const print_python & c, unsigned level) const
454 printseq(c, '[', ',', ']', precedence(), precedence()+1);
457 template <template <class T, class = std::allocator<T>> class C>
458 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
461 printseq(c, '(', ',', ')', precedence(), precedence()+1);
464 template <template <class T, class = std::allocator<T>> class C>
465 ex container<C>::op(size_t i) const
467 GINAC_ASSERT(i < nops());
469 const_iterator it = this->seq.begin();
474 template <template <class T, class = std::allocator<T>> class C>
475 ex & container<C>::let_op(size_t i)
477 GINAC_ASSERT(i < nops());
479 ensure_if_modifiable();
480 typename STLT::iterator it = this->seq.begin();
485 template <template <class T, class = std::allocator<T>> class C>
486 ex container<C>::eval(int level) const
491 return thiscontainer(evalchildren(level));
494 template <template <class T, class = std::allocator<T>> class C>
495 ex container<C>::subs(const exmap & m, unsigned options) const
497 // After having subs'ed all children, this method subs'es one final
498 // level, but only if the intermediate result is a container! This is
499 // because if the intermediate result has eval'ed to a non-container a
500 // last level substitution would be wrong, as this example involving a
501 // function f and its inverse f^-1 shows:
502 // f(x).subs(x==f^-1(x))
503 // -> f(f^-1(x)) [subschildren]
504 // -> x [eval] /* must not subs(x==f^-1(x))! */
505 STLT subsed = subschildren(m, options);
506 if (!subsed.empty()) {
507 ex result(thiscontainer(subsed));
508 if (is_a<container<C>>(result))
509 return ex_to<basic>(result).subs_one_level(m, options);
513 if (is_a<container<C>>(*this))
514 return subs_one_level(m, options);
520 /** Compare two containers of the same type. */
521 template <template <class T, class = std::allocator<T>> class C>
522 int container<C>::compare_same_type(const basic & other) const
524 GINAC_ASSERT(is_a<container>(other));
525 const container & o = static_cast<const container &>(other);
527 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
528 it2 = o.seq.begin(), it2end = o.seq.end();
530 while (it1 != it1end && it2 != it2end) {
531 int cmpval = it1->compare(*it2);
537 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
540 template <template <class T, class = std::allocator<T>> class C>
541 bool container<C>::is_equal_same_type(const basic & other) const
543 GINAC_ASSERT(is_a<container>(other));
544 const container & o = static_cast<const container &>(other);
546 if (this->seq.size() != o.seq.size())
549 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
550 while (it1 != it1end) {
551 if (!it1->is_equal(*it2))
559 /** Add element at front. */
560 template <template <class T, class = std::allocator<T>> class C>
561 container<C> & container<C>::prepend(const ex & b)
563 ensure_if_modifiable();
564 this->seq.push_front(b);
568 /** Add element at back. */
569 template <template <class T, class = std::allocator<T>> class C>
570 container<C> & container<C>::append(const ex & b)
572 ensure_if_modifiable();
573 this->seq.push_back(b);
577 /** Remove first element. */
578 template <template <class T, class = std::allocator<T>> class C>
579 container<C> & container<C>::remove_first()
581 ensure_if_modifiable();
582 this->seq.pop_front();
586 /** Remove last element. */
587 template <template <class T, class = std::allocator<T>> class C>
588 container<C> & container<C>::remove_last()
590 ensure_if_modifiable();
591 this->seq.pop_back();
595 /** Remove all elements. */
596 template <template <class T, class = std::allocator<T>> class C>
597 container<C> & container<C>::remove_all()
599 ensure_if_modifiable();
604 /** Sort elements. */
605 template <template <class T, class = std::allocator<T>> class C>
606 container<C> & container<C>::sort()
608 ensure_if_modifiable();
609 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
613 /** Specialization of container::unique_() for std::list. */
614 template<> inline void container<std::list>::unique_()
616 this->seq.unique(ex_is_equal());
619 /** Remove adjacent duplicate elements. */
620 template <template <class T, class = std::allocator<T>> class C>
621 container<C> & container<C>::unique()
623 ensure_if_modifiable();
628 /** Print sequence of contained elements. */
629 template <template <class T, class = std::allocator<T>> class C>
630 void container<C>::printseq(const print_context & c, char openbracket, char delim,
631 char closebracket, unsigned this_precedence,
632 unsigned upper_precedence) const
634 if (this_precedence <= upper_precedence)
637 if (!this->seq.empty()) {
638 const_iterator it = this->seq.begin(), itend = this->seq.end();
640 while (it != itend) {
641 it->print(c, this_precedence);
645 it->print(c, this_precedence);
648 if (this_precedence <= upper_precedence)
652 template <template <class T, class = std::allocator<T>> class C>
653 typename container<C>::STLT container<C>::evalchildren(int level) const
657 else if (level == -max_recursion_level)
658 throw std::runtime_error("max recursion level reached");
661 this->reserve(s, this->seq.size());
664 const_iterator it = this->seq.begin(), itend = this->seq.end();
665 while (it != itend) {
666 s.push_back(it->eval(level));
673 template <template <class T, class = std::allocator<T>> class C>
674 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
676 // returns an empty container if nothing had to be substituted
677 // returns a STLT with substituted elements otherwise
679 const_iterator cit = this->seq.begin(), end = this->seq.end();
681 const ex & subsed_ex = cit->subs(m, options);
682 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
684 // copy first part of seq which hasn't changed
685 STLT s(this->seq.begin(), cit);
686 this->reserve(s, this->seq.size());
688 // insert changed element
689 s.push_back(subsed_ex);
694 s.push_back(cit->subs(m, options));
704 return STLT(); // nothing has changed
709 #endif // ndef GINAC_CONTAINER_H