]> www.ginac.de Git - ginac.git/blob - ginac/container.pl
- fixed bug in normal(): normal(x^a) became (x^(-a))^(-1)
[ginac.git] / ginac / container.pl
1 #!/usr/bin/perl -w
2
3 if (($#ARGV!=0) and ($#ARGV!=1)) {
4     die 'usage: container.pl type [maxargs] (type=lst or exprseq)';
5 }
6
7 if ($ARGV[0] eq 'lst') {
8     $type='lst';
9 } elsif ($ARGV[0] eq 'exprseq') {
10     $type='exprseq';
11 } else {
12     die 'only lst and exprseq supported';
13 }
14
15 if ($#ARGV==1) {
16     $maxargs=$ARGV[1];
17 } else {
18     $maxargs=15; # must be greater or equal than the value used in function.pl
19 }
20
21 if ($type eq 'exprseq') {
22
23     # settings for exprseq
24     $CONTAINER="exprseq";
25     $STLHEADER="vector";
26     $reserve=1;
27     $prepend=0;
28     $let_op=0;
29     $open_bracket='(';
30     $close_bracket=')';
31     
32 } elsif ($type eq 'lst') {
33  
34     # settings for lst
35     $CONTAINER="lst";
36     $STLHEADER="list";
37     $reserve=0;
38     $prepend=1;
39     $let_op=1;
40     $open_bracket='[';
41     $close_bracket=']';
42
43 } else {
44     die "invalid type $type";
45 }
46
47 $CONTAINER_UC=uc(${CONTAINER});
48 $STLT="ex".$STLHEADER;
49
50 if ($reserve) {
51     $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) (s).reserve(size)";
52 } else {
53     $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) // no reserve needed for ${STLHEADER}";
54 }
55
56 if ($prepend) {
57     $PREPEND_INTERFACE=<<END_OF_PREPEND_INTERFACE;
58     virtual ${CONTAINER} & prepend(const ex & b);
59 END_OF_PREPEND_INTERFACE
60
61     $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
62 ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
63 {
64     ensure_if_modifiable();
65     seq.push_front(b);
66     return *this;
67 }
68 END_OF_PREPEND_IMPLEMENTATION
69 } else {
70     $PREPEND_INTERFACE="    // no prepend possible for ${CONTAINER}";
71     $PREPEND_IMPLEMENTATION="";
72 }
73
74 if ($let_op) {
75     $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
76 ex & ${CONTAINER}::let_op(int i)
77 {
78     GINAC_ASSERT(i>=0);
79     GINAC_ASSERT(i<nops());
80
81     ${STLT}::iterator it=seq.begin();
82     for (int j=0; j<i; j++) {
83         ++it;
84     }
85     return *it;
86 }
87 END_OF_LET_OP_IMPLEMENTATION
88 } else {
89     $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
90 }
91
92 sub generate_seq {
93     my ($seq_template,$n,$separator)=@_;
94     my ($res,$N);
95     
96     $res='';
97     for ($N=1; $N<=$n; $N++) {
98         $res .= eval('"' . $seq_template . '"');
99         if ($N!=$n) {
100             $res .= $separator;
101         }
102     }
103     return $res;
104 }
105
106 sub generate_from_to {
107     my ($template,$seq_template1,$seq_separator1,$seq_template2,
108         $seq_separator2,$from,$to)=@_;
109     my ($res,$N,$SEQ);
110
111     $res='';
112     for ($N=$from; $N<=$to; $N++) {
113         $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
114         $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
115         $res .= eval('"' . $template . '"');
116         $SEQ1=''; # to avoid main::SEQ1 used only once warning
117         $SEQ2=''; # same as above
118     }
119     return $res;
120 }
121
122 sub generate {
123     my ($template,$seq_template1,$seq_separator1,$seq_template2,
124         $seq_separator2)=@_;
125     return generate_from_to($template,$seq_template1,$seq_separator1,
126                             $seq_template2,$seq_separator2,1,$maxargs);
127 }
128
129 $constructors_interface=generate(
130 '    explicit ${CONTAINER}(${SEQ1});'."\n",
131 'const ex & param${N}',', ','','');
132
133 $constructors_implementation=generate(
134     <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ','    seq.push_back(param${N});',"\n");
135 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
136 {
137     debugmsg(\"${CONTAINER} constructor from ${N}*ex\",LOGLEVEL_CONSTRUCT);
138     RESERVE(seq,${N});
139 ${SEQ2}
140 }
141 END_OF_CONSTRUCTORS_IMPLEMENTATION
142
143 $interface=<<END_OF_INTERFACE;
144 /** \@file ${CONTAINER}.h
145  *
146  *  Definition of GiNaC's ${CONTAINER}. */
147
148 /*
149  *  This file was generated automatically by container.pl.
150  *  Please do not modify it directly, edit the perl script instead!
151  *  container.pl options: \$CONTAINER=${CONTAINER}
152  *                        \$STLHEADER=${STLHEADER}
153  *                        \$reserve=${reserve}
154  *                        \$prepend=${prepend}
155  *                        \$let_op=${let_op}
156  *                        \$open_bracket=${open_bracket}
157  *                        \$close_bracket=${close_bracket}
158  *                        \$maxargs=${maxargs}
159  *
160  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
161  *
162  *  This program is free software; you can redistribute it and/or modify
163  *  it under the terms of the GNU General Public License as published by
164  *  the Free Software Foundation; either version 2 of the License, or
165  *  (at your option) any later version.
166  *
167  *  This program is distributed in the hope that it will be useful,
168  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
169  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
170  *  GNU General Public License for more details.
171  *
172  *  You should have received a copy of the GNU General Public License
173  *  along with this program; if not, write to the Free Software
174  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
175  */
176
177 #ifndef __GINAC_${CONTAINER_UC}_H__
178 #define __GINAC_${CONTAINER_UC}_H__
179
180 #include <${STLHEADER}>
181
182 // CINT needs <algorithm> to work properly with <vector> and <list> 
183 #include <algorithm>
184
185 #include "basic.h"
186 #include "ex.h"
187
188 #ifndef NO_NAMESPACE_GINAC
189 namespace GiNaC {
190 #endif // ndef NO_NAMESPACE_GINAC
191
192 // typedef ${STLHEADER}<ex> ${STLT};
193 typedef ${STLHEADER}<ex,malloc_alloc> ${STLT}; // CINT does not like ${STLHEADER}<...,default_alloc>
194
195 class ${CONTAINER} : public basic
196 {
197     GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
198
199 public:
200     ${CONTAINER}();
201     ~${CONTAINER}();
202     ${CONTAINER}(${CONTAINER} const & other);
203     ${CONTAINER} const & operator=(${CONTAINER} const & other);
204 protected:
205     void copy(${CONTAINER} const & other);
206     void destroy(bool call_parent);
207
208 public:
209     ${CONTAINER}(${STLT} const & s, bool discardable=0);
210     ${CONTAINER}(${STLT} * vp); // vp will be deleted
211 ${constructors_interface}
212
213 public:
214     basic * duplicate() const;
215     void printraw(ostream & os) const;
216     void print(ostream & os, unsigned upper_precedence=0) const;
217     void printtree(ostream & os, unsigned indent) const;
218     bool info(unsigned inf) const;
219     unsigned nops() const;
220     ex & let_op(int i);
221     ex expand(unsigned options=0) const;
222     bool has(const ex & other) const;
223     ex eval(int level=0) const;
224     ex evalf(int level=0) const;
225     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
226     ex derivative(const symbol & s) const;
227     ex subs(const lst & ls, const lst & lr) const;
228 protected:
229     int compare_same_type(const basic & other) const;
230     bool is_equal_same_type(const basic & other) const;
231     unsigned return_type(void) const;
232
233     // new virtual functions which can be overridden by derived classes
234 public:
235     virtual ${CONTAINER} & append(const ex & b);
236 ${PREPEND_INTERFACE}
237 protected:
238     virtual void printseq(ostream & os, char openbracket, char delim,
239                           char closebracket, unsigned this_precedence,
240                           unsigned upper_precedence=0) const;
241     virtual ex this${CONTAINER}(${STLT} const & v) const;
242     virtual ex this${CONTAINER}(${STLT} * vp) const;
243
244 protected:
245     bool is_canonical() const;
246     ${STLT} evalchildren(int level) const;
247     ${STLT} evalfchildren(int level) const;
248     ${STLT} normalchildren(int level) const;
249     ${STLT} diffchildren(const symbol & s) const;
250     ${STLT} * subschildren(const lst & ls, const lst & lr) const;
251
252 protected:
253     ${STLT} seq;
254     static unsigned precedence;
255 };
256
257 // global constants
258
259 extern const ${CONTAINER} some_${CONTAINER};
260 extern const type_info & typeid_${CONTAINER};
261
262 // utility functions
263 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
264 {
265     return static_cast<const ${CONTAINER} &>(*e.bp);
266 }
267
268 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
269 {
270     return static_cast<${CONTAINER} &>(*e.bp);
271 }
272
273 #ifndef NO_NAMESPACE_GINAC
274 } // namespace GiNaC
275 #endif // ndef NO_NAMESPACE_GINAC
276
277 #endif // ndef __GINAC_${CONTAINER_UC}_H__
278
279 END_OF_INTERFACE
280
281 $implementation=<<END_OF_IMPLEMENTATION;
282 /** \@file ${CONTAINER}.cpp
283  *
284  *  Implementation of GiNaC's ${CONTAINER}. */
285
286 /*
287  *  This file was generated automatically by container.pl.
288  *  Please do not modify it directly, edit the perl script instead!
289  *  container.pl options: \$CONTAINER=${CONTAINER}
290  *                        \$STLHEADER=${STLHEADER}
291  *                        \$reserve=${reserve}
292  *                        \$prepend=${prepend}
293  *                        \$let_op=${let_op}
294  *                        \$open_bracket=${open_bracket}
295  *                        \$close_bracket=${close_bracket}
296  *                        \$maxargs=${maxargs}
297  *
298  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
299  *
300  *  This program is free software; you can redistribute it and/or modify
301  *  it under the terms of the GNU General Public License as published by
302  *  the Free Software Foundation; either version 2 of the License, or
303  *  (at your option) any later version.
304  *
305  *  This program is distributed in the hope that it will be useful,
306  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
307  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
308  *  GNU General Public License for more details.
309  *
310  *  You should have received a copy of the GNU General Public License
311  *  along with this program; if not, write to the Free Software
312  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
313  */
314
315 #include <iostream>
316 #include <stdexcept>
317
318 #include "${CONTAINER}.h"
319 #include "ex.h"
320 #include "archive.h"
321 #include "debugmsg.h"
322
323 #ifndef NO_NAMESPACE_GINAC
324 namespace GiNaC {
325 #endif // ndef NO_NAMESPACE_GINAC
326
327 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
328
329 ${RESERVE_IMPLEMENTATION}
330
331 //////////
332 // default constructor, destructor, copy constructor assignment operator and helpers
333 //////////
334
335 // public
336
337 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
338 {
339     debugmsg("${CONTAINER} default constructor",LOGLEVEL_CONSTRUCT);
340 }
341
342 ${CONTAINER}::~${CONTAINER}()
343 {
344     debugmsg("${CONTAINER} destructor",LOGLEVEL_DESTRUCT);
345     destroy(0);
346 }
347
348 ${CONTAINER}::${CONTAINER}(${CONTAINER} const & other)
349 {
350     debugmsg("${CONTAINER} copy constructor",LOGLEVEL_CONSTRUCT);
351     copy(other);
352 }
353
354 ${CONTAINER} const & ${CONTAINER}::operator=(${CONTAINER} const & other)
355 {
356     debugmsg("${CONTAINER} operator=",LOGLEVEL_ASSIGNMENT);
357     if (this != &other) {
358         destroy(1);
359         copy(other);
360     }
361     return *this;
362 }
363
364 // protected
365
366 void ${CONTAINER}::copy(${CONTAINER} const & other)
367 {
368     inherited::copy(other);
369     seq=other.seq;
370 }
371
372 void ${CONTAINER}::destroy(bool call_parent)
373 {
374     seq.clear();
375     if (call_parent) inherited::destroy(call_parent);
376 }
377
378 //////////
379 // other constructors
380 //////////
381
382 // public
383
384 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) :  basic(TINFO_${CONTAINER})
385 {
386     debugmsg("${CONTAINER} constructor from ${STLT}",
387              LOGLEVEL_CONSTRUCT);
388     if (discardable) {
389         seq.swap(const_cast<${STLT} &>(s));
390     } else {
391         seq=s;
392     }
393 }
394
395 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
396 {
397     debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT);
398     GINAC_ASSERT(vp!=0);
399     seq.swap(*vp);
400     delete vp;
401 }
402
403 ${constructors_implementation}
404
405 //////////
406 // archiving
407 //////////
408
409 /** Construct object from archive_node. */
410 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
411 {
412     debugmsg("${CONTAINER} constructor from archive_node", LOGLEVEL_CONSTRUCT);
413     for (unsigned int i=0; true; i++) {
414         ex e;
415         if (n.find_ex("seq", e, sym_lst, i))
416             seq.push_back(e);
417         else
418             break;
419     }
420 }
421
422 /** Unarchive the object. */
423 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
424 {
425     return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
426 }
427
428 /** Archive the object. */
429 void ${CONTAINER}::archive(archive_node &n) const
430 {
431     inherited::archive(n);
432     ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
433     while (i != iend) {
434         n.add_ex("seq", *i);
435         i++;
436     }
437 }
438
439 //////////
440 // functions overriding virtual functions from bases classes
441 //////////
442
443 // public
444
445 basic * ${CONTAINER}::duplicate() const
446 {
447     debugmsg("${CONTAINER} duplicate",LOGLEVEL_DUPLICATE);
448     return new ${CONTAINER}(*this);
449 }
450
451 void ${CONTAINER}::printraw(ostream & os) const
452 {
453     debugmsg("${CONTAINER} printraw",LOGLEVEL_PRINT);
454
455     os << "${CONTAINER}(";
456     for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
457         (*cit).bp->printraw(os);
458         os << ",";
459     }
460     os << ")";
461 }
462
463 void ${CONTAINER}::print(ostream & os, unsigned upper_precedence) const
464 {
465     debugmsg("${CONTAINER} print",LOGLEVEL_PRINT);
466     // always print brackets around seq, ignore upper_precedence
467     printseq(os,'${open_bracket}',',','${close_bracket}',precedence,precedence+1);
468 }
469
470 void ${CONTAINER}::printtree(ostream & os, unsigned indent) const
471 {
472     debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT);
473
474     os << string(indent,' ') << "type=" << typeid(*this).name()
475        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
476        << ", flags=" << flags
477        << ", nops=" << nops() << endl;
478     for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
479         (*cit).printtree(os,indent+delta_indent);
480     }
481     os << string(indent+delta_indent,' ') << "=====" << endl;
482 }
483
484 // ${CONTAINER}::info() will be implemented by user elsewhere";
485
486 unsigned ${CONTAINER}::nops() const
487 {
488     return seq.size();
489 }
490
491 ${LET_OP_IMPLEMENTATION}
492
493 ex ${CONTAINER}::expand(unsigned options) const
494 {
495     ${STLT} s;
496     RESERVE(s,seq.size());
497     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
498         s.push_back((*it).expand(options));
499     }
500
501     return this${CONTAINER}(s);
502 }
503
504 // a ${CONTAINER} 'has' an expression if it is this expression itself or a child 'has' it
505
506 bool ${CONTAINER}::has(const ex & other) const
507 {
508     GINAC_ASSERT(other.bp!=0);
509     if (is_equal(*other.bp)) return true;
510     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
511         if ((*it).has(other)) return true;
512     }
513     return false;
514 }
515
516 ex ${CONTAINER}::eval(int level) const
517 {
518     if (level==1) {
519         return this->hold();
520     }
521     return this${CONTAINER}(evalchildren(level));
522 }
523
524 ex ${CONTAINER}::evalf(int level) const
525 {
526     return this${CONTAINER}(evalfchildren(level));
527 }
528
529 /** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments
530  *  and replaces the ${CONTAINER} by a temporary symbol.
531  *  \@see ex::normal */
532 ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const
533 {
534     ex n=this${CONTAINER}(normalchildren(level));
535     return n.bp->basic::normal(sym_lst,repl_lst,level);
536 }
537
538 ex ${CONTAINER}::derivative(const symbol & s) const
539 {
540     return this${CONTAINER}(diffchildren(s));
541 }
542
543 ex ${CONTAINER}::subs(const lst & ls, const lst & lr) const
544 {
545     ${STLT} * vp=subschildren(ls,lr);
546     if (vp==0) {
547         return *this;
548     }
549     return this${CONTAINER}(vp);
550 }
551
552 // protected
553
554 int ${CONTAINER}::compare_same_type(const basic & other) const
555 {
556     GINAC_ASSERT(is_of_type(other,${CONTAINER}));
557     ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
558                                     (const_cast<basic &>(other));
559     int cmpval;
560     ${STLT}::const_iterator it1=seq.begin();
561     ${STLT}::const_iterator it2=o.seq.begin();
562
563     for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
564         cmpval=(*it1).compare(*it2);
565         if (cmpval!=0) return cmpval;
566     }
567
568     if (it1==seq.end()) {
569         return (it2==o.seq.end() ? 0 : -1);
570     }
571
572     return 1;
573 }
574
575 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
576 {
577     GINAC_ASSERT(is_of_type(other,${CONTAINER}));
578     ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
579                                     (const_cast<basic &>(other));
580     if (seq.size()!=o.seq.size()) return false;
581
582     ${STLT}::const_iterator it1=seq.begin();
583     ${STLT}::const_iterator it2=o.seq.begin();
584
585     for (; it1!=seq.end(); ++it1, ++it2) {
586         if (!(*it1).is_equal(*it2)) return false;
587     }
588
589     return true;
590 }
591
592 unsigned ${CONTAINER}::return_type(void) const
593 {
594     return return_types::noncommutative_composite;
595 }
596
597 //////////
598 // new virtual functions which can be overridden by derived classes
599 //////////
600
601 // public
602
603 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
604 {
605     ensure_if_modifiable();
606     seq.push_back(b);
607     return *this;
608 }
609
610 ${PREPEND_IMPLEMENTATION}
611
612 // protected
613
614 void ${CONTAINER}::printseq(ostream & os, char openbracket, char delim,
615                          char closebracket, unsigned this_precedence,
616                          unsigned upper_precedence) const
617 {
618     if (this_precedence<=upper_precedence) os << openbracket;
619     if (seq.size()!=0) {
620         ${STLT}::const_iterator it,it_last;
621         it=seq.begin();
622         it_last=seq.end();
623         --it_last;
624         for (; it!=it_last; ++it) {
625             (*it).bp->print(os,this_precedence);
626             os << delim;
627         }
628         (*it).bp->print(os,this_precedence);
629     }
630     if (this_precedence<=upper_precedence) os << closebracket;
631 }
632
633 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
634 {
635     return ${CONTAINER}(v);
636 }
637
638 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
639 {
640     return ${CONTAINER}(vp);
641 }
642
643 //////////
644 // non-virtual functions in this class
645 //////////
646
647 // public
648
649 // none
650
651 // protected
652
653 bool ${CONTAINER}::is_canonical() const
654 {
655     if (seq.size()<=1) { return 1; }
656
657     ${STLT}::const_iterator it=seq.begin();
658     ${STLT}::const_iterator it_last=it;
659     for (++it; it!=seq.end(); it_last=it, ++it) {
660         if ((*it_last).compare(*it)>0) {
661             if ((*it_last).compare(*it)>0) {
662                 cout << *it_last << ">" << *it << "\\n";
663                 return 0;
664                 }
665         }
666     }
667     return 1;
668 }
669
670
671 ${STLT} ${CONTAINER}::evalchildren(int level) const
672 {
673     ${STLT} s;
674     RESERVE(s,seq.size());
675
676     if (level==1) {
677         return seq;
678     }
679     if (level == -max_recursion_level) {
680         throw(std::runtime_error("max recursion level reached"));
681     }
682     --level;
683     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
684         s.push_back((*it).eval(level));
685     }
686     return s;
687 }
688
689 ${STLT} ${CONTAINER}::evalfchildren(int level) const
690 {
691     ${STLT} s;
692     RESERVE(s,seq.size());
693
694     if (level==1) {
695         return seq;
696     }
697     if (level == -max_recursion_level) {
698         throw(std::runtime_error("max recursion level reached"));
699     }
700     --level;
701     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
702         s.push_back((*it).evalf(level));
703     }
704     return s;
705 }
706
707 ${STLT} ${CONTAINER}::normalchildren(int level) const
708 {
709     ${STLT} s;
710     RESERVE(s,seq.size());
711
712     if (level==1) {
713         return seq;
714     }
715     if (level == -max_recursion_level) {
716         throw(std::runtime_error("max recursion level reached"));
717     }
718     --level;
719     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
720         s.push_back((*it).normal(level));
721     }
722     return s;
723 }
724
725 ${STLT} ${CONTAINER}::diffchildren(const symbol & y) const
726 {
727     ${STLT} s;
728     RESERVE(s,seq.size());
729     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
730         s.push_back((*it).diff(y));
731     }
732     return s;
733 }
734
735 /* obsolete subschildren
736 ${STLT} ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
737 {
738     ${STLT} s;
739     RESERVE(s,seq.size());
740     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
741         s.push_back((*it).subs(ls,lr));
742     }
743     return s;
744 }
745 */
746
747 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const
748 {
749     // returns a NULL pointer if nothing had to be substituted
750     // returns a pointer to a newly created epvector otherwise
751     // (which has to be deleted somewhere else)
752
753     ${STLT}::const_iterator last=seq.end();
754     ${STLT}::const_iterator cit=seq.begin();
755     while (cit!=last) {
756         const ex & subsed_ex=(*cit).subs(ls,lr);
757         if (!are_ex_trivially_equal(*cit,subsed_ex)) {
758
759             // something changed, copy seq, subs and return it
760             ${STLT} *s=new ${STLT};
761             RESERVE(*s,seq.size());
762
763             // copy parts of seq which are known not to have changed
764             ${STLT}::const_iterator cit2=seq.begin();
765             while (cit2!=cit) {
766                 s->push_back(*cit2);
767                 ++cit2;
768             }
769             // copy first changed element
770             s->push_back(subsed_ex);
771             ++cit2;
772             // copy rest
773             while (cit2!=last) {
774                 s->push_back((*cit2).subs(ls,lr));
775                 ++cit2;
776             }
777             return s;
778         }
779         ++cit;
780     }
781     
782     return 0; // nothing has changed
783 }
784
785 //////////
786 // static member variables
787 //////////
788
789 // protected
790
791 unsigned ${CONTAINER}::precedence=10;
792
793 //////////
794 // global constants
795 //////////
796
797 const ${CONTAINER} some_${CONTAINER};
798 const type_info & typeid_${CONTAINER}=typeid(some_${CONTAINER});
799
800 #ifndef NO_NAMESPACE_GINAC
801 } // namespace GiNaC
802 #endif // ndef NO_NAMESPACE_GINAC
803
804 END_OF_IMPLEMENTATION
805
806 print "Creating interface file ${CONTAINER}.h...";
807 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
808 print OUT $interface;
809 close OUT;
810 print "ok.\n";
811
812 print "Creating implementation file ${CONTAINER}.cpp...";
813 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
814 print OUT $implementation;
815 close OUT;
816 print "ok.\n";
817
818 print "done.\n";