- added find() (like has(), but returns list of all occurrences)
[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=16; # 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         $sort=0;
27         $let_op=0;
28         $open_bracket='(';
29         $close_bracket=')';
30         
31 } elsif ($type eq 'lst') {
32  
33         # settings for lst
34         $CONTAINER="lst";
35         $STLHEADER="list";
36         $reserve=0;
37         $prepend=1;
38         $sort=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         virtual ${CONTAINER} & remove_first(void);
60 END_OF_PREPEND_INTERFACE
61
62         $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
63 ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
64 {
65         ensure_if_modifiable();
66         seq.push_front(b);
67         return *this;
68 }
69
70 ${CONTAINER} & ${CONTAINER}::remove_first(void)
71 {
72         ensure_if_modifiable();
73         seq.pop_front();
74         return *this;
75 }
76 END_OF_PREPEND_IMPLEMENTATION
77 } else {
78         $PREPEND_INTERFACE="    // no prepend possible for ${CONTAINER}";
79         $PREPEND_IMPLEMENTATION="";
80 }
81
82 if ($sort) {
83         $SORT_INTERFACE=<<END_OF_SORT_INTERFACE;
84         virtual ${CONTAINER} & sort(void);
85         virtual ${CONTAINER} & unique(void);
86 END_OF_SORT_INTERFACE
87
88         $SORT_IMPLEMENTATION=<<END_OF_SORT_IMPLEMENTATION;
89 ${CONTAINER} & ${CONTAINER}::sort(void)
90 {
91         ensure_if_modifiable();
92         seq.sort(ex_is_less());
93         return *this;
94 }
95
96 ${CONTAINER} & ${CONTAINER}::unique(void)
97 {
98         ensure_if_modifiable();
99         seq.unique(ex_is_equal());
100         return *this;
101 }
102 END_OF_SORT_IMPLEMENTATION
103 } else {
104         $SORT_INTERFACE="    // no sort possible for ${CONTAINER}";
105         $SORT_IMPLEMENTATION="";
106 }
107
108 if ($let_op) {
109         $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
110 ex & ${CONTAINER}::let_op(int i)
111 {
112         GINAC_ASSERT(i>=0);
113         GINAC_ASSERT(i<nops());
114
115         ${STLT}::iterator it=seq.begin();
116         for (int j=0; j<i; j++) {
117                 ++it;
118         }
119         return *it;
120 }
121 END_OF_LET_OP_IMPLEMENTATION
122 } else {
123         $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
124 }
125
126 sub generate_seq {
127         my ($seq_template,$n,$separator)=@_;
128         my ($res,$N);
129         
130         $res='';
131         for ($N=1; $N<=$n; $N++) {
132                 $res .= eval('"' . $seq_template . '"');
133                 if ($N!=$n) {
134                         $res .= $separator;
135                 }
136         }
137         return $res;
138 }
139
140 sub generate_from_to {
141         my ($template,$seq_template1,$seq_separator1,$seq_template2,
142             $seq_separator2,$from,$to)=@_;
143         my ($res,$N,$SEQ);
144
145         $res='';
146         for ($N=$from; $N<=$to; $N++) {
147                 $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
148                 $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
149                 $res .= eval('"' . $template . '"');
150                 $SEQ1=''; # to avoid main::SEQ1 used only once warning
151                 $SEQ2=''; # same as above
152         }
153         return $res;
154 }
155
156 sub generate {
157         my ($template,$seq_template1,$seq_separator1,$seq_template2,
158             $seq_separator2)=@_;
159         return generate_from_to($template,$seq_template1,$seq_separator1,
160                                                         $seq_template2,$seq_separator2,1,$maxargs);
161 }
162
163 $constructors_interface=generate(
164 '       explicit ${CONTAINER}(${SEQ1});'."\n",
165 'const ex & param${N}',', ','','');
166
167 $constructors_implementation=generate(
168         <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ','    seq.push_back(param${N});',"\n");
169 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
170 {
171         debugmsg(\"${CONTAINER} ctor from ${N}*ex\",LOGLEVEL_CONSTRUCT);
172         RESERVE(seq,${N});
173 ${SEQ2}
174 }
175 END_OF_CONSTRUCTORS_IMPLEMENTATION
176
177 $interface=<<END_OF_INTERFACE;
178 /** \@file ${CONTAINER}.h
179  *
180  *  Definition of GiNaC's ${CONTAINER}. */
181
182 /*
183  *  This file was generated automatically by container.pl.
184  *  Please do not modify it directly, edit the perl script instead!
185  *  container.pl options: \$CONTAINER=${CONTAINER}
186  *                        \$STLHEADER=${STLHEADER}
187  *                        \$reserve=${reserve}
188  *                        \$prepend=${prepend}
189  *                        \$sort=${sort}
190  *                        \$let_op=${let_op}
191  *                        \$open_bracket=${open_bracket}
192  *                        \$close_bracket=${close_bracket}
193  *                        \$maxargs=${maxargs}
194  *
195  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
196  *
197  *  This program is free software; you can redistribute it and/or modify
198  *  it under the terms of the GNU General Public License as published by
199  *  the Free Software Foundation; either version 2 of the License, or
200  *  (at your option) any later version.
201  *
202  *  This program is distributed in the hope that it will be useful,
203  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
204  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
205  *  GNU General Public License for more details.
206  *
207  *  You should have received a copy of the GNU General Public License
208  *  along with this program; if not, write to the Free Software
209  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
210  */
211
212 #ifndef __GINAC_${CONTAINER_UC}_H__
213 #define __GINAC_${CONTAINER_UC}_H__
214
215 #include <${STLHEADER}>
216
217 // CINT needs <algorithm> to work properly with <vector> and <list> 
218 #include <algorithm>
219
220 #include "basic.h"
221 #include "ex.h"
222
223 namespace GiNaC {
224
225
226 typedef std::${STLHEADER}<ex> ${STLT};
227
228 class ${CONTAINER} : public basic
229 {
230         GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
231
232 public:
233         ${CONTAINER}(${STLT} const & s, bool discardable = false);
234         ${CONTAINER}(${STLT} * vp); // vp will be deleted
235 ${constructors_interface}
236
237 public:
238         void print(const print_context & c, unsigned level = 0) const;
239         unsigned precedence(void) const {return 10;}
240         bool info(unsigned inf) const;
241         unsigned nops() const;
242         ex & let_op(int i);
243         ex map(map_function & f) const;
244         ex eval(int level=0) const;
245         ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
246 protected:
247         bool is_equal_same_type(const basic & other) const;
248         unsigned return_type(void) const;
249
250         // new virtual functions which can be overridden by derived classes
251 public:
252         virtual ${CONTAINER} & append(const ex & b);
253         virtual ${CONTAINER} & remove_last(void);
254 ${PREPEND_INTERFACE}
255 ${SORT_INTERFACE}
256 protected:
257         virtual void printseq(const print_context & c, char openbracket, char delim,
258                               char closebracket, unsigned this_precedence,
259                               unsigned upper_precedence = 0) const;
260         virtual ex this${CONTAINER}(${STLT} const & v) const;
261         virtual ex this${CONTAINER}(${STLT} * vp) const;
262
263 protected:
264         bool is_canonical() const;
265         ${STLT} evalchildren(int level) const;
266         ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
267
268 protected:
269         ${STLT} seq;
270 };
271
272 // utility functions
273
274 /** Return the ${CONTAINER} object handled by an ex.  Deprecated: use ex_to<${CONTAINER}>().
275  *  This is unsafe: you need to check the type first. */
276 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
277 {
278         return static_cast<const ${CONTAINER} &>(*e.bp);
279 }
280
281 /** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */
282 template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj)
283 {
284         return obj.tinfo()==TINFO_${CONTAINER};
285 }
286
287 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
288 {
289         return static_cast<${CONTAINER} &>(*e.bp);
290 }
291
292 } // namespace GiNaC
293
294 #endif // ndef __GINAC_${CONTAINER_UC}_H__
295
296 END_OF_INTERFACE
297
298 $implementation=<<END_OF_IMPLEMENTATION;
299 /** \@file ${CONTAINER}.cpp
300  *
301  *  Implementation of GiNaC's ${CONTAINER}. */
302
303 /*
304  *  This file was generated automatically by container.pl.
305  *  Please do not modify it directly, edit the perl script instead!
306  *  container.pl options: \$CONTAINER=${CONTAINER}
307  *                        \$STLHEADER=${STLHEADER}
308  *                        \$reserve=${reserve}
309  *                        \$prepend=${prepend}
310  *                        \$let_op=${let_op}
311  *                        \$open_bracket=${open_bracket}
312  *                        \$close_bracket=${close_bracket}
313  *                        \$maxargs=${maxargs}
314  *
315  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
316  *
317  *  This program is free software; you can redistribute it and/or modify
318  *  it under the terms of the GNU General Public License as published by
319  *  the Free Software Foundation; either version 2 of the License, or
320  *  (at your option) any later version.
321  *
322  *  This program is distributed in the hope that it will be useful,
323  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
324  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
325  *  GNU General Public License for more details.
326  *
327  *  You should have received a copy of the GNU General Public License
328  *  along with this program; if not, write to the Free Software
329  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
330  */
331
332 #include <iostream>
333 #include <stdexcept>
334
335 #include "${CONTAINER}.h"
336 #include "ex.h"
337 #include "print.h"
338 #include "archive.h"
339 #include "debugmsg.h"
340
341 namespace GiNaC {
342
343 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
344
345 ${RESERVE_IMPLEMENTATION}
346
347 //////////
348 // default ctor, dtor, copy ctor assignment operator and helpers
349 //////////
350
351 // public
352
353 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
354 {
355         debugmsg("${CONTAINER} default ctor",LOGLEVEL_CONSTRUCT);
356 }
357
358 // protected
359
360 void ${CONTAINER}::copy(${CONTAINER} const & other)
361 {
362         inherited::copy(other);
363         seq=other.seq;
364 }
365
366 void ${CONTAINER}::destroy(bool call_parent)
367 {
368         seq.clear();
369         if (call_parent) inherited::destroy(call_parent);
370 }
371
372 //////////
373 // other ctors
374 //////////
375
376 // public
377
378 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) :  basic(TINFO_${CONTAINER})
379 {
380         debugmsg("${CONTAINER} ctor from ${STLT}", LOGLEVEL_CONSTRUCT);
381         if (discardable) {
382                 seq.swap(const_cast<${STLT} &>(s));
383         } else {
384                 seq=s;
385         }
386 }
387
388 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
389 {
390         debugmsg("${CONTAINER} ctor from ${STLT} *",LOGLEVEL_CONSTRUCT);
391         GINAC_ASSERT(vp!=0);
392         seq.swap(*vp);
393         delete vp;
394 }
395
396 ${constructors_implementation}
397
398 //////////
399 // archiving
400 //////////
401
402 /** Construct object from archive_node. */
403 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
404 {
405         debugmsg("${CONTAINER} ctor from archive_node", LOGLEVEL_CONSTRUCT);
406         for (unsigned int i=0; true; i++) {
407                 ex e;
408                 if (n.find_ex("seq", e, sym_lst, i))
409                         seq.push_back(e);
410                 else
411                         break;
412         }
413 }
414
415 /** Unarchive the object. */
416 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
417 {
418         return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
419 }
420
421 /** Archive the object. */
422 void ${CONTAINER}::archive(archive_node &n) const
423 {
424         inherited::archive(n);
425         ${STLT}::const_iterator i = seq.begin(), end = seq.end();
426         while (i != end) {
427                 n.add_ex("seq", *i);
428                 ++i;
429         }
430 }
431
432 //////////
433 // functions overriding virtual functions from bases classes
434 //////////
435
436 // public
437
438 void ${CONTAINER}::print(const print_context & c, unsigned level) const
439 {
440         debugmsg("${CONTAINER} print", LOGLEVEL_PRINT);
441
442         if (is_a<print_tree>(c)) {
443
444                 c.s << std::string(level, ' ') << class_name()
445                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
446                     << ", nops=" << nops()
447                     << std::endl;
448                 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
449                 ${STLT}::const_iterator i = seq.begin(), end = seq.end();
450                 while (i != end) {
451                         i->print(c, level + delta_indent);
452                         ++i;
453                 }
454                 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
455
456         } else {
457                 // always print brackets around seq, ignore upper_precedence
458                 printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1);
459         }
460 }
461
462 // ${CONTAINER}::info() will be implemented by user elsewhere";
463
464 unsigned ${CONTAINER}::nops() const
465 {
466         return seq.size();
467 }
468
469 ${LET_OP_IMPLEMENTATION}
470
471 ex ${CONTAINER}::map(map_function & f) const
472 {
473         // This implementation is here because basic::map() uses let_op()
474         // which is not defined for all containers
475         ${STLT} s;
476         RESERVE(s,seq.size());
477         ${STLT}::const_iterator i = seq.begin(), end = seq.end();
478         while (i != end) {
479                 s.push_back(f(*i));
480                 ++i;
481         }
482
483         return this${CONTAINER}(s);
484 }
485
486 ex ${CONTAINER}::eval(int level) const
487 {
488         if (level==1) {
489                 return this->hold();
490         }
491         return this${CONTAINER}(evalchildren(level));
492 }
493
494 ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const
495 {
496         ${STLT} *vp = subschildren(ls, lr, no_pattern);
497         if (vp)
498                 return this${CONTAINER}(vp).bp->basic::subs(ls, lr, no_pattern);
499         else
500                 return basic::subs(ls, lr, no_pattern);
501 }
502
503 // protected
504
505 int ${CONTAINER}::compare_same_type(const basic & other) const
506 {
507         GINAC_ASSERT(is_a<${CONTAINER}>(other));
508         ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
509
510         ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
511                                 it2 = o.seq.begin(), it2end = o.seq.end();
512
513         while (it1 != it1end && it2 != it2end) {
514                 int cmpval = it1->compare(*it2);
515                 if (cmpval)
516                         return cmpval;
517                 ++it1; ++it2;
518         }
519
520         return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
521 }
522
523 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
524 {
525         GINAC_ASSERT(is_a<${CONTAINER}>(other));
526         ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(other);
527
528         if (seq.size() != o.seq.size())
529                 return false;
530
531         ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
532                                 it2 = o.seq.begin();
533
534         while (it1 != it1end) {
535                 if (!it1->is_equal(*it2))
536                         return false;
537                 ++it1; ++it2;
538         }
539
540         return true;
541 }
542
543 unsigned ${CONTAINER}::return_type(void) const
544 {
545         return return_types::noncommutative_composite;
546 }
547
548 //////////
549 // new virtual functions which can be overridden by derived classes
550 //////////
551
552 // public
553
554 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
555 {
556         ensure_if_modifiable();
557         seq.push_back(b);
558         return *this;
559 }
560
561 ${CONTAINER} & ${CONTAINER}::remove_last(void)
562 {
563         ensure_if_modifiable();
564         seq.pop_back();
565         return *this;
566 }
567
568 ${PREPEND_IMPLEMENTATION}
569
570 ${SORT_IMPLEMENTATION}
571
572 // protected
573
574 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
575                             char closebracket, unsigned this_precedence,
576                             unsigned upper_precedence) const
577 {
578         if (this_precedence <= upper_precedence)
579                 c.s << openbracket;
580
581         if (!seq.empty()) {
582                 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
583                 --itend;
584                 while (it != itend) {
585                         it->print(c, this_precedence);
586                         c.s << delim;
587                         ++it;
588                 }
589                 it->print(c, this_precedence);
590         }
591
592         if (this_precedence <= upper_precedence)
593                 c.s << closebracket;
594 }
595
596 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
597 {
598         return ${CONTAINER}(v);
599 }
600
601 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
602 {
603         return ${CONTAINER}(vp);
604 }
605
606 //////////
607 // non-virtual functions in this class
608 //////////
609
610 // public
611
612 // none
613
614 // protected
615
616 bool ${CONTAINER}::is_canonical() const
617 {
618         if (seq.size()<=1) { return 1; }
619
620         ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
621         ${STLT}::const_iterator it_last=it;
622         for (++it; it!=itend; it_last=it, ++it) {
623                 if (it_last->compare(*it)>0) {
624                         if (it_last->compare(*it)>0) {
625                                 std::cout << *it_last << ">" << *it << "\\n";
626                                 return 0;
627                         }
628                 }
629         }
630         return 1;
631 }
632
633
634 ${STLT} ${CONTAINER}::evalchildren(int level) const
635 {
636         ${STLT} s;
637         RESERVE(s,seq.size());
638
639         if (level==1) {
640                 return seq;
641         }
642         if (level == -max_recursion_level) {
643                 throw(std::runtime_error("max recursion level reached"));
644         }
645         --level;
646         ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
647         while (it != itend) {
648                 s.push_back(it->eval(level));
649                 ++it;
650         }
651         return s;
652 }
653
654 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pattern) const
655 {
656         // returns a NULL pointer if nothing had to be substituted
657         // returns a pointer to a newly created epvector otherwise
658         // (which has to be deleted somewhere else)
659
660         ${STLT}::const_iterator cit = seq.begin(), end = seq.end();
661         while (cit != end) {
662                 const ex & subsed_ex = cit->subs(ls, lr, no_pattern);
663                 if (!are_ex_trivially_equal(*cit, subsed_ex)) {
664
665                         // something changed, copy seq, subs and return it
666                         ${STLT} *s=new ${STLT};
667                         RESERVE(*s, seq.size());
668
669                         // copy parts of seq which are known not to have changed
670                         ${STLT}::const_iterator cit2 = seq.begin();
671                         while (cit2 != cit) {
672                                 s->push_back(*cit2);
673                                 ++cit2;
674                         }
675
676                         // copy first changed element
677                         s->push_back(subsed_ex);
678                         ++cit2;
679
680                         // copy rest
681                         while (cit2 != end) {
682                                 s->push_back(cit2->subs(ls, lr, no_pattern));
683                                 ++cit2;
684                         }
685                         return s;
686                 }
687                 ++cit;
688         }
689         
690         return 0; // nothing has changed
691 }
692
693 } // namespace GiNaC
694
695 END_OF_IMPLEMENTATION
696
697 print "Creating interface file ${CONTAINER}.h...";
698 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
699 print OUT $interface;
700 close OUT;
701 print "ok.\n";
702
703 print "Creating implementation file ${CONTAINER}.cpp...";
704 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
705 print OUT $implementation;
706 close OUT;
707 print "ok.\n";
708
709 print "done.\n";