diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fb06ae6..bd615b74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,15 @@ SET (IS_RELEASE FALSE CACHE BOOL "Indicate if we are building w SET (SAC2C_EXTRA_INC -DHAVE_CONFIG_H -I${PROJECT_BINARY_DIR}/include - -I${PROJECT_SOURCE_DIR}/include) + -I${PROJECT_SOURCE_DIR}/include + -I${PROJECT_SOURCE_DIR} + ) SET (SAC2C_CPP_INC -DHAVE_CONFIG_H -cppI${PROJECT_BINARY_DIR}/include - -cppI${PROJECT_SOURCE_DIR}/include) + -cppI${PROJECT_SOURCE_DIR}/include + -cppI${PROJECT_SOURCE_DIR}) # Check whether sac2c is operational LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-common") diff --git a/src/structures/StringArray.sac b/src/structures/StringArray.sac index 60990f3d..a3680799 100644 --- a/src/structures/StringArray.sac +++ b/src/structures/StringArray.sac @@ -24,38 +24,43 @@ external typedef stringArray; #pragma linkobj "src/StringArray/alloc.o" #pragma freefun "SAC_StringArray_free" #pragma copyfun "SAC_StringArray_copy" + #pragma ctype "array *" external int dim(stringArray s); #pragma linkname "SAC_StringArray_dim" #pragma linkobj "src/StringArray/dim.o" - #pragma refcounting [0,1] - #pragma linksign [1,2] + #pragma linksign [0,1] + #pragma header "src/structures/src/StringArray/StringArray.h" external int[.] shape(stringArray s); #pragma linkname "SAC_StringArray_shape" #pragma linkobj "src/StringArray/shape.o" - #pragma refcounting [0,1] - #pragma linksign [1,2] + #pragma linksign [0,1] + #pragma sacarg [0] + #pragma header "src/structures/src/StringArray/StringArray.h" external stringArray _to_stringArray(int[.] shp, string s); #pragma linkname "SAC_StringArray_genarray" #pragma linkobj "src/StringArray/genarray.o" - #pragma refcounting [0,1,2] - #pragma linksign [1,2,3] + #pragma linksign [0,1,2] + #pragma sacarg [1, 2] + #pragma header "src/structures/src/StringArray/StringArray.h" external string _sel(int[.] idx, stringArray labs); #pragma linkname "SAC_StringArray_sel" #pragma linkobj "src/StringArray/sel.o" #pragma linkobj "src/StringArray/index2offset.o" - #pragma refcounting [0,1,2] - #pragma linksign [1,2,3] + #pragma linksign [0,1,2] + #pragma sacarg [0, 1] + #pragma header "src/structures/src/StringArray/StringArray.h" external stringArray _modarray(stringArray labs, int[.] idx, string str); #pragma linkname "SAC_StringArray_modarray" #pragma linkobj "src/StringArray/modarray.o" #pragma linkobj "src/StringArray/index2offset.o" - #pragma refcounting [0,1,2,3] - #pragma linksign [1,2,3,4] + #pragma linksign [1,1,2,3] + #pragma sacarg [2, 3] + #pragma header "src/structures/src/StringArray/StringArray.h" stringArray modarray(stringArray arr, int[n] idx, stringArray val) { diff --git a/src/structures/src/StringArray/StringArray.h b/src/structures/src/StringArray/StringArray.h index f2310eb0..4b66ca2e 100644 --- a/src/structures/src/StringArray/StringArray.h +++ b/src/structures/src/StringArray/StringArray.h @@ -6,22 +6,32 @@ #include #include -#include "sac.h" +#include - -#define TRACE 0 +/* Keep consistent with src/String/StringC.h */ +typedef char* string; typedef struct ARRAY { - int dim; - int size; - int *shp; - char **data; - SAC_array_descriptor_t *descs; + sac_int dim; + sac_int size; + sac_int *shp; + SACarg **elems; } array; -extern array *SAC_StringArray_alloc( int dim, int size); -extern int SAC_StringArray_index2offset( int dim, int *idx, int *shp); +extern array *SAC_StringArray_alloc(sac_int dim, sac_int size); + +extern sac_int dim(array *s); + +/* This is just a helper function */ +extern sac_int SAC_StringArray_index2offset(SACarg *idx, sac_int *shp); + +extern array *SAC_StringArray_free(array *arr); +extern array *SAC_StringArray_copy(array *arr); + +extern SACarg *shape(array *s); + +extern array *SAC_StringArray_genarray(SACarg *shp, SACarg *s); -extern void SAC_StringArray_free( array *arr); -extern array *SAC_StringArray_copy( array *arr); +extern SACarg *SAC_StringArray_sel(SACarg *idx, array *labs); +extern void SAC_StringArray_modarray(array *labs, SACarg *idx, SACarg *str); diff --git a/src/structures/src/StringArray/alloc.c b/src/structures/src/StringArray/alloc.c index 31856761..37d1bcf6 100644 --- a/src/structures/src/StringArray/alloc.c +++ b/src/structures/src/StringArray/alloc.c @@ -1,20 +1,19 @@ #include #include +#include #include "StringArray.h" -array *SAC_StringArray_alloc( int dim, int size) +array *SAC_StringArray_alloc(sac_int dim, sac_int size) { array *res; - res = (array *)malloc( sizeof( array)); - res->dim = dim; - res->size = size; - res->shp = (int*)malloc( res->dim * sizeof( int)); - res->data = (char**) malloc( res->size * sizeof( char *)); - res->descs = (SAC_array_descriptor_t*) - malloc( res->size * sizeof( SAC_array_descriptor_t *)); + res = (array *)malloc(sizeof(array)); + res->dim = dim; + res->size = size; + res->shp = (sac_int *)malloc(res->dim * sizeof(sac_int)); + res->elems = (SACarg **)malloc(res->size * sizeof(SACarg *)); - return( res); + return res; } diff --git a/src/structures/src/StringArray/copy.c b/src/structures/src/StringArray/copy.c index 034fff54..9ec62f31 100644 --- a/src/structures/src/StringArray/copy.c +++ b/src/structures/src/StringArray/copy.c @@ -3,26 +3,18 @@ #include "StringArray.h" -#define subs_nt (subs, T_OLD((SCL, (HID, (NUQ,))))) - -array *SAC_StringArray_copy( array *arr) +array *SAC_StringArray_copy(array *arr) { - int i; - array *res; - SAC_ND_DECL__DESC( subs_nt, ); - char * SAC_ND_A_FIELD( subs_nt); + array *res = SAC_StringArray_alloc(arr->dim, arr->size); + + for (sac_int l = 0; l < arr->dim; l++) { + (res->shp)[l] = (arr->shp)[l]; + } + + for (sac_int i = 0; i < arr->size; i++) { + (res->elems)[i] = SACARGduplicateSaCArray((arr->elems)[i]); + } - res = SAC_StringArray_alloc( arr->dim, arr->size); - for( i=0; idim; i++) { - res->shp[i] = arr->shp[i]; - } - for( i=0; isize; i++) { - SAC_ND_A_DESC( subs_nt) = arr->descs[i]; - SAC_ND_A_FIELD( subs_nt) = arr->data[i]; - SAC_ND_INC_RC( subs_nt, 1 ); - res->descs[i] = SAC_ND_A_DESC( subs_nt); - res->data[i] = SAC_ND_A_FIELD( subs_nt); - } - return( res); + return res; } diff --git a/src/structures/src/StringArray/dim.c b/src/structures/src/StringArray/dim.c index bc39d48b..3a3abcad 100644 --- a/src/structures/src/StringArray/dim.c +++ b/src/structures/src/StringArray/dim.c @@ -1,18 +1,7 @@ #include "StringArray.h" -#define s_nt (s, T_OLD((SCL, (HID, (NUQ,))))) -#define dim_nt (dim, T_OLD((SCL, (NHD, (NUQ,))))) -#define res_nt (res, T_OLD((SCL, (NHD, (NUQ,))))) - -void SAC_StringArray_dim( SAC_ND_PARAM_out( dim_nt, int), - SAC_ND_PARAM_in( s_nt, array *)) +sac_int SAC_StringArray_dim(array *x) { - SAC_ND_DECL__DESC( res_nt, ); - SAC_ND_DECL__DATA( res_nt, int,); - - SAC_ND_ALLOC__DESC( res_nt, 0); - SAC_ND_CREATE__SCALAR__DATA( res_nt, s->dim); - - SAC_ND_RET_out( dim_nt , res_nt ) + return x->dim; } diff --git a/src/structures/src/StringArray/free.c b/src/structures/src/StringArray/free.c index 19af2bbd..56b8bbeb 100644 --- a/src/structures/src/StringArray/free.c +++ b/src/structures/src/StringArray/free.c @@ -3,22 +3,14 @@ #include "StringArray.h" -#define subs_nt (subs, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_StringArray_free( array *arr) +array *SAC_StringArray_free(array *arr) { - int i; - SAC_ND_DECL__DESC( subs_nt, ); - char * SAC_ND_A_FIELD( subs_nt); - - for( i=0; isize; i++) { - SAC_ND_A_DESC( subs_nt) = arr->descs[i]; - SAC_ND_A_FIELD( subs_nt) = arr->data[i]; - SAC_ND_DEC_RC_FREE( subs_nt, 1 , free ); - } - free( arr->shp); - free( arr->data); - free( arr->descs); - free( arr); + for (sac_int i = 0; i < arr->size; i++) { + SACARGdeleteSacArray(&(arr->elems)[i]); + } + free(arr->shp); + free(arr->elems); + free(arr); + return NULL; } diff --git a/src/structures/src/StringArray/genarray.c b/src/structures/src/StringArray/genarray.c index 877d59f8..0e537e14 100644 --- a/src/structures/src/StringArray/genarray.c +++ b/src/structures/src/StringArray/genarray.c @@ -3,46 +3,23 @@ #include "StringArray.h" -#define resout_nt (resout, T_OLD((SCL, (HID, (NUQ,))))) -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define shp_nt (shp, T_OLD((AKD, (NHD, (NUQ,))))) -#define s_nt (s, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_StringArray_genarray( SAC_ND_PARAM_out( resout_nt, array *), - SAC_ND_PARAM_in( shp_nt, int ), - SAC_ND_PARAM_in( s_nt, char *)) +array *SAC_StringArray_genarray(SACarg *shp, SACarg *s) { - SAC_ND_DECL__DESC( res_nt, ); - array *SAC_ND_A_FIELD( res_nt ); - int dim, size; - int i; - - SAC_ND_ALLOC__DESC( res_nt, 0); - SAC_ND_SET__RC( res_nt, 1); - - dim = SAC_ND_A_DESC_SIZE( shp_nt ); - size = 1; - for( i=0; i < dim; i++) { - size *= SAC_ND_READ( shp_nt, i); - } - - res = SAC_StringArray_alloc( dim, size); - - for( i=0; i < dim; i++) { - res->shp[i] = SAC_ND_READ( shp_nt, i); - } - - for( i=0; i < size ; i++) { - res->data[i] = SAC_ND_A_FIELD( s_nt ); - res->descs[i] = SAC_ND_A_DESC( s_nt ); - SAC_ND_INC_RC( s_nt, 1); - } - - SAC_ND_DEC_RC_FREE( s_nt, 1 , free ); - SAC_ND_DEC_RC_FREE( shp_nt, 1,); - - SAC_ND_RET_out( resout_nt , res_nt ) - + sac_int dim = SACARGgetDim(shp); + sac_int size = 1; + for (sac_int pos = 0; pos < dim; pos++) { + size *= SACARGgetShape(shp, pos); + } + array *res = SAC_StringArray_alloc(dim, size); + for (sac_int pos = 0; pos < dim; pos++) { + res->shp[pos] = SACARGgetShape(shp, pos); + } + + for (sac_int i = 0; i < size; i++) { + res->elems[i] = SACARGduplicateSaCArray(s); + } + + return res; } diff --git a/src/structures/src/StringArray/index2offset.c b/src/structures/src/StringArray/index2offset.c index 4478086d..283efd1b 100644 --- a/src/structures/src/StringArray/index2offset.c +++ b/src/structures/src/StringArray/index2offset.c @@ -3,17 +3,18 @@ #include "StringArray.h" -int SAC_StringArray_index2offset( int dim, int *idx, int *shp) +sac_int SAC_StringArray_index2offset(SACarg *idx_in, sac_int *shp) { - int i, offset, size; + sac_int dim = SACARGgetDim(idx_in); - offset = 0; - size = 1; - for( i=dim - 1; i >= 0; i--) { - offset += size * idx[i]; - size *= shp[i]; - } - - return( offset); + sac_int *idx = (sac_int *)SACARGgetSharedData (SACTYPE__MAIN__int, idx_in); + + sac_int offset = 0; + sac_int size = 1; + for (sac_int i = dim - 1; i >= 0; i--) { + offset += size * idx[i]; + size *= shp[i]; + } + + return offset; } - diff --git a/src/structures/src/StringArray/modarray.c b/src/structures/src/StringArray/modarray.c index 967ce527..099b67c5 100644 --- a/src/structures/src/StringArray/modarray.c +++ b/src/structures/src/StringArray/modarray.c @@ -3,39 +3,10 @@ #include "StringArray.h" -#define arrout_nt (arrout, T_OLD((SCL, (HID, (NUQ,))))) -#define str_nt (str, T_OLD((SCL, (HID, (NUQ,))))) -#define val_nt (val, T_OLD((SCL, (HID, (NUQ,))))) -#define idx_nt (idx, T_OLD((AKD, (NHD, (NUQ,))))) -#define arr_nt (arr, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_StringArray_modarray( SAC_ND_PARAM_out( arrout_nt, array *), - SAC_ND_PARAM_in( arr_nt, array *), - SAC_ND_PARAM_in( idx_nt, int ), - SAC_ND_PARAM_in( val_nt, char * )) +void SAC_StringArray_modarray(array *labs, SACarg *idx, SACarg *str) { - int offset; - SAC_ND_DECL__DESC( str_nt, ); - char *SAC_ND_A_FIELD( str_nt ); - - if( SAC_ND_A_RC( arr_nt) > 1 ) { - SAC_ND_DEC_RC( arr_nt, 1); - SAC_ND_A_FIELD( arr_nt) = SAC_StringArray_copy( SAC_ND_A_FIELD( arr_nt)); - SAC_ND_ALLOC__DESC( arr_nt, 0); - SAC_ND_SET__RC( arr_nt, 1); - } - offset = SAC_StringArray_index2offset( arr->dim, - SAC_ND_A_FIELD(idx_nt), - arr->shp); - - SAC_ND_A_FIELD( str_nt) = arr->data[offset]; - SAC_ND_A_DESC( str_nt) = arr->descs[offset]; - SAC_ND_DEC_RC_FREE( str_nt, 1, free); - arr->data[offset] = SAC_ND_A_FIELD( val_nt); - arr->descs[offset] = SAC_ND_A_DESC( val_nt); - - SAC_ND_DEC_RC_FREE( idx_nt, 1 , ); - - SAC_ND_RET_out( arrout_nt , arr_nt ) + sac_int offset = SAC_StringArray_index2offset(idx, labs->shp); + SACARGdeleteSacArray(&(labs->elems[offset])); + labs->elems[offset] = SACARGduplicateSaCArray(str); } diff --git a/src/structures/src/StringArray/sel.c b/src/structures/src/StringArray/sel.c index 660f46a6..9de5b231 100644 --- a/src/structures/src/StringArray/sel.c +++ b/src/structures/src/StringArray/sel.c @@ -3,34 +3,9 @@ #include "StringArray.h" -#define strout_nt (strout, T_OLD((SCL, (HID, (NUQ,))))) -#define str_nt (str, T_OLD((SCL, (HID, (NUQ,))))) -#define idx_nt (idx, T_OLD((AKD, (NHD, (NUQ,))))) -#define arr_nt (arr, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_StringArray_sel( SAC_ND_PARAM_out( strout_nt, char *), - SAC_ND_PARAM_in( idx_nt, int ), - SAC_ND_PARAM_in( arr_nt, array *)) +SACarg *SAC_StringArray_sel(SACarg *idx, array *labs) { - int offset; - SAC_ND_DECL__DESC( str_nt, ); - char *SAC_ND_A_FIELD( str_nt ); - - offset = SAC_StringArray_index2offset( arr->dim, - SAC_ND_A_FIELD(idx_nt), - arr->shp); - - SAC_ND_A_FIELD( str_nt) = arr->data[offset]; - SAC_ND_A_DESC( str_nt) = arr->descs[offset]; - SAC_ND_INC_RC( str_nt, 1); + sac_int offset = SAC_StringArray_index2offset(idx, labs->shp); - /* DSR: Removing the line below makes StringArray usable for now - * but we need to investigate why this works. - * See bug 616 for details. - */ - SAC_ND_DEC_RC_FREE( idx_nt, 1 , ); - SAC_ND_DEC_RC_FREE( arr_nt, 1 , SAC_StringArray_free ); - - SAC_ND_RET_out( strout_nt , str_nt ) + return SACARGduplicateSaCArray(labs->elems[offset]); } - diff --git a/src/structures/src/StringArray/shape.c b/src/structures/src/StringArray/shape.c index 48496aa6..d41f964b 100644 --- a/src/structures/src/StringArray/shape.c +++ b/src/structures/src/StringArray/shape.c @@ -1,31 +1,15 @@ #include "StringArray.h" -#define s_nt (s, T_OLD((SCL, (HID, (NUQ,))))) -#define shp_nt (shp, T_OLD((AKD, (NHD, (NUQ,))))) -#define res_nt (res, T_OLD((AKD, (NHD, (NUQ,))))) - -void SAC_StringArray_shape( SAC_ND_PARAM_out( shp_nt, int), - SAC_ND_PARAM_in( s_nt, array *)) +SACarg *SAC_StringArray_shape(array *x) { - int i; - int SAC_ND_A_MIRROR_DIM( res_nt) = 1; - SAC_ND_DECL__DESC( res_nt, ); - SAC_ND_DECL__DATA( res_nt, int, ); - - SAC_ND_ALLOC__DESC( res_nt, 1); + sac_int d = x->dim; + sac_int *shp = malloc(d * sizeof(sac_int)); - SAC_ND_A_DESC_SHAPE( res_nt, 0) = s->dim; + for (sac_int l = 0; l < d; l++) { + shp[l] = x->shp[l]; + } - SAC_ND_SET__RC( res_nt, 1); - SAC_ND_A_DESC_SIZE( res_nt) = SAC_ND_A_DESC_SHAPE( res_nt, 0); - SAC_ND_A_FIELD( res_nt) = SAC_MALLOC( s->dim * sizeof( int)); - for( i=0; idim; i++) { - SAC_ND_A_FIELD( res_nt)[i] = s->shp[i]; - } + sac_int n[1] = {d}; - SAC_ND_RET_out( shp_nt , res_nt ) + return SACARGcreateFromPointer(SACTYPE__MAIN__int, shp, d, n); } - -#undef s_nt -#undef shp_nt -#undef res_nt