3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2021 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 /** Wrapper template for making GiNaC classes out of STL containers. */
72 template <template <class T, class = std::allocator<T>> class C>
73 class container : public basic, public container_storage<C> {
74 GINAC_DECLARE_REGISTERED_CLASS(container, basic)
76 typedef typename container_storage<C>::STLT STLT;
79 typedef typename STLT::const_iterator const_iterator;
80 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
84 static unsigned get_default_flags() { return 0; }
85 static char get_open_delim() { return '('; }
86 static char get_close_delim() { return ')'; }
90 container(STLT const & s)
92 setflag(get_default_flags());
96 explicit container(STLT && v)
98 setflag(get_default_flags());
102 container(exvector::const_iterator b, exvector::const_iterator e)
103 : container_storage<C>(b, e)
105 setflag(get_default_flags());
108 container(std::initializer_list<ex> il)
109 : container_storage<C>(il)
111 setflag(get_default_flags());
114 // functions overriding virtual functions from base classes
116 bool info(unsigned inf) const override { return inherited::info(inf); }
117 unsigned precedence() const override { return 10; }
118 size_t nops() const override { return this->seq.size(); }
119 ex op(size_t i) const override;
120 ex & let_op(size_t i) override;
121 ex subs(const exmap & m, unsigned options = 0) const override;
123 void read_archive(const archive_node &n, lst &sym_lst) override
125 inherited::read_archive(n, sym_lst);
126 setflag(get_default_flags());
128 auto range = n.find_property_range("seq", "seq");
129 this->reserve(this->seq, range.end - range.begin);
130 for (archive_node::archive_node_cit i=range.begin; i<range.end; ++i) {
132 n.find_ex_by_loc(i, e, sym_lst);
133 this->seq.emplace_back(e);
137 /** Archive the object. */
138 void archive(archive_node &n) const override
140 inherited::archive(n);
141 for (auto & i : this->seq) {
147 ex conjugate() const override
149 STLT *newcont = nullptr;
150 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
152 newcont->push_back(i->conjugate());
155 ex x = i->conjugate();
156 if (are_ex_trivially_equal(x, *i)) {
160 this->reserve(*newcont, this->seq.size());
161 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
162 newcont->push_back(*j);
164 newcont->push_back(x);
167 ex result = thiscontainer(*newcont);
174 ex real_part() const override
177 this->reserve(cont, nops());
178 const_iterator b = begin();
179 const_iterator e = end();
180 for(const_iterator i=b; i!=e; ++i)
181 cont.push_back(i->real_part());
182 return thiscontainer(cont);
185 ex imag_part() const override
188 this->reserve(cont, nops());
189 const_iterator b = begin();
190 const_iterator e = end();
191 for(const_iterator i=b; i!=e; ++i)
192 cont.push_back(i->imag_part());
193 return thiscontainer(cont);
196 bool is_equal_same_type(const basic & other) const override;
198 // new virtual functions which can be overridden by derived classes
200 /** Similar to duplicate(), but with a preset sequence. Must be
201 * overridden by derived classes. */
202 virtual ex thiscontainer(const STLT & v) const { return container(v); }
204 /** Similar to duplicate(), but with a preset sequence (which gets
205 * pilfered). Must be overridden by derived classes. */
206 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
208 virtual void printseq(const print_context & c, char openbracket, char delim,
209 char closebracket, unsigned this_precedence,
210 unsigned upper_precedence = 0) const;
212 // non-virtual functions in this class
214 void sort_(std::random_access_iterator_tag)
216 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
219 void sort_(std::input_iterator_tag)
221 this->seq.sort(ex_is_less());
226 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
227 this->seq.erase(p, this->seq.end());
231 container & prepend(const ex & b);
232 container & append(const ex & b);
233 container & remove_first();
234 container & remove_last();
235 container & remove_all();
237 container & unique();
239 const_iterator begin() const {return this->seq.begin();}
240 const_iterator end() const {return this->seq.end();}
241 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
242 const_reverse_iterator rend() const {return this->seq.rend();}
245 void do_print(const print_context & c, unsigned level) const;
246 void do_print_tree(const print_tree & c, unsigned level) const;
247 void do_print_python(const print_python & c, unsigned level) const;
248 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
249 STLT subschildren(const exmap & m, unsigned options = 0) const;
252 /** Default constructor */
253 template <template <class T, class = std::allocator<T>> class C>
254 container<C>::container()
256 setflag(get_default_flags());
259 template <template <class T, class = std::allocator<T>> class C>
260 void container<C>::do_print(const print_context & c, unsigned level) const
262 // always print brackets around seq, ignore upper_precedence
263 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
266 template <template <class T, class = std::allocator<T>> class C>
267 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
269 c.s << std::string(level, ' ') << class_name() << " @" << this
270 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
271 << ", nops=" << nops()
273 const_iterator i = this->seq.begin(), end = this->seq.end();
275 i->print(c, level + c.delta_indent);
278 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
281 template <template <class T, class = std::allocator<T>> class C>
282 void container<C>::do_print_python(const print_python & c, unsigned level) const
284 printseq(c, '[', ',', ']', precedence(), precedence()+1);
287 template <template <class T, class = std::allocator<T>> class C>
288 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
291 printseq(c, '(', ',', ')', precedence(), precedence()+1);
294 template <template <class T, class = std::allocator<T>> class C>
295 ex container<C>::op(size_t i) const
297 GINAC_ASSERT(i < nops());
299 const_iterator it = this->seq.begin();
304 template <template <class T, class = std::allocator<T>> class C>
305 ex & container<C>::let_op(size_t i)
307 GINAC_ASSERT(i < nops());
309 ensure_if_modifiable();
310 typename STLT::iterator it = this->seq.begin();
315 template <template <class T, class = std::allocator<T>> class C>
316 ex container<C>::subs(const exmap & m, unsigned options) const
318 // After having subs'ed all children, this method subs'es one final
319 // level, but only if the intermediate result is a container! This is
320 // because if the intermediate result has eval'ed to a non-container a
321 // last level substitution would be wrong, as this example involving a
322 // function f and its inverse f^-1 shows:
323 // f(x).subs(x==f^-1(x))
324 // -> f(f^-1(x)) [subschildren]
325 // -> x [eval] /* must not subs(x==f^-1(x))! */
326 STLT subsed = subschildren(m, options);
327 if (!subsed.empty()) {
328 ex result(thiscontainer(subsed));
329 if (is_a<container<C>>(result))
330 return ex_to<basic>(result).subs_one_level(m, options);
334 if (is_a<container<C>>(*this))
335 return subs_one_level(m, options);
341 /** Compare two containers of the same type. */
342 template <template <class T, class = std::allocator<T>> class C>
343 int container<C>::compare_same_type(const basic & other) const
345 GINAC_ASSERT(is_a<container>(other));
346 const container & o = static_cast<const container &>(other);
348 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
349 it2 = o.seq.begin(), it2end = o.seq.end();
351 while (it1 != it1end && it2 != it2end) {
352 int cmpval = it1->compare(*it2);
358 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
361 template <template <class T, class = std::allocator<T>> class C>
362 bool container<C>::is_equal_same_type(const basic & other) const
364 GINAC_ASSERT(is_a<container>(other));
365 const container & o = static_cast<const container &>(other);
367 if (this->seq.size() != o.seq.size())
370 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
371 while (it1 != it1end) {
372 if (!it1->is_equal(*it2))
380 /** Add element at front. */
381 template <template <class T, class = std::allocator<T>> class C>
382 container<C> & container<C>::prepend(const ex & b)
384 ensure_if_modifiable();
385 this->seq.push_front(b);
389 /** Add element at back. */
390 template <template <class T, class = std::allocator<T>> class C>
391 container<C> & container<C>::append(const ex & b)
393 ensure_if_modifiable();
394 this->seq.push_back(b);
398 /** Remove first element. */
399 template <template <class T, class = std::allocator<T>> class C>
400 container<C> & container<C>::remove_first()
402 ensure_if_modifiable();
403 this->seq.pop_front();
407 /** Remove last element. */
408 template <template <class T, class = std::allocator<T>> class C>
409 container<C> & container<C>::remove_last()
411 ensure_if_modifiable();
412 this->seq.pop_back();
416 /** Remove all elements. */
417 template <template <class T, class = std::allocator<T>> class C>
418 container<C> & container<C>::remove_all()
420 ensure_if_modifiable();
425 /** Sort elements. */
426 template <template <class T, class = std::allocator<T>> class C>
427 container<C> & container<C>::sort()
429 ensure_if_modifiable();
430 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
434 /** Specialization of container::unique_() for std::list. */
435 template<> inline void container<std::list>::unique_()
437 this->seq.unique(ex_is_equal());
440 /** Remove adjacent duplicate elements. */
441 template <template <class T, class = std::allocator<T>> class C>
442 container<C> & container<C>::unique()
444 ensure_if_modifiable();
449 /** Print sequence of contained elements. */
450 template <template <class T, class = std::allocator<T>> class C>
451 void container<C>::printseq(const print_context & c, char openbracket, char delim,
452 char closebracket, unsigned this_precedence,
453 unsigned upper_precedence) const
455 if (this_precedence <= upper_precedence)
458 if (!this->seq.empty()) {
459 const_iterator it = this->seq.begin(), itend = this->seq.end();
461 while (it != itend) {
462 it->print(c, this_precedence);
466 it->print(c, this_precedence);
469 if (this_precedence <= upper_precedence)
473 template <template <class T, class = std::allocator<T>> class C>
474 typename container<C>::STLT container<C>::subschildren(const exmap & m, unsigned options) const
476 // returns an empty container if nothing had to be substituted
477 // returns a STLT with substituted elements otherwise
479 const_iterator cit = this->seq.begin(), end = this->seq.end();
481 const ex & subsed_ex = cit->subs(m, options);
482 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
484 // copy first part of seq which hasn't changed
485 STLT s(this->seq.begin(), cit);
486 this->reserve(s, this->seq.size());
488 // insert changed element
489 s.push_back(subsed_ex);
494 s.push_back(cit->subs(m, options));
504 return STLT(); // nothing has changed
509 #endif // ndef GINAC_CONTAINER_H