1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | #ifndef INCL_INT_INT_H |
---|
5 | #define INCL_INT_INT_H |
---|
6 | |
---|
7 | #include <config.h> |
---|
8 | |
---|
9 | #ifndef NOSTREAMIO |
---|
10 | #ifdef HAVE_IOSTREAM |
---|
11 | #include <iostream> |
---|
12 | #define OSTREAM std::ostream |
---|
13 | #elif defined(HAVE_IOSTREAM_H) |
---|
14 | #include <iostream.h> |
---|
15 | #define OSTREAM ostream |
---|
16 | #endif |
---|
17 | #endif /* NOSTREAMIO */ |
---|
18 | |
---|
19 | #include "assert.h" |
---|
20 | |
---|
21 | #include "int_cf.h" |
---|
22 | #include "cf_gmp.h" |
---|
23 | #include "gmpext.h" |
---|
24 | |
---|
25 | #ifdef HAVE_SINGULAR |
---|
26 | #ifndef OM_NDEBUG |
---|
27 | #define OM_NDEBUG |
---|
28 | #endif |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef HAVE_OMALLOC |
---|
32 | #include <omalloc.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | class InternalInteger : public InternalCF |
---|
36 | { |
---|
37 | private: |
---|
38 | MP_INT thempi; |
---|
39 | |
---|
40 | // auxilliary methods |
---|
41 | inline InternalCF * normalizeMyself (); |
---|
42 | inline InternalCF * uiNormalizeMyself (); |
---|
43 | |
---|
44 | static inline InternalCF * normalizeMPI ( MP_INT & ); |
---|
45 | static inline InternalCF * uiNormalizeMPI ( MP_INT & ); |
---|
46 | |
---|
47 | static inline MP_INT & MPI ( const InternalCF * const c ); |
---|
48 | #ifdef HAVE_OMALLOC |
---|
49 | static const omBin InternalInteger_bin; |
---|
50 | #endif |
---|
51 | public: |
---|
52 | #ifdef HAVE_OMALLOC |
---|
53 | void* operator new(size_t size) |
---|
54 | { |
---|
55 | void* addr; |
---|
56 | omTypeAllocBin(void*, addr, InternalInteger_bin); |
---|
57 | return addr; |
---|
58 | } |
---|
59 | void operator delete(void* addr, size_t size) |
---|
60 | { |
---|
61 | omFreeBin(addr, InternalInteger_bin); |
---|
62 | } |
---|
63 | #endif |
---|
64 | |
---|
65 | InternalInteger(); |
---|
66 | InternalInteger( const InternalCF& ) |
---|
67 | { |
---|
68 | ASSERT( 0, "ups there is something wrong in your code" ); |
---|
69 | } |
---|
70 | InternalInteger( const int i ); |
---|
71 | InternalInteger( const char * str, const int base=10 ); |
---|
72 | InternalInteger( const MP_INT & ); |
---|
73 | ~InternalInteger(); |
---|
74 | InternalCF* deepCopyObject() const; |
---|
75 | const char * const classname() const { return "InternalInteger"; } |
---|
76 | #ifndef NOSTREAMIO |
---|
77 | void print( OSTREAM&, char* ); |
---|
78 | #endif /* NOSTREAMIO */ |
---|
79 | InternalCF* genZero(); |
---|
80 | InternalCF* genOne(); |
---|
81 | |
---|
82 | bool is_imm() const; |
---|
83 | |
---|
84 | int levelcoeff() const { return IntegerDomain; } |
---|
85 | InternalCF* neg(); |
---|
86 | |
---|
87 | int comparesame( InternalCF* ); |
---|
88 | |
---|
89 | InternalCF* addsame( InternalCF* ); |
---|
90 | InternalCF* subsame( InternalCF* ); |
---|
91 | InternalCF* mulsame( InternalCF* ); |
---|
92 | InternalCF* dividesame( InternalCF* ); |
---|
93 | InternalCF* modulosame( InternalCF* ); |
---|
94 | InternalCF* divsame( InternalCF* ); |
---|
95 | InternalCF* modsame( InternalCF* ); |
---|
96 | void divremsame( InternalCF*, InternalCF*&, InternalCF*& ); |
---|
97 | bool divremsamet( InternalCF*, InternalCF*&, InternalCF*& ); |
---|
98 | |
---|
99 | int comparecoeff( InternalCF* ); |
---|
100 | |
---|
101 | InternalCF* addcoeff( InternalCF* ); |
---|
102 | InternalCF* subcoeff( InternalCF*, bool ); |
---|
103 | InternalCF* mulcoeff( InternalCF* ); |
---|
104 | InternalCF* dividecoeff( InternalCF*, bool ); |
---|
105 | InternalCF* modulocoeff( InternalCF*, bool ); |
---|
106 | InternalCF* divcoeff( InternalCF*, bool ); |
---|
107 | InternalCF* modcoeff( InternalCF*, bool ); |
---|
108 | void divremcoeff( InternalCF*, InternalCF*&, InternalCF*&, bool ); |
---|
109 | bool divremcoefft( InternalCF*, InternalCF*&, InternalCF*&, bool ); |
---|
110 | |
---|
111 | InternalCF * bgcdsame ( const InternalCF * const ) const; |
---|
112 | InternalCF * bgcdcoeff ( const InternalCF * const ); |
---|
113 | |
---|
114 | InternalCF * bextgcdsame ( InternalCF *, CanonicalForm &, CanonicalForm & ); |
---|
115 | InternalCF * bextgcdcoeff ( InternalCF *, CanonicalForm &, CanonicalForm & ); |
---|
116 | |
---|
117 | int intval() const; |
---|
118 | |
---|
119 | int intmod( int p ) const; |
---|
120 | |
---|
121 | int sign() const; |
---|
122 | |
---|
123 | InternalCF* sqrt(); |
---|
124 | |
---|
125 | int ilog2(); |
---|
126 | |
---|
127 | friend class InternalRational; |
---|
128 | #ifdef SINGULAR |
---|
129 | friend void gmp_numerator ( const CanonicalForm & f, mpz_ptr result); |
---|
130 | friend void gmp_denominator ( const CanonicalForm & f, mpz_ptr result ); |
---|
131 | #endif /* SINGULAR */ |
---|
132 | friend MP_INT getmpi ( InternalCF * value, bool symmetric ); |
---|
133 | }; |
---|
134 | |
---|
135 | //{{{ inline InternalCF * InternalInteger::normalizeMyself, uiNormalizeMyself () |
---|
136 | //{{{ docu |
---|
137 | // |
---|
138 | // normalizeMyself(), uiNormalizeMyself() - normalize CO. |
---|
139 | // |
---|
140 | // If CO fits into an immediate integer, delete CO and return the |
---|
141 | // immediate. Otherwise, return a pointer to CO. |
---|
142 | // |
---|
143 | // `uiNormalizeMyself()' is the same as `normalizeMyself()' |
---|
144 | // except that CO is expected to be non-begative. In this case, |
---|
145 | // we may use `mpz_get_ui()' to convert the underlying mpi into |
---|
146 | // an immediate which is slightly faster than the signed variant. |
---|
147 | // |
---|
148 | // Note: We do not mind reference counting at this point! CO is |
---|
149 | // deleted unconditionally! |
---|
150 | // |
---|
151 | //}}} |
---|
152 | inline InternalCF * |
---|
153 | InternalInteger::normalizeMyself () |
---|
154 | { |
---|
155 | ASSERT( getRefCount() == 1, "internal error: must not delete CO" ); |
---|
156 | |
---|
157 | if ( mpz_is_imm( &thempi ) ) { |
---|
158 | InternalCF * result = int2imm( mpz_get_si( &thempi ) ); |
---|
159 | delete this; |
---|
160 | return result; |
---|
161 | } else |
---|
162 | return this; |
---|
163 | } |
---|
164 | |
---|
165 | inline InternalCF * |
---|
166 | InternalInteger::uiNormalizeMyself () |
---|
167 | { |
---|
168 | ASSERT( getRefCount() == 1, "internal error: must not delete CO" ); |
---|
169 | |
---|
170 | if ( mpz_is_imm( &thempi ) ) { |
---|
171 | InternalCF * result = int2imm( mpz_get_ui( &thempi ) ); |
---|
172 | delete this; |
---|
173 | return result; |
---|
174 | } else |
---|
175 | return this; |
---|
176 | } |
---|
177 | //}}} |
---|
178 | |
---|
179 | //{{{ static inline InternalCF * InternalInteger::normalizeMPI, uiNormalizeMPI ( MP_INT & aMpi ) |
---|
180 | //{{{ docu |
---|
181 | // |
---|
182 | // normalizeMPI(), uiNormalizeMPI() - normalize a mpi. |
---|
183 | // |
---|
184 | // If `aMpi' fits into an immediate integer, clear `aMpi' and |
---|
185 | // return the immediate. Otherwise, return a new |
---|
186 | // `InternalInteger' with `aMpi' as underlying mpi. |
---|
187 | // |
---|
188 | // `uiNormalizeMPI()' is the same as `normalizeMPI()' except that |
---|
189 | // `aMpi' is expected to be non-begative. In this case, we may |
---|
190 | // use `mpz_get_ui()' to convert `aMpi' into an immediate which |
---|
191 | // is slightly faster than the signed variant. |
---|
192 | // |
---|
193 | //}}} |
---|
194 | inline InternalCF * |
---|
195 | InternalInteger::normalizeMPI ( MP_INT & aMpi ) |
---|
196 | { |
---|
197 | if ( mpz_is_imm( &aMpi ) ) { |
---|
198 | InternalCF * result = int2imm( mpz_get_si( &aMpi ) ); |
---|
199 | mpz_clear( &aMpi ); |
---|
200 | return result; |
---|
201 | } else |
---|
202 | return new InternalInteger( aMpi ); |
---|
203 | } |
---|
204 | |
---|
205 | inline InternalCF * |
---|
206 | InternalInteger::uiNormalizeMPI ( MP_INT & aMpi ) |
---|
207 | { |
---|
208 | if ( mpz_is_imm( &aMpi ) ) { |
---|
209 | InternalCF * result = int2imm( mpz_get_ui( &aMpi ) ); |
---|
210 | mpz_clear( &aMpi ); |
---|
211 | return result; |
---|
212 | } else |
---|
213 | return new InternalInteger( aMpi ); |
---|
214 | } |
---|
215 | //}}} |
---|
216 | |
---|
217 | //{{{ inline MP_INT & InternalInteger::MPI ( const InternalCF * const c ) |
---|
218 | //{{{ docu |
---|
219 | // |
---|
220 | // MPI() - return underlying mpi of `c'. |
---|
221 | // |
---|
222 | // `c' is expected to be an `InternalInteger *'. `c's underlying |
---|
223 | // mpi is returned. |
---|
224 | // |
---|
225 | //}}} |
---|
226 | inline MP_INT & |
---|
227 | InternalInteger::MPI ( const InternalCF * const c ) |
---|
228 | { |
---|
229 | return (((InternalInteger*)c)->thempi); |
---|
230 | } |
---|
231 | //}}} |
---|
232 | |
---|
233 | #endif /* ! INCL_INT_INT_H */ |
---|