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 bool info(unsigned inf) const { return inherited::info(inf); }
276 unsigned precedence() const { return 10; }
277 size_t nops() const { return this->seq.size(); }
278 ex op(size_t i) const;
279 ex & let_op(size_t i);
280 ex eval(int level = 0) const;
281 ex subs(const exmap & m, unsigned options = 0) const;
284 bool is_equal_same_type(const basic & other) const;
286 // new virtual functions which can be overridden by derived classes
288 /** Similar to duplicate(), but with a preset sequence. Must be
289 * overridden by derived classes. */
290 virtual ex thiscontainer(const STLT & v) const { return container(v); }
292 /** Similar to duplicate(), but with a preset sequence (which gets
293 * deleted). Must be overridden by derived classes. */
294 virtual ex thiscontainer(STLT * vp) const { return container(vp); }
296 virtual void printseq(const print_context & c, char openbracket, char delim,
297 char closebracket, unsigned this_precedence,
298 unsigned upper_precedence = 0) const;
300 // non-virtual functions in this class
302 void sort_(std::random_access_iterator_tag)
304 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
307 void sort_(std::input_iterator_tag)
309 this->seq.sort(ex_is_less());
314 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
315 this->seq.erase(p, this->seq.end());
319 container & prepend(const ex & b);
320 container & append(const ex & b);
321 container & remove_first();
322 container & remove_last();
323 container & remove_all();
325 container & unique();
327 const_iterator begin() const {return this->seq.begin();}
328 const_iterator end() const {return this->seq.end();}
329 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
330 const_reverse_iterator rend() const {return this->seq.rend();}
333 void do_print(const print_context & c, unsigned level) const;
334 void do_print_tree(const print_tree & c, unsigned level) const;
335 void do_print_python(const print_python & c, unsigned level) const;
336 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
337 STLT evalchildren(int level) const;
338 STLT *subschildren(const exmap & m, unsigned options = 0) const;
341 /** Default constructor */
342 template <template <class> class C>
343 container<C>::container() : inherited(get_tinfo()) {}
345 /** Construct object from archive_node. */
346 template <template <class> class C>
347 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
349 for (unsigned int i=0; true; i++) {
351 if (n.find_ex("seq", e, sym_lst, i))
352 this->seq.push_back(e);
358 /** Unarchive the object. */
359 template <template <class> class C>
360 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
362 return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
365 /** Archive the object. */
366 template <template <class> class C>
367 void container<C>::archive(archive_node &n) const
369 inherited::archive(n);
370 const_iterator i = this->seq.begin(), end = this->seq.end();
377 template <template <class> class C>
378 void container<C>::do_print(const print_context & c, unsigned level) const
380 // always print brackets around seq, ignore upper_precedence
381 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
384 template <template <class> class C>
385 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
387 c.s << std::string(level, ' ') << class_name()
388 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
389 << ", nops=" << nops()
391 const_iterator i = this->seq.begin(), end = this->seq.end();
393 i->print(c, level + c.delta_indent);
396 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
399 template <template <class> class C>
400 void container<C>::do_print_python(const print_python & c, unsigned level) const
402 printseq(c, '[', ',', ']', precedence(), precedence()+1);
405 template <template <class> class C>
406 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
409 printseq(c, '(', ',', ')', precedence(), precedence()+1);
412 template <template <class> class C>
413 ex container<C>::op(size_t i) const
415 GINAC_ASSERT(i < nops());
417 const_iterator it = this->seq.begin();
422 template <template <class> class C>
423 ex & container<C>::let_op(size_t i)
425 GINAC_ASSERT(i < nops());
427 ensure_if_modifiable();
428 typename STLT::iterator it = this->seq.begin();
433 template <template <class> class C>
434 ex container<C>::eval(int level) const
439 return thiscontainer(evalchildren(level));
442 template <template <class> class C>
443 ex container<C>::subs(const exmap & m, unsigned options) const
445 STLT *vp = subschildren(m, options);
447 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
449 return subs_one_level(m, options);
452 /** Compare two containers of the same type. */
453 template <template <class> class C>
454 int container<C>::compare_same_type(const basic & other) const
456 GINAC_ASSERT(is_a<container>(other));
457 const container & o = static_cast<const container &>(other);
459 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
460 it2 = o.seq.begin(), it2end = o.seq.end();
462 while (it1 != it1end && it2 != it2end) {
463 int cmpval = it1->compare(*it2);
469 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
472 template <template <class> class C>
473 bool container<C>::is_equal_same_type(const basic & other) const
475 GINAC_ASSERT(is_a<container>(other));
476 const container & o = static_cast<const container &>(other);
478 if (this->seq.size() != o.seq.size())
481 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
482 while (it1 != it1end) {
483 if (!it1->is_equal(*it2))
491 /** Add element at front. */
492 template <template <class> class C>
493 container<C> & container<C>::prepend(const ex & b)
495 ensure_if_modifiable();
496 this->seq.push_front(b);
500 /** Add element at back. */
501 template <template <class> class C>
502 container<C> & container<C>::append(const ex & b)
504 ensure_if_modifiable();
505 this->seq.push_back(b);
509 /** Remove first element. */
510 template <template <class> class C>
511 container<C> & container<C>::remove_first()
513 ensure_if_modifiable();
514 this->seq.pop_front();
518 /** Remove last element. */
519 template <template <class> class C>
520 container<C> & container<C>::remove_last()
522 ensure_if_modifiable();
523 this->seq.pop_back();
527 /** Remove all elements. */
528 template <template <class> class C>
529 container<C> & container<C>::remove_all()
531 ensure_if_modifiable();
536 /** Sort elements. */
537 template <template <class> class C>
538 container<C> & container<C>::sort()
540 ensure_if_modifiable();
541 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
545 /** Specialization of container::unique_() for std::list. */
546 inline void container<std::list>::unique_()
548 this->seq.unique(ex_is_equal());
551 /** Remove adjacent duplicate elements. */
552 template <template <class> class C>
553 container<C> & container<C>::unique()
555 ensure_if_modifiable();
560 /** Print sequence of contained elements. */
561 template <template <class> class C>
562 void container<C>::printseq(const print_context & c, char openbracket, char delim,
563 char closebracket, unsigned this_precedence,
564 unsigned upper_precedence) const
566 if (this_precedence <= upper_precedence)
569 if (!this->seq.empty()) {
570 const_iterator it = this->seq.begin(), itend = this->seq.end();
572 while (it != itend) {
573 it->print(c, this_precedence);
577 it->print(c, this_precedence);
580 if (this_precedence <= upper_precedence)
584 template <template <class> class C>
585 typename container<C>::STLT container<C>::evalchildren(int level) const
589 else if (level == -max_recursion_level)
590 throw std::runtime_error("max recursion level reached");
593 reserve(s, this->seq.size());
596 const_iterator it = this->seq.begin(), itend = this->seq.end();
597 while (it != itend) {
598 s.push_back(it->eval(level));
605 template <template <class> class C>
606 typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigned options) const
608 // returns a NULL pointer if nothing had to be substituted
609 // returns a pointer to a newly created epvector otherwise
610 // (which has to be deleted somewhere else)
612 const_iterator cit = this->seq.begin(), end = this->seq.end();
614 const ex & subsed_ex = cit->subs(m, options);
615 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
617 // copy first part of seq which hasn't changed
618 STLT *s = new STLT(this->seq.begin(), cit);
619 reserve(*s, this->seq.size());
621 // insert changed element
622 s->push_back(subsed_ex);
627 s->push_back(cit->subs(m, options));
637 return 0; // nothing has changed
642 #endif // ndef __GINAC_CONTAINER_H__