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})
174 END_OF_CONSTRUCTORS_IMPLEMENTATION
176 $interface=<<END_OF_INTERFACE;
177 /** \@file ${CONTAINER}.h
179 * Definition of GiNaC's ${CONTAINER}. */
182 * This file was generated automatically by container.pl.
183 * Please do not modify it directly, edit the perl script instead!
184 * container.pl options: \$CONTAINER=${CONTAINER}
185 * \$STLHEADER=${STLHEADER}
186 * \$reserve=${reserve}
187 * \$prepend=${prepend}
190 * \$open_bracket=${open_bracket}
191 * \$close_bracket=${close_bracket}
192 * \$maxargs=${maxargs}
194 * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
196 * This program is free software; you can redistribute it and/or modify
197 * it under the terms of the GNU General Public License as published by
198 * the Free Software Foundation; either version 2 of the License, or
199 * (at your option) any later version.
201 * This program is distributed in the hope that it will be useful,
202 * but WITHOUT ANY WARRANTY; without even the implied warranty of
203 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
204 * GNU General Public License for more details.
206 * You should have received a copy of the GNU General Public License
207 * along with this program; if not, write to the Free Software
208 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
211 #ifndef __GINAC_${CONTAINER_UC}_H__
212 #define __GINAC_${CONTAINER_UC}_H__
214 #include <${STLHEADER}>
216 // CINT needs <algorithm> to work properly with <vector> and <list>
225 typedef std::${STLHEADER}<ex> ${STLT};
227 class ${CONTAINER} : public basic
229 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
232 ${CONTAINER}(${STLT} const & s, bool discardable = false);
233 ${CONTAINER}(${STLT} * vp); // vp will be deleted
234 ${constructors_interface}
237 void print(const print_context & c, unsigned level = 0) const;
238 unsigned precedence(void) const {return 10;}
239 bool info(unsigned inf) const;
240 unsigned nops() const;
242 ex map(map_function & f) const;
243 ex eval(int level=0) const;
244 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
246 bool is_equal_same_type(const basic & other) const;
247 unsigned return_type(void) const;
249 // new virtual functions which can be overridden by derived classes
251 virtual ${CONTAINER} & append(const ex & b);
252 virtual ${CONTAINER} & remove_last(void);
256 virtual void printseq(const print_context & c, char openbracket, char delim,
257 char closebracket, unsigned this_precedence,
258 unsigned upper_precedence = 0) const;
259 virtual ex this${CONTAINER}(${STLT} const & v) const;
260 virtual ex this${CONTAINER}(${STLT} * vp) const;
263 bool is_canonical() const;
264 ${STLT} evalchildren(int level) const;
265 ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
273 /** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */
274 template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj)
276 return obj.tinfo()==TINFO_${CONTAINER};
281 #endif // ndef __GINAC_${CONTAINER_UC}_H__
285 $implementation=<<END_OF_IMPLEMENTATION;
286 /** \@file ${CONTAINER}.cpp
288 * Implementation of GiNaC's ${CONTAINER}. */
291 * This file was generated automatically by container.pl.
292 * Please do not modify it directly, edit the perl script instead!
293 * container.pl options: \$CONTAINER=${CONTAINER}
294 * \$STLHEADER=${STLHEADER}
295 * \$reserve=${reserve}
296 * \$prepend=${prepend}
298 * \$open_bracket=${open_bracket}
299 * \$close_bracket=${close_bracket}
300 * \$maxargs=${maxargs}
302 * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
304 * This program is free software; you can redistribute it and/or modify
305 * it under the terms of the GNU General Public License as published by
306 * the Free Software Foundation; either version 2 of the License, or
307 * (at your option) any later version.
309 * This program is distributed in the hope that it will be useful,
310 * but WITHOUT ANY WARRANTY; without even the implied warranty of
311 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
312 * GNU General Public License for more details.
314 * You should have received a copy of the GNU General Public License
315 * along with this program; if not, write to the Free Software
316 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
322 #include "${CONTAINER}.h"
329 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
331 ${RESERVE_IMPLEMENTATION}
334 // default ctor, dtor, copy ctor, assignment operator and helpers
339 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER}) {}
343 void ${CONTAINER}::copy(${CONTAINER} const & other)
345 inherited::copy(other);
349 void ${CONTAINER}::destroy(bool call_parent)
352 if (call_parent) inherited::destroy(call_parent);
361 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
364 seq.swap(const_cast<${STLT} &>(s));
370 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
377 ${constructors_implementation}
383 /** Construct object from archive_node. */
384 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
386 for (unsigned int i=0; true; i++) {
388 if (n.find_ex("seq", e, sym_lst, i))
395 /** Unarchive the object. */
396 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
398 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
401 /** Archive the object. */
402 void ${CONTAINER}::archive(archive_node &n) const
404 inherited::archive(n);
405 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
413 // functions overriding virtual functions from base classes
418 void ${CONTAINER}::print(const print_context & c, unsigned level) const
420 if (is_a<print_tree>(c)) {
422 c.s << std::string(level, ' ') << class_name()
423 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
424 << ", nops=" << nops()
426 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
427 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
429 i->print(c, level + delta_indent);
432 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
433 } else if (is_a<print_python>(c)) {
434 printseq(c, '[', ',', ']', precedence(), precedence()+1);
435 } else if (is_a<print_python_repr>(c)) {
436 c.s << class_name ();
437 printseq(c, '(', ',', ')', precedence(), precedence()+1);
439 // always print brackets around seq, ignore upper_precedence
440 printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1);
444 // ${CONTAINER}::info() will be implemented by user elsewhere";
446 unsigned ${CONTAINER}::nops() const
451 ${LET_OP_IMPLEMENTATION}
453 ex ${CONTAINER}::map(map_function & f) const
455 // This implementation is here because basic::map() uses let_op()
456 // which is not defined for all containers
458 RESERVE(s,seq.size());
459 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
465 return this${CONTAINER}(s);
468 ex ${CONTAINER}::eval(int level) const
473 return this${CONTAINER}(evalchildren(level));
476 ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const
478 ${STLT} *vp = subschildren(ls, lr, no_pattern);
480 return ex_to<basic>(this${CONTAINER}(vp)).basic::subs(ls, lr, no_pattern);
482 return basic::subs(ls, lr, no_pattern);
487 int ${CONTAINER}::compare_same_type(const basic & other) const
489 GINAC_ASSERT(is_a<${CONTAINER}>(other));
490 ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
492 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
493 it2 = o.seq.begin(), it2end = o.seq.end();
495 while (it1 != it1end && it2 != it2end) {
496 int cmpval = it1->compare(*it2);
502 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
505 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
507 GINAC_ASSERT(is_a<${CONTAINER}>(other));
508 ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(other);
510 if (seq.size() != o.seq.size())
513 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
516 while (it1 != it1end) {
517 if (!it1->is_equal(*it2))
525 unsigned ${CONTAINER}::return_type(void) const
527 return return_types::noncommutative_composite;
531 // new virtual functions which can be overridden by derived classes
536 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
538 ensure_if_modifiable();
543 ${CONTAINER} & ${CONTAINER}::remove_last(void)
545 ensure_if_modifiable();
550 ${PREPEND_IMPLEMENTATION}
552 ${SORT_IMPLEMENTATION}
556 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
557 char closebracket, unsigned this_precedence,
558 unsigned upper_precedence) const
560 if (this_precedence <= upper_precedence)
564 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
566 while (it != itend) {
567 it->print(c, this_precedence);
571 it->print(c, this_precedence);
574 if (this_precedence <= upper_precedence)
578 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
580 return ${CONTAINER}(v);
583 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
585 return ${CONTAINER}(vp);
589 // non-virtual functions in this class
598 bool ${CONTAINER}::is_canonical() const
600 if (seq.size()<=1) { return 1; }
602 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
603 ${STLT}::const_iterator it_last=it;
604 for (++it; it!=itend; it_last=it, ++it) {
605 if (it_last->compare(*it)>0) {
606 if (it_last->compare(*it)>0) {
607 std::cout << *it_last << ">" << *it << "\\n";
616 ${STLT} ${CONTAINER}::evalchildren(int level) const
619 RESERVE(s,seq.size());
624 if (level == -max_recursion_level) {
625 throw(std::runtime_error("max recursion level reached"));
628 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
629 while (it != itend) {
630 s.push_back(it->eval(level));
636 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pattern) const
638 // returns a NULL pointer if nothing had to be substituted
639 // returns a pointer to a newly created epvector otherwise
640 // (which has to be deleted somewhere else)
642 ${STLT}::const_iterator cit = seq.begin(), end = seq.end();
644 const ex & subsed_ex = cit->subs(ls, lr, no_pattern);
645 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
647 // something changed, copy seq, subs and return it
648 ${STLT} *s=new ${STLT};
649 RESERVE(*s, seq.size());
651 // copy parts of seq which are known not to have changed
652 ${STLT}::const_iterator cit2 = seq.begin();
653 while (cit2 != cit) {
658 // copy first changed element
659 s->push_back(subsed_ex);
663 while (cit2 != end) {
664 s->push_back(cit2->subs(ls, lr, no_pattern));
672 return 0; // nothing has changed
677 END_OF_IMPLEMENTATION
679 print "Creating interface file ${CONTAINER}.h...";
680 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
681 print OUT $interface;
685 print "Creating implementation file ${CONTAINER}.cpp...";
686 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
687 print OUT $implementation;