3 * Interface to GiNaC's ABC. */
22 typedef vector<ex> exvector;
24 #define INLINE_BASIC_CONSTRUCTORS
26 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
27 * It is responsible for the reference counting. */
34 // default constructor, destructor, copy constructor assignment operator and helpers
37 #ifdef INLINE_BASIC_CONSTRUCTORS
38 : tinfo_key(TINFO_BASIC), flags(0), refcount(0)
40 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
45 #endif // def INLINE_BASIC_CONSTRUCTORS
48 #ifdef INLINE_BASIC_CONSTRUCTORS
50 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
52 ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
56 #endif // def INLINE_BASIC_CONSTRUCTORS
58 basic(basic const & other)
59 #ifdef INLINE_BASIC_CONSTRUCTORS
61 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
66 #endif // def INLINE_BASIC_CONSTRUCTORS
68 virtual basic const & operator=(basic const & other);
71 void copy(basic const & other)
73 flags = other.flags & ~status_flags::dynallocated;
74 hashvalue = other.hashvalue;
75 tinfo_key = other.tinfo_key;
77 void destroy(bool call_parent) {}
81 #ifdef INLINE_BASIC_CONSTRUCTORS
82 : tinfo_key(ti), flags(0), refcount(0)
84 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
89 #endif // def INLINE_BASIC_CONSTRUCTORS
91 // functions overriding virtual functions from bases classes
94 // new virtual functions which can be overridden by derived classes
95 public: // only const functions please (may break reference counting)
96 virtual basic * duplicate() const;
97 virtual void printraw(ostream & os) const;
98 virtual void printtree(ostream & os, unsigned indent) const;
99 virtual void print(ostream & os,unsigned upper_precedence=0) const;
100 virtual void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
101 virtual void dbgprint(void) const;
102 virtual void dbgprinttree(void) const;
103 virtual bool info(unsigned inf) const;
104 virtual int nops() const;
105 virtual ex op(int const i) const;
106 virtual ex & let_op(int const i);
107 virtual ex operator[](ex const & index) const;
108 virtual ex operator[](int const i) const;
109 virtual bool has(ex const & other) const;
110 virtual int degree(symbol const & s) const;
111 virtual int ldegree(symbol const & s) const;
112 virtual ex coeff(symbol const & s, int const n=1) const;
113 virtual ex collect(symbol const & s) const;
114 virtual ex eval(int level=0) const;
115 virtual ex evalf(int level=0) const;
116 virtual ex diff(symbol const & s) const;
117 virtual ex series(symbol const & s, ex const & point, int order) const;
118 virtual ex subs(lst const & ls, lst const & lr) const;
119 virtual ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
120 virtual numeric integer_content(void) const;
121 virtual ex smod(numeric const &xi) const;
122 virtual numeric max_coefficient(void) const;
123 virtual exvector get_indices(void) const;
124 virtual ex simplify_ncmul(exvector const & v) const;
125 protected: // non-const functions should be called from class ex only
126 virtual int compare_same_type(basic const & other) const;
127 virtual bool is_equal_same_type(basic const & other) const;
128 virtual unsigned return_type(void) const;
129 virtual unsigned return_type_tinfo(void) const;
130 virtual unsigned calchash(void) const;
131 virtual ex expand(unsigned options=0) const;
133 // non-virtual functions in this class
135 ex subs(ex const & e) const;
136 int compare(basic const & other) const;
137 bool is_equal(basic const & other) const;
138 basic const & hold(void) const;
139 unsigned gethash(void) const {if (flags & status_flags::hash_calculated) return hashvalue; else return calchash();}
140 unsigned tinfo(void) const {return tinfo_key;}
142 basic const & setflag(unsigned f) const {flags |= f; return *this;}
143 basic const & clearflag(unsigned f) const {flags &= ~f; return *this;}
144 void ensure_if_modifiable(void) const;
150 mutable unsigned flags;
151 mutable unsigned hashvalue;
152 static unsigned precedence;
153 static unsigned delta_indent;
160 extern const basic some_basic;
161 extern type_info const & typeid_basic;
165 extern int max_recursion_level;
170 #define is_of_type(OBJ,TYPE) \
171 (dynamic_cast<TYPE *>(const_cast<basic *>(&OBJ))!=0)
174 #define is_exactly_of_type(OBJ,TYPE) \
175 (typeid(OBJ)==typeid(some_##TYPE))
177 #define is_exactly_of_type(OBJ,TYPE) \
178 ((OBJ).tinfo()==(some_##TYPE).tinfo())
183 #define is_of_type(OBJ,TYPE) \
184 (ASSERT(typeid(OBJ)!=typeid(exZERO())), \
185 (dynamic_cast<TYPE *>(const_cast<basic *>(&OBJ))!=0))
187 #define is_exactly_of_type(OBJ,TYPE) \
188 (ASSERT(typeid(OBJ)!=typeid(exZERO())), \
189 (typeid(OBJ)==typeid(some_##TYPE))
190 #endif // ndef _DEBUG
193 #define is_ex_of_type(OBJ,TYPE) \
194 (dynamic_cast<TYPE *>(const_cast<basic *>((OBJ).bp))!=0)
197 #define is_ex_exactly_of_type(OBJ,TYPE) \
198 (typeid(*(OBJ).bp)==typeid(some_##TYPE))
201 #define is_ex_exactly_of_type(OBJ,TYPE) \
202 ((*(OBJ).bp).tinfo()==(some_##TYPE).tinfo())
204 #define are_ex_trivially_equal(EX1,EX2) \
209 inline unsigned rotate_left_31(unsigned n)
211 // clear highest bit and shift 1 bit to the left
212 n=(n & 0x7FFFFFFFU) << 1;
214 // overflow? clear highest bit and set lowest bit
215 if (n & 0x80000000U) {
216 n=(n & 0x7FFFFFFFU) | 0x00000001U;
219 ASSERT(n<0x80000000U);
224 inline unsigned golden_ratio_hash(unsigned n)
227 // This requires ´long long´ (or an equivalent 64 bit type)---which is,
228 // unfortunately, not ANSI-compliant:
229 unsigned long long l = n * 0x4f1bbcddLL;
230 return (l & 0x7fffffffU) ^ (l >> 32);
232 // This requires ´long double´ to have a mantissa of at least 64 bit---
233 // which is not guaranteed by any standard:
234 const static long double golden_ratio=.618033988749894848204586834370;
235 long double m=golden_ratio*n;
236 return unsigned((m-int(m))*0x80000000);
240 #endif // ndef _BASIC_H_