]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
- enforced GiNaC coding standards :-)
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifndef __GINAC_EXPAIRSEQ_H__
21 #define __GINAC_EXPAIRSEQ_H__
22
23 #include <vector>
24 #include <list>
25
26 //#define EXPAIRSEQ_USE_HASHTAB
27
28 typedef vector<expair> epvector;
29 typedef epvector::iterator epviter;
30
31 inline void iter_swap(epvector::iterator it1, epvector::iterator it2)
32 {
33     debugmsg("iter_swap epvector",LOGLEVEL_NONMEMBER_FUNCTION);
34     (*it1).rest.swap((*it2).rest);
35     (*it1).coeff.swap((*it2).coeff);
36 }
37
38 typedef epvector::iterator epp;
39 typedef list<epp> epplist;
40 typedef vector<epplist> epplistvector;
41
42 /** A sequence of class expair.
43  *  This is used for time-critical classes like sums and products of terms
44  *  since handling a list of coeff and rest is much faster than handling a
45  *  list of products or powers, respectively. (Incidentally, Maple does it
46  *  the same way.) */
47 class expairseq : public basic
48 {
49 // member functions
50
51     // default constructor, destructor, copy constructor assignment operator and helpers
52 public:
53     expairseq() : basic(TINFO_EXPAIRSEQ)
54 #ifdef EXPAIRSEQ_USE_HASHTAB
55         , hashtabsize(0)
56 #endif // def EXPAIRSEQ_USE_HASHTAB
57         {
58             debugmsg("expairseq default constructor",LOGLEVEL_CONSTRUCT);
59         }
60     ~expairseq()
61         {
62             debugmsg("expairseq destructor",LOGLEVEL_DESTRUCT);
63             destroy(0);
64         }
65     expairseq(expairseq const & other);
66     expairseq const & operator=(expairseq const & other);
67 protected:
68     void copy(expairseq const & other);
69     void destroy(bool call_parent)
70         {
71             if (call_parent) basic::destroy(call_parent);
72         };
73
74     // other constructors
75 public:
76     expairseq(ex const & lh, ex const & rh);
77     expairseq(exvector const & v);
78     expairseq(epvector const & v, ex const & oc);
79     expairseq(epvector * vp, ex const & oc); // vp will be deleted
80
81     // functions overriding virtual functions from bases classes
82 public:
83     basic * duplicate() const;
84     void printraw(ostream & os) const;
85     void printtree(ostream & os, unsigned indent) const;
86     void print(ostream & os, unsigned upper_precedence=0) const;
87     bool info(unsigned inf) const;
88     int nops() const;
89     ex op(int const i) const;
90     ex & let_op(int const i);
91     ex eval(int level=0) const;
92     ex evalf(int level=0) const;
93     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
94     ex diff(symbol const & s) const;
95     ex subs(lst const & ls, lst const & lr) const;
96 protected:
97     int compare_same_type(basic const & other) const;
98     bool is_equal_same_type(basic const & other) const;
99     unsigned return_type(void) const;
100     unsigned calchash(void) const;
101     ex expand(unsigned options=0) const;
102
103     // new virtual functions which can be overridden by derived classes
104 protected:
105     virtual ex thisexpairseq(epvector const & v, ex const & oc) const;
106     virtual ex thisexpairseq(epvector * vp, ex const & oc) const;
107     virtual void printseq(ostream & os, char delim, unsigned this_precedence,
108                           unsigned upper_precedence) const;
109     virtual void printpair(ostream & os, expair const & p,
110                            unsigned upper_precedence) const;
111     virtual expair split_ex_to_pair(ex const & e) const;
112     virtual expair combine_ex_with_coeff_to_pair(ex const & e,
113                                                  ex const & c) const;
114     virtual expair combine_pair_with_coeff_to_pair(expair const & p,
115                                                    ex const & c) const;
116     virtual ex recombine_pair_to_ex(expair const & p) const;
117     virtual bool expair_needs_further_processing(epp it);
118     virtual ex default_overall_coeff(void) const;
119     virtual void combine_overall_coeff(ex const & c);
120     virtual void combine_overall_coeff(ex const & c1, ex const & c2);
121     virtual bool can_make_flat(expair const & p) const;
122     
123     // non-virtual functions in this class
124 protected:
125     void construct_from_2_ex_via_exvector(ex const & lh, ex const & rh);
126     void construct_from_2_ex(ex const & lh, ex const & rh);
127     void construct_from_2_expairseq(expairseq const & s1,
128                                     expairseq const & s2);
129     void construct_from_expairseq_ex(expairseq const & s,
130                                      ex const & e);
131     void construct_from_exvector(exvector const & v);
132     void construct_from_epvector(epvector const & v);
133     void make_flat(exvector const & v);
134     void make_flat(epvector const & v);
135     epvector * bubblesort(epvector::iterator itbegin, epvector::iterator itend);
136     epvector * mergesort(epvector::iterator itbegin, epvector::iterator itend);
137     void canonicalize(void);
138     void combine_same_terms_sorted_seq(void);
139 #ifdef EXPAIRSEQ_USE_HASHTAB
140     void combine_same_terms(void);
141     unsigned calc_hashtabsize(unsigned sz) const;
142     unsigned calc_hashindex(ex const & e) const;
143     void shrink_hashtab(void);
144     void remove_hashtab_entry(epvector::const_iterator element);
145     void move_hashtab_entry(epvector::const_iterator oldpos,
146                             epvector::iterator newpos);
147     void sorted_insert(epplist & eppl, epp elem);
148     void build_hashtab_and_combine(epvector::iterator & first_numeric,
149                                    epvector::iterator & last_non_zero,
150                                    vector<bool> & touched,
151                                    unsigned & number_of_zeroes);
152     void drop_coeff_0_terms(epvector::iterator & first_numeric,
153                             epvector::iterator & last_non_zero,
154                             vector<bool> & touched,
155                             unsigned & number_of_zeroes);
156     bool has_coeff_0(void) const;
157     void add_numerics_to_hashtab(epvector::iterator first_numeric,                                              epvector::const_iterator last_non_zero);
158 #endif // def EXPAIRSEQ_USE_HASHTAB
159     bool is_canonical() const;
160     epvector * expandchildren(unsigned options) const;
161     epvector * evalchildren(int level) const;
162     epvector evalfchildren(int level) const;
163     epvector normalchildren(int level) const;
164     epvector diffchildren(symbol const & s) const;
165     epvector * subschildren(lst const & ls, lst const & lr) const;
166     
167 // member variables
168     
169 protected:
170     epvector seq;
171     ex overall_coeff;
172     static unsigned precedence;
173 #ifdef EXPAIRSEQ_USE_HASHTAB
174     epplistvector hashtab;
175     unsigned hashtabsize;
176     unsigned hashmask;
177     static unsigned maxhashtabsize;
178     static unsigned minhashtabsize;
179     static unsigned hashtabfactor;
180 #endif // def EXPAIRSEQ_USE_HASHTAB
181 };
182
183 // global constants
184
185 extern const expairseq some_expairseq;
186 extern type_info const & typeid_expairseq;
187
188 #define ex_to_expairseq(X) static_cast<expairseq const &>(*(X).bp)
189
190 #endif // ndef __GINAC_EXPAIRSEQ_H__