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 basic * duplicate() const;
210 void printraw(std::ostream & os) const;
211 void print(std::ostream & os, unsigned upper_precedence=0) const;
212 void printtree(std::ostream & os, unsigned indent) const;
213 bool info(unsigned inf) const;
214 unsigned nops() const;
216 ex expand(unsigned options=0) const;
217 bool has(const ex & other) const;
218 ex eval(int level=0) const;
219 ex evalf(int level=0) const;
220 ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
221 ex derivative(const symbol & s) const;
222 ex subs(const lst & ls, const lst & lr) const;
224 int compare_same_type(const basic & other) const;
225 bool is_equal_same_type(const basic & other) const;
226 unsigned return_type(void) const;
228 // new virtual functions which can be overridden by derived classes
230 virtual ${CONTAINER} & append(const ex & b);
233 virtual void printseq(std::ostream & os, char openbracket, char delim,
234 char closebracket, unsigned this_precedence,
235 unsigned upper_precedence=0) const;
236 virtual ex this${CONTAINER}(${STLT} const & v) const;
237 virtual ex this${CONTAINER}(${STLT} * vp) const;
240 bool is_canonical() const;
241 ${STLT} evalchildren(int level) const;
242 ${STLT} evalfchildren(int level) const;
243 ${STLT} normalchildren(int level) const;
244 ${STLT} diffchildren(const symbol & s) const;
245 ${STLT} * subschildren(const lst & ls, const lst & lr) const;
249 static unsigned precedence;
254 extern const ${CONTAINER} some_${CONTAINER};
255 extern const std::type_info & typeid_${CONTAINER};
258 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
260 return static_cast<const ${CONTAINER} &>(*e.bp);
263 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
265 return static_cast<${CONTAINER} &>(*e.bp);
268 #ifndef NO_NAMESPACE_GINAC
270 #endif // ndef NO_NAMESPACE_GINAC
272 #endif // ndef __GINAC_${CONTAINER_UC}_H__
276 $implementation=<<END_OF_IMPLEMENTATION;
277 /** \@file ${CONTAINER}.cpp
279 * Implementation of GiNaC's ${CONTAINER}. */
282 * This file was generated automatically by container.pl.
283 * Please do not modify it directly, edit the perl script instead!
284 * container.pl options: \$CONTAINER=${CONTAINER}
285 * \$STLHEADER=${STLHEADER}
286 * \$reserve=${reserve}
287 * \$prepend=${prepend}
289 * \$open_bracket=${open_bracket}
290 * \$close_bracket=${close_bracket}
291 * \$maxargs=${maxargs}
293 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
295 * This program is free software; you can redistribute it and/or modify
296 * it under the terms of the GNU General Public License as published by
297 * the Free Software Foundation; either version 2 of the License, or
298 * (at your option) any later version.
300 * This program is distributed in the hope that it will be useful,
301 * but WITHOUT ANY WARRANTY; without even the implied warranty of
302 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
303 * GNU General Public License for more details.
305 * You should have received a copy of the GNU General Public License
306 * along with this program; if not, write to the Free Software
307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
313 #include "${CONTAINER}.h"
316 #include "debugmsg.h"
318 #ifndef NO_NAMESPACE_GINAC
320 #endif // ndef NO_NAMESPACE_GINAC
322 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
324 ${RESERVE_IMPLEMENTATION}
327 // default constructor, destructor, copy constructor assignment operator and helpers
332 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
334 debugmsg("${CONTAINER} default constructor",LOGLEVEL_CONSTRUCT);
339 void ${CONTAINER}::copy(${CONTAINER} const & other)
341 inherited::copy(other);
345 void ${CONTAINER}::destroy(bool call_parent)
348 if (call_parent) inherited::destroy(call_parent);
352 // other constructors
357 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
359 debugmsg("${CONTAINER} constructor from ${STLT}", LOGLEVEL_CONSTRUCT);
361 seq.swap(const_cast<${STLT} &>(s));
367 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
369 debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT);
375 ${constructors_implementation}
381 /** Construct object from archive_node. */
382 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
384 debugmsg("${CONTAINER} constructor from archive_node", LOGLEVEL_CONSTRUCT);
385 for (unsigned int i=0; true; i++) {
387 if (n.find_ex("seq", e, sym_lst, i))
394 /** Unarchive the object. */
395 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
397 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
400 /** Archive the object. */
401 void ${CONTAINER}::archive(archive_node &n) const
403 inherited::archive(n);
404 ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
412 // functions overriding virtual functions from bases classes
417 basic * ${CONTAINER}::duplicate() const
419 debugmsg("${CONTAINER} duplicate",LOGLEVEL_DUPLICATE);
420 return new ${CONTAINER}(*this);
423 void ${CONTAINER}::printraw(std::ostream & os) const
425 debugmsg("${CONTAINER} printraw",LOGLEVEL_PRINT);
427 os << "${CONTAINER}(";
428 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
429 (*cit).bp->printraw(os);
435 void ${CONTAINER}::print(std::ostream & os, unsigned upper_precedence) const
437 debugmsg("${CONTAINER} print",LOGLEVEL_PRINT);
438 // always print brackets around seq, ignore upper_precedence
439 printseq(os,'${open_bracket}',',','${close_bracket}',precedence,precedence+1);
442 void ${CONTAINER}::printtree(std::ostream & os, unsigned indent) const
444 debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT);
446 os << std::string(indent,' ') << "type=" << class_name()
447 << ", hash=" << hashvalue
448 << " (0x" << std::hex << hashvalue << std::dec << ")"
449 << ", flags=" << flags
450 << ", nops=" << nops() << std::endl;
451 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
452 (*cit).printtree(os,indent+delta_indent);
454 os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
457 // ${CONTAINER}::info() will be implemented by user elsewhere";
459 unsigned ${CONTAINER}::nops() const
464 ${LET_OP_IMPLEMENTATION}
466 ex ${CONTAINER}::expand(unsigned options) const
469 RESERVE(s,seq.size());
470 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
471 s.push_back((*it).expand(options));
474 return this${CONTAINER}(s);
477 // a ${CONTAINER} 'has' an expression if it is this expression itself or a child 'has' it
479 bool ${CONTAINER}::has(const ex & other) const
481 GINAC_ASSERT(other.bp!=0);
482 if (is_equal(*other.bp)) return true;
483 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
484 if ((*it).has(other)) return true;
489 ex ${CONTAINER}::eval(int level) const
494 return this${CONTAINER}(evalchildren(level));
497 ex ${CONTAINER}::evalf(int level) const
499 return this${CONTAINER}(evalfchildren(level));
502 /** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments
503 * and replaces the ${CONTAINER} by a temporary symbol.
504 * \@see ex::normal */
505 ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const
507 ex n=this${CONTAINER}(normalchildren(level));
508 return n.bp->basic::normal(sym_lst,repl_lst,level);
511 ex ${CONTAINER}::derivative(const symbol & s) const
513 return this${CONTAINER}(diffchildren(s));
516 ex ${CONTAINER}::subs(const lst & ls, const lst & lr) const
518 ${STLT} * vp=subschildren(ls,lr);
522 return this${CONTAINER}(vp);
527 int ${CONTAINER}::compare_same_type(const basic & other) const
529 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
530 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
531 (const_cast<basic &>(other));
533 ${STLT}::const_iterator it1=seq.begin();
534 ${STLT}::const_iterator it2=o.seq.begin();
536 for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
537 cmpval=(*it1).compare(*it2);
538 if (cmpval!=0) return cmpval;
541 if (it1==seq.end()) {
542 return (it2==o.seq.end() ? 0 : -1);
548 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
550 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
551 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
552 (const_cast<basic &>(other));
553 if (seq.size()!=o.seq.size()) return false;
555 ${STLT}::const_iterator it1=seq.begin();
556 ${STLT}::const_iterator it2=o.seq.begin();
558 for (; it1!=seq.end(); ++it1, ++it2) {
559 if (!(*it1).is_equal(*it2)) return false;
565 unsigned ${CONTAINER}::return_type(void) const
567 return return_types::noncommutative_composite;
571 // new virtual functions which can be overridden by derived classes
576 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
578 ensure_if_modifiable();
583 ${PREPEND_IMPLEMENTATION}
587 void ${CONTAINER}::printseq(std::ostream & os, char openbracket, char delim,
588 char closebracket, unsigned this_precedence,
589 unsigned upper_precedence) const
591 if (this_precedence<=upper_precedence) os << openbracket;
593 ${STLT}::const_iterator it,it_last;
597 for (; it!=it_last; ++it) {
598 (*it).bp->print(os,this_precedence);
601 (*it).bp->print(os,this_precedence);
603 if (this_precedence<=upper_precedence) os << closebracket;
606 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
608 return ${CONTAINER}(v);
611 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
613 return ${CONTAINER}(vp);
617 // non-virtual functions in this class
626 bool ${CONTAINER}::is_canonical() const
628 if (seq.size()<=1) { return 1; }
630 ${STLT}::const_iterator it=seq.begin();
631 ${STLT}::const_iterator it_last=it;
632 for (++it; it!=seq.end(); it_last=it, ++it) {
633 if ((*it_last).compare(*it)>0) {
634 if ((*it_last).compare(*it)>0) {
635 std::cout << *it_last << ">" << *it << "\\n";
644 ${STLT} ${CONTAINER}::evalchildren(int level) const
647 RESERVE(s,seq.size());
652 if (level == -max_recursion_level) {
653 throw(std::runtime_error("max recursion level reached"));
656 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
657 s.push_back((*it).eval(level));
662 ${STLT} ${CONTAINER}::evalfchildren(int level) const
665 RESERVE(s,seq.size());
670 if (level == -max_recursion_level) {
671 throw(std::runtime_error("max recursion level reached"));
674 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
675 s.push_back((*it).evalf(level));
680 ${STLT} ${CONTAINER}::normalchildren(int level) const
683 RESERVE(s,seq.size());
688 if (level == -max_recursion_level) {
689 throw(std::runtime_error("max recursion level reached"));
692 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
693 s.push_back((*it).normal(level));
698 ${STLT} ${CONTAINER}::diffchildren(const symbol & y) const
701 RESERVE(s,seq.size());
702 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
703 s.push_back((*it).diff(y));
708 /* obsolete subschildren
709 ${STLT} ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
712 RESERVE(s,seq.size());
713 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
714 s.push_back((*it).subs(ls,lr));
720 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
722 // returns a NULL pointer if nothing had to be substituted
723 // returns a pointer to a newly created epvector otherwise
724 // (which has to be deleted somewhere else)
726 ${STLT}::const_iterator last=seq.end();
727 ${STLT}::const_iterator cit=seq.begin();
729 const ex & subsed_ex=(*cit).subs(ls,lr);
730 if (!are_ex_trivially_equal(*cit,subsed_ex)) {
732 // something changed, copy seq, subs and return it
733 ${STLT} *s=new ${STLT};
734 RESERVE(*s,seq.size());
736 // copy parts of seq which are known not to have changed
737 ${STLT}::const_iterator cit2=seq.begin();
742 // copy first changed element
743 s->push_back(subsed_ex);
747 s->push_back((*cit2).subs(ls,lr));
755 return 0; // nothing has changed
759 // static member variables
764 unsigned ${CONTAINER}::precedence=10;
770 const ${CONTAINER} some_${CONTAINER};
771 const std::type_info & typeid_${CONTAINER} = typeid(some_${CONTAINER});
773 #ifndef NO_NAMESPACE_GINAC
775 #endif // ndef NO_NAMESPACE_GINAC
777 END_OF_IMPLEMENTATION
779 print "Creating interface file ${CONTAINER}.h...";
780 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
781 print OUT $interface;
785 print "Creating implementation file ${CONTAINER}.cpp...";
786 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
787 print OUT $implementation;