1 if (($#ARGV!=0) and ($#ARGV!=1)) {
2 die 'usage: container.pl type [maxargs] (type=lst or exprseq)';
5 if ($ARGV[0] eq 'lst') {
7 } elsif ($ARGV[0] eq 'exprseq') {
10 die 'only lst and exprseq supported';
16 $maxargs=16; # must be greater or equal than the value used in function.pl
19 if ($type eq 'exprseq') {
21 # settings for exprseq
31 } elsif ($type eq 'lst') {
44 die "invalid type $type";
47 $CONTAINER_UC=uc(${CONTAINER});
48 $STLT="ex".$STLHEADER;
51 $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) (s).reserve(size)";
53 $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) // no reserve needed for ${STLHEADER}";
57 $PREPEND_INTERFACE=<<END_OF_PREPEND_INTERFACE;
58 virtual ${CONTAINER} & prepend(const ex & b);
59 virtual ${CONTAINER} & remove_first(void);
60 END_OF_PREPEND_INTERFACE
62 $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
63 ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
65 ensure_if_modifiable();
70 ${CONTAINER} & ${CONTAINER}::remove_first(void)
72 ensure_if_modifiable();
76 END_OF_PREPEND_IMPLEMENTATION
78 $PREPEND_INTERFACE=" // no prepend possible for ${CONTAINER}";
79 $PREPEND_IMPLEMENTATION="";
83 $SORT_INTERFACE=<<END_OF_SORT_INTERFACE;
84 virtual ${CONTAINER} & sort(void);
85 virtual ${CONTAINER} & unique(void);
88 $SORT_IMPLEMENTATION=<<END_OF_SORT_IMPLEMENTATION;
89 ${CONTAINER} & ${CONTAINER}::sort(void)
91 ensure_if_modifiable();
92 seq.sort(ex_is_less());
96 ${CONTAINER} & ${CONTAINER}::unique(void)
98 ensure_if_modifiable();
99 seq.unique(ex_is_equal());
102 END_OF_SORT_IMPLEMENTATION
104 $SORT_INTERFACE=" // no sort possible for ${CONTAINER}";
105 $SORT_IMPLEMENTATION="";
109 $LET_OP_DEFINITION=<<END_OF_LET_OP_DEFINITION;
110 ex & let_op(size_t i);
111 END_OF_LET_OP_DEFINITION
113 $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION;
114 ex ${CONTAINER}::op(size_t i) const
116 GINAC_ASSERT(i<nops());
118 ${STLT}::const_iterator it=seq.begin();
119 for (size_t j=0; j<i; j++) {
125 ex & ${CONTAINER}::let_op(size_t i)
127 GINAC_ASSERT(i<nops());
129 ensure_if_modifiable();
130 ${STLT}::iterator it=seq.begin();
131 for (size_t j=0; j<i; j++) {
136 END_OF_LET_OP_IMPLEMENTATION
138 $LET_OP_DEFINITION="";
139 $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
143 my ($seq_template,$n,$separator)=@_;
147 for ($N=1; $N<=$n; $N++) {
148 $res .= eval('"' . $seq_template . '"');
156 sub generate_from_to {
157 my ($template,$seq_template1,$seq_separator1,$seq_template2,
158 $seq_separator2,$from,$to)=@_;
162 for ($N=$from; $N<=$to; $N++) {
163 $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
164 $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
165 $res .= eval('"' . $template . '"');
166 $SEQ1=''; # to avoid main::SEQ1 used only once warning
167 $SEQ2=''; # same as above
173 my ($template,$seq_template1,$seq_separator1,$seq_template2,
175 return generate_from_to($template,$seq_template1,$seq_separator1,
176 $seq_template2,$seq_separator2,1,$maxargs);
179 $constructors_interface=generate(
180 ' explicit ${CONTAINER}(${SEQ1});'."\n",
181 'const ex & param${N}',', ','','');
183 $constructors_implementation=generate(
184 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ',' seq.push_back(param${N});',"\n");
185 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
190 END_OF_CONSTRUCTORS_IMPLEMENTATION
192 $interface=<<END_OF_INTERFACE;
193 /** \@file ${CONTAINER}.h
195 * Definition of GiNaC's ${CONTAINER}. */
198 * This file was generated automatically by container.pl.
199 * Please do not modify it directly, edit the perl script instead!
200 * container.pl options: \$CONTAINER=${CONTAINER}
201 * \$STLHEADER=${STLHEADER}
202 * \$reserve=${reserve}
203 * \$prepend=${prepend}
206 * \$open_bracket=${open_bracket}
207 * \$close_bracket=${close_bracket}
208 * \$maxargs=${maxargs}
210 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
212 * This program is free software; you can redistribute it and/or modify
213 * it under the terms of the GNU General Public License as published by
214 * the Free Software Foundation; either version 2 of the License, or
215 * (at your option) any later version.
217 * This program is distributed in the hope that it will be useful,
218 * but WITHOUT ANY WARRANTY; without even the implied warranty of
219 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
220 * GNU General Public License for more details.
222 * You should have received a copy of the GNU General Public License
223 * along with this program; if not, write to the Free Software
224 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
227 #ifndef __GINAC_${CONTAINER_UC}_H__
228 #define __GINAC_${CONTAINER_UC}_H__
230 #include <${STLHEADER}>
232 // CINT needs <algorithm> to work properly with <vector> and <list>
241 typedef std::${STLHEADER}<ex> ${STLT};
243 class ${CONTAINER} : public basic
245 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
248 typedef ${STLT}::const_iterator const_iterator;
249 typedef ${STLT}::const_reverse_iterator const_reverse_iterator;
252 ${CONTAINER}(${STLT} const & s, bool discardable = false);
253 ${CONTAINER}(${STLT} * vp); // vp will be deleted
254 ${CONTAINER}(exvector::const_iterator b, exvector::const_iterator e);
255 ${constructors_interface}
258 void print(const print_context & c, unsigned level = 0) const;
259 unsigned precedence(void) const {return 10;}
260 bool info(unsigned inf) const;
262 ex op(size_t i) const;
264 ex map(map_function & f) const;
265 ex eval(int level=0) const;
266 ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
268 bool is_equal_same_type(const basic & other) const;
270 // new virtual functions which can be overridden by derived classes
272 virtual ${CONTAINER} & append(const ex & b);
273 virtual ${CONTAINER} & remove_last(void);
274 virtual ${CONTAINER} & remove_all(void);
278 virtual void printseq(const print_context & c, char openbracket, char delim,
279 char closebracket, unsigned this_precedence,
280 unsigned upper_precedence = 0) const;
281 virtual ex this${CONTAINER}(${STLT} const & v) const;
282 virtual ex this${CONTAINER}(${STLT} * vp) const;
284 // non-virtual functions in this class
286 const_iterator begin() const {return seq.begin();}
287 const_iterator end() const {return seq.end();}
288 const_reverse_iterator rbegin() const {return seq.rbegin();}
289 const_reverse_iterator rend() const {return seq.rend();}
292 bool is_canonical() const;
293 ${STLT} evalchildren(int level) const;
294 ${STLT} * subschildren(const lst & ls, const lst & lr, unsigned options = 0) const;
302 /** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */
303 template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj)
305 return obj.tinfo()==TINFO_${CONTAINER};
310 #endif // ndef __GINAC_${CONTAINER_UC}_H__
314 $implementation=<<END_OF_IMPLEMENTATION;
315 /** \@file ${CONTAINER}.cpp
317 * Implementation of GiNaC's ${CONTAINER}. */
320 * This file was generated automatically by container.pl.
321 * Please do not modify it directly, edit the perl script instead!
322 * container.pl options: \$CONTAINER=${CONTAINER}
323 * \$STLHEADER=${STLHEADER}
324 * \$reserve=${reserve}
325 * \$prepend=${prepend}
327 * \$open_bracket=${open_bracket}
328 * \$close_bracket=${close_bracket}
329 * \$maxargs=${maxargs}
331 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
333 * This program is free software; you can redistribute it and/or modify
334 * it under the terms of the GNU General Public License as published by
335 * the Free Software Foundation; either version 2 of the License, or
336 * (at your option) any later version.
338 * This program is distributed in the hope that it will be useful,
339 * but WITHOUT ANY WARRANTY; without even the implied warranty of
340 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
341 * GNU General Public License for more details.
343 * You should have received a copy of the GNU General Public License
344 * along with this program; if not, write to the Free Software
345 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
351 #include "${CONTAINER}.h"
358 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
360 ${RESERVE_IMPLEMENTATION}
363 // default ctor, dtor, copy ctor, assignment operator and helpers
368 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER}) {}
372 void ${CONTAINER}::copy(${CONTAINER} const & other)
374 inherited::copy(other);
378 void ${CONTAINER}::destroy(bool call_parent)
381 if (call_parent) inherited::destroy(call_parent);
390 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
393 seq.swap(const_cast<${STLT} &>(s));
399 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
402 // lst(0) ends up here
410 ${CONTAINER}::${CONTAINER}(exvector::const_iterator b, exvector::const_iterator e) : basic(TINFO_${CONTAINER}), seq(b, e)
414 ${constructors_implementation}
420 /** Construct object from archive_node. */
421 ${CONTAINER}::${CONTAINER}(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
423 for (unsigned int i=0; true; i++) {
425 if (n.find_ex("seq", e, sym_lst, i))
432 /** Unarchive the object. */
433 ex ${CONTAINER}::unarchive(const archive_node &n, lst &sym_lst)
435 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
438 /** Archive the object. */
439 void ${CONTAINER}::archive(archive_node &n) const
441 inherited::archive(n);
442 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
450 // functions overriding virtual functions from base classes
455 void ${CONTAINER}::print(const print_context & c, unsigned level) const
457 if (is_a<print_tree>(c)) {
459 c.s << std::string(level, ' ') << class_name()
460 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
461 << ", nops=" << nops()
463 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
464 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
466 i->print(c, level + delta_indent);
469 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
470 } else if (is_a<print_python>(c)) {
471 printseq(c, '[', ',', ']', precedence(), precedence()+1);
472 } else if (is_a<print_python_repr>(c)) {
473 c.s << class_name ();
474 printseq(c, '(', ',', ')', precedence(), precedence()+1);
476 // always print brackets around seq, ignore upper_precedence
477 printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1);
481 // ${CONTAINER}::info() will be implemented by user elsewhere";
483 size_t ${CONTAINER}::nops() const
488 ${LET_OP_IMPLEMENTATION}
490 ex ${CONTAINER}::map(map_function & f) const
492 // This implementation is here because basic::map() uses let_op()
493 // which is not defined for all containers
495 RESERVE(s,seq.size());
496 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
502 return this${CONTAINER}(s);
505 ex ${CONTAINER}::eval(int level) const
510 return this${CONTAINER}(evalchildren(level));
513 ex ${CONTAINER}::subs(const lst & ls, const lst & lr, unsigned options) const
515 ${STLT} *vp = subschildren(ls, lr, options);
517 return ex_to<basic>(this${CONTAINER}(vp)).basic::subs(ls, lr, options);
519 return basic::subs(ls, lr, options);
524 int ${CONTAINER}::compare_same_type(const basic & other) const
526 GINAC_ASSERT(is_a<${CONTAINER}>(other));
527 ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
529 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
530 it2 = o.seq.begin(), it2end = o.seq.end();
532 while (it1 != it1end && it2 != it2end) {
533 int cmpval = it1->compare(*it2);
539 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
542 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
544 GINAC_ASSERT(is_a<${CONTAINER}>(other));
545 ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(other);
547 if (seq.size() != o.seq.size())
550 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
553 while (it1 != it1end) {
554 if (!it1->is_equal(*it2))
563 // new virtual functions which can be overridden by derived classes
568 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
570 ensure_if_modifiable();
575 ${CONTAINER} & ${CONTAINER}::remove_last(void)
577 ensure_if_modifiable();
582 ${CONTAINER} & ${CONTAINER}::remove_all(void)
584 ensure_if_modifiable();
589 ${PREPEND_IMPLEMENTATION}
591 ${SORT_IMPLEMENTATION}
595 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
596 char closebracket, unsigned this_precedence,
597 unsigned upper_precedence) const
599 if (this_precedence <= upper_precedence)
603 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
605 while (it != itend) {
606 it->print(c, this_precedence);
610 it->print(c, this_precedence);
613 if (this_precedence <= upper_precedence)
617 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
619 return ${CONTAINER}(v);
622 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
624 return ${CONTAINER}(vp);
628 // non-virtual functions in this class
637 bool ${CONTAINER}::is_canonical() const
639 if (seq.size()<=1) { return 1; }
641 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
642 ${STLT}::const_iterator it_last=it;
643 for (++it; it!=itend; it_last=it, ++it) {
644 if (it_last->compare(*it)>0) {
645 if (it_last->compare(*it)>0) {
646 std::cout << *it_last << ">" << *it << "\\n";
655 ${STLT} ${CONTAINER}::evalchildren(int level) const
658 RESERVE(s,seq.size());
663 if (level == -max_recursion_level) {
664 throw(std::runtime_error("max recursion level reached"));
667 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
668 while (it != itend) {
669 s.push_back(it->eval(level));
675 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, unsigned options) const
677 // returns a NULL pointer if nothing had to be substituted
678 // returns a pointer to a newly created epvector otherwise
679 // (which has to be deleted somewhere else)
681 ${STLT}::const_iterator cit = seq.begin(), end = seq.end();
683 const ex & subsed_ex = cit->subs(ls, lr, options);
684 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
686 // something changed, copy seq, subs and return it
687 ${STLT} *s=new ${STLT};
688 RESERVE(*s, seq.size());
690 // copy parts of seq which are known not to have changed
691 ${STLT}::const_iterator cit2 = seq.begin();
692 while (cit2 != cit) {
697 // copy first changed element
698 s->push_back(subsed_ex);
702 while (cit2 != end) {
704 s->push_back(cit2->subs(ls, lr, options));
716 return 0; // nothing has changed
721 END_OF_IMPLEMENTATION
723 print "Creating interface file ${CONTAINER}.h...";
724 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
725 print OUT $interface;
729 print "Creating implementation file ${CONTAINER}.cpp...";
730 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
731 print OUT $implementation;