3 if (($#ARGV!=0) and ($#ARGV!=1)) {
4 die 'usage: container.pl type [maxargs] (type=lst or exprseq)';
7 if ($ARGV[0] eq 'lst') {
9 } elsif ($ARGV[0] eq 'exprseq') {
12 die 'only lst and exprseq supported';
18 $maxargs=15; # must be greater or equal than the value used in function.pl
21 if ($type eq 'exprseq') {
23 # settings for exprseq
32 } 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 END_OF_PREPEND_INTERFACE
61 $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
62 ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
64 ensure_if_modifiable();
68 END_OF_PREPEND_IMPLEMENTATION
70 $PREPEND_INTERFACE=" // no prepend possible for ${CONTAINER}";
71 $PREPEND_IMPLEMENTATION="";
75 $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
76 ex & ${CONTAINER}::let_op(int i)
79 GINAC_ASSERT(i<nops());
81 ${STLT}::iterator it=seq.begin();
82 for (int j=0; j<i; j++) {
87 END_OF_LET_OP_IMPLEMENTATION
89 $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
93 my ($seq_template,$n,$separator)=@_;
97 for ($N=1; $N<=$n; $N++) {
98 $res .= eval('"' . $seq_template . '"');
106 sub generate_from_to {
107 my ($template,$seq_template1,$seq_separator1,$seq_template2,
108 $seq_separator2,$from,$to)=@_;
112 for ($N=$from; $N<=$to; $N++) {
113 $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
114 $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
115 $res .= eval('"' . $template . '"');
116 $SEQ1=''; # to avoid main::SEQ1 used only once warning
117 $SEQ2=''; # same as above
123 my ($template,$seq_template1,$seq_separator1,$seq_template2,
125 return generate_from_to($template,$seq_template1,$seq_separator1,
126 $seq_template2,$seq_separator2,1,$maxargs);
129 $constructors_interface=generate(
130 ' explicit ${CONTAINER}(${SEQ1});'."\n",
131 'const ex & param${N}',', ','','');
133 $constructors_implementation=generate(
134 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ',' seq.push_back(param${N});',"\n");
135 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
137 debugmsg(\"${CONTAINER} constructor from ${N}*ex\",LOGLEVEL_CONSTRUCT);
141 END_OF_CONSTRUCTORS_IMPLEMENTATION
143 $interface=<<END_OF_INTERFACE;
144 /** \@file ${CONTAINER}.h
146 * Definition of GiNaC's ${CONTAINER}. */
149 * This file was generated automatically by container.pl.
150 * Please do not modify it directly, edit the perl script instead!
151 * container.pl options: \$CONTAINER=${CONTAINER}
152 * \$STLHEADER=${STLHEADER}
153 * \$reserve=${reserve}
154 * \$prepend=${prepend}
156 * \$open_bracket=${open_bracket}
157 * \$close_bracket=${close_bracket}
158 * \$maxargs=${maxargs}
160 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
162 * This program is free software; you can redistribute it and/or modify
163 * it under the terms of the GNU General Public License as published by
164 * the Free Software Foundation; either version 2 of the License, or
165 * (at your option) any later version.
167 * This program is distributed in the hope that it will be useful,
168 * but WITHOUT ANY WARRANTY; without even the implied warranty of
169 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
170 * GNU General Public License for more details.
172 * You should have received a copy of the GNU General Public License
173 * along with this program; if not, write to the Free Software
174 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
177 #ifndef __GINAC_${CONTAINER_UC}_H__
178 #define __GINAC_${CONTAINER_UC}_H__
180 #include <${STLHEADER}>
182 // CINT needs <algorithm> to work properly with <vector> and <list>
188 #ifndef NO_NAMESPACE_GINAC
190 #endif // ndef NO_NAMESPACE_GINAC
192 // typedef ${STLHEADER}<ex> ${STLT};
193 typedef ${STLHEADER}<ex,malloc_alloc> ${STLT}; // CINT does not like ${STLHEADER}<...,default_alloc>
195 class ${CONTAINER} : public basic
197 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
202 ${CONTAINER}(${CONTAINER} const & other);
203 ${CONTAINER} const & operator=(${CONTAINER} const & other);
205 void copy(${CONTAINER} const & other);
206 void destroy(bool call_parent);
209 ${CONTAINER}(${STLT} const & s, bool discardable=0);
210 ${CONTAINER}(${STLT} * vp); // vp will be deleted
211 ${constructors_interface}
214 basic * duplicate() const;
215 void printraw(ostream & os) const;
216 void print(ostream & os, unsigned upper_precedence=0) const;
217 void printtree(ostream & os, unsigned indent) const;
218 bool info(unsigned inf) const;
219 unsigned nops() const;
221 ex expand(unsigned options=0) const;
222 bool has(const ex & other) const;
223 ex eval(int level=0) const;
224 ex evalf(int level=0) const;
225 ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
226 ex derivative(const symbol & s) const;
227 ex subs(const lst & ls, const lst & lr) const;
229 int compare_same_type(const basic & other) const;
230 bool is_equal_same_type(const basic & other) const;
231 unsigned return_type(void) const;
233 // new virtual functions which can be overridden by derived classes
235 virtual ${CONTAINER} & append(const ex & b);
238 virtual void printseq(ostream & os, char openbracket, char delim,
239 char closebracket, unsigned this_precedence,
240 unsigned upper_precedence=0) const;
241 virtual ex this${CONTAINER}(${STLT} const & v) const;
242 virtual ex this${CONTAINER}(${STLT} * vp) const;
245 bool is_canonical() const;
246 ${STLT} evalchildren(int level) const;
247 ${STLT} evalfchildren(int level) const;
248 ${STLT} normalchildren(int level) const;
249 ${STLT} diffchildren(const symbol & s) const;
250 ${STLT} * subschildren(const lst & ls, const lst & lr) const;
254 static unsigned precedence;
259 extern const ${CONTAINER} some_${CONTAINER};
260 extern const type_info & typeid_${CONTAINER};
263 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
265 return static_cast<const ${CONTAINER} &>(*e.bp);
268 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
270 return static_cast<${CONTAINER} &>(*e.bp);
273 #ifndef NO_NAMESPACE_GINAC
275 #endif // ndef NO_NAMESPACE_GINAC
277 #endif // ndef __GINAC_${CONTAINER_UC}_H__
281 $implementation=<<END_OF_IMPLEMENTATION;
282 /** \@file ${CONTAINER}.cpp
284 * Implementation of GiNaC's ${CONTAINER}. */
287 * This file was generated automatically by container.pl.
288 * Please do not modify it directly, edit the perl script instead!
289 * container.pl options: \$CONTAINER=${CONTAINER}
290 * \$STLHEADER=${STLHEADER}
291 * \$reserve=${reserve}
292 * \$prepend=${prepend}
294 * \$open_bracket=${open_bracket}
295 * \$close_bracket=${close_bracket}
296 * \$maxargs=${maxargs}
298 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
300 * This program is free software; you can redistribute it and/or modify
301 * it under the terms of the GNU General Public License as published by
302 * the Free Software Foundation; either version 2 of the License, or
303 * (at your option) any later version.
305 * This program is distributed in the hope that it will be useful,
306 * but WITHOUT ANY WARRANTY; without even the implied warranty of
307 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
308 * GNU General Public License for more details.
310 * You should have received a copy of the GNU General Public License
311 * along with this program; if not, write to the Free Software
312 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
318 #include "${CONTAINER}.h"
321 #include "debugmsg.h"
323 #ifndef NO_NAMESPACE_GINAC
325 #endif // ndef NO_NAMESPACE_GINAC
327 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
329 ${RESERVE_IMPLEMENTATION}
332 // default constructor, destructor, copy constructor assignment operator and helpers
337 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
339 debugmsg("${CONTAINER} default constructor",LOGLEVEL_CONSTRUCT);
342 ${CONTAINER}::~${CONTAINER}()
344 debugmsg("${CONTAINER} destructor",LOGLEVEL_DESTRUCT);
348 ${CONTAINER}::${CONTAINER}(${CONTAINER} const & other)
350 debugmsg("${CONTAINER} copy constructor",LOGLEVEL_CONSTRUCT);
354 ${CONTAINER} const & ${CONTAINER}::operator=(${CONTAINER} const & other)
356 debugmsg("${CONTAINER} operator=",LOGLEVEL_ASSIGNMENT);
357 if (this != &other) {
366 void ${CONTAINER}::copy(${CONTAINER} const & other)
368 inherited::copy(other);
372 void ${CONTAINER}::destroy(bool call_parent)
375 if (call_parent) inherited::destroy(call_parent);
379 // other constructors
384 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
386 debugmsg("${CONTAINER} constructor from ${STLT}",
389 seq.swap(const_cast<${STLT} &>(s));
395 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
397 debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT);
403 ${constructors_implementation}
409 /** Construct object from archive_node. */
410 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
412 debugmsg("${CONTAINER} constructor from archive_node", LOGLEVEL_CONSTRUCT);
413 for (unsigned int i=0; true; i++) {
415 if (n.find_ex("seq", e, sym_lst, i))
422 /** Unarchive the object. */
423 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
425 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
428 /** Archive the object. */
429 void ${CONTAINER}::archive(archive_node &n) const
431 inherited::archive(n);
432 ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
440 // functions overriding virtual functions from bases classes
445 basic * ${CONTAINER}::duplicate() const
447 debugmsg("${CONTAINER} duplicate",LOGLEVEL_DUPLICATE);
448 return new ${CONTAINER}(*this);
451 void ${CONTAINER}::printraw(ostream & os) const
453 debugmsg("${CONTAINER} printraw",LOGLEVEL_PRINT);
455 os << "${CONTAINER}(";
456 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
457 (*cit).bp->printraw(os);
463 void ${CONTAINER}::print(ostream & os, unsigned upper_precedence) const
465 debugmsg("${CONTAINER} print",LOGLEVEL_PRINT);
466 // always print brackets around seq, ignore upper_precedence
467 printseq(os,'${open_bracket}',',','${close_bracket}',precedence,precedence+1);
470 void ${CONTAINER}::printtree(ostream & os, unsigned indent) const
472 debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT);
474 os << string(indent,' ') << "type=" << typeid(*this).name()
475 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
476 << ", flags=" << flags
477 << ", nops=" << nops() << endl;
478 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
479 (*cit).printtree(os,indent+delta_indent);
481 os << string(indent+delta_indent,' ') << "=====" << endl;
484 // ${CONTAINER}::info() will be implemented by user elsewhere";
486 unsigned ${CONTAINER}::nops() const
491 ${LET_OP_IMPLEMENTATION}
493 ex ${CONTAINER}::expand(unsigned options) const
496 RESERVE(s,seq.size());
497 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
498 s.push_back((*it).expand(options));
501 return this${CONTAINER}(s);
504 // a ${CONTAINER} 'has' an expression if it is this expression itself or a child 'has' it
506 bool ${CONTAINER}::has(const ex & other) const
508 GINAC_ASSERT(other.bp!=0);
509 if (is_equal(*other.bp)) return true;
510 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
511 if ((*it).has(other)) return true;
516 ex ${CONTAINER}::eval(int level) const
521 return this${CONTAINER}(evalchildren(level));
524 ex ${CONTAINER}::evalf(int level) const
526 return this${CONTAINER}(evalfchildren(level));
529 /** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments
530 * and replaces the ${CONTAINER} by a temporary symbol.
531 * \@see ex::normal */
532 ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const
534 ex n=this${CONTAINER}(normalchildren(level));
535 return n.bp->basic::normal(sym_lst,repl_lst,level);
538 ex ${CONTAINER}::derivative(const symbol & s) const
540 return this${CONTAINER}(diffchildren(s));
543 ex ${CONTAINER}::subs(const lst & ls, const lst & lr) const
545 ${STLT} * vp=subschildren(ls,lr);
549 return this${CONTAINER}(vp);
554 int ${CONTAINER}::compare_same_type(const basic & other) const
556 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
557 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
558 (const_cast<basic &>(other));
560 ${STLT}::const_iterator it1=seq.begin();
561 ${STLT}::const_iterator it2=o.seq.begin();
563 for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
564 cmpval=(*it1).compare(*it2);
565 if (cmpval!=0) return cmpval;
568 if (it1==seq.end()) {
569 return (it2==o.seq.end() ? 0 : -1);
575 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
577 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
578 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
579 (const_cast<basic &>(other));
580 if (seq.size()!=o.seq.size()) return false;
582 ${STLT}::const_iterator it1=seq.begin();
583 ${STLT}::const_iterator it2=o.seq.begin();
585 for (; it1!=seq.end(); ++it1, ++it2) {
586 if (!(*it1).is_equal(*it2)) return false;
592 unsigned ${CONTAINER}::return_type(void) const
594 return return_types::noncommutative_composite;
598 // new virtual functions which can be overridden by derived classes
603 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
605 ensure_if_modifiable();
610 ${PREPEND_IMPLEMENTATION}
614 void ${CONTAINER}::printseq(ostream & os, char openbracket, char delim,
615 char closebracket, unsigned this_precedence,
616 unsigned upper_precedence) const
618 if (this_precedence<=upper_precedence) os << openbracket;
620 ${STLT}::const_iterator it,it_last;
624 for (; it!=it_last; ++it) {
625 (*it).bp->print(os,this_precedence);
628 (*it).bp->print(os,this_precedence);
630 if (this_precedence<=upper_precedence) os << closebracket;
633 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
635 return ${CONTAINER}(v);
638 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
640 return ${CONTAINER}(vp);
644 // non-virtual functions in this class
653 bool ${CONTAINER}::is_canonical() const
655 if (seq.size()<=1) { return 1; }
657 ${STLT}::const_iterator it=seq.begin();
658 ${STLT}::const_iterator it_last=it;
659 for (++it; it!=seq.end(); it_last=it, ++it) {
660 if ((*it_last).compare(*it)>0) {
661 if ((*it_last).compare(*it)>0) {
662 cout << *it_last << ">" << *it << "\\n";
671 ${STLT} ${CONTAINER}::evalchildren(int level) const
674 RESERVE(s,seq.size());
679 if (level == -max_recursion_level) {
680 throw(std::runtime_error("max recursion level reached"));
683 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
684 s.push_back((*it).eval(level));
689 ${STLT} ${CONTAINER}::evalfchildren(int level) const
692 RESERVE(s,seq.size());
697 if (level == -max_recursion_level) {
698 throw(std::runtime_error("max recursion level reached"));
701 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
702 s.push_back((*it).evalf(level));
707 ${STLT} ${CONTAINER}::normalchildren(int level) const
710 RESERVE(s,seq.size());
715 if (level == -max_recursion_level) {
716 throw(std::runtime_error("max recursion level reached"));
719 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
720 s.push_back((*it).normal(level));
725 ${STLT} ${CONTAINER}::diffchildren(const symbol & y) const
728 RESERVE(s,seq.size());
729 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
730 s.push_back((*it).diff(y));
735 /* obsolete subschildren
736 ${STLT} ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
739 RESERVE(s,seq.size());
740 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
741 s.push_back((*it).subs(ls,lr));
747 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
749 // returns a NULL pointer if nothing had to be substituted
750 // returns a pointer to a newly created epvector otherwise
751 // (which has to be deleted somewhere else)
753 ${STLT}::const_iterator last=seq.end();
754 ${STLT}::const_iterator cit=seq.begin();
756 const ex & subsed_ex=(*cit).subs(ls,lr);
757 if (!are_ex_trivially_equal(*cit,subsed_ex)) {
759 // something changed, copy seq, subs and return it
760 ${STLT} *s=new ${STLT};
761 RESERVE(*s,seq.size());
763 // copy parts of seq which are known not to have changed
764 ${STLT}::const_iterator cit2=seq.begin();
769 // copy first changed element
770 s->push_back(subsed_ex);
774 s->push_back((*cit2).subs(ls,lr));
782 return 0; // nothing has changed
786 // static member variables
791 unsigned ${CONTAINER}::precedence=10;
797 const ${CONTAINER} some_${CONTAINER};
798 const type_info & typeid_${CONTAINER}=typeid(some_${CONTAINER});
800 #ifndef NO_NAMESPACE_GINAC
802 #endif // ndef NO_NAMESPACE_GINAC
804 END_OF_IMPLEMENTATION
806 print "Creating interface file ${CONTAINER}.h...";
807 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
808 print OUT $interface;
812 print "Creating implementation file ${CONTAINER}.cpp...";
813 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
814 print OUT $implementation;