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=15; # must be greater or equal than the value used in function.pl
19 if ($type eq 'exprseq') {
21 # settings for exprseq
30 } elsif ($type eq 'lst') {
42 die "invalid type $type";
45 $CONTAINER_UC=uc(${CONTAINER});
46 $STLT="ex".$STLHEADER;
49 $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) (s).reserve(size)";
51 $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) // no reserve needed for ${STLHEADER}";
55 $PREPEND_INTERFACE=<<END_OF_PREPEND_INTERFACE;
56 virtual ${CONTAINER} & prepend(const ex & b);
57 END_OF_PREPEND_INTERFACE
59 $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
60 ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
62 ensure_if_modifiable();
66 END_OF_PREPEND_IMPLEMENTATION
68 $PREPEND_INTERFACE=" // no prepend possible for ${CONTAINER}";
69 $PREPEND_IMPLEMENTATION="";
73 $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
74 ex & ${CONTAINER}::let_op(int i)
77 GINAC_ASSERT(i<nops());
79 ${STLT}::iterator it=seq.begin();
80 for (int j=0; j<i; j++) {
85 END_OF_LET_OP_IMPLEMENTATION
87 $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
91 my ($seq_template,$n,$separator)=@_;
95 for ($N=1; $N<=$n; $N++) {
96 $res .= eval('"' . $seq_template . '"');
104 sub generate_from_to {
105 my ($template,$seq_template1,$seq_separator1,$seq_template2,
106 $seq_separator2,$from,$to)=@_;
110 for ($N=$from; $N<=$to; $N++) {
111 $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
112 $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
113 $res .= eval('"' . $template . '"');
114 $SEQ1=''; # to avoid main::SEQ1 used only once warning
115 $SEQ2=''; # same as above
121 my ($template,$seq_template1,$seq_separator1,$seq_template2,
123 return generate_from_to($template,$seq_template1,$seq_separator1,
124 $seq_template2,$seq_separator2,1,$maxargs);
127 $constructors_interface=generate(
128 ' explicit ${CONTAINER}(${SEQ1});'."\n",
129 'const ex & param${N}',', ','','');
131 $constructors_implementation=generate(
132 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ',' seq.push_back(param${N});',"\n");
133 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
135 debugmsg(\"${CONTAINER} constructor from ${N}*ex\",LOGLEVEL_CONSTRUCT);
139 END_OF_CONSTRUCTORS_IMPLEMENTATION
141 $interface=<<END_OF_INTERFACE;
142 /** \@file ${CONTAINER}.h
144 * Definition of GiNaC's ${CONTAINER}. */
147 * This file was generated automatically by container.pl.
148 * Please do not modify it directly, edit the perl script instead!
149 * container.pl options: \$CONTAINER=${CONTAINER}
150 * \$STLHEADER=${STLHEADER}
151 * \$reserve=${reserve}
152 * \$prepend=${prepend}
154 * \$open_bracket=${open_bracket}
155 * \$close_bracket=${close_bracket}
156 * \$maxargs=${maxargs}
158 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
160 * This program is free software; you can redistribute it and/or modify
161 * it under the terms of the GNU General Public License as published by
162 * the Free Software Foundation; either version 2 of the License, or
163 * (at your option) any later version.
165 * This program is distributed in the hope that it will be useful,
166 * but WITHOUT ANY WARRANTY; without even the implied warranty of
167 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
168 * GNU General Public License for more details.
170 * You should have received a copy of the GNU General Public License
171 * along with this program; if not, write to the Free Software
172 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
175 #ifndef __GINAC_${CONTAINER_UC}_H__
176 #define __GINAC_${CONTAINER_UC}_H__
178 #include <${STLHEADER}>
180 // CINT needs <algorithm> to work properly with <vector> and <list>
186 #ifndef NO_NAMESPACE_GINAC
188 #endif // ndef NO_NAMESPACE_GINAC
191 // Cint does not like ${STLHEADER}<..,default_alloc> but malloc_alloc is
192 // unstandardized and not supported by newer GCCs.
193 #if defined(__GNUC__) && ((__GNUC__ == 2) && (__GNUC_MINOR__ < 97))
194 typedef std::${STLHEADER}<ex,malloc_alloc> ${STLT};
196 typedef std::${STLHEADER}<ex> ${STLT};
199 class ${CONTAINER} : public basic
201 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
204 ${CONTAINER}(${STLT} const & s, bool discardable=0);
205 ${CONTAINER}(${STLT} * vp); // vp will be deleted
206 ${constructors_interface}
209 void printraw(std::ostream & os) const;
210 void print(std::ostream & os, unsigned upper_precedence=0) const;
211 void printtree(std::ostream & os, unsigned indent) const;
212 bool info(unsigned inf) const;
213 unsigned nops() const;
215 ex expand(unsigned options=0) const;
216 bool has(const ex & other) const;
217 ex eval(int level=0) const;
218 ex evalf(int level=0) const;
219 ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
220 ex derivative(const symbol & s) const;
221 ex subs(const lst & ls, const lst & lr) const;
223 bool is_equal_same_type(const basic & other) const;
224 unsigned return_type(void) const;
226 // new virtual functions which can be overridden by derived classes
228 virtual ${CONTAINER} & append(const ex & b);
231 virtual void printseq(std::ostream & os, char openbracket, char delim,
232 char closebracket, unsigned this_precedence,
233 unsigned upper_precedence=0) const;
234 virtual ex this${CONTAINER}(${STLT} const & v) const;
235 virtual ex this${CONTAINER}(${STLT} * vp) const;
238 bool is_canonical() const;
239 ${STLT} evalchildren(int level) const;
240 ${STLT} evalfchildren(int level) const;
241 ${STLT} normalchildren(int level) const;
242 ${STLT} diffchildren(const symbol & s) const;
243 ${STLT} * subschildren(const lst & ls, const lst & lr) const;
247 static unsigned precedence;
252 extern const ${CONTAINER} some_${CONTAINER};
253 extern const std::type_info & typeid_${CONTAINER};
256 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
258 return static_cast<const ${CONTAINER} &>(*e.bp);
261 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
263 return static_cast<${CONTAINER} &>(*e.bp);
266 #ifndef NO_NAMESPACE_GINAC
268 #endif // ndef NO_NAMESPACE_GINAC
270 #endif // ndef __GINAC_${CONTAINER_UC}_H__
274 $implementation=<<END_OF_IMPLEMENTATION;
275 /** \@file ${CONTAINER}.cpp
277 * Implementation of GiNaC's ${CONTAINER}. */
280 * This file was generated automatically by container.pl.
281 * Please do not modify it directly, edit the perl script instead!
282 * container.pl options: \$CONTAINER=${CONTAINER}
283 * \$STLHEADER=${STLHEADER}
284 * \$reserve=${reserve}
285 * \$prepend=${prepend}
287 * \$open_bracket=${open_bracket}
288 * \$close_bracket=${close_bracket}
289 * \$maxargs=${maxargs}
291 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
293 * This program is free software; you can redistribute it and/or modify
294 * it under the terms of the GNU General Public License as published by
295 * the Free Software Foundation; either version 2 of the License, or
296 * (at your option) any later version.
298 * This program is distributed in the hope that it will be useful,
299 * but WITHOUT ANY WARRANTY; without even the implied warranty of
300 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
301 * GNU General Public License for more details.
303 * You should have received a copy of the GNU General Public License
304 * along with this program; if not, write to the Free Software
305 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
311 #include "${CONTAINER}.h"
314 #include "debugmsg.h"
316 #ifndef NO_NAMESPACE_GINAC
318 #endif // ndef NO_NAMESPACE_GINAC
320 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
322 ${RESERVE_IMPLEMENTATION}
325 // default constructor, destructor, copy constructor assignment operator and helpers
330 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
332 debugmsg("${CONTAINER} default constructor",LOGLEVEL_CONSTRUCT);
337 void ${CONTAINER}::copy(${CONTAINER} const & other)
339 inherited::copy(other);
343 void ${CONTAINER}::destroy(bool call_parent)
346 if (call_parent) inherited::destroy(call_parent);
350 // other constructors
355 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
357 debugmsg("${CONTAINER} constructor from ${STLT}", LOGLEVEL_CONSTRUCT);
359 seq.swap(const_cast<${STLT} &>(s));
365 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
367 debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT);
373 ${constructors_implementation}
379 /** Construct object from archive_node. */
380 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
382 debugmsg("${CONTAINER} constructor from archive_node", LOGLEVEL_CONSTRUCT);
383 for (unsigned int i=0; true; i++) {
385 if (n.find_ex("seq", e, sym_lst, i))
392 /** Unarchive the object. */
393 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
395 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
398 /** Archive the object. */
399 void ${CONTAINER}::archive(archive_node &n) const
401 inherited::archive(n);
402 ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
410 // functions overriding virtual functions from bases classes
415 void ${CONTAINER}::printraw(std::ostream & os) const
417 debugmsg("${CONTAINER} printraw",LOGLEVEL_PRINT);
419 os << "${CONTAINER}(";
420 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
421 (*cit).bp->printraw(os);
427 void ${CONTAINER}::print(std::ostream & os, unsigned upper_precedence) const
429 debugmsg("${CONTAINER} print",LOGLEVEL_PRINT);
430 // always print brackets around seq, ignore upper_precedence
431 printseq(os,'${open_bracket}',',','${close_bracket}',precedence,precedence+1);
434 void ${CONTAINER}::printtree(std::ostream & os, unsigned indent) const
436 debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT);
438 os << std::string(indent,' ') << "type=" << class_name()
439 << ", hash=" << hashvalue
440 << " (0x" << std::hex << hashvalue << std::dec << ")"
441 << ", flags=" << flags
442 << ", nops=" << nops() << std::endl;
443 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
444 (*cit).printtree(os,indent+delta_indent);
446 os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
449 // ${CONTAINER}::info() will be implemented by user elsewhere";
451 unsigned ${CONTAINER}::nops() const
456 ${LET_OP_IMPLEMENTATION}
458 ex ${CONTAINER}::expand(unsigned options) const
461 RESERVE(s,seq.size());
462 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
463 s.push_back((*it).expand(options));
466 return this${CONTAINER}(s);
469 // a ${CONTAINER} 'has' an expression if it is this expression itself or a child 'has' it
471 bool ${CONTAINER}::has(const ex & other) const
473 GINAC_ASSERT(other.bp!=0);
474 if (is_equal(*other.bp)) return true;
475 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
476 if ((*it).has(other)) return true;
481 ex ${CONTAINER}::eval(int level) const
486 return this${CONTAINER}(evalchildren(level));
489 ex ${CONTAINER}::evalf(int level) const
491 return this${CONTAINER}(evalfchildren(level));
494 /** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments
495 * and replaces the ${CONTAINER} by a temporary symbol.
496 * \@see ex::normal */
497 ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const
499 ex n=this${CONTAINER}(normalchildren(level));
500 return n.bp->basic::normal(sym_lst,repl_lst,level);
503 ex ${CONTAINER}::derivative(const symbol & s) const
505 return this${CONTAINER}(diffchildren(s));
508 ex ${CONTAINER}::subs(const lst & ls, const lst & lr) const
510 ${STLT} * vp=subschildren(ls,lr);
514 return this${CONTAINER}(vp);
519 int ${CONTAINER}::compare_same_type(const basic & other) const
521 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
522 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
523 (const_cast<basic &>(other));
525 ${STLT}::const_iterator it1=seq.begin();
526 ${STLT}::const_iterator it2=o.seq.begin();
528 for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
529 cmpval=(*it1).compare(*it2);
530 if (cmpval!=0) return cmpval;
533 if (it1==seq.end()) {
534 return (it2==o.seq.end() ? 0 : -1);
540 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
542 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
543 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
544 (const_cast<basic &>(other));
545 if (seq.size()!=o.seq.size()) return false;
547 ${STLT}::const_iterator it1=seq.begin();
548 ${STLT}::const_iterator it2=o.seq.begin();
550 for (; it1!=seq.end(); ++it1, ++it2) {
551 if (!(*it1).is_equal(*it2)) return false;
557 unsigned ${CONTAINER}::return_type(void) const
559 return return_types::noncommutative_composite;
563 // new virtual functions which can be overridden by derived classes
568 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
570 ensure_if_modifiable();
575 ${PREPEND_IMPLEMENTATION}
579 void ${CONTAINER}::printseq(std::ostream & os, char openbracket, char delim,
580 char closebracket, unsigned this_precedence,
581 unsigned upper_precedence) const
583 if (this_precedence<=upper_precedence) os << openbracket;
585 ${STLT}::const_iterator it,it_last;
589 for (; it!=it_last; ++it) {
590 (*it).bp->print(os,this_precedence);
593 (*it).bp->print(os,this_precedence);
595 if (this_precedence<=upper_precedence) os << closebracket;
598 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
600 return ${CONTAINER}(v);
603 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
605 return ${CONTAINER}(vp);
609 // non-virtual functions in this class
618 bool ${CONTAINER}::is_canonical() const
620 if (seq.size()<=1) { return 1; }
622 ${STLT}::const_iterator it=seq.begin();
623 ${STLT}::const_iterator it_last=it;
624 for (++it; it!=seq.end(); it_last=it, ++it) {
625 if ((*it_last).compare(*it)>0) {
626 if ((*it_last).compare(*it)>0) {
627 std::cout << *it_last << ">" << *it << "\\n";
636 ${STLT} ${CONTAINER}::evalchildren(int level) const
639 RESERVE(s,seq.size());
644 if (level == -max_recursion_level) {
645 throw(std::runtime_error("max recursion level reached"));
648 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
649 s.push_back((*it).eval(level));
654 ${STLT} ${CONTAINER}::evalfchildren(int level) const
657 RESERVE(s,seq.size());
662 if (level == -max_recursion_level) {
663 throw(std::runtime_error("max recursion level reached"));
666 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
667 s.push_back((*it).evalf(level));
672 ${STLT} ${CONTAINER}::normalchildren(int level) const
675 RESERVE(s,seq.size());
680 if (level == -max_recursion_level) {
681 throw(std::runtime_error("max recursion level reached"));
684 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
685 s.push_back((*it).normal(level));
690 ${STLT} ${CONTAINER}::diffchildren(const symbol & y) const
693 RESERVE(s,seq.size());
694 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
695 s.push_back((*it).diff(y));
700 /* obsolete subschildren
701 ${STLT} ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
704 RESERVE(s,seq.size());
705 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
706 s.push_back((*it).subs(ls,lr));
712 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
714 // returns a NULL pointer if nothing had to be substituted
715 // returns a pointer to a newly created epvector otherwise
716 // (which has to be deleted somewhere else)
718 ${STLT}::const_iterator last=seq.end();
719 ${STLT}::const_iterator cit=seq.begin();
721 const ex & subsed_ex=(*cit).subs(ls,lr);
722 if (!are_ex_trivially_equal(*cit,subsed_ex)) {
724 // something changed, copy seq, subs and return it
725 ${STLT} *s=new ${STLT};
726 RESERVE(*s,seq.size());
728 // copy parts of seq which are known not to have changed
729 ${STLT}::const_iterator cit2=seq.begin();
734 // copy first changed element
735 s->push_back(subsed_ex);
739 s->push_back((*cit2).subs(ls,lr));
747 return 0; // nothing has changed
751 // static member variables
756 unsigned ${CONTAINER}::precedence=10;
762 const ${CONTAINER} some_${CONTAINER};
763 const std::type_info & typeid_${CONTAINER} = typeid(some_${CONTAINER});
765 #ifndef NO_NAMESPACE_GINAC
767 #endif // ndef NO_NAMESPACE_GINAC
769 END_OF_IMPLEMENTATION
771 print "Creating interface file ${CONTAINER}.h...";
772 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
773 print OUT $interface;
777 print "Creating implementation file ${CONTAINER}.cpp...";
778 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
779 print OUT $implementation;