]> www.ginac.de Git - cln.git/blob - src/integer/ring/cl_I_ring.cc
Janitorial clean-up.
[cln.git] / src / integer / ring / cl_I_ring.cc
1 // Ring of integers.
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "cln/integer_ring.h"
8
9
10 // Implementation.
11
12 #include "cln/integer.h"
13 #include "cln/integer_io.h"
14 #define zerop zerop_inline
15 #include "integer/cl_I.h"
16 #undef zerop
17
18 namespace cln {
19
20 static void I_fprint (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x)
21 {
22         unused R;
23         fprint(stream,The(cl_I)(x));
24 }
25
26 static bool I_equal (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
27 {
28         unused R;
29         return equal(The(cl_I)(x),The(cl_I)(y));
30 }
31
32 static const _cl_ring_element I_zero (cl_heap_ring* R)
33 {
34         return _cl_ring_element(R, (cl_I)0);
35 }
36
37 static bool CL_FLATTEN I_zerop (cl_heap_ring* R, const _cl_ring_element& x)
38 {
39         unused R;
40         return zerop_inline(The(cl_I)(x));
41 }
42
43 static const _cl_ring_element I_plus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
44 {
45         return _cl_ring_element(R, The(cl_I)(x) + The(cl_I)(y));
46 }
47
48 static const _cl_ring_element I_minus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
49 {
50         return _cl_ring_element(R, The(cl_I)(x) - The(cl_I)(y));
51 }
52
53 static const _cl_ring_element I_uminus (cl_heap_ring* R, const _cl_ring_element& x)
54 {
55         return _cl_ring_element(R, - The(cl_I)(x));
56 }
57
58 static const _cl_ring_element I_one (cl_heap_ring* R)
59 {
60         return _cl_ring_element(R, (cl_I)1);
61 }
62
63 static const _cl_ring_element I_canonhom (cl_heap_ring* R, const cl_I& x)
64 {
65         return _cl_ring_element(R, (cl_I)x);
66 }
67
68 static const _cl_ring_element I_mul (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
69 {
70         return _cl_ring_element(R, The(cl_I)(x) * The(cl_I)(y));
71 }
72
73 static const _cl_ring_element I_square (cl_heap_ring* R, const _cl_ring_element& x)
74 {
75         return _cl_ring_element(R, square(The(cl_I)(x)));
76 }
77
78 static const _cl_ring_element I_expt_pos (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y)
79 {
80         return _cl_ring_element(R, expt_pos(The(cl_I)(x),y));
81 }
82
83 static bool cl_I_p (const cl_number& x)
84 {
85         return (!x.pointer_p()
86                 ? x.nonpointer_tag() == cl_FN_tag
87                 : x.pointer_type() == &cl_class_bignum);
88 }
89
90 static cl_ring_setops I_setops = {
91         I_fprint,
92         I_equal
93 };
94 static cl_ring_addops I_addops = {
95         I_zero,
96         I_zerop,
97         I_plus,
98         I_minus,
99         I_uminus
100 };
101 static cl_ring_mulops I_mulops = {
102         I_one,
103         I_canonhom,
104         I_mul,
105         I_square,
106         I_expt_pos
107 };
108
109 static cl_number_ring_ops<cl_I> I_ops = {
110         cl_I_p,
111         equal,
112         zerop,
113         operator+,
114         operator-,
115         operator-,
116         operator*,
117         square,
118         expt_pos
119 };
120
121 class cl_heap_integer_ring : public cl_heap_number_ring {
122         SUBCLASS_cl_heap_ring()
123 public:
124         // Constructor.
125         cl_heap_integer_ring ()
126                 : cl_heap_number_ring (&I_setops,&I_addops,&I_mulops,
127                                        (cl_number_ring_ops<cl_number>*) &I_ops)
128                 { type = &cl_class_integer_ring; }
129         // Destructor.
130         ~cl_heap_integer_ring () {}
131 };
132
133 static void cl_integer_ring_destructor (cl_heap* pointer)
134 {
135         (*(cl_heap_integer_ring*)pointer).~cl_heap_integer_ring();
136 }
137
138 static void cl_integer_ring_dprint (cl_heap* pointer)
139 {
140         unused pointer;
141         fprint(cl_debugout, "(cl_integer_ring) cl_I_ring");
142 }
143
144 cl_class cl_class_integer_ring;
145 static cl_heap_integer_ring* cl_heap_integer_ring_instance;
146
147 // Constructor.
148 template <>
149 inline cl_integer_ring::cl_specialized_number_ring ()
150         : cl_number_ring(cl_heap_integer_ring_instance) {}
151
152 const cl_integer_ring cl_I_ring = cl_I_ring;
153
154 int cl_I_ring_init_helper::count = 0;
155
156 cl_I_ring_init_helper::cl_I_ring_init_helper()
157 {
158         if (count++ == 0) {
159                 cl_class_integer_ring.destruct = cl_integer_ring_destructor;
160                 cl_class_integer_ring.flags = cl_class_flags_number_ring;
161                 cl_class_integer_ring.dprint = cl_integer_ring_dprint;
162                 cl_heap_integer_ring_instance = new cl_heap_integer_ring();
163                 new ((void *)&cl_I_ring) cl_integer_ring();
164         }
165 }
166
167 cl_I_ring_init_helper::~cl_I_ring_init_helper()
168 {
169         if (--count == 0) {
170                 delete cl_heap_integer_ring_instance;
171         }
172 }
173
174 }  // namespace cln
175