void array_boolean_array(boolean_array_t* dest,int n,boolean_array_t first,...) { int i,j,c; va_list ap; boolean_array_t *elts=(boolean_array_t*)malloc(sizeof(boolean_array_t) * n); assert(elts); /* collect all array ptrs to simplify traversal.*/ va_start(ap,first); elts[0] = first; for(i = 1; i < n; ++i) { elts[i] = va_arg(ap, boolean_array_t); } va_end(ap); check_base_array_dim_sizes(elts,n); for(i = 0, c = 0; i < n; ++i) { int m = base_array_nr_of_elements(elts[i]); for(j = 0; j < m; ++j) { boolean_set(dest, c, boolean_get(elts[i], j)); c++; } } free(elts); }
m_boolean scalar_boolean_array(const boolean_array_t* a) { assert(base_array_ok(a)); assert(base_array_one_element_ok(a)); return boolean_get(*a, 0); }
/* function: transpose_boolean_array * * Implementation of transpose(A) for matrix A. */ void transpose_boolean_array(const boolean_array_t* a, boolean_array_t* dest) { size_t i; size_t j; /* size_t k;*/ size_t n,m; if(a->ndims == 1) { copy_boolean_array_data(*a,dest); return; } assert(a->ndims==2 && dest->ndims==2); n = a->dim_size[0]; m = a->dim_size[1]; assert(dest->dim_size[0] == m && dest->dim_size[1] == n); for(i = 0; i < n; ++i) { for(j = 0; j < m; ++j) { boolean_set(dest, (j*n)+i, boolean_get(*a, (i*m)+j)); } } }
/* Zero based index */ void simple_indexed_assign_boolean_array1(const boolean_array_t* source, int i1, boolean_array_t* dest) { /* Assert that source has the correct dimension */ /* Assert that dest has the correct dimension */ boolean_set(dest, i1, boolean_get(*source, i1)); }
int main() { object_t *object, *copy; plan(10); object = hash( "string", string("foo"), "real", real(4.2), "integer", integer(42), "boolean", boolean(true), "array", array( integer(1), integer(2), integer(3) ) ); copy = object_copy(object); hash_set(object, "string", string("bar")); ok(!strcmp("bar", string_to_c_str(hash_get(object, "string"))), "object[\"string\"] is \"bar\""); ok(!strcmp("foo", string_to_c_str(hash_get(copy, "string"))), "copy[\"string\"] is \"foo\""); hash_set(object, "real", real(3.4)); ok(real_get(hash_get(object, "real")) == 3.4, "object[\"real\"] == 3.4"); ok(real_get(hash_get(copy, "real")) == 4.2, "copy[\"real\"] == 4.2"); hash_set(object, "integer", integer(34)); ok(integer_get(hash_get(object, "integer")) == 34, "object[\"integer\"] == 34"); ok(integer_get(hash_get(copy, "integer")) == 42, "copy[\"integer\"] == 42"); hash_set(object, "boolean", boolean(false)); ok(boolean_get(hash_get(object, "boolean")) == false, "object[\"boolean\"] is false"); ok(boolean_get(hash_get(copy, "boolean")) == true, "copy[\"boolean\"] is true"); array_splice(hash_get(object, "array"), 0, 3, NULL); ok(array_size(hash_get(object, "array")) == 0, "object[\"array\"] contains 0 elements"); ok(array_size(hash_get(copy, "array")) == 3, "copy[\"array\"] contains 3 elements"); object_free(copy); object_free(object); return 0; }
void simple_indexed_assign_boolean_array2(const boolean_array_t* source, int i1, int i2, boolean_array_t* dest) { size_t index; /* Assert that source has correct dimension */ /* Assert that dest has correct dimension */ index = (i1 * source->dim_size[1]) + i2; boolean_set(dest, index, boolean_get(*source, index)); }
void index_boolean_array(const boolean_array_t* source, const index_spec_t* source_spec, boolean_array_t* dest) { _index_t* idx_vec1; _index_t* idx_vec2; _index_t* idx_size; int j; int i; assert(base_array_ok(source)); assert(base_array_ok(dest)); assert(index_spec_ok(source_spec)); assert(index_spec_fit_base_array(source_spec,source)); for(i = 0, j = 0; i < source->ndims; ++i) { if((source_spec->index_type[i] == 'W') || (source_spec->index_type[i] == 'A')) { ++j; } } assert(j == dest->ndims); idx_vec1 = size_alloc(source->ndims); /*indices in the source array*/ idx_vec2 = size_alloc(dest->ndims); /* indices in the destination array*/ idx_size = size_alloc(source_spec->ndims); for(i = 0; i < source->ndims; ++i) { idx_vec1[i] = 0; } for(i = 0; i < source_spec->ndims; ++i) { if(source_spec->index[i]) { idx_size[i] = imax(source_spec->dim_size[i],1); } else { idx_size[i] = source->dim_size[i]; } } do { for(i = 0, j = 0; i < source->ndims; ++i) { if((source_spec->index_type[i] == 'W') || (source_spec->index_type[i] == 'A')) { idx_vec2[j] = idx_vec1[i]; j++; } } boolean_set(dest, calc_base_index(dest->ndims, idx_vec2, dest), boolean_get(*source, calc_base_index_spec(source->ndims, idx_vec1, source, source_spec))); } while(0 == next_index(source->ndims, idx_vec1, idx_size)); }
/* function: cat_boolean_array * * Concatenates n boolean arrays along the k:th dimension. * k is one based */ void cat_boolean_array(int k, boolean_array_t* dest, int n, const boolean_array_t* first,...) { va_list ap; int i, j, r, c; int n_sub = 1, n_super = 1; int new_k_dim_size = 0; const boolean_array_t **elts = (const boolean_array_t**)malloc(sizeof(boolean_array_t *) * n); assert(elts); /* collect all array ptrs to simplify traversal.*/ va_start(ap, first); elts[0] = first; for(i = 1; i < n; i++) { elts[i] = va_arg(ap,const boolean_array_t*); } va_end(ap); /* check dim sizes of all inputs and dest */ assert(elts[0]->ndims >= k); for(i = 0; i < n; i++) { assert(dest->ndims == elts[i]->ndims); for(j = 0; j < (k - 1); j++) { assert(dest->dim_size[j] == elts[i]->dim_size[j]); } new_k_dim_size += elts[i]->dim_size[k-1]; for(j = k; j < elts[0]->ndims; j++) { assert(dest->dim_size[j] == elts[i]->dim_size[j]); } } assert(dest->dim_size[k-1] == new_k_dim_size); /* calculate size of sub and super structure in 1-dim data representation */ for(i = 0; i < (k - 1); i++) { n_super *= elts[0]->dim_size[i]; } for(i = k; i < elts[0]->ndims; i++) { n_sub *= elts[0]->dim_size[i]; } /* concatenation along k-th dimension */ j = 0; for(i = 0; i < n_super; i++) { for(c = 0; c < n; c++) { int n_sub_k = n_sub * elts[c]->dim_size[k-1]; for(r = 0; r < n_sub_k; r++) { boolean_set(dest, j, boolean_get(*elts[c], r + (i * n_sub_k))); j++; } } } free(elts); }
void or_boolean_array(const boolean_array_t *source1, const boolean_array_t *source2, boolean_array_t *dest) { size_t i, nr_of_elements; assert(base_array_ok(source1)); assert(base_array_ok(source2)); assert(base_array_shape_eq(source1, source2)); clone_base_array_spec(source1, dest); // assert(base_array_ok(dest)); // assert(base_array_shape_eq(source1, dest)); alloc_boolean_array_data(dest); nr_of_elements = base_array_nr_of_elements(*source1); for(i = 0; i < nr_of_elements; ++i) { boolean_set(dest, i, boolean_get(*source1, i) || boolean_get(*source2, i)); } }
void vector_boolean_array(const boolean_array_t* a, boolean_array_t* dest) { size_t i, nr_of_elements; /* Assert that a has at most one dimension with dim_size>1*/ nr_of_elements = base_array_nr_of_elements(*a); for(i = 0; i < nr_of_elements; ++i) { boolean_set(dest, i, boolean_get(*a, i)); } }
void simple_index_boolean_array2(const boolean_array_t* source, int i1, int i2, boolean_array_t* dest) { size_t i; size_t nr_of_elements = base_array_nr_of_elements(*dest); size_t off = nr_of_elements * ((source->dim_size[1] * i1) + i2); for(i = 0 ; i < nr_of_elements ; i++) { boolean_set(dest, i, boolean_get(*source, off + i)); } }
void copy_boolean_array_data_mem(const boolean_array_t source, modelica_boolean *dest) { size_t i, nr_of_elements; assert(base_array_ok(&source)); nr_of_elements = base_array_nr_of_elements(source); for(i = 0; i < nr_of_elements; ++i) { dest[i] = boolean_get(source, i); } }
void simple_index_boolean_array1(const boolean_array_t* source, int i1, boolean_array_t* dest) { size_t i; size_t nr_of_elements = base_array_nr_of_elements(*dest); size_t off = nr_of_elements * i1; assert(dest->ndims == (source->ndims - 1)); for(i = 0 ; i < nr_of_elements ; i++) { boolean_set(dest, i, boolean_get(*source, off + i)); } }
void matrix_boolean_array(const boolean_array_t* a, boolean_array_t* dest) { size_t i, cnt; /* Assert that size(A,i)=1 for 2 <i<=ndims(A)*/ dest->dim_size[0] = a->dim_size[0]; dest->dim_size[1] = (a->ndims < 2)? 1 : a->dim_size[1]; cnt = dest->dim_size[0] * dest->dim_size[1]; for(i = 0; i < cnt; ++i) { boolean_set(dest, i, boolean_get(*a, i)); } }
void copy_boolean_array_data(const boolean_array_t source, boolean_array_t *dest) { size_t i, nr_of_elements; assert(base_array_ok(&source)); assert(base_array_ok(dest)); assert(base_array_shape_eq(&source, dest)); nr_of_elements = base_array_nr_of_elements(source); for(i = 0; i < nr_of_elements; ++i) { boolean_set(dest, i, boolean_get(source, i)); } }
void not_boolean_array(const boolean_array_t source, boolean_array_t *dest) { size_t i, nr_of_elements; assert(base_array_ok(&source)); clone_base_array_spec(&source, dest); alloc_boolean_array_data(dest); nr_of_elements = base_array_nr_of_elements(source); for(i = 0; i < nr_of_elements; ++i) { boolean_set(dest, i, !boolean_get(source, i)); } }
void print_boolean_matrix(const boolean_array_t *source) { _index_t i,j; modelica_boolean value; if(source->ndims == 2) { printf("%d X %d matrix:\n", (int) source->dim_size[0], (int) source->dim_size[1]); for(i = 0; i < source->dim_size[0]; ++i) { for(j = 0; j < source->dim_size[1]; ++j) { value = boolean_get(*source, (i * source->dim_size[1]) + j); printf("%c\t", value ? 'T' : 'F'); } printf("\n"); } } else { printf("array with %d dimensions\n", source->ndims); } }
void indexed_assign_boolean_array(const boolean_array_t source, boolean_array_t* dest, const index_spec_t* dest_spec) { _index_t *idx_vec1, *idx_size; int j; indexed_assign_base_array_size_alloc(&source, dest, dest_spec, &idx_vec1, &idx_size); j = 0; do { boolean_set(dest, calc_base_index_spec(dest->ndims, idx_vec1, dest, dest_spec), boolean_get(source, j)); j++; } while(0 == next_index(dest_spec->ndims, idx_vec1, idx_size)); omc_assert_macro(j == base_array_nr_of_elements(source)); }
void array_alloc_boolean_array(boolean_array_t* dest, int n, boolean_array_t first,...) { int i,j,c; va_list ap; boolean_array_t *elts = (boolean_array_t*)malloc(sizeof(boolean_array_t) * n); assert(elts); /* collect all array ptrs to simplify traversal.*/ va_start(ap,first); elts[0] = first; for(i = 1; i < n; ++i) { elts[i] = va_arg(ap, boolean_array_t); } va_end(ap); check_base_array_dim_sizes(elts,n); if(first.ndims == 1) { alloc_boolean_array(dest, 2, n, first.dim_size[0]); } else if(first.ndims == 2) { alloc_boolean_array(dest, 3, n, first.dim_size[0], first.dim_size[1]); } else if(first.ndims == 3) { alloc_boolean_array(dest, 4, n, first.dim_size[0],first.dim_size[1], first.dim_size[2]); } else if(first.ndims == 4) { alloc_boolean_array(dest, 5, n, first.dim_size[0], first.dim_size[1], first.dim_size[2], first.dim_size[3]); } else { assert(0 && "Dimension size > 4 not impl. yet"); } for(i = 0, c = 0; i < n; ++i) { int m = base_array_nr_of_elements(elts[i]); for(j = 0; j < m; ++j) { boolean_set(dest, c, boolean_get(elts[i], j)); c++; } } free(elts); }