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