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} ctor 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>
189 // Cint currently does not like ${STLHEADER}<..,default_alloc> but malloc_alloc is
190 // unstandardized and not supported by newer GCCs. This ugly hack will go
192 #if (defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ < 97)) || (defined(G__GNUC) && (G__GNUC == 2) && (G__GNUC_MINOR < 97))
193 typedef std::${STLHEADER}<GiNaC::ex,malloc_alloc> ${STLT};
195 typedef std::${STLHEADER}<ex> ${STLT};
198 class ${CONTAINER} : public basic
200 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
203 ${CONTAINER}(${STLT} const & s, bool discardable=0);
204 ${CONTAINER}(${STLT} * vp); // vp will be deleted
205 ${constructors_interface}
208 void print(const print_context & c, unsigned level = 0) const;
209 unsigned precedence(void) const {return 10;}
210 bool info(unsigned inf) const;
211 unsigned nops() const;
213 ex expand(unsigned options=0) const;
214 ex eval(int level=0) const;
215 ex evalf(int level=0) const;
216 ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
217 ex derivative(const symbol & s) const;
218 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
220 bool is_equal_same_type(const basic & other) const;
221 unsigned return_type(void) const;
223 // new virtual functions which can be overridden by derived classes
225 virtual ${CONTAINER} & append(const ex & b);
228 virtual void printseq(const print_context & c, char openbracket, char delim,
229 char closebracket, unsigned this_precedence,
230 unsigned upper_precedence = 0) const;
231 virtual ex this${CONTAINER}(${STLT} const & v) const;
232 virtual ex this${CONTAINER}(${STLT} * vp) const;
235 bool is_canonical() const;
236 ${STLT} evalchildren(int level) const;
237 ${STLT} evalfchildren(int level) const;
238 ${STLT} normalchildren(int level) const;
239 ${STLT} diffchildren(const symbol & s) const;
240 ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
247 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
249 return static_cast<const ${CONTAINER} &>(*e.bp);
252 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
254 return static_cast<${CONTAINER} &>(*e.bp);
259 #endif // ndef __GINAC_${CONTAINER_UC}_H__
263 $implementation=<<END_OF_IMPLEMENTATION;
264 /** \@file ${CONTAINER}.cpp
266 * Implementation of GiNaC's ${CONTAINER}. */
269 * This file was generated automatically by container.pl.
270 * Please do not modify it directly, edit the perl script instead!
271 * container.pl options: \$CONTAINER=${CONTAINER}
272 * \$STLHEADER=${STLHEADER}
273 * \$reserve=${reserve}
274 * \$prepend=${prepend}
276 * \$open_bracket=${open_bracket}
277 * \$close_bracket=${close_bracket}
278 * \$maxargs=${maxargs}
280 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
282 * This program is free software; you can redistribute it and/or modify
283 * it under the terms of the GNU General Public License as published by
284 * the Free Software Foundation; either version 2 of the License, or
285 * (at your option) any later version.
287 * This program is distributed in the hope that it will be useful,
288 * but WITHOUT ANY WARRANTY; without even the implied warranty of
289 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
290 * GNU General Public License for more details.
292 * You should have received a copy of the GNU General Public License
293 * along with this program; if not, write to the Free Software
294 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
300 #include "${CONTAINER}.h"
304 #include "debugmsg.h"
308 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
310 ${RESERVE_IMPLEMENTATION}
313 // default ctor, dtor, copy ctor assignment operator and helpers
318 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
320 debugmsg("${CONTAINER} default ctor",LOGLEVEL_CONSTRUCT);
325 void ${CONTAINER}::copy(${CONTAINER} const & other)
327 inherited::copy(other);
331 void ${CONTAINER}::destroy(bool call_parent)
334 if (call_parent) inherited::destroy(call_parent);
343 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
345 debugmsg("${CONTAINER} ctor from ${STLT}", LOGLEVEL_CONSTRUCT);
347 seq.swap(const_cast<${STLT} &>(s));
353 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
355 debugmsg("${CONTAINER} ctor from ${STLT} *",LOGLEVEL_CONSTRUCT);
361 ${constructors_implementation}
367 /** Construct object from archive_node. */
368 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
370 debugmsg("${CONTAINER} ctor from archive_node", LOGLEVEL_CONSTRUCT);
371 for (unsigned int i=0; true; i++) {
373 if (n.find_ex("seq", e, sym_lst, i))
380 /** Unarchive the object. */
381 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
383 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
386 /** Archive the object. */
387 void ${CONTAINER}::archive(archive_node &n) const
389 inherited::archive(n);
390 ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
398 // functions overriding virtual functions from bases classes
403 void ${CONTAINER}::print(const print_context & c, unsigned level) const
405 debugmsg("${CONTAINER} print", LOGLEVEL_PRINT);
407 if (is_of_type(c, print_tree)) {
409 c.s << std::string(level, ' ') << class_name()
410 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
411 << ", nops=" << nops()
413 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
414 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit)
415 cit->print(c, level + delta_indent);
416 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
419 // always print brackets around seq, ignore upper_precedence
420 printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1);
424 // ${CONTAINER}::info() will be implemented by user elsewhere";
426 unsigned ${CONTAINER}::nops() const
431 ${LET_OP_IMPLEMENTATION}
433 ex ${CONTAINER}::expand(unsigned options) const
436 RESERVE(s,seq.size());
437 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
438 s.push_back((*it).expand(options));
441 return this${CONTAINER}(s);
444 ex ${CONTAINER}::eval(int level) const
449 return this${CONTAINER}(evalchildren(level));
452 ex ${CONTAINER}::evalf(int level) const
454 return this${CONTAINER}(evalfchildren(level));
457 /** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments
458 * and replaces the ${CONTAINER} by a temporary symbol.
459 * \@see ex::normal */
460 ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const
462 ex n=this${CONTAINER}(normalchildren(level));
463 return n.bp->basic::normal(sym_lst,repl_lst,level);
466 ex ${CONTAINER}::derivative(const symbol & s) const
468 return this${CONTAINER}(diffchildren(s));
471 ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const
473 ${STLT} *vp = subschildren(ls, lr, no_pattern);
475 return this${CONTAINER}(vp).bp->basic::subs(ls, lr, no_pattern);
477 return basic::subs(ls, lr, no_pattern);
482 int ${CONTAINER}::compare_same_type(const basic & other) const
484 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
485 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
486 (const_cast<basic &>(other));
488 ${STLT}::const_iterator it1=seq.begin();
489 ${STLT}::const_iterator it2=o.seq.begin();
491 for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
492 cmpval=(*it1).compare(*it2);
493 if (cmpval!=0) return cmpval;
496 if (it1==seq.end()) {
497 return (it2==o.seq.end() ? 0 : -1);
503 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
505 GINAC_ASSERT(is_of_type(other,${CONTAINER}));
506 ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
507 (const_cast<basic &>(other));
508 if (seq.size()!=o.seq.size()) return false;
510 ${STLT}::const_iterator it1=seq.begin();
511 ${STLT}::const_iterator it2=o.seq.begin();
513 for (; it1!=seq.end(); ++it1, ++it2) {
514 if (!(*it1).is_equal(*it2)) return false;
520 unsigned ${CONTAINER}::return_type(void) const
522 return return_types::noncommutative_composite;
526 // new virtual functions which can be overridden by derived classes
531 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
533 ensure_if_modifiable();
538 ${PREPEND_IMPLEMENTATION}
542 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
543 char closebracket, unsigned this_precedence,
544 unsigned upper_precedence) const
546 if (this_precedence <= upper_precedence)
549 if (seq.size() != 0) {
550 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
552 while (it != itend) {
553 it->print(c, this_precedence);
557 it->print(c, this_precedence);
560 if (this_precedence <= upper_precedence)
564 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
566 return ${CONTAINER}(v);
569 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
571 return ${CONTAINER}(vp);
575 // non-virtual functions in this class
584 bool ${CONTAINER}::is_canonical() const
586 if (seq.size()<=1) { return 1; }
588 ${STLT}::const_iterator it=seq.begin();
589 ${STLT}::const_iterator it_last=it;
590 for (++it; it!=seq.end(); it_last=it, ++it) {
591 if ((*it_last).compare(*it)>0) {
592 if ((*it_last).compare(*it)>0) {
593 std::cout << *it_last << ">" << *it << "\\n";
602 ${STLT} ${CONTAINER}::evalchildren(int level) const
605 RESERVE(s,seq.size());
610 if (level == -max_recursion_level) {
611 throw(std::runtime_error("max recursion level reached"));
614 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
615 s.push_back((*it).eval(level));
620 ${STLT} ${CONTAINER}::evalfchildren(int level) const
623 RESERVE(s,seq.size());
628 if (level == -max_recursion_level) {
629 throw(std::runtime_error("max recursion level reached"));
632 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
633 s.push_back((*it).evalf(level));
638 ${STLT} ${CONTAINER}::normalchildren(int level) const
641 RESERVE(s,seq.size());
646 if (level == -max_recursion_level) {
647 throw(std::runtime_error("max recursion level reached"));
650 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
651 s.push_back((*it).normal(level));
656 ${STLT} ${CONTAINER}::diffchildren(const symbol & y) const
659 RESERVE(s,seq.size());
660 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
661 s.push_back((*it).diff(y));
666 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pattern) const
668 // returns a NULL pointer if nothing had to be substituted
669 // returns a pointer to a newly created epvector otherwise
670 // (which has to be deleted somewhere else)
672 ${STLT}::const_iterator last=seq.end();
673 ${STLT}::const_iterator cit=seq.begin();
675 const ex & subsed_ex=(*cit).subs(ls,lr,no_pattern);
676 if (!are_ex_trivially_equal(*cit,subsed_ex)) {
678 // something changed, copy seq, subs and return it
679 ${STLT} *s=new ${STLT};
680 RESERVE(*s,seq.size());
682 // copy parts of seq which are known not to have changed
683 ${STLT}::const_iterator cit2=seq.begin();
688 // copy first changed element
689 s->push_back(subsed_ex);
693 s->push_back((*cit2).subs(ls,lr,no_pattern));
701 return 0; // nothing has changed
706 END_OF_IMPLEMENTATION
708 print "Creating interface file ${CONTAINER}.h...";
709 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
710 print OUT $interface;
714 print "Creating implementation file ${CONTAINER}.cpp...";
715 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
716 print OUT $implementation;