-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathniml_struct.c
368 lines (315 loc) · 11.6 KB
/
niml_struct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "niml_private.h"
/*-----------------------------------------------------------*/
/*! Holds the table of all registered structs. */
static Htable *ni_struct_table=NULL ;
/*-----------------------------------------------------------*/
/*! Register a struct by its idcode (if it has one). */
void NI_register_struct( void *ndd )
{
NI_struct *nd = (NI_struct *)ndd ;
void *vp ;
/* can't register without idcode */
if( nd == NULL || nd->idcode == NULL ) return ;
/* 1st time in ==> create hash table */
if( ni_struct_table == NULL )
ni_struct_table = new_Htable( 1031 ) ;
/* see if it already is registered */
vp = findin_Htable( nd->idcode , ni_struct_table ) ;
if( vp != NULL ) return ; /* duplicate entry */
/* OK, add it to the table */
addto_Htable( nd->idcode , nd , ni_struct_table ) ;
return ;
}
/*-----------------------------------------------------------*/
/*! Find a struct by its idcode. */
void * NI_find_struct( char *idcode )
{
void *vp ;
if( idcode == NULL ) return NULL ; /* nothing to do */
vp = findin_Htable( idcode , ni_struct_table ) ;
return vp ;
}
/*-----------------------------------------------------------*/
/*! Remove a struct from the table. */
void NI_unregister_struct( void *ndd )
{
NI_struct *nd = (NI_struct *)ndd ;
if( nd == NULL || nd->idcode == NULL ) return ;
removefrom_Htable( nd->idcode , ni_struct_table ) ;
return ;
}
/*-----------------------------------------------------------*/
/*! Return a copy of the pointer to the struct,
also incrementing its reference counter. */
void * NI_pointto_struct( void *ndd )
{
NI_struct *nd = (NI_struct *)ndd ;
if( nd == NULL ) return NULL ;
nd->nref ++ ;
return (void *)nd ;
}
/*-----------------------------------------------------------*/
/* This macro does the basic stuff necessary to delete a
struct from the hash table and from memory. It is used
at the very end of the NI_free_struct() function.
Type specific code is also needed to delete any memory
used by sub-structs or sub-arrays.
-------------------------------------------------------------*/
#undef DELETE_STRUCT
#define DELETE_STRUCT(nq) \
do{ NI_unregister_struct(nq); \
NI_free(nq->idcode) ; \
NI_free(nq->name) ; \
NI_free(nq) ; } while(0)
/*-----------------------------------------------------------*/
/*! Decrement the reference counter, and destroy the struct
(recursively in some cases) if the counter goes to zero.
-------------------------------------------------------------*/
void NI_free_struct( void *ndd )
{
NI_struct *nd = (NI_struct *)ndd ;
if( nd == NULL ) return ;
/* decrementation */
nd->nref -- ;
if( nd->nref > 0 ) return ; /* keep it */
/* OK, blot it from the universe */
switch( nd->type ){ /* N.B.: there is no default */
case NI_STRUCT_TYPE: /* These types have no sub-structs */
case NI_FLOAT_ONE_TYPE: /* or sub-arrays that need deleting */
case NI_AFFINE_3DMAP_TYPE:
case NI_RECT_DOMAIN_TYPE:
DELETE_STRUCT(nd) ;
break ;
case NI_STATISTIC_TYPE:{
NI_statistic *ns = (NI_statistic *)nd ;
NI_index_t ii ;
if( ns->param != NULL ){
for( ii=0 ; ii < ns->param_num ; ii++ )
NI_free_struct( ns->param[ii] ) ; /* recursion */
NI_free(ns->param) ;
}
}
DELETE_STRUCT(nd) ;
break ;
case NI_VECTOR_TYPE:
case NI_BYTE_VECTOR_TYPE:
case NI_SHORT_VECTOR_TYPE:
case NI_INT_VECTOR_TYPE:
case NI_FLOAT_VECTOR_TYPE:
case NI_DOUBLE_VECTOR_TYPE:
case NI_COMPLEX_VECTOR_TYPE:
case NI_RGB_VECTOR_TYPE:
case NI_RGBA_VECTOR_TYPE:{
NI_vector *nv = (NI_vector *)nd ;
NI_free( nv->vec ) ;
NI_free( nv->vec_range ) ;
NI_free( nv->statistic ) ;
}
DELETE_STRUCT(nd) ;
break ;
case NI_STRING_VECTOR_TYPE:{
NI_string_vector *nv = (NI_string_vector *)nd ;
NI_index_t ii ;
if( nv->vec != NULL ){
for( ii=0 ; ii < nv->vec_len ; ii++ )
NI_free( nv->vec[ii] ) ;
NI_free( nv->vec ) ;
}
/* vec_range not used for string vectors */
/* statistic not used for string vectors */
}
DELETE_STRUCT(nd) ;
break ;
case NI_POINTS_DOMAIN_TYPE:{
NI_points_domain *np = (NI_points_domain *)nd ;
NI_free( np->id ) ;
NI_free( np->x ) ;
NI_free( np->y ) ;
NI_free( np->z ) ;
}
DELETE_STRUCT(nd) ;
break ;
case NI_DATASET_TYPE:{
NI_dataset *nn = (NI_dataset *)nd ;
if( nn->vec != NULL ){
NI_index_t nv , ii ;
nv = NI_dataset_vecnum(nn) ;
for( ii=0 ; ii < nv ; ii++ )
NI_free_struct( nn->vec[ii] ) ; /* recursion */
NI_free( nn->vec ) ;
}
NI_free_struct( nn->domain ) ; /* recursion */
}
DELETE_STRUCT(nd) ;
break ;
}
return ;
}
/*-----------------------------------------------------------*/
/* This macro copies the basic elements of a struct,
from struct qold to struct qnew. Of course, the new
struct gets a new idcode. This macro may be used after
creating a new struct with NI_new(), for example.
-------------------------------------------------------------*/
#undef COPY_BASIC_STRUCT
#define COPY_BASIC_STRUCT(qnew,qold) \
do{ (qnew)->type = (qold)->type ; \
(qnew)->nref = 1 ; \
(qnew)->idcode = UNIQ_idcode() ; \
NI_register_struct( (qnew) ) ; \
(qnew)->name = NI_strdup((qold)->name) ; \
} while(0)
/*-----------------------------------------------------------*/
/* This macro makes a new struct of type TTYPE, copies
the basic elements, and points ndnew to the new struct,
for eventual return from NI_copy_struct(). This macro
is used only in that function.
-------------------------------------------------------------*/
#undef DUPLICATE_STRUCT
#define DUPLICATE_STRUCT(TTYPE) \
TTYPE *nn = NI_new(TTYPE) ; \
TTYPE *qq = (TTYPE *)nd ; \
COPY_BASIC_STRUCT(nn,qq) ; \
ndnew = (NI_struct *)nn
/*-----------------------------------------------------------*/
/*! Make a copy of a struct, as opposed to a new
reference (which is what NI_pointto_struct() does).
-------------------------------------------------------------*/
void * NI_copy_struct( void *ndd )
{
NI_struct *nd = (NI_struct *)ndd ;
NI_struct *ndnew=NULL ;
if( nd == NULL ) return NULL ; /* bad input :-( */
switch( nd->type ){ /* N.B.: there is no default */
case NI_STRUCT_TYPE:{
DUPLICATE_STRUCT(NI_struct) ;
}
break ;
case NI_FLOAT_ONE_TYPE:{
DUPLICATE_STRUCT(NI_float_one) ;
nn->val = qq->val ;
}
break ;
case NI_AFFINE_3DMAP_TYPE:{
DUPLICATE_STRUCT(NI_affine_3dmap) ;
nn->mat[0][0] = qq->mat[0][0]; nn->mat[0][1] = qq->mat[0][1];
nn->mat[0][2] = qq->mat[0][2]; nn->mat[0][3] = qq->mat[0][3];
nn->mat[1][0] = qq->mat[1][0]; nn->mat[1][1] = qq->mat[1][1];
nn->mat[1][2] = qq->mat[1][2]; nn->mat[1][3] = qq->mat[1][3];
nn->mat[2][0] = qq->mat[2][0]; nn->mat[2][1] = qq->mat[2][1];
nn->mat[2][2] = qq->mat[2][2]; nn->mat[2][3] = qq->mat[2][3];
nn->mat[3][0] = qq->mat[3][0]; nn->mat[3][1] = qq->mat[3][1];
nn->mat[3][2] = qq->mat[3][2]; nn->mat[3][3] = qq->mat[3][3];
}
break ;
case NI_RECT_DOMAIN_TYPE:{
DUPLICATE_STRUCT(NI_rect_domain) ;
nn->nx = qq->nx; nn->ny = qq->ny; nn->nz = qq->nz; nn->nt = qq->nt;
nn->dx = qq->dx; nn->dy = qq->dy; nn->dz = qq->dz; nn->dt = qq->dt;
nn->xo = qq->xo; nn->yo = qq->yo; nn->zo = qq->zo; nn->to = qq->to;
}
break ;
case NI_STATISTIC_TYPE:{
NI_index_t ii ;
DUPLICATE_STRUCT(NI_statistic) ;
nn->statcode = qq->statcode ;
nn->param_num = qq->param_num ;
if( qq->param != NULL ){
nn->param = NI_malloc(NI_struct*, sizeof(NI_struct *)*nn->param_num) ;
for( ii=0 ; ii < nn->param_num ; ii++ )
nn->param[ii] = (NI_struct *)NI_copy_struct( qq->param[ii] ) ; /* recursion */
} else {
nn->param = NULL ;
}
}
break ;
case NI_VECTOR_TYPE:
case NI_BYTE_VECTOR_TYPE:
case NI_SHORT_VECTOR_TYPE:
case NI_INT_VECTOR_TYPE:
case NI_FLOAT_VECTOR_TYPE:
case NI_DOUBLE_VECTOR_TYPE:
case NI_COMPLEX_VECTOR_TYPE:
case NI_RGB_VECTOR_TYPE:
case NI_RGBA_VECTOR_TYPE:{
NI_index_t ii ;
DUPLICATE_STRUCT(NI_vector) ;
nn->vec_len = qq->vec_len ;
nn->vec_typ = qq->vec_typ ;
if( qq->vec != NULL ){ /* copy array */
ii = nn->vec_len * NI_datatype_size(nn->vec_typ) ;
nn->vec = NI_malloc(void, ii) ;
memcpy( nn->vec , qq->vec , ii ) ;
} else {
nn->vec = NULL ;
}
if( qq->vec_range != NULL ){ /* copy array */
ii = 2 * NI_datatype_size(nn->vec_typ) ;
nn->vec_range = NI_malloc(void, ii) ;
memcpy( nn->vec_range , qq->vec_range , ii ) ;
} else {
nn->vec_range = NULL ;
}
nn->statistic = (NI_statistic *)NI_copy_struct( qq->statistic ) ; /* recursion */
}
break ;
case NI_STRING_VECTOR_TYPE:{
NI_index_t ii ;
DUPLICATE_STRUCT(NI_string_vector) ;
nn->vec_len = qq->vec_len ;
nn->vec_typ = qq->vec_typ ;
if( qq->vec != NULL ){ /* copy array */
nn->vec = NI_malloc(char*, sizeof(char *)*nn->vec_len) ;
for( ii=0 ; ii < nn->vec_len ; ii++ )
nn->vec[ii] = NI_strdup(qq->vec[ii]) ;
} else {
nn->vec = NULL ;
}
nn->vec_range = NULL ; /* string vectors don't use vec_range */
nn->statistic = NULL ;
}
break ;
case NI_POINTS_DOMAIN_TYPE:{
NI_index_t ii ;
DUPLICATE_STRUCT(NI_points_domain) ;
nn->num_node = ii = qq->num_node ;
if( qq->id != NULL ){ /* copy array */
nn->id = NI_malloc(NI_index_t, ii*sizeof(NI_index_t)) ;
memcpy( nn->id , qq->id , ii*sizeof(NI_index_t) ) ;
}
if( qq->x != NULL ){ /* copy array */
nn->x = NI_malloc(float, ii*sizeof(float)) ;
memcpy( nn->x , qq->x , ii*sizeof(float) ) ;
}
if( qq->y != NULL ){ /* copy array */
nn->y = NI_malloc(float, ii*sizeof(float)) ;
memcpy( nn->y , qq->y , ii*sizeof(float) ) ;
}
if( qq->z != NULL ){ /* copy array */
nn->z = NI_malloc(float, ii*sizeof(float)) ;
memcpy( nn->z , qq->z , ii*sizeof(float) ) ;
}
nn->seq = qq->seq; nn->seqbase = qq->seqbase; nn->sorted = qq->sorted;
}
break ;
case NI_DATASET_TYPE:{
DUPLICATE_STRUCT(NI_dataset) ;
nn->num_node = qq->num_node ;
nn->num_val = qq->num_val ;
nn->order = qq->order ;
if( qq->vec != NULL ){
NI_index_t nv , ii ;
nv = NI_dataset_vecnum(nn) ;
nn->vec = NI_malloc(NI_vector*, sizeof(NI_vector *)*nv) ;
for( ii=0 ; ii < nv ; ii++ )
nn->vec[ii] = (NI_vector *)NI_copy_struct( qq->vec[ii] ) ; /* recursion */
} else {
nn->vec = NULL ;
}
nn->domain = (NI_struct *)NI_copy_struct( qq->domain ) ; /* recursion */
}
break ;
}
return (void *)ndnew ;
}
#undef DUPLICATE_STRUCT