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