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 (pxx::WString& _str) : |
---|
24 | Term(type_int) |
---|
25 | { |
---|
26 | mpz_t* p = mpz_allocator.allocate(); |
---|
27 | ptr_data2 = p; |
---|
28 | size_t len = wcstombs(null, _str.get_data(), 0); |
---|
29 | char* s = static_cast<char*>(alloca(len + 1)); |
---|
30 | wcstombs(s, _str.get_data(), len); |
---|
31 | s[len] = 0; |
---|
32 | mpz_init_set_str(*p, s, 0); |
---|
33 | } |
---|
34 | |
---|
35 | inline Integer::Integer (Integer const& _n) : |
---|
36 | Term(type_int) |
---|
37 | { |
---|
38 | mpz_t* p = mpz_allocator.allocate(); |
---|
39 | ptr_data2 = p; |
---|
40 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
41 | mpz_init_set(*p, *q); |
---|
42 | } |
---|
43 | |
---|
44 | inline Integer::~Integer () |
---|
45 | { |
---|
46 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
47 | mpz_clear(*p); |
---|
48 | mpz_allocator.deallocate(p); |
---|
49 | } |
---|
50 | |
---|
51 | inline Integer& Integer::operator = (Integer const& _n) |
---|
52 | { |
---|
53 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
54 | mpz_clear(*p); |
---|
55 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
56 | mpz_init_set(*p, *q); |
---|
57 | return self; |
---|
58 | } |
---|
59 | |
---|
60 | inline bool Integer::operator == (Integer const& _n) const |
---|
61 | { |
---|
62 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
63 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
64 | return mpz_cmp(*p, *q) == 0; |
---|
65 | } |
---|
66 | |
---|
67 | inline int Integer::cmp (Integer const& _n) const |
---|
68 | { |
---|
69 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
70 | mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2); |
---|
71 | return mpz_cmp(*p, *q); |
---|
72 | } |
---|
73 | |
---|
74 | inline Integer::operator pxx::WString () const |
---|
75 | { |
---|
76 | mpz_t* p = static_cast<mpz_t*>(ptr_data2); |
---|
77 | size_t len = mpz_sizeinbase(*p, 10) + 2; |
---|
78 | char* s = static_cast<char*>(alloca(len)); |
---|
79 | mpz_get_str(s, 10, *p); |
---|
80 | len = strlen(s) + 1; |
---|
81 | wchar_t* ws = static_cast<wchar_t*>(alloca((len + 1)* sizeof(wchar_t))); |
---|
82 | len = mbstowcs(ws, s, len); |
---|
83 | ws[len] = 0; |
---|
84 | return pxx::WString(ws, len); |
---|
85 | } |
---|
86 | |
---|
87 | inline Expr Integer::create_expr (intptr_t _n) |
---|
88 | { |
---|
89 | Expr e = Term::create_expr(1); |
---|
90 | new(e.get_first()) Integer(_n); |
---|
91 | return e; |
---|
92 | } |
---|
93 | |
---|
94 | inline mpz_t* Integer::get_mpz_ptr () |
---|
95 | { |
---|
96 | return static_cast<mpz_t*>(ptr_data2); |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | #endif // __rf_integer_ih__ |
---|