Skip to content

Commit

Permalink
Handle indexed assignment of string arrays
Browse files Browse the repository at this point in the history
Synchronize the indexed assignment function for string arrays to match
the other types. Also refactored the code shared by indexed assignment
and allocation functions. This fixes ticket:4705.

Belonging to [master]:
  - OpenModelica/OMCompiler#2102
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Jan 11, 2018
1 parent fe5c35d commit cbe247e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 238 deletions.
64 changes: 64 additions & 0 deletions SimulationRuntime/c/util/base_array.c
Expand Up @@ -375,3 +375,67 @@ void clone_reverse_base_array_spec(const base_array_t* source, base_array_t* des
dest->dim_size[i] = source->dim_size[dest->ndims - 1 - i];
}
}

void index_alloc_base_array_size(const real_array_t * source,
const index_spec_t* source_spec,
base_array_t* dest)
{
int i;
int j;

omc_assert_macro(base_array_ok(source));
omc_assert_macro(index_spec_ok(source_spec));
omc_assert_macro(index_spec_fit_base_array(source_spec, source));

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
++j;
}
}
dest->ndims = j;
dest->dim_size = size_alloc(dest->ndims);

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
if(source_spec->index[i] != NULL) { /* is 'A' */
dest->dim_size[j] = source_spec->dim_size[i];
} else { /* is 'W' */
dest->dim_size[j] = source->dim_size[i];
}

++j;
}
}
}

void indexed_assign_base_array_size_alloc(const base_array_t *source, base_array_t *dest, const index_spec_t *dest_spec, _index_t** _idx_vec1, _index_t** _idx_size)
{
_index_t* idx_vec1;
_index_t* idx_size;
int i, j;
omc_assert_macro(base_array_ok(source));
omc_assert_macro(base_array_ok(dest));
omc_assert_macro(index_spec_ok(dest_spec));
omc_assert_macro(index_spec_fit_base_array(dest_spec, dest));
for(i = 0,j = 0; i < dest_spec->ndims; ++i) {
if(dest_spec->dim_size[i] != 0) {
++j;
}
}
omc_assert_macro(j == source->ndims);

idx_vec1 = size_alloc(dest->ndims);
idx_size = size_alloc(dest_spec->ndims);

for(i = 0; i < dest_spec->ndims; ++i) {
idx_vec1[i] = 0;

if(dest_spec->index[i] != NULL) { /* is 'S' or 'A' */
idx_size[i] = imax(dest_spec->dim_size[i],1);
} else { /* is 'W' */
idx_size[i] = dest->dim_size[i];
}
}
*_idx_vec1 = idx_vec1;
*_idx_size = idx_size;
}
5 changes: 5 additions & 0 deletions SimulationRuntime/c/util/base_array.h
Expand Up @@ -104,4 +104,9 @@ size_t calc_base_index_dims_subs(int ndims,...);

int index_spec_fit_base_array(const index_spec_t *s, const base_array_t *a);

/* Helper function for index_alloc_TYPE_array; allocates the ndims and dim_size */
void index_alloc_base_array_size(const base_array_t * source, const index_spec_t* source_spec, base_array_t* dest);
/* Helper function for indexed_assign_TYPE_array; allocates the ndims and dim_size */
void indexed_assign_base_array_size_alloc(const base_array_t *source, base_array_t *dest, const index_spec_t *dest_spec, _index_t** _idx_vec1, _index_t** _idx_size);

#endif /* BASE_ARRAY_H_ */
65 changes: 8 additions & 57 deletions SimulationRuntime/c/util/boolean_array.c
Expand Up @@ -31,6 +31,7 @@

#include "boolean_array.h"
#include "gc/omc_gc.h"
#include "omc_error.h"

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -306,44 +307,20 @@ void simple_indexed_assign_boolean_array2(const boolean_array_t* source,
void indexed_assign_boolean_array(const boolean_array_t source, boolean_array_t* dest,
const index_spec_t* dest_spec)
{
_index_t* idx_vec1;
_index_t* idx_size;
int i,j;

assert(base_array_ok(&source));
assert(base_array_ok(dest));
assert(index_spec_ok(dest_spec));
assert(index_spec_fit_base_array(dest_spec, dest));
for(i = 0,j = 0; i < dest_spec->ndims; ++i) {
if(dest_spec->dim_size[i] != 0) {
++j;
}
}
assert(j == source.ndims);

idx_vec1 = size_alloc(dest->ndims);
idx_size = size_alloc(dest_spec->ndims);

for(i = 0; i < dest_spec->ndims; ++i) {
idx_vec1[i] = 0;

if(dest_spec->index[i] != NULL) { /* is 'S' or 'A' */
idx_size[i] = imax(dest_spec->dim_size[i],1);
} else { /* is 'W' */
idx_size[i] = dest->dim_size[i];
}
}
_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));
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));

assert(j == base_array_nr_of_elements(source));
omc_assert_macro(j == base_array_nr_of_elements(source));
}

/*
Expand Down Expand Up @@ -428,33 +405,7 @@ void index_alloc_boolean_array(const boolean_array_t* source,
const index_spec_t* source_spec,
boolean_array_t* dest)
{
int i;
int j;

assert(base_array_ok(source));
assert(index_spec_ok(source_spec));
assert(index_spec_fit_base_array(source_spec, source));

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
++j;
}
}
dest->ndims = j;
dest->dim_size = size_alloc(dest->ndims);

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
if(source_spec->index[i] != NULL) { /* is 'A' */
dest->dim_size[j] = source_spec->dim_size[i];
} else { /* is 'W' */
dest->dim_size[j] = source->dim_size[i];
}

++j;
}
}

index_alloc_base_array_size(source, source_spec, dest);
alloc_boolean_array_data(dest);
index_boolean_array(source, source_spec, dest);
}
Expand Down
59 changes: 4 additions & 55 deletions SimulationRuntime/c/util/integer_array.c
Expand Up @@ -302,33 +302,9 @@ void simple_indexed_assign_integer_array2(const integer_array_t * source,
void indexed_assign_integer_array(const integer_array_t source, integer_array_t* dest,
const index_spec_t* dest_spec)
{
_index_t* idx_vec1;
_index_t* idx_size;
int i,j;

omc_assert_macro(base_array_ok(&source));
omc_assert_macro(base_array_ok(dest));
omc_assert_macro(index_spec_ok(dest_spec));
omc_assert_macro(index_spec_fit_base_array(dest_spec, dest));
for(i = 0,j = 0; i < dest_spec->ndims; ++i) {
if(dest_spec->dim_size[i] != 0) {
++j;
}
}
omc_assert_macro(j == source.ndims);

idx_vec1 = size_alloc(dest->ndims);
idx_size = size_alloc(dest_spec->ndims);

for(i = 0; i < dest_spec->ndims; ++i) {
idx_vec1[i] = 0;

if(dest_spec->index[i] != NULL) { /* is 'S' or 'A' */
idx_size[i] = imax(dest_spec->dim_size[i],1);
} else { /* is 'W' */
idx_size[i] = dest->dim_size[i];
}
}
_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 {
Expand Down Expand Up @@ -424,34 +400,7 @@ void index_alloc_integer_array(const integer_array_t * source,
const index_spec_t* source_spec,
integer_array_t* dest)
{
int i;
int j;

omc_assert_macro(base_array_ok(source));
omc_assert_macro(index_spec_ok(source_spec));
omc_assert_macro(index_spec_fit_base_array(source_spec,source));


for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
++j;
}
}
dest->ndims = j;
dest->dim_size = size_alloc(dest->ndims);

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
if(source_spec->index[i] != NULL) { /* is 'A' */
dest->dim_size[j] = source_spec->dim_size[i];
} else { /* is 'W' */
dest->dim_size[j] = source->dim_size[i];
}

++j;
}
}

index_alloc_base_array_size(source, source_spec, dest);
alloc_integer_array_data(dest);
index_integer_array(source, source_spec, dest);
}
Expand Down
58 changes: 4 additions & 54 deletions SimulationRuntime/c/util/real_array.c
Expand Up @@ -279,33 +279,9 @@ void simple_indexed_assign_real_array2(const real_array_t * source,
void indexed_assign_real_array(const real_array_t source, real_array_t* dest,
const index_spec_t* dest_spec)
{
_index_t* idx_vec1;
_index_t* idx_size;
int i,j;

omc_assert_macro(base_array_ok(&source));
omc_assert_macro(base_array_ok(dest));
omc_assert_macro(index_spec_ok(dest_spec));
omc_assert_macro(index_spec_fit_base_array(dest_spec, dest));
for(i = 0,j = 0; i < dest_spec->ndims; ++i) {
if(dest_spec->dim_size[i] != 0) {
++j;
}
}
omc_assert_macro(j == source.ndims);

idx_vec1 = size_alloc(dest->ndims);
idx_size = size_alloc(dest_spec->ndims);

for(i = 0; i < dest_spec->ndims; ++i) {
idx_vec1[i] = 0;

if(dest_spec->index[i] != NULL) { /* is 'S' or 'A' */
idx_size[i] = imax(dest_spec->dim_size[i],1);
} else { /* is 'W' */
idx_size[i] = dest->dim_size[i];
}
}
_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 {
Expand Down Expand Up @@ -413,33 +389,7 @@ void index_alloc_real_array(const real_array_t * source,
const index_spec_t* source_spec,
real_array_t* dest)
{
int i;
int j;

omc_assert_macro(base_array_ok(source));
omc_assert_macro(index_spec_ok(source_spec));
omc_assert_macro(index_spec_fit_base_array(source_spec, source));

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
++j;
}
}
dest->ndims = j;
dest->dim_size = size_alloc(dest->ndims);

for(i = 0, j = 0; i < source_spec->ndims; ++i) {
if(source_spec->dim_size[i] != 0) { /* is 'W' or 'A' */
if(source_spec->index[i] != NULL) { /* is 'A' */
dest->dim_size[j] = source_spec->dim_size[i];
} else { /* is 'W' */
dest->dim_size[j] = source->dim_size[i];
}

++j;
}
}

index_alloc_base_array_size(source, source_spec, dest);
alloc_real_array_data(dest);
index_real_array(source, source_spec, dest);
}
Expand Down

0 comments on commit cbe247e

Please sign in to comment.