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__
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);
128 /** Wrapper template for making GiNaC classes out of STL containers. */
129 template <template <class> class C>
130 class container : public basic, public container_storage<C> {
131 GINAC_DECLARE_REGISTERED_CLASS(container, basic)
133 typedef typename container_storage<C>::STLT STLT;
136 typedef typename STLT::const_iterator const_iterator;
137 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
141 static unsigned get_tinfo() { return TINFO_fail; }
142 static unsigned get_default_flags() { return 0; }
143 static char get_open_delim() { return '('; }
144 static char get_close_delim() { return ')'; }
148 container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
150 setflag(get_default_flags());
153 this->seq.swap(const_cast<STLT &>(s));
158 explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
160 setflag(get_default_flags());
164 container(exvector::const_iterator b, exvector::const_iterator e)
165 : inherited(get_tinfo()), container_storage<C>(b, e)
167 setflag(get_default_flags());
170 explicit container(const ex & p1)
171 : inherited(get_tinfo()), container_storage<C>(1, p1)
173 setflag(get_default_flags());
176 container(const ex & p1, const ex & p2) : inherited(get_tinfo())
178 setflag(get_default_flags());
179 reserve(this->seq, 2);
180 this->seq.push_back(p1); this->seq.push_back(p2);
183 container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
185 setflag(get_default_flags());
186 reserve(this->seq, 3);
187 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
190 container(const ex & p1, const ex & p2, const ex & p3,
191 const ex & p4) : inherited(get_tinfo())
193 setflag(get_default_flags());
194 reserve(this->seq, 4);
195 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
196 this->seq.push_back(p4);
199 container(const ex & p1, const ex & p2, const ex & p3,
200 const ex & p4, const ex & p5) : inherited(get_tinfo())
202 setflag(get_default_flags());
203 reserve(this->seq, 5);
204 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
205 this->seq.push_back(p4); this->seq.push_back(p5);
208 container(const ex & p1, const ex & p2, const ex & p3,
209 const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
211 setflag(get_default_flags());
212 reserve(this->seq, 6);
213 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
214 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
217 container(const ex & p1, const ex & p2, const ex & p3,
218 const ex & p4, const ex & p5, const ex & p6,
219 const ex & p7) : inherited(get_tinfo())
221 setflag(get_default_flags());
222 reserve(this->seq, 7);
223 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
224 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
225 this->seq.push_back(p7);
228 container(const ex & p1, const ex & p2, const ex & p3,
229 const ex & p4, const ex & p5, const ex & p6,
230 const ex & p7, const ex & p8) : inherited(get_tinfo())
232 setflag(get_default_flags());
233 reserve(this->seq, 8);
234 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
235 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
236 this->seq.push_back(p7); this->seq.push_back(p8);
239 container(const ex & p1, const ex & p2, const ex & p3,
240 const ex & p4, const ex & p5, const ex & p6,
241 const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
243 setflag(get_default_flags());
244 reserve(this->seq, 9);
245 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
246 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
247 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
250 container(const ex & p1, const ex & p2, const ex & p3,
251 const ex & p4, const ex & p5, const ex & p6,
252 const ex & p7, const ex & p8, const ex & p9,
253 const ex & p10) : inherited(get_tinfo())
255 setflag(get_default_flags());
256 reserve(this->seq, 10);
257 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
258 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
259 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
260 this->seq.push_back(p10);
263 container(const ex & p1, const ex & p2, const ex & p3,
264 const ex & p4, const ex & p5, const ex & p6,
265 const ex & p7, const ex & p8, const ex & p9,
266 const ex & p10, const ex & p11) : inherited(get_tinfo())
268 setflag(get_default_flags());
269 reserve(this->seq, 11);
270 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
271 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
272 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
273 this->seq.push_back(p10); this->seq.push_back(p11);
276 container(const ex & p1, const ex & p2, const ex & p3,
277 const ex & p4, const ex & p5, const ex & p6,
278 const ex & p7, const ex & p8, const ex & p9,
279 const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
281 setflag(get_default_flags());
282 reserve(this->seq, 12);
283 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
284 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
285 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
286 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
289 container(const ex & p1, const ex & p2, const ex & p3,
290 const ex & p4, const ex & p5, const ex & p6,
291 const ex & p7, const ex & p8, const ex & p9,
292 const ex & p10, const ex & p11, const ex & p12,
293 const ex & p13) : inherited(get_tinfo())
295 setflag(get_default_flags());
296 reserve(this->seq, 13);
297 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
298 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
299 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
300 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
301 this->seq.push_back(p13);
304 container(const ex & p1, const ex & p2, const ex & p3,
305 const ex & p4, const ex & p5, const ex & p6,
306 const ex & p7, const ex & p8, const ex & p9,
307 const ex & p10, const ex & p11, const ex & p12,
308 const ex & p13, const ex & p14) : inherited(get_tinfo())
310 setflag(get_default_flags());
311 reserve(this->seq, 14);
312 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
313 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
314 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
315 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
316 this->seq.push_back(p13); this->seq.push_back(p14);
319 container(const ex & p1, const ex & p2, const ex & p3,
320 const ex & p4, const ex & p5, const ex & p6,
321 const ex & p7, const ex & p8, const ex & p9,
322 const ex & p10, const ex & p11, const ex & p12,
323 const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
325 setflag(get_default_flags());
326 reserve(this->seq, 15);
327 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
328 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
329 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
330 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
331 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
334 container(const ex & p1, const ex & p2, const ex & p3,
335 const ex & p4, const ex & p5, const ex & p6,
336 const ex & p7, const ex & p8, const ex & p9,
337 const ex & p10, const ex & p11, const ex & p12,
338 const ex & p13, const ex & p14, const ex & p15,
339 const ex & p16) : inherited(get_tinfo())
341 setflag(get_default_flags());
342 reserve(this->seq, 16);
343 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
344 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
345 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
346 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
347 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
348 this->seq.push_back(p16);
351 // First step of initialization of container with a comma-separated
352 // sequence of expressions. Subsequent steps are handled by
353 // container_init<>::operator,().
354 container_init<ex, STLT> operator=(const ex & x)
356 this->seq.push_back(x);
357 return container_init<ex, STLT>(this->seq);
360 // functions overriding virtual functions from base classes
362 bool info(unsigned inf) const { return inherited::info(inf); }
363 unsigned precedence() const { return 10; }
364 size_t nops() const { return this->seq.size(); }
365 ex op(size_t i) const;
366 ex & let_op(size_t i);
367 ex eval(int level = 0) const;
368 ex subs(const exmap & m, unsigned options = 0) const;
371 bool is_equal_same_type(const basic & other) const;
373 // new virtual functions which can be overridden by derived classes
375 /** Similar to duplicate(), but with a preset sequence. Must be
376 * overridden by derived classes. */
377 virtual ex thiscontainer(const STLT & v) const { return container(v); }
379 /** Similar to duplicate(), but with a preset sequence (which gets
380 * deleted). Must be overridden by derived classes. */
381 virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
383 virtual void printseq(const print_context & c, char openbracket, char delim,
384 char closebracket, unsigned this_precedence,
385 unsigned upper_precedence = 0) const;
387 // non-virtual functions in this class
389 void sort_(std::random_access_iterator_tag)
391 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
394 void sort_(std::input_iterator_tag)
396 this->seq.sort(ex_is_less());
401 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
402 this->seq.erase(p, this->seq.end());
406 container & prepend(const ex & b);
407 container & append(const ex & b);
408 container & remove_first();
409 container & remove_last();
410 container & remove_all();
412 container & unique();
414 const_iterator begin() const {return this->seq.begin();}
415 const_iterator end() const {return this->seq.end();}
416 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
417 const_reverse_iterator rend() const {return this->seq.rend();}
420 void do_print(const print_context & c, unsigned level) const;
421 void do_print_tree(const print_tree & c, unsigned level) const;
422 void do_print_python(const print_python & c, unsigned level) const;
423 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
424 STLT evalchildren(int level) const;
425 std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
428 /** Default constructor */
429 template <template <class> class C>
430 container<C>::container() : inherited(get_tinfo())
432 setflag(get_default_flags());
435 /** Construct object from archive_node. */
436 template <template <class> class C>
437 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
439 setflag(get_default_flags());
441 for (unsigned int i=0; true; i++) {
443 if (n.find_ex("seq", e, sym_lst, i))
444 this->seq.push_back(e);
450 /** Unarchive the object. */
451 template <template <class> class C>
452 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
454 return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
457 /** Archive the object. */
458 template <template <class> class C>
459 void container<C>::archive(archive_node &n) const
461 inherited::archive(n);
462 const_iterator i = this->seq.begin(), end = this->seq.end();
469 template <template <class> class C>
470 void container<C>::do_print(const print_context & c, unsigned level) const
472 // always print brackets around seq, ignore upper_precedence
473 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
476 template <template <class> class C>
477 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
479 c.s << std::string(level, ' ') << class_name()
480 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
481 << ", nops=" << nops()
483 const_iterator i = this->seq.begin(), end = this->seq.end();
485 i->print(c, level + c.delta_indent);
488 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
491 template <template <class> class C>
492 void container<C>::do_print_python(const print_python & c, unsigned level) const
494 printseq(c, '[', ',', ']', precedence(), precedence()+1);
497 template <template <class> class C>
498 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
501 printseq(c, '(', ',', ')', precedence(), precedence()+1);
504 template <template <class> class C>
505 ex container<C>::op(size_t i) const
507 GINAC_ASSERT(i < nops());
509 const_iterator it = this->seq.begin();
514 template <template <class> class C>
515 ex & container<C>::let_op(size_t i)
517 GINAC_ASSERT(i < nops());
519 ensure_if_modifiable();
520 typename STLT::iterator it = this->seq.begin();
525 template <template <class> class C>
526 ex container<C>::eval(int level) const
531 return thiscontainer(evalchildren(level));
534 template <template <class> class C>
535 ex container<C>::subs(const exmap & m, unsigned options) const
537 std::auto_ptr<STLT> vp = subschildren(m, options);
539 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
541 return subs_one_level(m, options);
544 /** Compare two containers of the same type. */
545 template <template <class> class C>
546 int container<C>::compare_same_type(const basic & other) const
548 GINAC_ASSERT(is_a<container>(other));
549 const container & o = static_cast<const container &>(other);
551 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
552 it2 = o.seq.begin(), it2end = o.seq.end();
554 while (it1 != it1end && it2 != it2end) {
555 int cmpval = it1->compare(*it2);
561 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
564 template <template <class> class C>
565 bool container<C>::is_equal_same_type(const basic & other) const
567 GINAC_ASSERT(is_a<container>(other));
568 const container & o = static_cast<const container &>(other);
570 if (this->seq.size() != o.seq.size())
573 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
574 while (it1 != it1end) {
575 if (!it1->is_equal(*it2))
583 /** Add element at front. */
584 template <template <class> class C>
585 container<C> & container<C>::prepend(const ex & b)
587 ensure_if_modifiable();
588 this->seq.push_front(b);
592 /** Add element at back. */
593 template <template <class> class C>
594 container<C> & container<C>::append(const ex & b)
596 ensure_if_modifiable();
597 this->seq.push_back(b);
601 /** Remove first element. */
602 template <template <class> class C>
603 container<C> & container<C>::remove_first()
605 ensure_if_modifiable();
606 this->seq.pop_front();
610 /** Remove last element. */
611 template <template <class> class C>
612 container<C> & container<C>::remove_last()
614 ensure_if_modifiable();
615 this->seq.pop_back();
619 /** Remove all elements. */
620 template <template <class> class C>
621 container<C> & container<C>::remove_all()
623 ensure_if_modifiable();
628 /** Sort elements. */
629 template <template <class> class C>
630 container<C> & container<C>::sort()
632 ensure_if_modifiable();
633 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
637 /** Specialization of container::unique_() for std::list. */
638 inline void container<std::list>::unique_()
640 this->seq.unique(ex_is_equal());
643 /** Remove adjacent duplicate elements. */
644 template <template <class> class C>
645 container<C> & container<C>::unique()
647 ensure_if_modifiable();
652 /** Print sequence of contained elements. */
653 template <template <class> class C>
654 void container<C>::printseq(const print_context & c, char openbracket, char delim,
655 char closebracket, unsigned this_precedence,
656 unsigned upper_precedence) const
658 if (this_precedence <= upper_precedence)
661 if (!this->seq.empty()) {
662 const_iterator it = this->seq.begin(), itend = this->seq.end();
664 while (it != itend) {
665 it->print(c, this_precedence);
669 it->print(c, this_precedence);
672 if (this_precedence <= upper_precedence)
676 template <template <class> class C>
677 typename container<C>::STLT container<C>::evalchildren(int level) const
681 else if (level == -max_recursion_level)
682 throw std::runtime_error("max recursion level reached");
685 reserve(s, this->seq.size());
688 const_iterator it = this->seq.begin(), itend = this->seq.end();
689 while (it != itend) {
690 s.push_back(it->eval(level));
697 template <template <class> class C>
698 std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
700 // returns a NULL pointer if nothing had to be substituted
701 // returns a pointer to a newly created epvector otherwise
702 // (and relinquishes responsibility for the epvector)
704 const_iterator cit = this->seq.begin(), end = this->seq.end();
706 const ex & subsed_ex = cit->subs(m, options);
707 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
709 // copy first part of seq which hasn't changed
710 std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
711 reserve(*s, this->seq.size());
713 // insert changed element
714 s->push_back(subsed_ex);
719 s->push_back(cit->subs(m, options));
729 return std::auto_ptr<STLT>(0); // nothing has changed
734 #endif // ndef __GINAC_CONTAINER_H__