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