]> www.ginac.de Git - cln.git/blob - include/cln/SV.h
cl_{GV,SV}: use std::size_t for size of vectors (instead of obscure uintC).
[cln.git] / include / cln / SV.h
1 // Simple vectors.
2
3 #ifndef _CL_SV_H
4 #define _CL_SV_H
5
6 #include "cln/object.h"
7 #include "cln/V.h"
8 #include "cln/exception.h"
9 #include <cstdlib>
10 #include <cstddef>
11
12 namespace cln {
13
14 // A simple vector has the same operations as a vector, but it can store
15 // _only_ cl_gcobject's.
16 // This class is here because the general vectors always need a function
17 // call for getting/setting the element of a vector. Our main application
18 // of the general vectors are the bit vectors, needed for implementing
19 // polynomials over modular integer rings. I don't want that polynomials
20 // over other rings (in particular cl_I) be penalized by the mere existence
21 // of polynomials over modular integer rings.
22
23 // When the vectors were implemented like this:
24 //
25 //    cl_GV<cl_I>  -->  cl_GV<cl_RA>  -->  cl_GV<cl_R>  -->  cl_GV<cl_N>
26 //
27 // a bit/byte-vector (of integers with limited range) could actually be
28 // treated correctly by all the functions which manipulate vectors of cl_N.
29 // This is not crucial, however. Here, we'll have disjoint sets
30 //
31 //    cl_SV<cl_I>  -->  cl_SV<cl_RA>  -->  cl_SV<cl_R>  -->  cl_SV<cl_N>
32 //
33 //    cl_GV<cl_I>
34 //
35 // i.e. the functions which manipulate a (simple!) vector of cl_N cannot
36 // deal with a bit/byte-vector.
37 // (This is the same issue as UPGRADED-ARRAY-ELEMENT-TYPE in Common Lisp.)
38
39 template <class T> class cl_SV_inner;
40
41 template <class T>
42 class cl_SV_inner {
43 protected:
44         std::size_t len; // number of elements
45 private:
46 //      T data[]; // the elements
47         T * data() { return (T *) (this+1); }
48         const T * data() const { return (const T *) (this+1); }
49 public:
50         std::size_t size() const { return len; } // number of elements
51         const T & operator[] (unsigned long index) const
52         {
53                 #ifndef CL_SV_NO_RANGECHECKS
54                 if (!(index < size())) throw runtime_exception();
55                 #endif
56                 return data()[index];
57         }
58         T & operator[] (unsigned long index)
59         {
60                 #ifndef CL_SV_NO_RANGECHECKS
61                 if (!(index < size())) throw runtime_exception();
62                 #endif
63                 return data()[index];
64         }
65         // New ANSI C++ compilers also want the following.
66         const T & operator[] (unsigned int index) const
67         { return operator[]((unsigned long)index); }
68         T & operator[] (unsigned int index)
69         { return operator[]((unsigned long)index); }
70         const T & operator[] (long index) const
71         { return operator[]((unsigned long)index); }
72         T & operator[] (long index)
73         { return operator[]((unsigned long)index); }
74         const T & operator[] (int index) const
75         { return operator[]((unsigned long)index); }
76         T & operator[] (int index)
77         { return operator[]((unsigned long)index); }
78 public: /* ugh */
79         // Constructor.
80         cl_SV_inner (std::size_t l) : len (l) {}
81 public:
82         // Destructor.
83         ~cl_SV_inner ();
84         // Ability to place an object at a given address.
85         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
86 private:
87 // No default constructor, copy constructor, assignment operator, new.
88         cl_SV_inner ();
89         cl_SV_inner (const cl_SV_inner&);
90         cl_SV_inner& operator= (const cl_SV_inner&);
91         void* operator new (size_t size);
92 };
93
94 // All member functions are inline.
95
96 template <class T>
97 inline cl_SV_inner<T>::~cl_SV_inner ()
98 {
99         std::size_t i = len;
100         while (i > 0) {
101                 i--;
102                 data()[i].~T();
103         }
104 }
105
106
107 // In memory, a simple vector looks like this:
108
109 template <class T>
110 struct cl_heap_SV : cl_heap {
111         cl_SV_inner<T> v;
112         // here room for the elements
113 };
114
115 template <class T, class BASE>
116 struct cl_SV : public BASE {
117 public:
118         // Length.
119         std::size_t size() const
120         {
121                 return ((const cl_heap_SV<T> *) this->pointer)->v.size();
122         }
123         // Reference. Forbid modification of `const cl_SV&' arguments.
124         const T & operator[] (unsigned long index) const
125         {
126                 return ((const cl_heap_SV<T> *) this->pointer)->v[index];
127         }
128         T & operator[] (unsigned long index)
129         {
130                 return ((cl_heap_SV<T> *) this->pointer)->v[index];
131         }
132         // New ANSI C++ compilers also want the following.
133         const T & operator[] (unsigned int index) const
134         { return operator[]((unsigned long)index); }
135         T & operator[] (unsigned int index)
136         { return operator[]((unsigned long)index); }
137         const T & operator[] (long index) const
138         { return operator[]((unsigned long)index); }
139         T & operator[] (long index)
140         { return operator[]((unsigned long)index); }
141         const T & operator[] (int index) const
142         { return operator[]((unsigned long)index); }
143         T & operator[] (int index)
144         { return operator[]((unsigned long)index); }
145         // Constructors.
146         cl_SV (const cl_SV&);
147         // Assignment operators.
148         cl_SV& operator= (const cl_SV&);
149         // Private pointer manipulations.
150         cl_SV (cl_heap_SV<T>* p) : BASE ((cl_private_thing)p) {}
151         cl_SV (cl_private_thing p) : BASE (p) {}
152 protected:
153         // Forbid use of default constructor.
154         cl_SV ();
155 };
156 #define CL_SV(T,BASE) cl_SV<T,BASE>
157 // Define copy constructor.
158 template <class T, class BASE>
159         _CL_DEFINE_COPY_CONSTRUCTOR2(CL_SV(T,BASE),cl_SV,BASE)
160 // Define assignment operator.
161 template <class T, class BASE>
162         CL_DEFINE_ASSIGNMENT_OPERATOR(CL_SV(T,BASE),CL_SV(T,BASE))
163 #undef CL_SV
164
165 // The "generic" simple vector type.
166
167 typedef cl_heap_SV<cl_gcobject> cl_heap_SV_any;
168 typedef cl_SV<cl_gcobject,cl_V_any> cl_SV_any;
169
170 // Copy a simple vector.
171 extern const cl_SV_any copy (const cl_SV_any&);
172
173
174 // Hack section.
175
176 // Conversions to subtypes without checking:
177   #define The(type)  *(const type *) & cl_identity
178 // This inline function is for type checking purposes only.
179   inline const cl_SV_any& cl_identity (const cl_SV_any& x) { return x; }
180
181 }  // namespace cln
182
183 #endif /* _CL_SV_H */