3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GINAC_CONTAINER_H__
24 #define __GINAC_CONTAINER_H__
35 #include "assertion.h"
40 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
41 template <template <class> class C>
42 class container_storage {
46 container_storage() {}
47 container_storage(size_t n, const ex & e) : seq(n, e) {}
50 container_storage(In b, In e) : seq(b, e) {}
52 void reserve(size_t) {}
53 static void reserve(STLT &, size_t) {}
57 // disallow destruction of container through a container_storage*
59 ~container_storage() {}
63 inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
66 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
69 /** Wrapper template for making GiNaC classes out of STL containers. */
70 template <template <class> class C>
71 class container : public basic, public container_storage<C> {
72 GINAC_DECLARE_REGISTERED_CLASS(container, basic)
74 typedef typename container_storage<C>::STLT STLT;
77 typedef typename STLT::const_iterator const_iterator;
78 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
82 static unsigned get_tinfo() { return TINFO_fail; }
83 static char get_open_delim() { return '('; }
84 static char get_close_delim() { return ')'; }
88 container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
91 this->seq.swap(const_cast<STLT &>(s));
96 explicit container(STLT * vp) : inherited(get_tinfo())
99 // lst(0) ends up here
100 this->seq.push_back(0);
107 container(exvector::const_iterator b, exvector::const_iterator e)
108 : inherited(get_tinfo()), container_storage<C>(b, e) {}
110 explicit container(const ex & p1)
111 : inherited(get_tinfo()), container_storage<C>(1, p1) {}
113 container(const ex & p1, const ex & p2) : inherited(get_tinfo())
115 reserve(this->seq, 2);
116 this->seq.push_back(p1); this->seq.push_back(p2);
119 container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
121 reserve(this->seq, 3);
122 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
125 container(const ex & p1, const ex & p2, const ex & p3,
126 const ex & p4) : inherited(get_tinfo())
128 reserve(this->seq, 4);
129 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
130 this->seq.push_back(p4);
133 container(const ex & p1, const ex & p2, const ex & p3,
134 const ex & p4, const ex & p5) : inherited(get_tinfo())
136 reserve(this->seq, 5);
137 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
138 this->seq.push_back(p4); this->seq.push_back(p5);
141 container(const ex & p1, const ex & p2, const ex & p3,
142 const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
144 reserve(this->seq, 6);
145 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
146 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
149 container(const ex & p1, const ex & p2, const ex & p3,
150 const ex & p4, const ex & p5, const ex & p6,
151 const ex & p7) : inherited(get_tinfo())
153 reserve(this->seq, 7);
154 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
155 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
156 this->seq.push_back(p7);
159 container(const ex & p1, const ex & p2, const ex & p3,
160 const ex & p4, const ex & p5, const ex & p6,
161 const ex & p7, const ex & p8) : inherited(get_tinfo())
163 reserve(this->seq, 8);
164 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
165 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
166 this->seq.push_back(p7); this->seq.push_back(p8);
169 container(const ex & p1, const ex & p2, const ex & p3,
170 const ex & p4, const ex & p5, const ex & p6,
171 const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
173 reserve(this->seq, 9);
174 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
175 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
176 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
179 container(const ex & p1, const ex & p2, const ex & p3,
180 const ex & p4, const ex & p5, const ex & p6,
181 const ex & p7, const ex & p8, const ex & p9,
182 const ex & p10) : inherited(get_tinfo())
184 reserve(this->seq, 10);
185 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
186 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
187 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
188 this->seq.push_back(p10);
191 container(const ex & p1, const ex & p2, const ex & p3,
192 const ex & p4, const ex & p5, const ex & p6,
193 const ex & p7, const ex & p8, const ex & p9,
194 const ex & p10, const ex & p11) : inherited(get_tinfo())
196 reserve(this->seq, 11);
197 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
198 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
199 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
200 this->seq.push_back(p10); this->seq.push_back(p11);
203 container(const ex & p1, const ex & p2, const ex & p3,
204 const ex & p4, const ex & p5, const ex & p6,
205 const ex & p7, const ex & p8, const ex & p9,
206 const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
208 reserve(this->seq, 12);
209 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
210 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
211 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
212 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
215 container(const ex & p1, const ex & p2, const ex & p3,
216 const ex & p4, const ex & p5, const ex & p6,
217 const ex & p7, const ex & p8, const ex & p9,
218 const ex & p10, const ex & p11, const ex & p12,
219 const ex & p13) : inherited(get_tinfo())
221 reserve(this->seq, 13);
222 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
223 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
224 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
225 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
226 this->seq.push_back(p13);
229 container(const ex & p1, const ex & p2, const ex & p3,
230 const ex & p4, const ex & p5, const ex & p6,
231 const ex & p7, const ex & p8, const ex & p9,
232 const ex & p10, const ex & p11, const ex & p12,
233 const ex & p13, const ex & p14) : inherited(get_tinfo())
235 reserve(this->seq, 14);
236 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
237 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
238 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
239 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
240 this->seq.push_back(p13); this->seq.push_back(p14);
243 container(const ex & p1, const ex & p2, const ex & p3,
244 const ex & p4, const ex & p5, const ex & p6,
245 const ex & p7, const ex & p8, const ex & p9,
246 const ex & p10, const ex & p11, const ex & p12,
247 const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
249 reserve(this->seq, 15);
250 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
251 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
252 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
253 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
254 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
257 container(const ex & p1, const ex & p2, const ex & p3,
258 const ex & p4, const ex & p5, const ex & p6,
259 const ex & p7, const ex & p8, const ex & p9,
260 const ex & p10, const ex & p11, const ex & p12,
261 const ex & p13, const ex & p14, const ex & p15,
262 const ex & p16) : inherited(get_tinfo())
264 reserve(this->seq, 16);
265 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
266 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
267 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
268 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
269 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
270 this->seq.push_back(p16);
273 // functions overriding virtual functions from base classes
275 void print(const print_context & c, unsigned level = 0) const;
276 bool info(unsigned inf) const { return inherited::info(inf); }
277 unsigned precedence() const { return 10; }
278 size_t nops() const { return this->seq.size(); }
279 ex op(size_t i) const;
280 ex & let_op(size_t i);
281 ex eval(int level = 0) const;
282 ex subs(const exmap & m, unsigned options = 0) const;
285 bool is_equal_same_type(const basic & other) const;
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 * deleted). Must be overridden by derived classes. */
295 virtual ex thiscontainer(STLT * vp) const { return container(vp); }
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 STLT evalchildren(int level) const;
335 STLT *subschildren(const exmap & m, unsigned options = 0) const;
338 /** Default constructor */
339 template <template <class> class C>
340 container<C>::container() : inherited(get_tinfo()) {}
342 /** Construct object from archive_node. */
343 template <template <class> class C>
344 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
346 for (unsigned int i=0; true; i++) {
348 if (n.find_ex("seq", e, sym_lst, i))
349 this->seq.push_back(e);
355 /** Unarchive the object. */
356 template <template <class> class C>
357 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
359 return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
362 /** Archive the object. */
363 template <template <class> class C>
364 void container<C>::archive(archive_node &n) const
366 inherited::archive(n);
367 const_iterator i = this->seq.begin(), end = this->seq.end();
374 template <template <class> class C>
375 void container<C>::print(const print_context & c, unsigned level) const
377 if (is_a<print_tree>(c)) {
378 c.s << std::string(level, ' ') << class_name()
379 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
380 << ", nops=" << nops()
382 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
383 const_iterator i = this->seq.begin(), end = this->seq.end();
385 i->print(c, level + delta_indent);
388 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
389 } else if (is_a<print_python>(c)) {
390 printseq(c, '[', ',', ']', precedence(), precedence()+1);
391 } else if (is_a<print_python_repr>(c)) {
392 c.s << class_name ();
393 printseq(c, '(', ',', ')', precedence(), precedence()+1);
395 // always print brackets around seq, ignore upper_precedence
396 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
400 template <template <class> class C>
401 ex container<C>::op(size_t i) const
403 GINAC_ASSERT(i < nops());
405 const_iterator it = this->seq.begin();
410 template <template <class> class C>
411 ex & container<C>::let_op(size_t i)
413 GINAC_ASSERT(i < nops());
415 ensure_if_modifiable();
416 typename STLT::iterator it = this->seq.begin();
421 template <template <class> class C>
422 ex container<C>::eval(int level) const
427 return thiscontainer(evalchildren(level));
430 template <template <class> class C>
431 ex container<C>::subs(const exmap & m, unsigned options) const
433 STLT *vp = subschildren(m, options);
435 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
437 return subs_one_level(m, options);
440 /** Compare two containers of the same type. */
441 template <template <class> class C>
442 int container<C>::compare_same_type(const basic & other) const
444 GINAC_ASSERT(is_a<container>(other));
445 const container & o = static_cast<const container &>(other);
447 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
448 it2 = o.seq.begin(), it2end = o.seq.end();
450 while (it1 != it1end && it2 != it2end) {
451 int cmpval = it1->compare(*it2);
457 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
460 template <template <class> class C>
461 bool container<C>::is_equal_same_type(const basic & other) const
463 GINAC_ASSERT(is_a<container>(other));
464 const container & o = static_cast<const container &>(other);
466 if (this->seq.size() != o.seq.size())
469 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
470 while (it1 != it1end) {
471 if (!it1->is_equal(*it2))
479 /** Add element at front. */
480 template <template <class> class C>
481 container<C> & container<C>::prepend(const ex & b)
483 ensure_if_modifiable();
484 this->seq.push_front(b);
488 /** Add element at back. */
489 template <template <class> class C>
490 container<C> & container<C>::append(const ex & b)
492 ensure_if_modifiable();
493 this->seq.push_back(b);
497 /** Remove first element. */
498 template <template <class> class C>
499 container<C> & container<C>::remove_first()
501 ensure_if_modifiable();
502 this->seq.pop_front();
506 /** Remove last element. */
507 template <template <class> class C>
508 container<C> & container<C>::remove_last()
510 ensure_if_modifiable();
511 this->seq.pop_back();
515 /** Remove all elements. */
516 template <template <class> class C>
517 container<C> & container<C>::remove_all()
519 ensure_if_modifiable();
524 /** Sort elements. */
525 template <template <class> class C>
526 container<C> & container<C>::sort()
528 ensure_if_modifiable();
529 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
533 /** Specialization of container::unique_() for std::list. */
534 inline void container<std::list>::unique_()
536 this->seq.unique(ex_is_equal());
539 /** Remove adjacent duplicate elements. */
540 template <template <class> class C>
541 container<C> & container<C>::unique()
543 ensure_if_modifiable();
548 /** Print sequence of contained elements. */
549 template <template <class> class C>
550 void container<C>::printseq(const print_context & c, char openbracket, char delim,
551 char closebracket, unsigned this_precedence,
552 unsigned upper_precedence) const
554 if (this_precedence <= upper_precedence)
557 if (!this->seq.empty()) {
558 const_iterator it = this->seq.begin(), itend = this->seq.end();
560 while (it != itend) {
561 it->print(c, this_precedence);
565 it->print(c, this_precedence);
568 if (this_precedence <= upper_precedence)
572 template <template <class> class C>
573 typename container<C>::STLT container<C>::evalchildren(int level) const
577 else if (level == -max_recursion_level)
578 throw std::runtime_error("max recursion level reached");
581 reserve(s, this->seq.size());
584 const_iterator it = this->seq.begin(), itend = this->seq.end();
585 while (it != itend) {
586 s.push_back(it->eval(level));
593 template <template <class> class C>
594 typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigned options) const
596 // returns a NULL pointer if nothing had to be substituted
597 // returns a pointer to a newly created epvector otherwise
598 // (which has to be deleted somewhere else)
600 const_iterator cit = this->seq.begin(), end = this->seq.end();
602 const ex & subsed_ex = cit->subs(m, options);
603 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
605 // copy first part of seq which hasn't changed
606 STLT *s = new STLT(this->seq.begin(), cit);
607 reserve(*s, this->seq.size());
609 // insert changed element
610 s->push_back(subsed_ex);
615 s->push_back(cit->subs(m, options));
625 return 0; // nothing has changed
630 #endif // ndef __GINAC_CONTAINER_H__