6 #include "cln/object.h"
8 #include "cln/exception.h"
13 // A simple vector has the same operations as a vector, but it can store
14 // _only_ cl_gcobject's.
15 // This class is here because the general vectors always need a function
16 // call for getting/setting the element of a vector. Our main application
17 // of the general vectors are the bit vectors, needed for implementing
18 // polynomials over modular integer rings. I don't want that polynomials
19 // over other rings (in particular cl_I) be penalized by the mere existence
20 // of polynomials over modular integer rings.
22 // When the vectors were implemented like this:
24 // cl_GV<cl_I> --> cl_GV<cl_RA> --> cl_GV<cl_R> --> cl_GV<cl_N>
26 // a bit/byte-vector (of integers with limited range) could actually be
27 // treated correctly by all the functions which manipulate vectors of cl_N.
28 // This is not crucial, however. Here, we'll have disjoint sets
30 // cl_SV<cl_I> --> cl_SV<cl_RA> --> cl_SV<cl_R> --> cl_SV<cl_N>
34 // i.e. the functions which manipulate a (simple!) vector of cl_N cannot
35 // deal with a bit/byte-vector.
36 // (This is the same issue as UPGRADED-ARRAY-ELEMENT-TYPE in Common Lisp.)
38 template <class T> class cl_SV_inner;
43 uintC len; // number of elements
45 // T data[]; // the elements
46 T * data() { return (T *) (this+1); }
47 const T * data() const { return (const T *) (this+1); }
49 uintC size() const { return len; } // number of elements
50 const T & operator[] (unsigned long index) const
52 #ifndef CL_SV_NO_RANGECHECKS
53 if (!(index < size())) throw runtime_exception();
57 T & operator[] (unsigned long index)
59 #ifndef CL_SV_NO_RANGECHECKS
60 if (!(index < size())) throw runtime_exception();
64 // New ANSI C++ compilers also want the following.
65 const T & operator[] (unsigned int index) const
66 { return operator[]((unsigned long)index); }
67 T & operator[] (unsigned int index)
68 { return operator[]((unsigned long)index); }
69 const T & operator[] (long index) const
70 { return operator[]((unsigned long)index); }
71 T & operator[] (long index)
72 { return operator[]((unsigned long)index); }
73 const T & operator[] (int index) const
74 { return operator[]((unsigned long)index); }
75 T & operator[] (int index)
76 { return operator[]((unsigned long)index); }
79 cl_SV_inner (uintC l) : len (l) {}
83 // Ability to place an object at a given address.
84 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
86 // No default constructor, copy constructor, assignment operator, new.
88 cl_SV_inner (const cl_SV_inner&);
89 cl_SV_inner& operator= (const cl_SV_inner&);
90 void* operator new (size_t size);
93 // All member functions are inline.
96 inline cl_SV_inner<T>::~cl_SV_inner ()
106 // In memory, a simple vector looks like this:
109 struct cl_heap_SV : cl_heap {
111 // here room for the elements
114 template <class T, class BASE>
115 struct cl_SV : public BASE {
120 return ((const cl_heap_SV<T> *) this->pointer)->v.size();
122 // Reference. Forbid modification of `const cl_SV&' arguments.
123 const T & operator[] (unsigned long index) const
125 return ((const cl_heap_SV<T> *) this->pointer)->v[index];
127 T & operator[] (unsigned long index)
129 return ((cl_heap_SV<T> *) this->pointer)->v[index];
131 // New ANSI C++ compilers also want the following.
132 const T & operator[] (unsigned int index) const
133 { return operator[]((unsigned long)index); }
134 T & operator[] (unsigned int index)
135 { return operator[]((unsigned long)index); }
136 const T & operator[] (long index) const
137 { return operator[]((unsigned long)index); }
138 T & operator[] (long index)
139 { return operator[]((unsigned long)index); }
140 const T & operator[] (int index) const
141 { return operator[]((unsigned long)index); }
142 T & operator[] (int index)
143 { return operator[]((unsigned long)index); }
145 cl_SV (const cl_SV&);
146 // Assignment operators.
147 cl_SV& operator= (const cl_SV&);
148 // Private pointer manipulations.
149 cl_SV (cl_heap_SV<T>* p) : BASE ((cl_private_thing)p) {}
150 cl_SV (cl_private_thing p) : BASE (p) {}
152 // Forbid use of default constructor.
155 #define CL_SV(T,BASE) cl_SV<T,BASE>
156 // Define copy constructor.
157 template <class T, class BASE>
158 _CL_DEFINE_COPY_CONSTRUCTOR2(CL_SV(T,BASE),cl_SV,BASE)
159 // Define assignment operator.
160 template <class T, class BASE>
161 CL_DEFINE_ASSIGNMENT_OPERATOR(CL_SV(T,BASE),CL_SV(T,BASE))
164 // The "generic" simple vector type.
166 typedef cl_heap_SV<cl_gcobject> cl_heap_SV_any;
167 typedef cl_SV<cl_gcobject,cl_V_any> cl_SV_any;
169 // Copy a simple vector.
170 extern const cl_SV_any copy (const cl_SV_any&);
175 // Conversions to subtypes without checking:
176 #define The(type) *(const type *) & cl_identity
177 // This inline function is for type checking purposes only.
178 inline const cl_SV_any& cl_identity (const cl_SV_any& x) { return x; }
182 #endif /* _CL_SV_H */