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_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
110 ex & ${CONTAINER}::let_op(int i)
113 GINAC_ASSERT(i<nops());
115 ${STLT}::iterator it=seq.begin();
116 for (int j=0; j<i; j++) {
121 END_OF_LET_OP_IMPLEMENTATION
123 $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
127 my ($seq_template,$n,$separator)=@_;
131 for ($N=1; $N<=$n; $N++) {
132 $res .= eval('"' . $seq_template . '"');
140 sub generate_from_to {
141 my ($template,$seq_template1,$seq_separator1,$seq_template2,
142 $seq_separator2,$from,$to)=@_;
146 for ($N=$from; $N<=$to; $N++) {
147 $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
148 $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
149 $res .= eval('"' . $template . '"');
150 $SEQ1=''; # to avoid main::SEQ1 used only once warning
151 $SEQ2=''; # same as above
157 my ($template,$seq_template1,$seq_separator1,$seq_template2,
159 return generate_from_to($template,$seq_template1,$seq_separator1,
160 $seq_template2,$seq_separator2,1,$maxargs);
163 $constructors_interface=generate(
164 ' explicit ${CONTAINER}(${SEQ1});'."\n",
165 'const ex & param${N}',', ','','');
167 $constructors_implementation=generate(
168 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ',' seq.push_back(param${N});',"\n");
169 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
171 debugmsg(\"${CONTAINER} ctor from ${N}*ex\",LOGLEVEL_CONSTRUCT);
175 END_OF_CONSTRUCTORS_IMPLEMENTATION
177 $interface=<<END_OF_INTERFACE;
178 /** \@file ${CONTAINER}.h
180 * Definition of GiNaC's ${CONTAINER}. */
183 * This file was generated automatically by container.pl.
184 * Please do not modify it directly, edit the perl script instead!
185 * container.pl options: \$CONTAINER=${CONTAINER}
186 * \$STLHEADER=${STLHEADER}
187 * \$reserve=${reserve}
188 * \$prepend=${prepend}
191 * \$open_bracket=${open_bracket}
192 * \$close_bracket=${close_bracket}
193 * \$maxargs=${maxargs}
195 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
197 * This program is free software; you can redistribute it and/or modify
198 * it under the terms of the GNU General Public License as published by
199 * the Free Software Foundation; either version 2 of the License, or
200 * (at your option) any later version.
202 * This program is distributed in the hope that it will be useful,
203 * but WITHOUT ANY WARRANTY; without even the implied warranty of
204 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
205 * GNU General Public License for more details.
207 * You should have received a copy of the GNU General Public License
208 * along with this program; if not, write to the Free Software
209 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
212 #ifndef __GINAC_${CONTAINER_UC}_H__
213 #define __GINAC_${CONTAINER_UC}_H__
215 #include <${STLHEADER}>
217 // CINT needs <algorithm> to work properly with <vector> and <list>
226 typedef std::${STLHEADER}<ex> ${STLT};
228 class ${CONTAINER} : public basic
230 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
233 ${CONTAINER}(${STLT} const & s, bool discardable = false);
234 ${CONTAINER}(${STLT} * vp); // vp will be deleted
235 ${constructors_interface}
238 void print(const print_context & c, unsigned level = 0) const;
239 unsigned precedence(void) const {return 10;}
240 bool info(unsigned inf) const;
241 unsigned nops() const;
243 ex map(map_function & f) const;
244 ex eval(int level=0) const;
245 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
247 bool is_equal_same_type(const basic & other) const;
248 unsigned return_type(void) const;
250 // new virtual functions which can be overridden by derived classes
252 virtual ${CONTAINER} & append(const ex & b);
253 virtual ${CONTAINER} & remove_last(void);
257 virtual void printseq(const print_context & c, char openbracket, char delim,
258 char closebracket, unsigned this_precedence,
259 unsigned upper_precedence = 0) const;
260 virtual ex this${CONTAINER}(${STLT} const & v) const;
261 virtual ex this${CONTAINER}(${STLT} * vp) const;
264 bool is_canonical() const;
265 ${STLT} evalchildren(int level) const;
266 ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
274 /** Return the ${CONTAINER} object handled by an ex. Deprecated: use ex_to<${CONTAINER}>().
275 * This is unsafe: you need to check the type first. */
276 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
278 return static_cast<const ${CONTAINER} &>(*e.bp);
281 /** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */
282 template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj)
284 return obj.tinfo()==TINFO_${CONTAINER};
287 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
289 return static_cast<${CONTAINER} &>(*e.bp);
294 #endif // ndef __GINAC_${CONTAINER_UC}_H__
298 $implementation=<<END_OF_IMPLEMENTATION;
299 /** \@file ${CONTAINER}.cpp
301 * Implementation of GiNaC's ${CONTAINER}. */
304 * This file was generated automatically by container.pl.
305 * Please do not modify it directly, edit the perl script instead!
306 * container.pl options: \$CONTAINER=${CONTAINER}
307 * \$STLHEADER=${STLHEADER}
308 * \$reserve=${reserve}
309 * \$prepend=${prepend}
311 * \$open_bracket=${open_bracket}
312 * \$close_bracket=${close_bracket}
313 * \$maxargs=${maxargs}
315 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
317 * This program is free software; you can redistribute it and/or modify
318 * it under the terms of the GNU General Public License as published by
319 * the Free Software Foundation; either version 2 of the License, or
320 * (at your option) any later version.
322 * This program is distributed in the hope that it will be useful,
323 * but WITHOUT ANY WARRANTY; without even the implied warranty of
324 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
325 * GNU General Public License for more details.
327 * You should have received a copy of the GNU General Public License
328 * along with this program; if not, write to the Free Software
329 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
335 #include "${CONTAINER}.h"
339 #include "debugmsg.h"
343 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
345 ${RESERVE_IMPLEMENTATION}
348 // default ctor, dtor, copy ctor assignment operator and helpers
353 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
355 debugmsg("${CONTAINER} default ctor",LOGLEVEL_CONSTRUCT);
360 void ${CONTAINER}::copy(${CONTAINER} const & other)
362 inherited::copy(other);
366 void ${CONTAINER}::destroy(bool call_parent)
369 if (call_parent) inherited::destroy(call_parent);
378 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
380 debugmsg("${CONTAINER} ctor from ${STLT}", LOGLEVEL_CONSTRUCT);
382 seq.swap(const_cast<${STLT} &>(s));
388 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
390 debugmsg("${CONTAINER} ctor from ${STLT} *",LOGLEVEL_CONSTRUCT);
396 ${constructors_implementation}
402 /** Construct object from archive_node. */
403 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
405 debugmsg("${CONTAINER} ctor from archive_node", LOGLEVEL_CONSTRUCT);
406 for (unsigned int i=0; true; i++) {
408 if (n.find_ex("seq", e, sym_lst, i))
415 /** Unarchive the object. */
416 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
418 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
421 /** Archive the object. */
422 void ${CONTAINER}::archive(archive_node &n) const
424 inherited::archive(n);
425 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
433 // functions overriding virtual functions from bases classes
438 void ${CONTAINER}::print(const print_context & c, unsigned level) const
440 debugmsg("${CONTAINER} print", LOGLEVEL_PRINT);
442 if (is_a<print_tree>(c)) {
444 c.s << std::string(level, ' ') << class_name()
445 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
446 << ", nops=" << nops()
448 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
449 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
451 i->print(c, level + delta_indent);
454 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
457 // always print brackets around seq, ignore upper_precedence
458 printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1);
462 // ${CONTAINER}::info() will be implemented by user elsewhere";
464 unsigned ${CONTAINER}::nops() const
469 ${LET_OP_IMPLEMENTATION}
471 ex ${CONTAINER}::map(map_function & f) const
473 // This implementation is here because basic::map() uses let_op()
474 // which is not defined for all containers
476 RESERVE(s,seq.size());
477 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
483 return this${CONTAINER}(s);
486 ex ${CONTAINER}::eval(int level) const
491 return this${CONTAINER}(evalchildren(level));
494 ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const
496 ${STLT} *vp = subschildren(ls, lr, no_pattern);
498 return this${CONTAINER}(vp).bp->basic::subs(ls, lr, no_pattern);
500 return basic::subs(ls, lr, no_pattern);
505 int ${CONTAINER}::compare_same_type(const basic & other) const
507 GINAC_ASSERT(is_a<${CONTAINER}>(other));
508 ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
510 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
511 it2 = o.seq.begin(), it2end = o.seq.end();
513 while (it1 != it1end && it2 != it2end) {
514 int cmpval = it1->compare(*it2);
520 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
523 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
525 GINAC_ASSERT(is_a<${CONTAINER}>(other));
526 ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(other);
528 if (seq.size() != o.seq.size())
531 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
534 while (it1 != it1end) {
535 if (!it1->is_equal(*it2))
543 unsigned ${CONTAINER}::return_type(void) const
545 return return_types::noncommutative_composite;
549 // new virtual functions which can be overridden by derived classes
554 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
556 ensure_if_modifiable();
561 ${CONTAINER} & ${CONTAINER}::remove_last(void)
563 ensure_if_modifiable();
568 ${PREPEND_IMPLEMENTATION}
570 ${SORT_IMPLEMENTATION}
574 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
575 char closebracket, unsigned this_precedence,
576 unsigned upper_precedence) const
578 if (this_precedence <= upper_precedence)
582 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
584 while (it != itend) {
585 it->print(c, this_precedence);
589 it->print(c, this_precedence);
592 if (this_precedence <= upper_precedence)
596 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
598 return ${CONTAINER}(v);
601 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
603 return ${CONTAINER}(vp);
607 // non-virtual functions in this class
616 bool ${CONTAINER}::is_canonical() const
618 if (seq.size()<=1) { return 1; }
620 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
621 ${STLT}::const_iterator it_last=it;
622 for (++it; it!=itend; it_last=it, ++it) {
623 if (it_last->compare(*it)>0) {
624 if (it_last->compare(*it)>0) {
625 std::cout << *it_last << ">" << *it << "\\n";
634 ${STLT} ${CONTAINER}::evalchildren(int level) const
637 RESERVE(s,seq.size());
642 if (level == -max_recursion_level) {
643 throw(std::runtime_error("max recursion level reached"));
646 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
647 while (it != itend) {
648 s.push_back(it->eval(level));
654 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pattern) const
656 // returns a NULL pointer if nothing had to be substituted
657 // returns a pointer to a newly created epvector otherwise
658 // (which has to be deleted somewhere else)
660 ${STLT}::const_iterator cit = seq.begin(), end = seq.end();
662 const ex & subsed_ex = cit->subs(ls, lr, no_pattern);
663 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
665 // something changed, copy seq, subs and return it
666 ${STLT} *s=new ${STLT};
667 RESERVE(*s, seq.size());
669 // copy parts of seq which are known not to have changed
670 ${STLT}::const_iterator cit2 = seq.begin();
671 while (cit2 != cit) {
676 // copy first changed element
677 s->push_back(subsed_ex);
681 while (cit2 != end) {
682 s->push_back(cit2->subs(ls, lr, no_pattern));
690 return 0; // nothing has changed
695 END_OF_IMPLEMENTATION
697 print "Creating interface file ${CONTAINER}.h...";
698 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
699 print OUT $interface;
703 print "Creating implementation file ${CONTAINER}.cpp...";
704 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
705 print OUT $implementation;