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