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