1 | #ifndef __rf_integer_ih__ |
---|
2 | #define __rf_integer_ih__ |
---|
3 | |
---|
4 | #include "rf_integer.hh" |
---|
5 | #include "rf_expr.ih" |
---|
6 | #include "rf_term.ih" |
---|
7 | #include "pxx_chunk_allocator.ih" |
---|
8 | #include "pxx_string.ih" |
---|
9 | |
---|
10 | namespace rftype |
---|
11 | { |
---|
12 | |
---|
13 | using namespace rfrt; |
---|
14 | |
---|
15 | inline Integer::Integer (intptr_t _n) : |
---|
16 | Term(type_int) |
---|
17 | { |
---|
18 | mpz_t* p = mpz_allocator.allocate(); |
---|
19 | ptr_data2 = p; |
---|
20 | mpz_init_set_si(*p, _n); |
---|
21 | } |
---|
22 | |
---|
23 | inline Integer::Integer (char const* _str) : |
---|
24 | Term(type_int) |
---|
25 | { |
---|
26 | mpz_t* p = mpz_allocator.allocate(); |
---|
27 | ptr_data2 = p; |
---|
28 | mpz_init_set_str(*p, _str, 0); |
---|
29 | } |
---|
30 | |
---|
31 | inline Integer::Integer (wchar_t const* _str) : |
---|
32 | Term(type_int) |
---|
33 | { |
---|
34 | mpz_t* p = mpz_allocator.allocate(); |
---|
35 | ptr_data2 = p; |
---|
36 | size_t len = wcstombs(null, _str, 0); |
---|
37 | char* s = static_cast<char*>(alloca(len + 1)); |
---|
38 | wcstombs(s, _str, len); |
---|
39 | s[len] = 0; |
---|
40 | mpz_init_set_str(*p, s, 0); |
---|
41 | } |
---|
42 | |
---|
43 | inline Integer::Integer (pxx::WString& _str) : |
---|
44 | Term(type_int) |
---|
45 | { |
---|
46 | mpz_t* p = mpz_allocator.allocate(); |
---|
47 | ptr_data2 = p; |
---|
48 | size_t len = wcstombs(null, _str.get_data(), 0); |
---|
49 | char* s = static_cast<char*>(alloca(len + 1)); |
---|
50 | wcstombs(s, _str.get_data(), len); |
---|
51 | s[len] = 0; |
---|
52 | mpz_init_set_str(*p, s, 0); |
---|
53 | } |
---|
54 | |
---|
55 | inline Integer::Integer (Integer const& _n) : |
---|
56 | Term(type_int) |
---|
57 | { |
---|
58 | mpz_t* p = mpz_allocator.allocate(); |
---|
59 | ptr_data2 = p; |
---|
60 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
61 | mpz_init_set(*p, *q); |
---|
62 | } |
---|
63 | |
---|
64 | inline Integer::~Integer () |
---|
65 | { |
---|
66 | // |
---|
67 | // As Integer destructor implicitly calls Term destructor and that calls |
---|
68 | // Integer::dtor() method which really destroys an Integer object, we should |
---|
69 | // not put any actions into Integer destructor body |
---|
70 | // mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
71 | // mpz_clear(*p); |
---|
72 | // mpz_allocator.deallocate(p); |
---|
73 | } |
---|
74 | |
---|
75 | inline Integer& Integer::operator = (Integer const& _n) |
---|
76 | { |
---|
77 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
78 | mpz_clear(*p); |
---|
79 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
80 | mpz_init_set(*p, *q); |
---|
81 | return self; |
---|
82 | } |
---|
83 | |
---|
84 | inline bool Integer::operator == (Integer const& _n) const |
---|
85 | { |
---|
86 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
87 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
88 | return mpz_cmp(*p, *q) == 0; |
---|
89 | } |
---|
90 | |
---|
91 | inline bool Integer::operator == (intptr_t _n) const |
---|
92 | { |
---|
93 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
94 | return mpz_cmp_si(*p, _n) == 0; |
---|
95 | } |
---|
96 | |
---|
97 | inline int Integer::cmp (Integer const& _n) const |
---|
98 | { |
---|
99 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
100 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
101 | return mpz_cmp(*p, *q); |
---|
102 | } |
---|
103 | |
---|
104 | inline int Integer::cmp (intptr_t _n) const |
---|
105 | { |
---|
106 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
107 | return mpz_cmp_si(*p, _n); |
---|
108 | } |
---|
109 | |
---|
110 | inline Integer Integer::operator + (Integer const& _n) const |
---|
111 | { |
---|
112 | Integer res(self); |
---|
113 | mpz_t* resp = res.get_mpz_ptr(); |
---|
114 | mpz_add(*resp, *resp, *(_n.get_mpz_ptr())); |
---|
115 | return res; |
---|
116 | } |
---|
117 | |
---|
118 | inline Integer Integer::operator + (intptr_t _n) const |
---|
119 | { |
---|
120 | Integer res(self); |
---|
121 | mpz_t* resp = res.get_mpz_ptr(); |
---|
122 | _n >= 0 ? mpz_add_ui(*resp, *resp, _n) : mpz_sub_ui(*resp, *resp, -_n); |
---|
123 | return res; |
---|
124 | } |
---|
125 | |
---|
126 | inline Integer operator + (intptr_t _n1, Integer const& _n2) |
---|
127 | { |
---|
128 | Integer res(_n2); |
---|
129 | mpz_t* resp = res.get_mpz_ptr(); |
---|
130 | _n1 >= 0 ? mpz_add_ui(*resp, *resp, _n1) : mpz_sub_ui(*resp, *resp, -_n1); |
---|
131 | return res; |
---|
132 | } |
---|
133 | |
---|
134 | inline Integer Integer::operator - (Integer const& _n) const |
---|
135 | { |
---|
136 | Integer res(self); |
---|
137 | mpz_t* resp = res.get_mpz_ptr(); |
---|
138 | mpz_sub(*resp, *resp, *(_n.get_mpz_ptr())); |
---|
139 | return res; |
---|
140 | } |
---|
141 | |
---|
142 | inline Integer Integer::operator - (intptr_t _n) const |
---|
143 | { |
---|
144 | Integer res(self); |
---|
145 | mpz_t* resp = res.get_mpz_ptr(); |
---|
146 | _n >= 0 ? mpz_sub_ui(*resp, *resp, _n) : mpz_add_ui(*resp, *resp, -_n); |
---|
147 | return res; |
---|
148 | } |
---|
149 | |
---|
150 | inline Integer operator - (intptr_t _n1, Integer const& _n2) |
---|
151 | { |
---|
152 | Integer res(_n2); |
---|
153 | mpz_t* resp = res.get_mpz_ptr(); |
---|
154 | mpz_neg(*resp, *resp); |
---|
155 | _n1 >= 0 ? mpz_add_ui(*resp, *resp, _n1) : mpz_sub_ui(*resp, *resp, -_n1); |
---|
156 | return res; |
---|
157 | } |
---|
158 | |
---|
159 | inline Integer Integer::operator * (Integer const& _n) const |
---|
160 | { |
---|
161 | Integer res(self); |
---|
162 | mpz_t* resp = res.get_mpz_ptr(); |
---|
163 | mpz_mul(*resp, *resp, *(_n.get_mpz_ptr())); |
---|
164 | return res; |
---|
165 | } |
---|
166 | |
---|
167 | inline Integer Integer::operator * (intptr_t _n) const |
---|
168 | { |
---|
169 | Integer res(self); |
---|
170 | mpz_t* resp = res.get_mpz_ptr(); |
---|
171 | mpz_mul_si(*resp, *resp, _n); |
---|
172 | return res; |
---|
173 | } |
---|
174 | |
---|
175 | inline Integer operator * (intptr_t _n1, Integer const& _n2) |
---|
176 | { |
---|
177 | Integer res(_n2); |
---|
178 | mpz_t* resp = res.get_mpz_ptr(); |
---|
179 | mpz_mul_si(*resp, *resp, _n1); |
---|
180 | return res; |
---|
181 | } |
---|
182 | |
---|
183 | inline Integer Integer::operator / (Integer const& _n) const |
---|
184 | { |
---|
185 | Integer res(self); |
---|
186 | mpz_t* resp = res.get_mpz_ptr(); |
---|
187 | mpz_tdiv_q(*resp, *resp, *(_n.get_mpz_ptr())); |
---|
188 | return res; |
---|
189 | } |
---|
190 | |
---|
191 | inline Integer Integer::operator / (intptr_t _n) const |
---|
192 | { |
---|
193 | Integer res(self); |
---|
194 | mpz_t* resp = res.get_mpz_ptr(); |
---|
195 | if (_n >= 0 ) { |
---|
196 | mpz_tdiv_q_ui(*resp, *resp, _n); |
---|
197 | } else { |
---|
198 | mpz_tdiv_q_ui(*resp, *resp, -_n); |
---|
199 | mpz_neg(*resp, *resp); |
---|
200 | } |
---|
201 | return res; |
---|
202 | } |
---|
203 | |
---|
204 | inline Integer operator / (intptr_t _n1, Integer const& _n2) |
---|
205 | { |
---|
206 | return Integer(_n1) / _n2; |
---|
207 | } |
---|
208 | |
---|
209 | inline Integer Integer::operator % (Integer const& _n) const |
---|
210 | { |
---|
211 | Integer res(self); |
---|
212 | mpz_t* resp = res.get_mpz_ptr(); |
---|
213 | mpz_tdiv_r(*resp, *resp, *(_n.get_mpz_ptr())); |
---|
214 | return res; |
---|
215 | } |
---|
216 | |
---|
217 | inline Integer Integer::operator % (intptr_t _n) const |
---|
218 | { |
---|
219 | Integer res(self); |
---|
220 | mpz_t* resp = res.get_mpz_ptr(); |
---|
221 | if (_n >= 0 ) { |
---|
222 | mpz_tdiv_r_ui(*resp, *resp, _n); |
---|
223 | } else { |
---|
224 | mpz_tdiv_r_ui(*resp, *resp, -_n); |
---|
225 | } |
---|
226 | return res; |
---|
227 | } |
---|
228 | |
---|
229 | inline Integer operator % (intptr_t _n1, Integer const& _n2) |
---|
230 | { |
---|
231 | return Integer(_n1) % _n2; |
---|
232 | } |
---|
233 | |
---|
234 | inline Integer Integer::div_rem (Integer const& _n, Integer* _remp) const |
---|
235 | { |
---|
236 | Integer res(self); |
---|
237 | mpz_t* resp = res.get_mpz_ptr(); |
---|
238 | if (_remp != null) { |
---|
239 | mpz_tdiv_qr(*resp, *(_remp->get_mpz_ptr()), *resp, *(_n.get_mpz_ptr())); |
---|
240 | } else { |
---|
241 | mpz_tdiv_q(*resp, *resp, *(_n.get_mpz_ptr())); |
---|
242 | } |
---|
243 | return res; |
---|
244 | } |
---|
245 | |
---|
246 | inline Integer Integer::div_rem (intptr_t _n, Integer* _remp) const |
---|
247 | { |
---|
248 | Integer res(self); |
---|
249 | mpz_t* resp = res.get_mpz_ptr(); |
---|
250 | if (_remp != null) { |
---|
251 | if (_n >= 0 ) { |
---|
252 | mpz_tdiv_qr_ui(*resp, *(_remp->get_mpz_ptr()), *resp, _n); |
---|
253 | } else { |
---|
254 | mpz_tdiv_qr_ui(*resp, *(_remp->get_mpz_ptr()), *resp, -_n); |
---|
255 | mpz_neg(*resp, *resp); |
---|
256 | } |
---|
257 | } else { |
---|
258 | if (_n >= 0 ) { |
---|
259 | mpz_tdiv_r_ui(*resp, *resp, _n); |
---|
260 | } else { |
---|
261 | mpz_tdiv_r_ui(*resp, *resp, -_n); |
---|
262 | mpz_neg(*resp, *resp); |
---|
263 | } |
---|
264 | } |
---|
265 | return res; |
---|
266 | } |
---|
267 | |
---|
268 | inline Integer div_rem (intptr_t _n1, Integer const& _n2, Integer* _remp) |
---|
269 | { |
---|
270 | return Integer(_n1).div_rem(_n2, _remp); |
---|
271 | } |
---|
272 | |
---|
273 | inline Integer Integer::operator - () const |
---|
274 | { |
---|
275 | Integer res(self); |
---|
276 | mpz_t* resp = res.get_mpz_ptr(); |
---|
277 | mpz_neg(*resp, *resp); |
---|
278 | return res; |
---|
279 | } |
---|
280 | |
---|
281 | inline int Integer::sign () const |
---|
282 | { |
---|
283 | return mpz_sgn(*(get_mpz_ptr())); |
---|
284 | } |
---|
285 | |
---|
286 | inline long Integer::to_int () const |
---|
287 | { |
---|
288 | return mpz_get_si(*(get_mpz_ptr())); |
---|
289 | } |
---|
290 | |
---|
291 | inline Integer::operator pxx::WString () const |
---|
292 | { |
---|
293 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
294 | size_t len = mpz_sizeinbase(*p, 10) + 2; |
---|
295 | char* s = static_cast<char*>(alloca(len)); |
---|
296 | mpz_get_str(s, 10, *p); |
---|
297 | len = strlen(s) + 1; |
---|
298 | wchar_t* ws = static_cast<wchar_t*>(alloca((len + 1)* sizeof(wchar_t))); |
---|
299 | len = mbstowcs(ws, s, len); |
---|
300 | ws[len] = 0; |
---|
301 | return pxx::WString(ws, len); |
---|
302 | } |
---|
303 | |
---|
304 | inline Expr Integer::create_expr (intptr_t _n) |
---|
305 | { |
---|
306 | Expr e = Term::create_expr(1); |
---|
307 | new(e.get_first()) Integer(_n); |
---|
308 | return e; |
---|
309 | } |
---|
310 | |
---|
311 | inline Expr Integer::create_expr (Integer const& _n) |
---|
312 | { |
---|
313 | Expr e = Term::create_expr(1); |
---|
314 | new(e.get_first()) Integer(_n); |
---|
315 | return e; |
---|
316 | } |
---|
317 | |
---|
318 | inline Expr Integer::create_expr (char const* _str) |
---|
319 | { |
---|
320 | Expr e = Term::create_expr(1); |
---|
321 | new(e.get_first()) Integer(_str); |
---|
322 | return e; |
---|
323 | } |
---|
324 | |
---|
325 | inline Expr Integer::create_expr (wchar_t const* _str) |
---|
326 | { |
---|
327 | Expr e = Term::create_expr(1); |
---|
328 | new(e.get_first()) Integer(_str); |
---|
329 | return e; |
---|
330 | } |
---|
331 | |
---|
332 | inline Expr Integer::create_expr (pxx::WString& _str) |
---|
333 | { |
---|
334 | wchar_t const* data = _str.get_data(); |
---|
335 | return Integer::create_expr(data); |
---|
336 | } |
---|
337 | |
---|
338 | inline mpz_t* Integer::get_mpz_ptr () const |
---|
339 | { |
---|
340 | return static_cast<mpz_t*>(ptr_data2); |
---|
341 | } |
---|
342 | |
---|
343 | } |
---|
344 | |
---|
345 | #endif // __rf_integer_ih__ |
---|