6 #include "cln/object.h"
8 #include "cln/exception.h"
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.
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;
29 std::size_t len; // number of elements
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;
43 cl_GV_inner (std::size_t l, cl_GV_vectorops<T>* ops) : len (l), vectorops (ops) {}
47 // Ability to place an object at a given address.
48 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
50 // No default constructor, copy constructor, assignment operator, new.
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>;
63 // This is the class of objects created by accessing a non-const vector
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;
78 // No assignment operator.
79 cl_GV_index& operator= (const cl_GV_index&);
82 // No default constructor.
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.
91 const cl_GV_inner<T>* vec;
95 cl_GV_constindex (const cl_GV_inner<T>* v, std::size_t i) : vec (v), index (i) {}
97 // No default constructor, assignment operator.
99 cl_GV_constindex& operator= (const cl_GV_constindex&);
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);
110 // All member functions are inline.
113 inline std::size_t cl_GV_inner<T>::size() const
119 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (unsigned long index)
121 return cl_GV_index<T>(this,index);
125 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (unsigned long index) const
127 return cl_GV_constindex<T>(this,index);
131 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (long index)
133 return operator[]((unsigned long)index);
137 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (long index) const
139 return operator[]((unsigned long)index);
143 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (unsigned int index)
145 return operator[]((unsigned long)index);
149 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (unsigned int index) const
151 return operator[]((unsigned long)index);
155 inline const cl_GV_index<T> cl_GV_inner<T>::operator[] (int index)
157 return operator[]((unsigned long)index);
161 inline const cl_GV_constindex<T> cl_GV_inner<T>::operator[] (int index) const
163 return operator[]((unsigned long)index);
167 inline cl_GV_inner<T>::~cl_GV_inner ()
169 vectorops->do_delete(this);
173 inline cl_GV_index<T>::operator T () const
175 #ifndef CL_GV_NO_RANGECHECKS
176 if (!(index < vec->len)) throw runtime_exception();
178 return vec->vectorops->element(vec,index);
182 inline void cl_GV_index<T>::operator= (const T& x) const
184 #ifndef CL_GV_NO_RANGECHECKS
185 if (!(index < vec->len)) throw runtime_exception();
187 vec->vectorops->set_element(vec,index,x);
191 inline cl_GV_constindex<T>::operator T () const
193 #ifndef CL_GV_NO_RANGECHECKS
194 if (!(index < vec->len)) throw runtime_exception();
196 return vec->vectorops->element(vec,index);
199 #if (defined(__sparc__) || defined(__mips__) || defined(__mips64__)) && !defined(__GNUC__) // maybe an SGI CC and Sun CC bug? handle "y[j] = x[i];"
201 inline void cl_GV_index<T>::operator= (const cl_GV_index<T>& x) const
202 { operator= ((T) x); }
204 inline void cl_GV_index<T>::operator= (const cl_GV_constindex<T>& x) const
205 { operator= ((T) x); }
209 // In memory, a vector looks like this:
212 struct cl_heap_GV : cl_heap {
214 // here room for the elements
217 // And a reference to a vector always looks like this:
219 template <class T, class BASE>
220 struct cl_GV : public BASE {
223 std::size_t size() const
225 return ((const cl_heap_GV<T> *) this->pointer)->v.size();
227 // Reference. Forbid modification of `const cl_GV&' arguments.
228 const cl_GV_constindex<T> operator[] (unsigned long index) const
230 return ((const cl_heap_GV<T> *) this->pointer)->v[index];
232 const cl_GV_index<T> operator[] (unsigned long index)
234 return ((cl_heap_GV<T> *) this->pointer)->v[index];
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); }
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)
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);
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) {}
267 // Forbid use of default constructor.
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
281 cl_heap_GV<T>* hpointer = (cl_heap_GV<T>*)this->pointer;
282 cl_inc_refcount(*this);
287 // The "generic" general vector type.
289 typedef cl_heap_GV<cl_gcobject> cl_heap_GV_any;
290 typedef cl_GV<cl_gcobject,cl_V_any> cl_GV_any;
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; }
302 #endif /* _CL_GV_H */