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 /** Wrapper template for making GiNaC classes out of STL containers. */
71 template <template <class> class C>
72 class container : public basic, public container_storage<C> {
73 GINAC_DECLARE_REGISTERED_CLASS(container, basic)
75 typedef typename container_storage<C>::STLT STLT;
78 typedef typename STLT::const_iterator const_iterator;
79 typedef typename STLT::const_reverse_iterator const_reverse_iterator;
83 static unsigned get_tinfo() { return TINFO_fail; }
84 static char get_open_delim() { return '('; }
85 static char get_close_delim() { return ')'; }
89 container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
92 this->seq.swap(const_cast<STLT &>(s));
97 explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
102 container(exvector::const_iterator b, exvector::const_iterator e)
103 : inherited(get_tinfo()), container_storage<C>(b, e) {}
105 explicit container(const ex & p1)
106 : inherited(get_tinfo()), container_storage<C>(1, p1) {}
108 container(const ex & p1, const ex & p2) : inherited(get_tinfo())
110 reserve(this->seq, 2);
111 this->seq.push_back(p1); this->seq.push_back(p2);
114 container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
116 reserve(this->seq, 3);
117 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
120 container(const ex & p1, const ex & p2, const ex & p3,
121 const ex & p4) : inherited(get_tinfo())
123 reserve(this->seq, 4);
124 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
125 this->seq.push_back(p4);
128 container(const ex & p1, const ex & p2, const ex & p3,
129 const ex & p4, const ex & p5) : inherited(get_tinfo())
131 reserve(this->seq, 5);
132 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
133 this->seq.push_back(p4); this->seq.push_back(p5);
136 container(const ex & p1, const ex & p2, const ex & p3,
137 const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
139 reserve(this->seq, 6);
140 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
141 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
144 container(const ex & p1, const ex & p2, const ex & p3,
145 const ex & p4, const ex & p5, const ex & p6,
146 const ex & p7) : inherited(get_tinfo())
148 reserve(this->seq, 7);
149 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
150 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
151 this->seq.push_back(p7);
154 container(const ex & p1, const ex & p2, const ex & p3,
155 const ex & p4, const ex & p5, const ex & p6,
156 const ex & p7, const ex & p8) : inherited(get_tinfo())
158 reserve(this->seq, 8);
159 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
160 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
161 this->seq.push_back(p7); this->seq.push_back(p8);
164 container(const ex & p1, const ex & p2, const ex & p3,
165 const ex & p4, const ex & p5, const ex & p6,
166 const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
168 reserve(this->seq, 9);
169 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
170 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
171 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
174 container(const ex & p1, const ex & p2, const ex & p3,
175 const ex & p4, const ex & p5, const ex & p6,
176 const ex & p7, const ex & p8, const ex & p9,
177 const ex & p10) : inherited(get_tinfo())
179 reserve(this->seq, 10);
180 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
181 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
182 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
183 this->seq.push_back(p10);
186 container(const ex & p1, const ex & p2, const ex & p3,
187 const ex & p4, const ex & p5, const ex & p6,
188 const ex & p7, const ex & p8, const ex & p9,
189 const ex & p10, const ex & p11) : inherited(get_tinfo())
191 reserve(this->seq, 11);
192 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
193 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
194 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
195 this->seq.push_back(p10); this->seq.push_back(p11);
198 container(const ex & p1, const ex & p2, const ex & p3,
199 const ex & p4, const ex & p5, const ex & p6,
200 const ex & p7, const ex & p8, const ex & p9,
201 const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
203 reserve(this->seq, 12);
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); this->seq.push_back(p6);
206 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
207 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
210 container(const ex & p1, const ex & p2, const ex & p3,
211 const ex & p4, const ex & p5, const ex & p6,
212 const ex & p7, const ex & p8, const ex & p9,
213 const ex & p10, const ex & p11, const ex & p12,
214 const ex & p13) : inherited(get_tinfo())
216 reserve(this->seq, 13);
217 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
218 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
219 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
220 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
221 this->seq.push_back(p13);
224 container(const ex & p1, const ex & p2, const ex & p3,
225 const ex & p4, const ex & p5, const ex & p6,
226 const ex & p7, const ex & p8, const ex & p9,
227 const ex & p10, const ex & p11, const ex & p12,
228 const ex & p13, const ex & p14) : inherited(get_tinfo())
230 reserve(this->seq, 14);
231 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
232 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
233 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
234 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
235 this->seq.push_back(p13); this->seq.push_back(p14);
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,
241 const ex & p10, const ex & p11, const ex & p12,
242 const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
244 reserve(this->seq, 15);
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);
248 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
249 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
252 container(const ex & p1, const ex & p2, const ex & p3,
253 const ex & p4, const ex & p5, const ex & p6,
254 const ex & p7, const ex & p8, const ex & p9,
255 const ex & p10, const ex & p11, const ex & p12,
256 const ex & p13, const ex & p14, const ex & p15,
257 const ex & p16) : inherited(get_tinfo())
259 reserve(this->seq, 16);
260 this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
261 this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
262 this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
263 this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
264 this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
265 this->seq.push_back(p16);
268 // functions overriding virtual functions from base classes
270 bool info(unsigned inf) const { return inherited::info(inf); }
271 unsigned precedence() const { return 10; }
272 size_t nops() const { return this->seq.size(); }
273 ex op(size_t i) const;
274 ex & let_op(size_t i);
275 ex eval(int level = 0) const;
276 ex subs(const exmap & m, unsigned options = 0) const;
279 bool is_equal_same_type(const basic & other) const;
281 // new virtual functions which can be overridden by derived classes
283 /** Similar to duplicate(), but with a preset sequence. Must be
284 * overridden by derived classes. */
285 virtual ex thiscontainer(const STLT & v) const { return container(v); }
287 /** Similar to duplicate(), but with a preset sequence (which gets
288 * deleted). Must be overridden by derived classes. */
289 virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
291 virtual void printseq(const print_context & c, char openbracket, char delim,
292 char closebracket, unsigned this_precedence,
293 unsigned upper_precedence = 0) const;
295 // non-virtual functions in this class
297 void sort_(std::random_access_iterator_tag)
299 std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
302 void sort_(std::input_iterator_tag)
304 this->seq.sort(ex_is_less());
309 typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
310 this->seq.erase(p, this->seq.end());
314 container & prepend(const ex & b);
315 container & append(const ex & b);
316 container & remove_first();
317 container & remove_last();
318 container & remove_all();
320 container & unique();
322 const_iterator begin() const {return this->seq.begin();}
323 const_iterator end() const {return this->seq.end();}
324 const_reverse_iterator rbegin() const {return this->seq.rbegin();}
325 const_reverse_iterator rend() const {return this->seq.rend();}
328 void do_print(const print_context & c, unsigned level) const;
329 void do_print_tree(const print_tree & c, unsigned level) const;
330 void do_print_python(const print_python & c, unsigned level) const;
331 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
332 STLT evalchildren(int level) const;
333 std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
336 /** Default constructor */
337 template <template <class> class C>
338 container<C>::container() : inherited(get_tinfo()) {}
340 /** Construct object from archive_node. */
341 template <template <class> class C>
342 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
344 for (unsigned int i=0; true; i++) {
346 if (n.find_ex("seq", e, sym_lst, i))
347 this->seq.push_back(e);
353 /** Unarchive the object. */
354 template <template <class> class C>
355 ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
357 return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
360 /** Archive the object. */
361 template <template <class> class C>
362 void container<C>::archive(archive_node &n) const
364 inherited::archive(n);
365 const_iterator i = this->seq.begin(), end = this->seq.end();
372 template <template <class> class C>
373 void container<C>::do_print(const print_context & c, unsigned level) const
375 // always print brackets around seq, ignore upper_precedence
376 printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
379 template <template <class> class C>
380 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
382 c.s << std::string(level, ' ') << class_name()
383 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
384 << ", nops=" << nops()
386 const_iterator i = this->seq.begin(), end = this->seq.end();
388 i->print(c, level + c.delta_indent);
391 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
394 template <template <class> class C>
395 void container<C>::do_print_python(const print_python & c, unsigned level) const
397 printseq(c, '[', ',', ']', precedence(), precedence()+1);
400 template <template <class> class C>
401 void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
404 printseq(c, '(', ',', ')', precedence(), precedence()+1);
407 template <template <class> class C>
408 ex container<C>::op(size_t i) const
410 GINAC_ASSERT(i < nops());
412 const_iterator it = this->seq.begin();
417 template <template <class> class C>
418 ex & container<C>::let_op(size_t i)
420 GINAC_ASSERT(i < nops());
422 ensure_if_modifiable();
423 typename STLT::iterator it = this->seq.begin();
428 template <template <class> class C>
429 ex container<C>::eval(int level) const
434 return thiscontainer(evalchildren(level));
437 template <template <class> class C>
438 ex container<C>::subs(const exmap & m, unsigned options) const
440 std::auto_ptr<STLT> vp = subschildren(m, options);
442 return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
444 return subs_one_level(m, options);
447 /** Compare two containers of the same type. */
448 template <template <class> class C>
449 int container<C>::compare_same_type(const basic & other) const
451 GINAC_ASSERT(is_a<container>(other));
452 const container & o = static_cast<const container &>(other);
454 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
455 it2 = o.seq.begin(), it2end = o.seq.end();
457 while (it1 != it1end && it2 != it2end) {
458 int cmpval = it1->compare(*it2);
464 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
467 template <template <class> class C>
468 bool container<C>::is_equal_same_type(const basic & other) const
470 GINAC_ASSERT(is_a<container>(other));
471 const container & o = static_cast<const container &>(other);
473 if (this->seq.size() != o.seq.size())
476 const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
477 while (it1 != it1end) {
478 if (!it1->is_equal(*it2))
486 /** Add element at front. */
487 template <template <class> class C>
488 container<C> & container<C>::prepend(const ex & b)
490 ensure_if_modifiable();
491 this->seq.push_front(b);
495 /** Add element at back. */
496 template <template <class> class C>
497 container<C> & container<C>::append(const ex & b)
499 ensure_if_modifiable();
500 this->seq.push_back(b);
504 /** Remove first element. */
505 template <template <class> class C>
506 container<C> & container<C>::remove_first()
508 ensure_if_modifiable();
509 this->seq.pop_front();
513 /** Remove last element. */
514 template <template <class> class C>
515 container<C> & container<C>::remove_last()
517 ensure_if_modifiable();
518 this->seq.pop_back();
522 /** Remove all elements. */
523 template <template <class> class C>
524 container<C> & container<C>::remove_all()
526 ensure_if_modifiable();
531 /** Sort elements. */
532 template <template <class> class C>
533 container<C> & container<C>::sort()
535 ensure_if_modifiable();
536 sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
540 /** Specialization of container::unique_() for std::list. */
541 inline void container<std::list>::unique_()
543 this->seq.unique(ex_is_equal());
546 /** Remove adjacent duplicate elements. */
547 template <template <class> class C>
548 container<C> & container<C>::unique()
550 ensure_if_modifiable();
555 /** Print sequence of contained elements. */
556 template <template <class> class C>
557 void container<C>::printseq(const print_context & c, char openbracket, char delim,
558 char closebracket, unsigned this_precedence,
559 unsigned upper_precedence) const
561 if (this_precedence <= upper_precedence)
564 if (!this->seq.empty()) {
565 const_iterator it = this->seq.begin(), itend = this->seq.end();
567 while (it != itend) {
568 it->print(c, this_precedence);
572 it->print(c, this_precedence);
575 if (this_precedence <= upper_precedence)
579 template <template <class> class C>
580 typename container<C>::STLT container<C>::evalchildren(int level) const
584 else if (level == -max_recursion_level)
585 throw std::runtime_error("max recursion level reached");
588 reserve(s, this->seq.size());
591 const_iterator it = this->seq.begin(), itend = this->seq.end();
592 while (it != itend) {
593 s.push_back(it->eval(level));
600 template <template <class> class C>
601 std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
603 // returns a NULL pointer if nothing had to be substituted
604 // returns a pointer to a newly created epvector otherwise
605 // (and relinquishes responsibility for the epvector)
607 const_iterator cit = this->seq.begin(), end = this->seq.end();
609 const ex & subsed_ex = cit->subs(m, options);
610 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
612 // copy first part of seq which hasn't changed
613 std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
614 reserve(*s, this->seq.size());
616 // insert changed element
617 s->push_back(subsed_ex);
622 s->push_back(cit->subs(m, options));
632 return std::auto_ptr<STLT>(0); // nothing has changed
637 #endif // ndef __GINAC_CONTAINER_H__