]> www.ginac.de Git - cln.git/blob - src/vector/cl_GV_number.cc
* Add support for OpenBSD.
[cln.git] / src / vector / cl_GV_number.cc
1 // cl_make_heap_GV_number().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_GV_number)
7
8 // Specification.
9 #include "cln/GV_number.h"
10
11
12 // Implementation.
13
14 #include "cln/abort.h"
15 #include "cl_offsetof.h"
16
17 namespace cln {
18
19 static void cl_gvector_number_destructor (cl_heap* pointer)
20 {
21 #if (defined(__mips__) || defined(__mips64__)) && !defined(__GNUC__) // workaround SGI CC bug
22         (*(cl_heap_GV_number*)pointer).~cl_heap_GV();
23 #else
24         (*(cl_heap_GV_number*)pointer).~cl_heap_GV_number();
25 #endif
26 }
27
28 cl_class cl_class_gvector_number = {
29         cl_gvector_number_destructor,
30         0
31 };
32
33
34 static inline cl_heap_GV_number * outcast (cl_GV_inner<cl_number>* vec)
35 {
36         return (cl_heap_GV_number *)((char *) vec - offsetof(cl_heap_GV_number,v));
37 }
38 static inline const cl_heap_GV_number * outcast (const cl_GV_inner<cl_number>* vec)
39 {
40         return (const cl_heap_GV_number *)((const char *) vec - offsetof(cl_heap_GV_number,v));
41 }
42
43
44 // Vectors of numbers.
45
46 struct cl_heap_GV_number_general : public cl_heap_GV_number {
47         cl_number data[1];
48         // Standard allocation disabled.
49         void* operator new (size_t size) { unused size; cl_abort(); return (void*)1; }
50         // Standard deallocation disabled.
51         void operator delete (void* ptr) { unused ptr; cl_abort(); }
52         // No default constructor.
53         cl_heap_GV_number_general ();
54 };
55
56 static const cl_number general_element (const cl_GV_inner<cl_number>* vec, uintL index)
57 {
58         return ((const cl_heap_GV_number_general *) outcast(vec))->data[index];
59 }
60
61 static void general_set_element (cl_GV_inner<cl_number>* vec, uintL index, const cl_number& x)
62 {
63         ((cl_heap_GV_number_general *) outcast(vec))->data[index] = x;
64 }
65
66 static void general_do_delete (cl_GV_inner<cl_number>* vec)
67 {
68         var cl_heap_GV_number_general* hv = (cl_heap_GV_number_general *) outcast(vec);
69         var uintL len = hv->v.length();
70         for (var uintL i = 0; i < len; i++)
71                 hv->data[i].~cl_number();
72 }
73
74 static void general_copy_elements (const cl_GV_inner<cl_number>* srcvec, uintL srcindex, cl_GV_inner<cl_number>* destvec, uintL destindex, uintL count)
75 {
76         if (count > 0) {
77                 var const cl_heap_GV_number_general* srcv =
78                   (const cl_heap_GV_number_general *) outcast(srcvec);
79                 var cl_heap_GV_number_general* destv =
80                   (cl_heap_GV_number_general *) outcast(destvec);
81                 var uintL srclen = srcv->v.length();
82                 var uintL destlen = destv->v.length();
83                 if (!(srcindex <= srcindex+count && srcindex+count <= srclen))
84                         cl_abort();
85                 if (!(destindex <= destindex+count && destindex+count <= destlen))
86                         cl_abort();
87                 do {
88                         destv->data[destindex++] = srcv->data[srcindex++];
89                 } while (--count > 0);
90         }
91 }
92
93 static cl_GV_vectorops<cl_number> general_vectorops = {
94         general_element,
95         general_set_element,
96         general_do_delete,
97         general_copy_elements
98 };
99
100 cl_heap_GV_number* cl_make_heap_GV_number (uintL len)
101 {
102         var cl_heap_GV_number_general* hv = (cl_heap_GV_number_general*) malloc_hook(offsetofa(cl_heap_GV_number_general,data)+sizeof(cl_number)*len);
103         hv->refcount = 1;
104         hv->type = &cl_class_gvector_number;
105         new (&hv->v) cl_GV_inner<cl_number> (len,&general_vectorops);
106         for (var uintL i = 0; i < len; i++)
107                 init1(cl_number, hv->data[i]) ();
108         return hv;
109 }
110
111 // An empty vector.
112 const cl_GV_number cl_null_GV_number = cl_GV_number((uintL)0);
113
114 }  // namespace cln
115
116 CL_PROVIDE_END(cl_GV_number)