3 * Interface to GiNaC's ABC.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifndef __GINAC_BASIC_H__
23 #define __GINAC_BASIC_H__
29 #include <ginac/flags.h>
30 #include <ginac/tinfos.h>
31 #include <ginac/debugmsg.h>
39 typedef vector<ex> exvector;
41 #define INLINE_BASIC_CONSTRUCTORS
43 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
44 * It is responsible for the reference counting. */
51 // default constructor, destructor, copy constructor assignment operator and helpers
54 #ifdef INLINE_BASIC_CONSTRUCTORS
55 : tinfo_key(TINFO_basic), flags(0), refcount(0)
57 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
62 #endif // def INLINE_BASIC_CONSTRUCTORS
65 #ifdef INLINE_BASIC_CONSTRUCTORS
67 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
69 ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
73 #endif // def INLINE_BASIC_CONSTRUCTORS
75 basic(basic const & other)
76 #ifdef INLINE_BASIC_CONSTRUCTORS
78 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
83 #endif // def INLINE_BASIC_CONSTRUCTORS
85 virtual basic const & operator=(basic const & other);
88 void copy(basic const & other)
90 flags = other.flags & ~status_flags::dynallocated;
91 hashvalue = other.hashvalue;
92 tinfo_key = other.tinfo_key;
94 void destroy(bool call_parent) {}
98 #ifdef INLINE_BASIC_CONSTRUCTORS
99 : tinfo_key(ti), flags(0), refcount(0)
101 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
106 #endif // def INLINE_BASIC_CONSTRUCTORS
108 // functions overriding virtual functions from bases classes
111 // new virtual functions which can be overridden by derived classes
112 public: // only const functions please (may break reference counting)
113 virtual basic * duplicate() const;
114 virtual void printraw(ostream & os) const;
115 virtual void printtree(ostream & os, unsigned indent) const;
116 virtual void print(ostream & os,unsigned upper_precedence=0) const;
117 virtual void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
118 virtual void dbgprint(void) const;
119 virtual void dbgprinttree(void) const;
120 virtual bool info(unsigned inf) const;
121 virtual int nops() const;
122 virtual ex op(int const i) const;
123 virtual ex & let_op(int const i);
124 virtual ex operator[](ex const & index) const;
125 virtual ex operator[](int const i) const;
126 virtual bool has(ex const & other) const;
127 virtual int degree(symbol const & s) const;
128 virtual int ldegree(symbol const & s) const;
129 virtual ex coeff(symbol const & s, int const n=1) const;
130 virtual ex collect(symbol const & s) const;
131 virtual ex eval(int level=0) const;
132 virtual ex evalf(int level=0) const;
133 virtual ex diff(symbol const & s) const;
134 virtual ex series(symbol const & s, ex const & point, int order) const;
135 virtual ex subs(lst const & ls, lst const & lr) const;
136 virtual ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
137 virtual numeric integer_content(void) const;
138 virtual ex smod(numeric const &xi) const;
139 virtual numeric max_coefficient(void) const;
140 virtual exvector get_indices(void) const;
141 virtual ex simplify_ncmul(exvector const & v) const;
142 protected: // non-const functions should be called from class ex only
143 virtual int compare_same_type(basic const & other) const;
144 virtual bool is_equal_same_type(basic const & other) const;
145 virtual unsigned return_type(void) const;
146 virtual unsigned return_type_tinfo(void) const;
147 virtual unsigned calchash(void) const;
148 virtual ex expand(unsigned options=0) const;
150 // non-virtual functions in this class
152 ex subs(ex const & e) const;
153 int compare(basic const & other) const;
154 bool is_equal(basic const & other) const;
155 basic const & hold(void) const;
156 unsigned gethash(void) const {if (flags & status_flags::hash_calculated) return hashvalue; else return calchash();}
157 unsigned tinfo(void) const {return tinfo_key;}
159 basic const & setflag(unsigned f) const {flags |= f; return *this;}
160 basic const & clearflag(unsigned f) const {flags &= ~f; return *this;}
161 void ensure_if_modifiable(void) const;
167 mutable unsigned flags;
168 mutable unsigned hashvalue;
169 static unsigned precedence;
170 static unsigned delta_indent;
177 extern const basic some_basic;
178 extern type_info const & typeid_basic;
182 extern int max_recursion_level;
184 // convenience macros
186 #define is_of_type(OBJ,TYPE) \
187 (dynamic_cast<TYPE *>(const_cast<basic *>(&OBJ))!=0)
189 #define is_exactly_of_type(OBJ,TYPE) \
190 ((OBJ).tinfo()==TINFO_##TYPE)
192 #define is_ex_of_type(OBJ,TYPE) \
193 (dynamic_cast<TYPE *>(const_cast<basic *>((OBJ).bp))!=0)
195 #define is_ex_exactly_of_type(OBJ,TYPE) \
196 ((*(OBJ).bp).tinfo()==TINFO_##TYPE)
198 #define are_ex_trivially_equal(EX1,EX2) \
203 inline unsigned rotate_left_31(unsigned n)
205 // clear highest bit and shift 1 bit to the left
206 n=(n & 0x7FFFFFFFU) << 1;
208 // overflow? clear highest bit and set lowest bit
209 if (n & 0x80000000U) {
210 n=(n & 0x7FFFFFFFU) | 0x00000001U;
213 ASSERT(n<0x80000000U);
218 inline unsigned golden_ratio_hash(unsigned n)
221 // This requires ´long long´ (or an equivalent 64 bit type)---which is,
222 // unfortunately, not ANSI-compliant:
223 unsigned long long l = n * 0x4f1bbcddLL;
224 return (l & 0x7fffffffU) ^ (l >> 32);
226 // This requires ´long double´ to have a mantissa of at least 64 bit---
227 // which is not guaranteed by any standard:
228 const static long double golden_ratio=.618033988749894848204586834370;
229 long double m=golden_ratio*n;
230 return unsigned((m-int(m))*0x80000000);
234 #endif // ndef __GINAC_BASIC_H__