Debian Codegraph @

contrib/ms-sbvar/utilities_dw/include/dw_array.h

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef __TARRAY__
19 #define __TARRAY__
20
21 /******************************** C-style arrays ********************************
22
23 Attempts to implement a C++ class template for multidimensional arrays. The goal
24 is to allow access through the bracket operator (a[i_1][i_2]...[i_n]) but provide
25 mechanisms for creating, destroying, and determining the dimensions of the array.
26
27 In this implementation, an array is a pointer to void. The exact behavior of the
28 implementation is determined by structure TElementSpecification. This, together
29 with the dimension of the array are stored before the first element of the array.
30 The dimension of the array can be obtained with the macro dw_DimA().
31
32 Additionally, mechanisms are provided for copying, initializing, printing, and
33 writing arrays. If the array uses non-standard mechanisms for creating or
34 destroying its elements, the functions dw_CopyArray() and dw_InitializeArray()
35 should not be used.
36
37 =================================================================================
38 Specification of functions avaiable to arrays
39
40 Destructor
41 void Destructor(void*)
42 The destructor is called to destroy each element of the array. It is assumed
43 that each element of the array is in a valid state. If the flag bit
44 dw_ARRAY_POINTER set, the calling syntax from an array is
45
46 Destructor(((void**)a)[i])
47
48 and the destructor should free the passed pointer if it is not null.
49 Otherwise the calling syntax is
50
51 Destructor((void*)(((char*)a) + i*size))
52
53 and the destructor must not free the passed pointer.
54
55 ---------------------------------------------------------------------------------
56
57 Default Constructor
58 void DefaultConstructor(void *)
59 The default constructor is called to initialize each element of the array.
60 The default constructor must not fail in the since that after a call to the
61 default constructor, the element is in a valid state. The calling syntax is
62
63 DefaultConstructor((void*)(((char*)a) + i*size))
64
65 Note that calling syntax is the same for static and pointer arrays. This
66 allows memory to be allocated to pointers.
67
68 ---------------------------------------------------------------------------------
69
70 Pointer Copy Constructor
71 void* PointerCopyConstructor(void*, void*)
72 The pointer copy constructor is called when coping or initializing elements.
73 The calling syntax is
74
75 CopyConstructor(((void**)d)[i],((void**)s)[i])
76
77 The pointer copy constructor returns a pointer to the destination. If the
78 destination was null, an attempt to create it can be made, and if successful
79 a pointer to the newly allocated destination is returned. Upon failure, a
80 null pointer is returned. In the event of failure, the pointer copy
81 constructor should leave the contents of the destination in a valid state.
82 It is assumed that source is in a valid state.
83
84 ---------------------------------------------------------------------------------
85
86 Static Copy Constructor
87 int StaticCopyConstructor(void*, void*)
88 The static copy constructor is called when coping or initializing elements.
89 The calling syntax is
90
91 CopyConstructor((void*)(((char*)d) + i*size),(void*)(((char*)s) + i*size)
92
93 The static copy constrctor should return one upon success and zero upon
94 failure. In the event of failure, the static copy constructor should leave
95 the contents of the destination in a state such that a call to the destructor
96 will behave as expected. The source is assume to be properly initialized.
97
98 ---------------------------------------------------------------------------------
99 Print Routine
100 int Print(FILE*, void*, char*)
101 The print routine prints an element to the file f using the formating
102 information in the character string. If the flag bit dw_ARRAY_POINTER is
103 set, the calling syntax is
104
105 Print(f,((void**)a)[i],format)
106
107 and otherwise is
108
109 Print(f,(void*)(((char*)a) + i*size),format)
110
111 ---------------------------------------------------------------------------------
112
113 Read Routine
114 int Read(FILE*, void*)
115 The read routine reads an element from the file f. If the flag bit
116 dw_ARRAY_POINTER is set, the calling syntax is
117
118 Read(f,((void**)a)[i])
119
120 and otherwise is
121
122 Read(f,(void*)(((char*)a) + i*size))
123
124 ********************************************************************************/
125
126 #include <stdio.h>
127 #include "prcsn.h"
128
129 #ifdef __cplusplus
130 extern "C"
131 {
132 #endif
133
134 //=========================== TElementSpecification ===========================//
135 #define dw_ARRAY_USE_MEMCPY 0x00000001
136 #define dw_ARRAY_POINTER 0x00000002
137 #define dw_ARRAY_ARRAY 0x00000004
138 #define dw_ARRAY_DELETE_SPECS 0x00000008
139
140 typedef struct
141 {
142 int flag;
143
144 int size;
145 int offset;
146
147 void (*destructor)(void*);
148 void (*default_constructor)(void *);
149 void* (*pointer_copy_constructor)(void*, void*);
150 int (*static_copy_constructor)(void*, void*);
151 int (*print_routine)(FILE*, void*, char*);
152 int (*read_routine)(FILE*, void*);
153
154 } TElementSpecification;
155
156 TElementSpecification* CreateArraySpecification_pointer(void (*destructor)(void *));
157
158 extern TElementSpecification dw_IntSpecs;
159 extern TElementSpecification dw_DoubleSpecs;
160 extern TElementSpecification dw_FloatSpecs;
161 extern TElementSpecification dw_CharSpecs;
162 extern TElementSpecification dw_StringSpecs;
163 extern TElementSpecification dw_ArraySpecs;
164 extern TElementSpecification dw_PointerSpecs;
165 //=============================================================================//
166
167 //=== Macros ===
168 #define dw_DimA(a) (((int*)(a))[-1])
169 #define dw_SpecsA(a) (*((TElementSpecification**)(((char*)(a))-(sizeof(TElementSpecification*)+sizeof(int)))))
170 #define dw_IsArrayA(a) (dw_SpecsA(a)->flag & dw_ARRAY_ARRAY)
171
172 //=== Destructor ===//
173 void dw_FreeArray(void* a);
174
175 //=== Constructors ===//
176 void* dw_CreateArray(TElementSpecification *specs, int dim);
177 void* dw_CreateMultidimensionalArray(TElementSpecification *specs, int depth, int *dim);
178 void* dw_CreateMultidimensionalArrayList(TElementSpecification *specs, int depth, ...);
179
180 //=== Routines ===//
181 void* dw_CopyArray(void* d, void* s);
182 int dw_PrintArray(FILE* f, void* a, char* format);
183 int dw_ReadArray(FILE* f, void* a);
184
185 // Array arrays
186 #define dw_CreateArray_array(dim) dw_CreateArray(&dw_ArraySpecs,dim)
187
188 // Pointer arrays
189 #define dw_CreateArray_pointer(dim,destructor) dw_CreateArray(CreateArraySpecification_pointer(destructor),dim)
190 void DefaultPointerConstructor(void*);
191
192 // String arrays
193 #define dw_CreateArray_string(dim) (char**)dw_CreateArray(&dw_StringSpecs,dim)
194 #define dw_CreateMultidimensionalArray_string(depth,dim) dw_CreateMultidimensionalArray(&dw_StringSpecs,depth,dim)
195 void* dw_CreateMultidimensionalArrayList_string(int depth, ...);
196 #define dw_CreateRectangularArray_string(row,col) (char***)dw_CreateMultidimensionalArrayList_string(2,row,col)
197 #define dw_InitializeArray_string(a,x) dw_InitializeArray(a,x)
198
199 // Integer arrays
200 #define dw_CreateArray_int(dim) (int*)dw_CreateArray(&dw_IntSpecs,dim)
201 #define dw_CreateMultidimensionalArray_int(depth,dim) dw_CreateMultidimensionalArray(&dw_IntSpecs,depth,dim)
202 void* dw_CreateMultidimensionalArrayList_int(int depth, ...);
203 #define dw_CreateRectangularArray_int(row,col) (int**)dw_CreateMultidimensionalArrayList_int(2,row,col)
204 int dw_InitializeArray_int(void *a, int x);
205
206 // Double arrays
207 #define dw_CreateArray_double(dim) (double*)dw_CreateArray(&dw_DoubleSpecs,dim)
208 #define dw_CreateMultidimensionalArray_double(depth,dim) dw_CreateMultidimensionalArray(&dw_DoubleSpecs,depth,dim)
209 void* dw_CreateMultidimensionalArrayList_double(int depth, ...);
210 #define dw_CreateRectangularArray_double(row,col) (double**)dw_CreateMultidimensionalArrayList_double(2,row,col)
211 int dw_InitializeArray_double(void *a, double x);
212
213 // Float arrays
214 #define dw_CreateArray_float(dim) (float*)dw_CreateArray(&dw_FloatSpecs,dim)
215 #define dw_CreateMultidimensionalArray_float(depth,dim) dw_CreateMultidimensionalArray(&dw_FloatSpecs,depth,dim)
216 void* dw_CreateMultidimensionalArrayList_float(int depth, ...);
217 #define dw_CreateRectangularArray_float(row,col) (float**)dw_CreateMultidimensionalArrayList_float(2,row,col)
218 int dw_InitializeArray_float(void *a, float x);
219
220 // Floating point arrays (PRECISION)
221 #if PRECISION_SIZE == 4
222 #define dw_CreateArray_floating dw_CreateArray_float
223 #define dw_CreateMultidimensionalArray_floating dw_CreateMultidimensionalArray_float
224 #define dw_CreateMultidimensionalArrayList_floating dw_CreateMultidimensionalArrayList_float
225 #define dw_CreateRectangularArray_floating dw_CreateRectangularArray_float
226 #define dw_InitializeArray_floating dw_InitializeArray_floating
227 #else
228 #define dw_CreateArray_floating dw_CreateArray_double
229 #define dw_CreateMultidimensionalArray_floating dw_CreateMultidimensionalArray_double
230 #define dw_CreateMultidimensionalArrayList_floating dw_CreateMultidimensionalArrayList_double
231 #define dw_CreateRectangularArray_floating dw_CreateRectangularArray_double
232 #define dw_InitializeArray_floating dw_InitializeArray_double
233 #endif
234
235 // Character arrays
236 #define dw_CreateArray_char(dim) (float*)dw_CreateArray(&dw_CharSpecs,dim)
237 #define dw_CreateMultidimensionalArray_char(depth,dim) dw_CreateMultidimensionalArray(&dw_CharSpecs,depth,dim)
238 void* dw_CreateMultidimensionalArrayList_char(int depth, ...);
239 #define dw_CreateRectangularArray_char(row,col) (char**)dw_CreateMultidimensionalArrayList_char(2,row,col)
240 int dw_InitializeArray_char(void *a, char x);
241
242 #ifdef __cplusplus
243 }
244 #endif
245
246 #endif