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