6 #include "cln/object.h"
8 #include "cln/exception.h"
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.
23 // When the vectors were implemented like this:
25 // cl_GV<cl_I> --> cl_GV<cl_RA> --> cl_GV<cl_R> --> cl_GV<cl_N>
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
31 // cl_SV<cl_I> --> cl_SV<cl_RA> --> cl_SV<cl_R> --> cl_SV<cl_N>
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.)
39 template <class T> class cl_SV_inner;
44 std::size_t len; // number of elements
46 // T data[]; // the elements
47 T * data() { return (T *) (this+1); }
48 const T * data() const { return (const T *) (this+1); }
50 std::size_t size() const { return len; } // number of elements
51 const T & operator[] (unsigned long index) const
53 #ifndef CL_SV_NO_RANGECHECKS
54 if (!(index < size())) throw runtime_exception();
58 T & operator[] (unsigned long index)
60 #ifndef CL_SV_NO_RANGECHECKS
61 if (!(index < size())) throw runtime_exception();
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); }
80 cl_SV_inner (std::size_t l) : len (l) {}
84 // Ability to place an object at a given address.
85 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
87 // No default constructor, copy constructor, assignment operator, new.
89 cl_SV_inner (const cl_SV_inner&);
90 cl_SV_inner& operator= (const cl_SV_inner&);
91 void* operator new (size_t size);
94 // All member functions are inline.
97 inline cl_SV_inner<T>::~cl_SV_inner ()
107 // In memory, a simple vector looks like this:
110 struct cl_heap_SV : cl_heap {
112 // here room for the elements
115 template <class T, class BASE>
116 struct cl_SV : public BASE {
119 std::size_t size() const
121 return ((const cl_heap_SV<T> *) this->pointer)->v.size();
123 // Reference. Forbid modification of `const cl_SV&' arguments.
124 const T & operator[] (unsigned long index) const
126 return ((const cl_heap_SV<T> *) this->pointer)->v[index];
128 T & operator[] (unsigned long index)
130 return ((cl_heap_SV<T> *) this->pointer)->v[index];
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); }
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) {}
153 // Forbid use of default constructor.
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))
165 // The "generic" simple vector type.
167 typedef cl_heap_SV<cl_gcobject> cl_heap_SV_any;
168 typedef cl_SV<cl_gcobject,cl_V_any> cl_SV_any;
170 // Copy a simple vector.
171 extern const cl_SV_any copy (const cl_SV_any&);
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; }
183 #endif /* _CL_SV_H */