3 * Wrapper template for making GiNaC classes out of STL containers. */
6 * GiNaC Copyright (C) 1999-2005 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__
36 #include "assertion.h"
41 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
42 template <template <class> class C>
43 class container_storage {
47 container_storage() {}
48 container_storage(size_t n, const ex & e) : seq(n, e) {}
51 container_storage(In b, In e) : seq(b, e) {}
53 void reserve(size_t) {}
54 static void reserve(STLT &, size_t) {}
58 // disallow destruction of container through a container_storage*
60 ~container_storage() {}
64 inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
67 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
70 /** Helper template to allow initialization of containers via an overloaded
71 * comma operator (idea stolen from Blitz++). */
72 template <typename T, typename STLT>
73 class container_init {
75 container_init(STLT & s) : stlt(s) {}
77 container_init<T, STLT> operator,(const T & x)
80 return container_init<T, STLT>(stlt);
83 // The following specializations produce much tighter code than the
86 container_init<T, STLT> operator,(int x)
89 return container_init<T, STLT>(stlt);
92 container_init<T, STLT> operator,(unsigned int x)
95 return container_init<T, STLT>(stlt);
98 container_init<T, STLT> operator,(long x)
101 return container_init<T, STLT>(stlt);
104 container_init<T, STLT> operator,(unsigned long x)
107 return container_init<T, STLT>(stlt);
110 container_init<T, STLT> operator,(double x)
113 return container_init<T, STLT>(stlt);
116 container_init<T, STLT> operator,(const symbol & x)
118 stlt.push_back(T(x));
119 return container_init<T, STLT>(stlt);
127 /** Wrapper template for making GiNaC classes out of STL containers. */
128 template <template <class> class C>
129 class container : public basic, public container_storage<C> {
130 GINAC_DECLARE_REGISTERED_CLASS(container, basic)
132 typedef typename container_storage<C>::STLT STLT;
135 typedef typename STLT::const_iterator const_iterator;
136 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
140 static unsigned get_tinfo() { return TINFO_fail; }
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, bool discardable = false) : inherited(get_tinfo())
149 setflag(get_default_flags());
152 this->seq.swap(const_cast<STLT &>(s));
157 explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
159 setflag(get_default_flags());
163 container(exvector::const_iterator b, exvector::const_iterator e)
164 : inherited(get_tinfo()), container_storage<C>(b, e)
166 setflag(get_default_flags());
169 explicit container(const ex & p1)
170 : inherited(get_tinfo()), container_storage<C>(1, p1)
172 setflag(get_default_flags());
175 container(const ex & p1, const ex & p2) : inherited(get_tinfo())
177 setflag(get_default_flags());
178 reserve(this->seq, 2);
179 this->seq.push_back(p1); this->seq.push_back(p2);
182 container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
184 setflag(get_default_flags());
185 reserve(this->seq, 3);
186 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
189 container(const ex & p1, const ex & p2, const ex & p3,
190 const ex & p4) : inherited(get_tinfo())
192 setflag(get_default_flags());
193 reserve(this->seq, 4);
194 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
195 this->seq.push_back(p4);
198 container(const ex & p1, const ex & p2, const ex & p3,
199 const ex & p4, const ex & p5) : inherited(get_tinfo())
201 setflag(get_default_flags());
202 reserve(this->seq, 5);
203 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
204 this->seq.push_back(p4); this->seq.push_back(p5);
207 container(const ex & p1, const ex & p2, const ex & p3,
208 const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
210 setflag(get_default_flags());
211 reserve(this->seq, 6);
212 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
213 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
216 container(const ex & p1, const ex & p2, const ex & p3,
217 const ex & p4, const ex & p5, const ex & p6,
218 const ex & p7) : inherited(get_tinfo())
220 setflag(get_default_flags());
221 reserve(this->seq, 7);
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);
227 container(const ex & p1, const ex & p2, const ex & p3,
228 const ex & p4, const ex & p5, const ex & p6,
229 const ex & p7, const ex & p8) : inherited(get_tinfo())
231 setflag(get_default_flags());
232 reserve(this->seq, 8);
233 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
234 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
235 this->seq.push_back(p7); this->seq.push_back(p8);
238 container(const ex & p1, const ex & p2, const ex & p3,
239 const ex & p4, const ex & p5, const ex & p6,
240 const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
242 setflag(get_default_flags());
243 reserve(this->seq, 9);
244 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
245 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
246 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
249 container(const ex & p1, const ex & p2, const ex & p3,
250 const ex & p4, const ex & p5, const ex & p6,
251 const ex & p7, const ex & p8, const ex & p9,
252 const ex & p10) : inherited(get_tinfo())
254 setflag(get_default_flags());
255 reserve(this->seq, 10);
256 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
257 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
258 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
259 this->seq.push_back(p10);
262 container(const ex & p1, const ex & p2, const ex & p3,
263 const ex & p4, const ex & p5, const ex & p6,
264 const ex & p7, const ex & p8, const ex & p9,
265 const ex & p10, const ex & p11) : inherited(get_tinfo())
267 setflag(get_default_flags());
268 reserve(this->seq, 11);
269 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
270 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
271 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
272 this->seq.push_back(p10); this->seq.push_back(p11);
275 container(const ex & p1, const ex & p2, const ex & p3,
276 const ex & p4, const ex & p5, const ex & p6,
277 const ex & p7, const ex & p8, const ex & p9,
278 const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
280 setflag(get_default_flags());
281 reserve(this->seq, 12);
282 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
283 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
284 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
285 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
288 container(const ex & p1, const ex & p2, const ex & p3,
289 const ex & p4, const ex & p5, const ex & p6,
290 const ex & p7, const ex & p8, const ex & p9,
291 const ex & p10, const ex & p11, const ex & p12,
292 const ex & p13) : inherited(get_tinfo())
294 setflag(get_default_flags());
295 reserve(this->seq, 13);
296 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
297 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
298 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
299 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
300 this->seq.push_back(p13);
303 container(const ex & p1, const ex & p2, const ex & p3,
304 const ex & p4, const ex & p5, const ex & p6,
305 const ex & p7, const ex & p8, const ex & p9,
306 const ex & p10, const ex & p11, const ex & p12,
307 const ex & p13, const ex & p14) : inherited(get_tinfo())
309 setflag(get_default_flags());
310 reserve(this->seq, 14);
311 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
312 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
313 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
314 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
315 this->seq.push_back(p13); this->seq.push_back(p14);
318 container(const ex & p1, const ex & p2, const ex & p3,
319 const ex & p4, const ex & p5, const ex & p6,
320 const ex & p7, const ex & p8, const ex & p9,
321 const ex & p10, const ex & p11, const ex & p12,
322 const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
324 setflag(get_default_flags());
325 reserve(this->seq, 15);
326 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
327 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
328 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
329 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
330 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
333 container(const ex & p1, const ex & p2, const ex & p3,
334 const ex & p4, const ex & p5, const ex & p6,
335 const ex & p7, const ex & p8, const ex & p9,
336 const ex & p10, const ex & p11, const ex & p12,
337 const ex & p13, const ex & p14, const ex & p15,
338 const ex & p16) : inherited(get_tinfo())
340 setflag(get_default_flags());
341 reserve(this->seq, 16);
342 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
343 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
344 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
345 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
346 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
347 this->seq.push_back(p16);
350 // First step of initialization of container with a comma-separated
351 // sequence of expressions. Subsequent steps are handled by
352 // container_init<>::operator,().
353 container_init<ex, STLT> operator=(const ex & x)
355 this->seq.push_back(x);
356 return container_init<ex, STLT>(this->seq);
359 // functions overriding virtual functions from base classes
361 bool info(unsigned inf) const { return inherited::info(inf); }
362 unsigned precedence() const { return 10; }
363 size_t nops() const { return this->seq.size(); }
364 ex op(size_t i) const;
365 ex & let_op(size_t i);
366 ex eval(int level = 0) const;
367 ex subs(const exmap & m, unsigned options = 0) const;
372 STLT *newcont = NULL;
373 for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
375 newcont->push_back(i->conjugate());
378 ex x = i->conjugate();
379 if (are_ex_trivially_equal(x, *i)) {
383 reserve(*newcont, this->seq.size());
384 for (const_iterator j=this->seq.begin(); j!=i; ++j) {
385 newcont->push_back(*j);
387 newcont->push_back(x);
390 ex result = thiscontainer(*newcont);
397 bool is_equal_same_type(const basic & other) const;
399 // new virtual functions which can be overridden by derived classes
401 /** Similar to duplicate(), but with a preset sequence. Must be
402 * overridden by derived classes. */
403 virtual ex thiscontainer(const STLT & v) const { return container(v); }
405 /** Similar to duplicate(), but with a preset sequence (which gets
406 * deleted). Must be overridden by derived classes. */
407 virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
409 virtual void printseq(const print_context & c, char openbracket, char delim,
410 char closebracket, unsigned this_precedence,
411 unsigned upper_precedence = 0) const;
413 // non-virtual functions in this class
415 void sort_(std::random_access_iterator_tag)
417 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
420 void sort_(std::input_iterator_tag)
422 this->seq.sort(ex_is_less());
427 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
428 this->seq.erase(p, this->seq.end());
432 container & prepend(const ex & b);
433 container & append(const ex & b);
434 container & remove_first();
435 container & remove_last();
436 container & remove_all();
438 container & unique();
440 const_iterator begin() const {return this->seq.begin();}
441 const_iterator end() const {return this->seq.end();}
442 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
443 const_reverse_iterator rend() const {return this->seq.rend();}
446 void do_print(const print_context & c, unsigned level) const;
447 void do_print_tree(const print_tree & c, unsigned level) const;
448 void do_print_python(const print_python & c, unsigned level) const;
449 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
450 STLT evalchildren(int level) const;
451 std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
454 /** Default constructor */
455 template <template <class> class C>
456 container<C>::container() : inherited(get_tinfo())
458 setflag(get_default_flags());
461 /** Construct object from archive_node. */
462 template <template <class> class C>
463 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
465 setflag(get_default_flags());
467 for (unsigned int i=0; true; i++) {
469 if (n.find_ex("seq", e, sym_lst, i))
470 this->seq.push_back(e);
476 /** Unarchive the object. */
477 template <template <class> class C>
478 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
480 return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
483 /** Archive the object. */
484 template <template <class> class C>
485 void container<C>::archive(archive_node &n) const
487 inherited::archive(n);
488 const_iterator i = this->seq.begin(), end = this->seq.end();
495 template <template <class> class C>
496 void container<C>::do_print(const print_context & c, unsigned level) const
498 // always print brackets around seq, ignore upper_precedence
499 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
502 template <template <class> class C>
503 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
505 c.s << std::string(level, ' ') << class_name() << " @" << this
506 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
507 << ", nops=" << nops()
509 const_iterator i = this->seq.begin(), end = this->seq.end();
511 i->print(c, level + c.delta_indent);
514 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
517 template <template <class> class C>
518 void container<C>::do_print_python(const print_python & c, unsigned level) const
520 printseq(c, '[', ',', ']', precedence(), precedence()+1);
523 template <template <class> class C>
524 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
527 printseq(c, '(', ',', ')', precedence(), precedence()+1);
530 template <template <class> class C>
531 ex container<C>::op(size_t i) const
533 GINAC_ASSERT(i < nops());
535 const_iterator it = this->seq.begin();
540 template <template <class> class C>
541 ex & container<C>::let_op(size_t i)
543 GINAC_ASSERT(i < nops());
545 ensure_if_modifiable();
546 typename STLT::iterator it = this->seq.begin();
551 template <template <class> class C>
552 ex container<C>::eval(int level) const
557 return thiscontainer(evalchildren(level));
560 template <template <class> class C>
561 ex container<C>::subs(const exmap & m, unsigned options) const
563 std::auto_ptr<STLT> vp = subschildren(m, options);
565 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
567 return subs_one_level(m, options);
570 /** Compare two containers of the same type. */
571 template <template <class> class C>
572 int container<C>::compare_same_type(const basic & other) const
574 GINAC_ASSERT(is_a<container>(other));
575 const container & o = static_cast<const container &>(other);
577 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
578 it2 = o.seq.begin(), it2end = o.seq.end();
580 while (it1 != it1end && it2 != it2end) {
581 int cmpval = it1->compare(*it2);
587 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
590 template <template <class> class C>
591 bool container<C>::is_equal_same_type(const basic & other) const
593 GINAC_ASSERT(is_a<container>(other));
594 const container & o = static_cast<const container &>(other);
596 if (this->seq.size() != o.seq.size())
599 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
600 while (it1 != it1end) {
601 if (!it1->is_equal(*it2))
609 /** Add element at front. */
610 template <template <class> class C>
611 container<C> & container<C>::prepend(const ex & b)
613 ensure_if_modifiable();
614 this->seq.push_front(b);
618 /** Add element at back. */
619 template <template <class> class C>
620 container<C> & container<C>::append(const ex & b)
622 ensure_if_modifiable();
623 this->seq.push_back(b);
627 /** Remove first element. */
628 template <template <class> class C>
629 container<C> & container<C>::remove_first()
631 ensure_if_modifiable();
632 this->seq.pop_front();
636 /** Remove last element. */
637 template <template <class> class C>
638 container<C> & container<C>::remove_last()
640 ensure_if_modifiable();
641 this->seq.pop_back();
645 /** Remove all elements. */
646 template <template <class> class C>
647 container<C> & container<C>::remove_all()
649 ensure_if_modifiable();
654 /** Sort elements. */
655 template <template <class> class C>
656 container<C> & container<C>::sort()
658 ensure_if_modifiable();
659 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
663 /** Specialization of container::unique_() for std::list. */
664 template<> inline void container<std::list>::unique_()
666 this->seq.unique(ex_is_equal());
669 /** Remove adjacent duplicate elements. */
670 template <template <class> class C>
671 container<C> & container<C>::unique()
673 ensure_if_modifiable();
678 /** Print sequence of contained elements. */
679 template <template <class> class C>
680 void container<C>::printseq(const print_context & c, char openbracket, char delim,
681 char closebracket, unsigned this_precedence,
682 unsigned upper_precedence) const
684 if (this_precedence <= upper_precedence)
687 if (!this->seq.empty()) {
688 const_iterator it = this->seq.begin(), itend = this->seq.end();
690 while (it != itend) {
691 it->print(c, this_precedence);
695 it->print(c, this_precedence);
698 if (this_precedence <= upper_precedence)
702 template <template <class> class C>
703 typename container<C>::STLT container<C>::evalchildren(int level) const
707 else if (level == -max_recursion_level)
708 throw std::runtime_error("max recursion level reached");
711 reserve(s, this->seq.size());
714 const_iterator it = this->seq.begin(), itend = this->seq.end();
715 while (it != itend) {
716 s.push_back(it->eval(level));
723 template <template <class> class C>
724 std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
726 // returns a NULL pointer if nothing had to be substituted
727 // returns a pointer to a newly created STLT otherwise
728 // (and relinquishes responsibility for the STLT)
730 const_iterator cit = this->seq.begin(), end = this->seq.end();
732 const ex & subsed_ex = cit->subs(m, options);
733 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
735 // copy first part of seq which hasn't changed
736 std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
737 reserve(*s, this->seq.size());
739 // insert changed element
740 s->push_back(subsed_ex);
745 s->push_back(cit->subs(m, options));
755 return std::auto_ptr<STLT>(0); // nothing has changed
760 #endif // ndef __GINAC_CONTAINER_H__