]> www.ginac.de Git - cln.git/blob - include/cln/GV.h
cl_{GV,SV}: use std::size_t for size of vectors (instead of obscure uintC).
[cln.git] / include / cln / GV.h
1 // General vectors.
2
3 #ifndef _CL_GV_H
4 #define _CL_GV_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 vector is a structure having the following interface:
15 //     v.size()        returns the number of elements
16 //     v[i]              returns the i-th element (0<=i<length), as a
17 //                       pseudo-lvalue (you can assign to it, but not take its
18 //                       address - exactly what you want for bit-vectors)
19 // This is implemented by letting v[i] be of a special "vector index" type.
20
21 template <class T> class cl_GV_inner;
22 template <class T> class cl_GV_index;
23 template <class T> class cl_GV_constindex;
24 template <class T> struct cl_GV_vectorops;
25
26 template <class T>
27 class cl_GV_inner {
28 protected:
29         std::size_t len; // number of elements
30 public:
31         std::size_t size() const; // number of elements
32         cl_GV_vectorops<T>* vectorops; // get/set element
33         const cl_GV_index<T> operator[] (unsigned long index);
34         const cl_GV_constindex<T> operator[] (unsigned long index) const;
35         const cl_GV_index<T> operator[] (long index);
36         const cl_GV_constindex<T> operator[] (long index) const;
37         const cl_GV_index<T> operator[] (unsigned int index);
38         const cl_GV_constindex<T> operator[] (unsigned int index) const;
39         const cl_GV_index<T> operator[] (int index);
40         const cl_GV_constindex<T> operator[] (int index) const;
41 public: /* ugh */
42         // Constructor.
43         cl_GV_inner (std::size_t l, cl_GV_vectorops<T>* ops) : len (l), vectorops (ops) {}
44 public:
45         // Destructor.
46         ~cl_GV_inner ();
47         // Ability to place an object at a given address.
48         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
49 private:
50 // No default constructor, copy constructor, assignment operator, new.
51         cl_GV_inner ();
52         cl_GV_inner (const cl_GV_inner&);
53         cl_GV_inner& operator= (const cl_GV_inner&);
54         void* operator new (size_t size)
55                 { (void)size; return (void*)1; } // SGI CC needs this definition
56 // Friend declarations. They are for the compiler. Just ignore them.
57         friend class cl_GV_index<T>;
58         friend class cl_GV_constindex<T>;
59 };
60
61 template <class T>
62 class cl_GV_index {
63         // This is the class of objects created by accessing a non-const vector
64         // through [].
65 public:
66         cl_GV_inner<T>* vec;
67         std::size_t index;
68         operator T () const;
69         // Constructor:
70         cl_GV_index (cl_GV_inner<T>* v, std::size_t i) : vec (v), index (i) {}
71         // Assignment operator.
72         void operator= (const T& x) const;
73 #if (defined(__sparc__) || defined(__sparc64__) || defined(__mips__) || defined(__mips64__)) && !defined(__GNUC__) // maybe an SGI CC and Sun CC bug?
74         void operator= (const cl_GV_index<T>&) const;
75         void operator= (const cl_GV_constindex<T>&) const;
76 #else
77 private:
78         // No assignment operator.
79         cl_GV_index& operator= (const cl_GV_index&);
80 #endif
81 private:
82 // No default constructor.
83         cl_GV_index ();
84 };
85
86 template <class T>
87 class cl_GV_constindex {
88         // This is the class of objects created by accessing a const vector
89         // through []. It lacks the assignment operator.
90 public:
91         const cl_GV_inner<T>* vec;
92         std::size_t index;
93         operator T () const;
94         // Constructor:
95         cl_GV_constindex (const cl_GV_inner<T>* v, std::size_t i) : vec (v), index (i) {}
96 private:
97 // No default constructor, assignment operator.
98         cl_GV_constindex ();
99         cl_GV_constindex& operator= (const cl_GV_constindex&);
100 };
101
102 template <class T>
103 struct cl_GV_vectorops {
104         const T (*element) (const cl_GV_inner<T>* vec, std::size_t index);
105         void (*set_element) (cl_GV_inner<T>* vec, std::size_t index, const T& x);
106         void (*do_delete) (cl_GV_inner<T>* vec);
107         void (*copy_elements) (const cl_GV_inner<T>* srcvec, std::size_t srcindex, cl_GV_inner<T>* destvec, std::size_t destindex, std::size_t count);
108 };
109
110 // All member functions are inline.
111
112 template <class T>
113 inline std::size_t cl_GV_inner<T>::size() const
114 {
115         return len;
116 }
117
118 template <class T>
119 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (unsigned long index)
120 {
121         return cl_GV_index<T>(this,index);
122 }
123
124 template <class T>
125 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (unsigned long index) const
126 {
127         return cl_GV_constindex<T>(this,index);
128 }
129
130 template <class T>
131 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (long index)
132 {
133         return operator[]((unsigned long)index);
134 }
135
136 template <class T>
137 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (long index) const
138 {
139         return operator[]((unsigned long)index);
140 }
141
142 template <class T>
143 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (unsigned int index)
144 {
145         return operator[]((unsigned long)index);
146 }
147
148 template <class T>
149 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (unsigned int index) const
150 {
151         return operator[]((unsigned long)index);
152 }
153
154 template <class T>
155 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (int index)
156 {
157         return operator[]((unsigned long)index);
158 }
159
160 template <class T>
161 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (int index) const
162 {
163         return operator[]((unsigned long)index);
164 }
165
166 template <class T>
167 inline cl_GV_inner<T>::~cl_GV_inner ()
168 {
169         vectorops->do_delete(this);
170 }
171
172 template <class T>
173 inline cl_GV_index<T>::operator T () const
174 {
175         #ifndef CL_GV_NO_RANGECHECKS
176         if (!(index < vec->len)) throw runtime_exception();
177         #endif
178         return vec->vectorops->element(vec,index);
179 }
180
181 template <class T>
182 inline void cl_GV_index<T>::operator= (const T& x) const
183 {
184         #ifndef CL_GV_NO_RANGECHECKS
185         if (!(index < vec->len)) throw runtime_exception();
186         #endif
187         vec->vectorops->set_element(vec,index,x);
188 }
189
190 template <class T>
191 inline cl_GV_constindex<T>::operator T () const
192 {
193         #ifndef CL_GV_NO_RANGECHECKS
194         if (!(index < vec->len)) throw runtime_exception();
195         #endif
196         return vec->vectorops->element(vec,index);
197 }
198
199 #if (defined(__sparc__) || defined(__mips__) || defined(__mips64__)) && !defined(__GNUC__) // maybe an SGI CC and Sun CC bug? handle "y[j] = x[i];"
200 template <class T>
201 inline void cl_GV_index<T>::operator= (const cl_GV_index<T>& x) const
202 { operator= ((T) x); }
203 template <class T>
204 inline void cl_GV_index<T>::operator= (const cl_GV_constindex<T>& x) const
205 { operator= ((T) x); }
206 #endif
207
208
209 // In memory, a vector looks like this:
210
211 template <class T>
212 struct cl_heap_GV : cl_heap {
213         cl_GV_inner<T> v;
214         // here room for the elements
215 };
216
217 // And a reference to a vector always looks like this:
218
219 template <class T, class BASE>
220 struct cl_GV : public BASE {
221 public:
222         // Length.
223         std::size_t size() const
224         {
225                 return ((const cl_heap_GV<T> *) this->pointer)->v.size();
226         }
227         // Reference. Forbid modification of `const cl_GV&' arguments.
228         const cl_GV_constindex<T> operator[] (unsigned long index) const
229         {
230                 return ((const cl_heap_GV<T> *) this->pointer)->v[index];
231         }
232         const cl_GV_index<T> operator[] (unsigned long index)
233         {
234                 return ((cl_heap_GV<T> *) this->pointer)->v[index];
235         }
236         const cl_GV_constindex<T> operator[] (long index) const
237         { return operator[]((unsigned long)index); }
238         const cl_GV_index<T> operator[] (long index)
239         { return operator[]((unsigned long)index); }
240         const cl_GV_constindex<T> operator[] (unsigned int index) const
241         { return operator[]((unsigned long)index); }
242         const cl_GV_index<T> operator[] (unsigned int index)
243         { return operator[]((unsigned long)index); }
244         const cl_GV_constindex<T> operator[] (int index) const
245         { return operator[]((unsigned long)index); }
246         const cl_GV_index<T> operator[] (int index)
247         { return operator[]((unsigned long)index); }
248         // Copy constructor.
249         cl_GV (const cl_GV&);
250         // Assignment operator.
251         cl_GV& operator= (const cl_GV&);
252         // Copy a piece of a vector into another vector.
253         // (Both vectors must be of the same type. Overlapping not allowed.)
254         static void copy_elements (const cl_GV& src, std::size_t srcindex, cl_GV& dest, std::size_t destindex, std::size_t count)
255         {
256                 const cl_heap_GV<T> * hsrc = (const cl_heap_GV<T> *) src.pointer;
257                 cl_heap_GV<T> * hdest = (cl_heap_GV<T> *) dest.pointer;
258                 if (!(hsrc->v.vectorops == hdest->v.vectorops))
259                         throw runtime_exception();
260                 hsrc->v.vectorops->copy_elements(&hsrc->v,srcindex,&hdest->v,destindex,count);
261         }
262         // Private pointer manipulations.
263         operator cl_heap_GV<T>* () const;
264         cl_GV (cl_heap_GV<T>* p) : BASE ((cl_private_thing) p) {}
265         cl_GV (cl_private_thing p) : BASE (p) {}
266 protected:
267         // Forbid use of default constructor.
268         cl_GV ();
269 };
270 #define CL_GV(T,BASE) cl_GV<T,BASE>
271 // Define copy constructor.
272 template <class T, class BASE>
273         _CL_DEFINE_COPY_CONSTRUCTOR2(CL_GV(T,BASE),cl_GV,BASE)
274 // Define assignment operator.
275 template <class T, class BASE>
276         CL_DEFINE_ASSIGNMENT_OPERATOR(CL_GV(T,BASE),CL_GV(T,BASE))
277 // Private pointer manipulations. Never throw away a `struct cl_heap_GV<T> *'!
278 template <class T, class BASE>
279 inline CL_GV(T,BASE)::operator cl_heap_GV<T>* () const
280 {
281         cl_heap_GV<T>* hpointer = (cl_heap_GV<T>*)this->pointer;
282         cl_inc_refcount(*this);
283         return hpointer;
284 }
285 #undef CL_GV
286
287 // The "generic" general vector type.
288
289 typedef cl_heap_GV<cl_gcobject> cl_heap_GV_any;
290 typedef cl_GV<cl_gcobject,cl_V_any> cl_GV_any;
291
292
293 // Hack section.
294
295 // Conversions to subtypes without checking:
296   #define The(type)  *(const type *) & cl_identity
297 // This inline function is for type checking purposes only.
298   inline const cl_GV_any& cl_identity (const cl_GV_any& x) { return x; }
299
300 }  // namespace cln
301
302 #endif /* _CL_GV_H */