GiNaC 1.8.10
container.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 Johannes Gutenberg University Mainz, Germany
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef GINAC_CONTAINER_H
23#define GINAC_CONTAINER_H
24
25#include "ex.h"
26#include "print.h"
27#include "archive.h"
28#include "assertion.h"
29#include "compiler.h"
30
31#include <algorithm>
32#include <iterator>
33#include <list>
34#include <memory>
35#include <stdexcept>
36#include <vector>
37
38namespace GiNaC {
39
41template <template <class T, class = std::allocator<T>> class C>
43protected:
44 typedef C<ex> STLT;
45
47 container_storage(size_t n, const ex & e) : seq(n, e) {}
48 container_storage(std::initializer_list<ex> il) : seq(il) {}
49
50 template <class In>
51 container_storage(In b, In e) : seq(b, e) {}
52
53 void reserve(size_t) {}
54 static void reserve(STLT &, size_t) {}
55
57
58 // disallow destruction of container through a container_storage*
59protected:
61};
62
63template <>
64inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
65
66template <>
67inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
68
69
71template <template <class T, class = std::allocator<T>> class C>
72class container : public basic, public container_storage<C> {
74protected:
76
77public:
78 typedef typename STLT::const_iterator const_iterator;
79 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
80
81protected:
82 // helpers
83 static unsigned get_default_flags() { return 0; }
84 static char get_open_delim() { return '('; }
85 static char get_close_delim() { return ')'; }
86
87 // constructors
88public:
89 container(STLT const & s)
90 {
92 this->seq = s;
93 }
94
95 explicit container(STLT && v)
96 {
98 this->seq.swap(v);
99 }
100
101 container(exvector::const_iterator b, exvector::const_iterator e)
102 : container_storage<C>(b, e)
103 {
105 }
106
107 container(std::initializer_list<ex> il)
108 : container_storage<C>(il)
109 {
111 }
112
113 // functions overriding virtual functions from base classes
114public:
115 bool info(unsigned inf) const override { return inherited::info(inf); }
116 unsigned precedence() const override { return 10; }
117 size_t nops() const override { return this->seq.size(); }
118 ex op(size_t i) const override;
119 ex & let_op(size_t i) override;
120 ex subs(const exmap & m, unsigned options = 0) const override;
121
122 void read_archive(const archive_node &n, lst &sym_lst) override
123 {
124 inherited::read_archive(n, sym_lst);
126
127 auto range = n.find_property_range("seq", "seq");
128 this->reserve(this->seq, range.end - range.begin);
129 for (archive_node::archive_node_cit i=range.begin; i<range.end; ++i) {
130 ex e;
131 n.find_ex_by_loc(i, e, sym_lst);
132 this->seq.emplace_back(e);
133 }
134 }
135
137 void archive(archive_node &n) const override
138 {
139 inherited::archive(n);
140 for (auto & i : this->seq) {
141 n.add_ex("seq", i);
142 }
143 }
144
145protected:
146 ex conjugate() const override
147 {
148 STLT *newcont = nullptr;
149 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
150 if (newcont) {
151 newcont->push_back(i->conjugate());
152 continue;
153 }
154 ex x = i->conjugate();
155 if (are_ex_trivially_equal(x, *i)) {
156 continue;
157 }
158 newcont = new STLT;
159 this->reserve(*newcont, this->seq.size());
160 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
161 newcont->push_back(*j);
162 }
163 newcont->push_back(x);
164 }
165 if (newcont) {
166 ex result = thiscontainer(*newcont);
167 delete newcont;
168 return result;
169 }
170 return *this;
171 }
172
173 ex real_part() const override
174 {
175 STLT cont;
176 this->reserve(cont, nops());
177 const_iterator b = begin();
178 const_iterator e = end();
179 for(const_iterator i=b; i!=e; ++i)
180 cont.push_back(i->real_part());
181 return thiscontainer(cont);
182 }
183
184 ex imag_part() const override
185 {
186 STLT cont;
187 this->reserve(cont, nops());
188 const_iterator b = begin();
189 const_iterator e = end();
190 for(const_iterator i=b; i!=e; ++i)
191 cont.push_back(i->imag_part());
192 return thiscontainer(cont);
193 }
194
195 bool is_equal_same_type(const basic & other) const override;
196
197 // new virtual functions which can be overridden by derived classes
198protected:
201 virtual ex thiscontainer(const STLT & v) const { return container(v); }
202
205 virtual ex thiscontainer(STLT && v) const { return container(std::move(v)); }
206
207 virtual void printseq(const print_context & c, char openbracket, char delim,
208 char closebracket, unsigned this_precedence,
209 unsigned upper_precedence = 0) const;
210
211 // non-virtual functions in this class
212private:
213 void sort_(std::random_access_iterator_tag)
214 {
215 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
216 }
217
218 void sort_(std::input_iterator_tag)
219 {
220 this->seq.sort(ex_is_less());
221 }
222
223 void unique_()
224 {
225 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
226 this->seq.erase(p, this->seq.end());
227 }
228
229public:
230 container & prepend(const ex & b);
231 container & append(const ex & b);
235 container & sort();
236 container & unique();
237
238 const_iterator begin() const {return this->seq.begin();}
239 const_iterator end() const {return this->seq.end();}
240 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
241 const_reverse_iterator rend() const {return this->seq.rend();}
242
243protected:
244 void do_print(const print_context & c, unsigned level) const;
245 void do_print_tree(const print_tree & c, unsigned level) const;
246 void do_print_python(const print_python & c, unsigned level) const;
247 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
248 STLT subschildren(const exmap & m, unsigned options = 0) const;
249};
250
252template <template <class T, class = std::allocator<T>> class C>
254{
255 setflag(get_default_flags());
256}
257
258template <template <class T, class = std::allocator<T>> class C>
259void container<C>::do_print(const print_context & c, unsigned level) const
260{
261 // always print brackets around seq, ignore upper_precedence
262 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
263}
264
265template <template <class T, class = std::allocator<T>> class C>
266void container<C>::do_print_tree(const print_tree & c, unsigned level) const
267{
268 c.s << std::string(level, ' ') << class_name() << " @" << this
269 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
270 << ", nops=" << nops()
271 << std::endl;
272 const_iterator i = this->seq.begin(), end = this->seq.end();
273 while (i != end) {
274 i->print(c, level + c.delta_indent);
275 ++i;
276 }
277 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
278}
279
280template <template <class T, class = std::allocator<T>> class C>
281void container<C>::do_print_python(const print_python & c, unsigned level) const
282{
283 printseq(c, '[', ',', ']', precedence(), precedence()+1);
284}
285
286template <template <class T, class = std::allocator<T>> class C>
287void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
288{
289 c.s << class_name();
290 printseq(c, '(', ',', ')', precedence(), precedence()+1);
291}
292
293template <template <class T, class = std::allocator<T>> class C>
294ex container<C>::op(size_t i) const
295{
296 GINAC_ASSERT(i < nops());
297
298 const_iterator it = this->seq.begin();
299 advance(it, i);
300 return *it;
301}
302
303template <template <class T, class = std::allocator<T>> class C>
305{
306 GINAC_ASSERT(i < nops());
307
308 ensure_if_modifiable();
309 typename STLT::iterator it = this->seq.begin();
310 advance(it, i);
311 return *it;
312}
313
314template <template <class T, class = std::allocator<T>> class C>
315ex container<C>::subs(const exmap & m, unsigned options) const
316{
317 // After having subs'ed all children, this method subs'es one final
318 // level, but only if the intermediate result is a container! This is
319 // because if the intermediate result has eval'ed to a non-container a
320 // last level substitution would be wrong, as this example involving a
321 // function f and its inverse f^-1 shows:
322 // f(x).subs(x==f^-1(x))
323 // -> f(f^-1(x)) [subschildren]
324 // -> x [eval] /* must not subs(x==f^-1(x))! */
325 STLT subsed = subschildren(m, options);
326 if (!subsed.empty()) {
327 ex result(thiscontainer(subsed));
328 if (is_a<container<C>>(result))
329 return ex_to<basic>(result).subs_one_level(m, options);
330 else
331 return result;
332 } else {
333 if (is_a<container<C>>(*this))
334 return subs_one_level(m, options);
335 else
336 return *this;
337 }
338}
339
341template <template <class T, class = std::allocator<T>> class C>
342int container<C>::compare_same_type(const basic & other) const
343{
344 GINAC_ASSERT(is_a<container>(other));
345 const container & o = static_cast<const container &>(other);
346
347 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
348 it2 = o.seq.begin(), it2end = o.seq.end();
349
350 while (it1 != it1end && it2 != it2end) {
351 int cmpval = it1->compare(*it2);
352 if (cmpval)
353 return cmpval;
354 ++it1; ++it2;
355 }
356
357 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
358}
359
360template <template <class T, class = std::allocator<T>> class C>
362{
363 GINAC_ASSERT(is_a<container>(other));
364 const container & o = static_cast<const container &>(other);
365
366 if (this->seq.size() != o.seq.size())
367 return false;
368
369 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
370 while (it1 != it1end) {
371 if (!it1->is_equal(*it2))
372 return false;
373 ++it1; ++it2;
374 }
375
376 return true;
377}
378
380template <template <class T, class = std::allocator<T>> class C>
382{
383 ensure_if_modifiable();
384 this->seq.push_front(b);
385 return *this;
386}
387
389template <template <class T, class = std::allocator<T>> class C>
391{
392 ensure_if_modifiable();
393 this->seq.push_back(b);
394 return *this;
395}
396
398template <template <class T, class = std::allocator<T>> class C>
400{
401 ensure_if_modifiable();
402 this->seq.pop_front();
403 return *this;
404}
405
407template <template <class T, class = std::allocator<T>> class C>
409{
410 ensure_if_modifiable();
411 this->seq.pop_back();
412 return *this;
413}
414
416template <template <class T, class = std::allocator<T>> class C>
418{
419 ensure_if_modifiable();
420 this->seq.clear();
421 return *this;
422}
423
425template <template <class T, class = std::allocator<T>> class C>
427{
428 ensure_if_modifiable();
429 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
430 return *this;
431}
432
434template<> inline void container<std::list>::unique_()
435{
436 this->seq.unique(ex_is_equal());
437}
438
440template <template <class T, class = std::allocator<T>> class C>
442{
443 ensure_if_modifiable();
444 unique_();
445 return *this;
446}
447
449template <template <class T, class = std::allocator<T>> class C>
450void container<C>::printseq(const print_context & c, char openbracket, char delim,
451 char closebracket, unsigned this_precedence,
452 unsigned upper_precedence) const
453{
454 if (this_precedence <= upper_precedence)
455 c.s << openbracket;
456
457 if (!this->seq.empty()) {
458 const_iterator it = this->seq.begin(), itend = this->seq.end();
459 --itend;
460 while (it != itend) {
461 it->print(c, this_precedence);
462 c.s << delim;
463 ++it;
464 }
465 it->print(c, this_precedence);
466 }
467
468 if (this_precedence <= upper_precedence)
469 c.s << closebracket;
470}
471
472template <template <class T, class = std::allocator<T>> class C>
474{
475 // returns an empty container if nothing had to be substituted
476 // returns a STLT with substituted elements otherwise
477
478 const_iterator cit = this->seq.begin(), end = this->seq.end();
479 while (cit != end) {
480 const ex & subsed_ex = cit->subs(m, options);
481 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
482
483 // copy first part of seq which hasn't changed
484 STLT s(this->seq.begin(), cit);
485 this->reserve(s, this->seq.size());
486
487 // insert changed element
488 s.push_back(subsed_ex);
489 ++cit;
490
491 // copy rest
492 while (cit != end) {
493 s.push_back(cit->subs(m, options));
494 ++cit;
495 }
496
497 return s;
498 }
499
500 ++cit;
501 }
502
503 return STLT(); // nothing has changed
504}
505
506} // namespace GiNaC
507
508#endif // ndef GINAC_CONTAINER_H
Archiving of GiNaC expressions.
Assertion macro definition.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
std::vector< property >::const_iterator archive_node_cit
Definition archive.h:83
void add_ex(const std::string &name, const ex &value)
Add property of type "ex" to node.
Definition archive.cpp:408
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
const basic & setflag(unsigned f) const
Set some status_flags.
Definition basic.h:287
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:718
Helper template for encapsulating the reserve() mechanics of STL containers.
Definition container.h:42
container_storage(In b, In e)
Definition container.h:51
static void reserve(STLT &, size_t)
Definition container.h:54
container_storage(std::initializer_list< ex > il)
Definition container.h:48
container_storage(size_t n, const ex &e)
Definition container.h:47
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
const_reverse_iterator rend() const
Definition container.h:241
container(exvector::const_iterator b, exvector::const_iterator e)
Definition container.h:101
static unsigned get_default_flags()
Specialization of container::get_default_flags() for lst.
Definition container.h:83
STLT subschildren(const exmap &m, unsigned options=0) const
Definition container.h:473
container(std::initializer_list< ex > il)
Definition container.h:107
ex real_part() const override
Definition container.h:173
const_reverse_iterator rbegin() const
Definition container.h:240
bool is_equal_same_type(const basic &other) const override
Returns true if two objects of same type are equal.
Definition container.h:361
virtual void printseq(const print_context &c, char openbracket, char delim, char closebracket, unsigned this_precedence, unsigned upper_precedence=0) const
Print sequence of contained elements.
Definition container.h:450
const_iterator end() const
Definition container.h:239
container & prepend(const ex &b)
Add element at front.
Definition container.h:381
STLT::const_iterator const_iterator
Definition container.h:78
ex conjugate() const override
Definition container.h:146
void read_archive(const archive_node &n, lst &sym_lst) override
Load (deserialize) the object from an archive node.
Definition container.h:122
const_iterator begin() const
Definition container.h:238
void archive(archive_node &n) const override
Archive the object.
Definition container.h:137
container(STLT const &s)
Definition container.h:89
bool info(unsigned inf) const override
Information about the object.
Definition container.h:115
size_t nops() const override
Number of operands/members.
Definition container.h:117
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition container.h:287
container & remove_last()
Remove last element.
Definition container.h:408
virtual ex thiscontainer(STLT &&v) const
Similar to duplicate(), but with a preset sequence (which gets pilfered).
Definition container.h:205
STLT::const_reverse_iterator const_reverse_iterator
Definition container.h:79
void sort_(std::random_access_iterator_tag)
Definition container.h:213
container_storage< C >::STLT STLT
Definition container.h:75
ex op(size_t i) const override
Return operand/member at position i.
Definition container.h:294
container & sort()
Sort elements.
Definition container.h:426
void sort_(std::input_iterator_tag)
Definition container.h:218
unsigned precedence() const override
Return relative operator precedence (for parenthezing output).
Definition container.h:116
container & unique()
Remove adjacent duplicate elements.
Definition container.h:441
void do_print(const print_context &c, unsigned level) const
Definition container.h:259
container & remove_all()
Remove all elements.
Definition container.h:417
ex subs(const exmap &m, unsigned options=0) const override
Substitute a set of objects by arbitrary expressions.
Definition container.h:315
ex & let_op(size_t i) override
Return modifiable operand/member at position i.
Definition container.h:304
static char get_close_delim()
Specialization of container::get_close_delim() for lst.
Definition container.h:85
container & remove_first()
Remove first element.
Definition container.h:399
virtual ex thiscontainer(const STLT &v) const
Similar to duplicate(), but with a preset sequence.
Definition container.h:201
void do_print_tree(const print_tree &c, unsigned level) const
Definition container.h:266
static char get_open_delim()
Specialization of container::get_open_delim() for lst.
Definition container.h:84
container & append(const ex &b)
Add element at back.
Definition container.h:390
container(STLT &&v)
Definition container.h:95
void do_print_python(const print_python &c, unsigned level) const
Definition container.h:281
ex imag_part() const override
Definition container.h:184
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
ex conjugate() const
Definition ex.h:146
ex subs(const exmap &m, unsigned options=0) const
Definition ex.h:841
Base class for print_contexts.
Definition print.h:101
Context for python-parsable output.
Definition print.h:137
Context for python pretty-print output.
Definition print.h:129
Context for tree-like output for debugging.
Definition print.h:145
Definition of optimizing macros.
Interface to GiNaC's light-weight expression handles.
unsigned options
Definition factor.cpp:2473
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
ex x
Definition factor.cpp:1609
mvec m
Definition factor.cpp:757
ex cont
Definition factor.cpp:2190
Definition add.cpp:35
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:49
bool are_ex_trivially_equal(const ex &e1, const ex &e2)
Compare two objects of class quickly without doing a deep tree traversal.
Definition ex.h:699
bool is_a(const basic &obj)
Check if obj is a T, including base classes.
Definition basic.h:312
size_t nops(const ex &thisex)
Definition ex.h:727
Definition of helper classes for expression output.
#define GINAC_DECLARE_REGISTERED_CLASS(classname, supername)
Macro for inclusion in the declaration of each registered class.
Definition registrar.h:151

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.