- modified GiNaC headers to Alexander's liking
[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/basic.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 "${CONTAINER}.h"
258 #include "ex.h"
259
260 ${RESERVE_IMPLEMENTATION}
261
262 //////////
263 // default constructor, destructor, copy constructor assignment operator and helpers
264 //////////
265
266 // public
267
268 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
269 {
270     debugmsg("${CONTAINER} default constructor",LOGLEVEL_CONSTRUCT);
271 }
272
273 ${CONTAINER}::~${CONTAINER}()
274 {
275     debugmsg("${CONTAINER} destructor",LOGLEVEL_DESTRUCT);
276     destroy(0);
277 }
278
279 ${CONTAINER}::${CONTAINER}(${CONTAINER} const & other)
280 {
281     debugmsg("${CONTAINER} copy constructor",LOGLEVEL_CONSTRUCT);
282     copy(other);
283 }
284
285 ${CONTAINER} const & ${CONTAINER}::operator=(${CONTAINER} const & other)
286 {
287     debugmsg("${CONTAINER} operator=",LOGLEVEL_ASSIGNMENT);
288     if (this != &other) {
289         destroy(1);
290         copy(other);
291     }
292     return *this;
293 }
294
295 // protected
296
297 void ${CONTAINER}::copy(${CONTAINER} const & other)
298 {
299     basic::copy(other);
300     seq=other.seq;
301 }
302
303 void ${CONTAINER}::destroy(bool call_parent)
304 {
305     seq.clear();
306     if (call_parent) basic::destroy(call_parent);
307 }
308
309 //////////
310 // other constructors
311 //////////
312
313 // public
314
315 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) :  basic(TINFO_${CONTAINER})
316 {
317     debugmsg("${CONTAINER} constructor from ${STLT}",
318              LOGLEVEL_CONSTRUCT);
319     if (discardable) {
320         seq.swap(const_cast<${STLT} &>(s));
321     } else {
322         seq=s;
323     }
324 }
325
326 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
327 {
328     debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT);
329     ASSERT(vp!=0);
330     seq.swap(*vp);
331     delete vp;
332 }
333
334 ${CONTAINER}::${CONTAINER}(ex const & e1) :  basic(TINFO_${CONTAINER})
335 {
336     debugmsg("${CONTAINER} constructor from 1 ex",
337              LOGLEVEL_CONSTRUCT);
338     RESERVE(seq,1);
339     seq.push_back(e1);
340 }
341
342 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2) : basic(TINFO_${CONTAINER})
343 {
344     debugmsg("${CONTAINER} constructor from 2 ex",
345              LOGLEVEL_CONSTRUCT);
346     RESERVE(seq,2);
347     seq.push_back(e1);
348     seq.push_back(e2);
349 }
350
351 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3)
352     : basic(TINFO_${CONTAINER})
353 {
354     debugmsg("${CONTAINER} constructor from 3 ex",
355              LOGLEVEL_CONSTRUCT);
356     RESERVE(seq,3);
357     seq.push_back(e1);
358     seq.push_back(e2);
359     seq.push_back(e3);
360 }
361
362 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
363                      ex const & e4) : basic(TINFO_${CONTAINER})
364 {
365     debugmsg("${CONTAINER} constructor from 4 ex",
366              LOGLEVEL_CONSTRUCT);
367     RESERVE(seq,4);
368     seq.push_back(e1);
369     seq.push_back(e2);
370     seq.push_back(e3);
371     seq.push_back(e4);
372 }
373
374 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
375                      ex const & e4, ex const & e5) : basic(TINFO_${CONTAINER})
376 {
377     debugmsg("${CONTAINER} constructor from 5 ex",
378              LOGLEVEL_CONSTRUCT);
379     RESERVE(seq,5);
380     seq.push_back(e1);
381     seq.push_back(e2);
382     seq.push_back(e3);
383     seq.push_back(e4);
384     seq.push_back(e5);
385 }
386
387 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
388                      ex const & e4, ex const & e5, ex const & e6)
389     : basic(TINFO_${CONTAINER})
390 {
391     debugmsg("${CONTAINER} constructor from 6 ex",
392              LOGLEVEL_CONSTRUCT);
393     RESERVE(seq,6);
394     seq.push_back(e1);
395     seq.push_back(e2);
396     seq.push_back(e3);
397     seq.push_back(e4);
398     seq.push_back(e5);
399     seq.push_back(e6);
400 }
401
402 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
403                      ex const & e4, ex const & e5, ex const & e6,
404                      ex const & e7) : basic(TINFO_${CONTAINER})
405 {
406     debugmsg("${CONTAINER} constructor from 7 ex",
407              LOGLEVEL_CONSTRUCT);
408     RESERVE(seq,7);
409     seq.push_back(e1);
410     seq.push_back(e2);
411     seq.push_back(e3);
412     seq.push_back(e4);
413     seq.push_back(e5);
414     seq.push_back(e6);
415     seq.push_back(e7);
416 }
417
418 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
419                      ex const & e4, ex const & e5, ex const & e6,
420                      ex const & e7, ex const & e8) : basic(TINFO_${CONTAINER})
421 {
422     debugmsg("${CONTAINER} constructor from 8 ex",
423              LOGLEVEL_CONSTRUCT);
424     RESERVE(seq,8);
425     seq.push_back(e1);
426     seq.push_back(e2);
427     seq.push_back(e3);
428     seq.push_back(e4);
429     seq.push_back(e5);
430     seq.push_back(e6);
431     seq.push_back(e7);
432     seq.push_back(e8);
433 }
434
435 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
436                      ex const & e4, ex const & e5, ex const & e6,
437                      ex const & e7, ex const & e8, ex const & e9)
438     : basic(TINFO_${CONTAINER})
439 {
440     debugmsg("${CONTAINER} constructor from 9 ex",
441              LOGLEVEL_CONSTRUCT);
442     RESERVE(seq,9);
443     seq.push_back(e1);
444     seq.push_back(e2);
445     seq.push_back(e3);
446     seq.push_back(e4);
447     seq.push_back(e5);
448     seq.push_back(e6);
449     seq.push_back(e7);
450     seq.push_back(e8);
451     seq.push_back(e9);
452 }
453
454 ${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
455                      ex const & e4, ex const & e5, ex const & e6,
456                      ex const & e7, ex const & e8, ex const & e9,
457                      ex const &e10)
458     : basic(TINFO_${CONTAINER})
459 {
460     debugmsg("${CONTAINER} constructor from 10 ex",
461              LOGLEVEL_CONSTRUCT);
462     RESERVE(seq,10);
463     seq.push_back(e1);
464     seq.push_back(e2);
465     seq.push_back(e3);
466     seq.push_back(e4);
467     seq.push_back(e5);
468     seq.push_back(e6);
469     seq.push_back(e7);
470     seq.push_back(e8);
471     seq.push_back(e9);
472     seq.push_back(e10);
473 }
474
475 //////////
476 // functions overriding virtual functions from bases classes
477 //////////
478
479 // public
480
481 basic * ${CONTAINER}::duplicate() const
482 {
483     debugmsg("${CONTAINER} duplicate",LOGLEVEL_DUPLICATE);
484     return new ${CONTAINER}(*this);
485 }
486
487 void ${CONTAINER}::printraw(ostream & os) const
488 {
489     debugmsg("${CONTAINER} printraw",LOGLEVEL_PRINT);
490
491     os << "${CONTAINER}(";
492     for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
493         (*cit).bp->printraw(os);
494         os << ",";
495     }
496     os << ")";
497 }
498
499 void ${CONTAINER}::print(ostream & os, unsigned upper_precedence) const
500 {
501     debugmsg("${CONTAINER} print",LOGLEVEL_PRINT);
502     // always print brackets around seq, ignore upper_precedence
503     printseq(os,'${open_bracket}',',','${close_bracket}',precedence,precedence+1);
504 }
505
506 void ${CONTAINER}::printtree(ostream & os, unsigned indent) const
507 {
508     debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT);
509
510     os << string(indent,' ') << "type=" << typeid(*this).name()
511        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
512        << ", flags=" << flags
513        << ", nops=" << nops() << endl;
514     for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
515         (*cit).printtree(os,indent+delta_indent);
516     }
517     os << string(indent+delta_indent,' ') << "=====" << endl;
518 }
519
520 // ${CONTAINER}::info() will be implemented by user elsewhere";
521
522 int ${CONTAINER}::nops() const
523 {
524     return seq.size();
525 }
526
527 ${LET_OP_IMPLEMENTATION}
528
529 ex ${CONTAINER}::expand(unsigned options) const
530 {
531     ${STLT} s;
532     RESERVE(s,seq.size());
533     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
534         s.push_back((*it).expand(options));
535     }
536
537     return this${CONTAINER}(s);
538 }
539
540 // a ${CONTAINER} 'has' an expression if it is this expression itself or a child 'has' it
541
542 bool ${CONTAINER}::has(ex const & other) const
543 {
544     ASSERT(other.bp!=0);
545     if (is_equal(*other.bp)) return true;
546     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
547         if ((*it).has(other)) return true;
548     }
549     return false;
550 }
551
552 ex ${CONTAINER}::eval(int level) const
553 {
554     if (level==1) {
555         return this->hold();
556     }
557     return this${CONTAINER}(evalchildren(level));
558 }
559
560 ex ${CONTAINER}::evalf(int level) const
561 {
562     return this${CONTAINER}(evalfchildren(level));
563 }
564
565 /** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments
566  *  and replaces the ${CONTAINER} by a temporary symbol.
567  *  \@see ex::normal */
568 ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const
569 {
570     ex n=this${CONTAINER}(normalchildren(level));
571     return n.bp->basic::normal(sym_lst,repl_lst,level);
572 }
573
574 ex ${CONTAINER}::diff(symbol const & s) const
575 {
576     return this${CONTAINER}(diffchildren(s));
577 }
578
579 ex ${CONTAINER}::subs(lst const & ls, lst const & lr) const
580 {
581     ${STLT} * vp=subschildren(ls,lr);
582     if (vp==0) {
583         return *this;
584     }
585     return this${CONTAINER}(vp);
586 }
587
588 // protected
589
590 int ${CONTAINER}::compare_same_type(basic const & other) const
591 {
592     ASSERT(is_of_type(other,${CONTAINER}));
593     ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
594                                     (const_cast<basic &>(other));
595     int cmpval;
596     ${STLT}::const_iterator it1=seq.begin();
597     ${STLT}::const_iterator it2=o.seq.begin();
598
599     for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
600         cmpval=(*it1).compare(*it2);
601         if (cmpval!=0) return cmpval;
602     }
603
604     if (it1==seq.end()) {
605         return (it2==o.seq.end() ? 0 : -1);
606     }
607
608     return 1;
609 }
610
611 bool ${CONTAINER}::is_equal_same_type(basic const & other) const
612 {
613     ASSERT(is_of_type(other,${CONTAINER}));
614     ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
615                                     (const_cast<basic &>(other));
616     if (seq.size()!=o.seq.size()) return false;
617
618     ${STLT}::const_iterator it1=seq.begin();
619     ${STLT}::const_iterator it2=o.seq.begin();
620
621     for (; it1!=seq.end(); ++it1, ++it2) {
622         if (!(*it1).is_equal(*it2)) return false;
623     }
624
625     return true;
626 }
627
628 unsigned ${CONTAINER}::return_type(void) const
629 {
630     return return_types::noncommutative_composite;
631 }
632
633 //////////
634 // new virtual functions which can be overridden by derived classes
635 //////////
636
637 // public
638
639 ${CONTAINER} & ${CONTAINER}::append(ex const & b)
640 {
641     ensure_if_modifiable();
642     seq.push_back(b);
643     return *this;
644 }
645
646 ${PREPEND_IMPLEMENTATION}
647
648 // protected
649
650 void ${CONTAINER}::printseq(ostream & os, char openbracket, char delim,
651                          char closebracket, unsigned this_precedence,
652                          unsigned upper_precedence) const
653 {
654     if (this_precedence<=upper_precedence) os << openbracket;
655     if (seq.size()!=0) {
656         ${STLT}::const_iterator it,it_last;
657         it=seq.begin();
658         it_last=seq.end();
659         --it_last;
660         for (; it!=it_last; ++it) {
661             (*it).bp->print(os,this_precedence);
662             os << delim;
663         }
664         (*it).bp->print(os,this_precedence);
665     }
666     if (this_precedence<=upper_precedence) os << closebracket;
667 }
668
669 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
670 {
671     return ${CONTAINER}(v);
672 }
673
674 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
675 {
676     return ${CONTAINER}(vp);
677 }
678
679 //////////
680 // non-virtual functions in this class
681 //////////
682
683 // public
684
685 // none
686
687 // protected
688
689 bool ${CONTAINER}::is_canonical() const
690 {
691     if (seq.size()<=1) { return 1; }
692
693     ${STLT}::const_iterator it=seq.begin();
694     ${STLT}::const_iterator it_last=it;
695     for (++it; it!=seq.end(); it_last=it, ++it) {
696         if ((*it_last).compare(*it)>0) {
697             if ((*it_last).compare(*it)>0) {
698                 cout << *it_last << ">" << *it << "\\n";
699                 return 0;
700                 }
701         }
702     }
703     return 1;
704 }
705
706
707 ${STLT} ${CONTAINER}::evalchildren(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).eval(level));
721     }
722     return s;
723 }
724
725 ${STLT} ${CONTAINER}::evalfchildren(int level) const
726 {
727     ${STLT} s;
728     RESERVE(s,seq.size());
729
730     if (level==1) {
731         return seq;
732     }
733     if (level == -max_recursion_level) {
734         throw(std::runtime_error("max recursion level reached"));
735     }
736     --level;
737     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
738         s.push_back((*it).evalf(level));
739     }
740     return s;
741 }
742
743 ${STLT} ${CONTAINER}::normalchildren(int level) const
744 {
745     ${STLT} s;
746     RESERVE(s,seq.size());
747
748     if (level==1) {
749         return seq;
750     }
751     if (level == -max_recursion_level) {
752         throw(std::runtime_error("max recursion level reached"));
753     }
754     --level;
755     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
756         s.push_back((*it).normal(level));
757     }
758     return s;
759 }
760
761 ${STLT} ${CONTAINER}::diffchildren(symbol const & y) const
762 {
763     ${STLT} s;
764     RESERVE(s,seq.size());
765     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
766         s.push_back((*it).diff(y));
767     }
768     return s;
769 }
770
771 /* obsolete subschildren
772 ${STLT} ${CONTAINER}::subschildren(lst const & ls, lst const & lr) const
773 {
774     ${STLT} s;
775     RESERVE(s,seq.size());
776     for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
777         s.push_back((*it).subs(ls,lr));
778     }
779     return s;
780 }
781 */
782
783 ${STLT} * ${CONTAINER}::subschildren(lst const & ls, lst const & lr) const
784 {
785     // returns a NULL pointer if nothing had to be substituted
786     // returns a pointer to a newly created epvector otherwise
787     // (which has to be deleted somewhere else)
788
789     ${STLT}::const_iterator last=seq.end();
790     ${STLT}::const_iterator cit=seq.begin();
791     while (cit!=last) {
792         ex const & subsed_ex=(*cit).subs(ls,lr);
793         if (!are_ex_trivially_equal(*cit,subsed_ex)) {
794
795             // something changed, copy seq, subs and return it
796             ${STLT} *s=new ${STLT};
797             RESERVE(*s,seq.size());
798
799             // copy parts of seq which are known not to have changed
800             ${STLT}::const_iterator cit2=seq.begin();
801             while (cit2!=cit) {
802                 s->push_back(*cit2);
803                 ++cit2;
804             }
805             // copy first changed element
806             s->push_back(subsed_ex);
807             ++cit2;
808             // copy rest
809             while (cit2!=last) {
810                 s->push_back((*cit2).subs(ls,lr));
811                 ++cit2;
812             }
813             return s;
814         }
815         ++cit;
816     }
817     
818     return 0; // nothing has changed
819 }
820
821 //////////
822 // static member variables
823 //////////
824
825 // protected
826
827 unsigned ${CONTAINER}::precedence=10;
828
829 //////////
830 // global constants
831 //////////
832
833 const ${CONTAINER} some_${CONTAINER};
834 type_info const & typeid_${CONTAINER}=typeid(some_${CONTAINER});
835
836 END_OF_IMPLEMENTATION
837
838 print "Creating interface file ${CONTAINER}.h...";
839 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
840 print OUT $interface;
841 close OUT;
842 print "ok.\n";
843
844 print "Creating implementation file ${CONTAINER}.cpp...";
845 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
846 print OUT $implementation;
847 close OUT;
848 print "ok.\n";
849
850 print "done.\n";