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