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