OMCompiler/SimulationRuntime/c/util/index_spec.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | |||
| 29 | #include "index_spec.h" | ||
| 30 | #include "../gc/omc_gc.h" | ||
| 31 | #include "../gc/omc_rc.h" | ||
| 32 | #include <stdio.h> | ||
| 33 | #include <stdlib.h> | ||
| 34 | #include <stdarg.h> | ||
| 35 | |||
| 36 | 4844673 | int index_spec_ok(const index_spec_t* s) | |
| 37 | { | ||
| 38 | int i; | ||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4844673 times.
|
4844673 | if(s == NULL) { |
| 40 | ✗ | fprintf(stderr,"index_spec_ok: the index spec is NULL!\n"); fflush(stderr); | |
| 41 | ✗ | return 0; | |
| 42 | } | ||
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4844673 times.
|
4844673 | if(s->ndims < 0) { |
| 44 | ✗ | fprintf(stderr,"index_spec_ok: the index spec dimensions are negative: %d!\n", (int)s->ndims); fflush(stderr); | |
| 45 | ✗ | return 0; | |
| 46 | } | ||
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4844673 times.
|
4844673 | if(s->dim_size == NULL) { |
| 48 | ✗ | fprintf(stderr,"index_spec_ok: the index spec dimensions sizes is NULL!\n"); fflush(stderr); | |
| 49 | ✗ | return 0; | |
| 50 | } | ||
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4844673 times.
|
4844673 | if(s->index == NULL) { |
| 52 | ✗ | fprintf(stderr,"index_spec_ok: the index spec index array is NULL!\n"); fflush(stderr); | |
| 53 | ✗ | return 0; | |
| 54 | } | ||
| 55 |
2/2✓ Branch 0 taken 5409650 times.
✓ Branch 1 taken 4844673 times.
|
10254323 | for(i = 0; i < s->ndims; ++i) { |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5409650 times.
|
5409650 | if(s->dim_size[i] < 0) { |
| 57 | ✗ | fprintf(stderr,"index_spec_ok: the index spec dimension size for dimension %d is negative: %d!\n", i, (int)s->dim_size[i]); fflush(stderr); | |
| 58 | ✗ | return 0; | |
| 59 | } | ||
| 60 |
3/4✓ Branch 0 taken 4222501 times.
✓ Branch 1 taken 1187149 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4222501 times.
|
5409650 | if((s->index[i] == 0) && (s->dim_size[i] != 1 && s->dim_size[i] != 0)) { |
| 61 | ✗ | fprintf(stderr,"index_spec_ok: index[%d] == 0, size == %d\n", i, (unsigned int) s->dim_size[i]); fflush(stderr); | |
| 62 | ✗ | return 0; | |
| 63 | } | ||
| 64 | } | ||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | ✗ | void alloc_index_spec(index_spec_t* s) | |
| 69 | { | ||
| 70 | int i; | ||
| 71 | ✗ | s->index = index_alloc(s->ndims); | |
| 72 | ✗ | for(i = 0; i < s->ndims; ++i) { | |
| 73 | ✗ | if(s->dim_size[i] > 0) { | |
| 74 | ✗ | s->index[i] = size_alloc(s->dim_size[i]); | |
| 75 | } else { | ||
| 76 | ✗ | s->index[i] = 0; | |
| 77 | } | ||
| 78 | } | ||
| 79 | ✗ | } | |
| 80 | /* | ||
| 81 | * create_index_spec | ||
| 82 | * | ||
| 83 | * Creates a subscript, i.e. index_spec_t from the arguments. | ||
| 84 | * nridx - number of indexes. | ||
| 85 | * Each index consist of a size and a pointer to indixes. | ||
| 86 | * | ||
| 87 | * For instance to create the indexes in a[1,{2,3}] you | ||
| 88 | * write: | ||
| 89 | * int tmp1[1]={1}; int tmp2[2]={2,3}; | ||
| 90 | * create_index_spec(&dest,2,1,&tmp1,2,&tmp2); | ||
| 91 | */ | ||
| 92 | |||
| 93 | 149700 | void create_index_spec(index_spec_t* dest, int nridx, ...) | |
| 94 | { | ||
| 95 | int i; | ||
| 96 | va_list ap; | ||
| 97 | 149700 | va_start(ap,nridx); | |
| 98 | |||
| 99 | 149700 | dest->ndims = nridx; | |
| 100 | 149700 | dest->dim_size = size_alloc(nridx); | |
| 101 | 149700 | dest->index = index_alloc(nridx); | |
| 102 | 149700 | dest->index_type = (char*)generic_alloc(nridx+1,sizeof(char)); | |
| 103 |
2/2✓ Branch 0 taken 231399 times.
✓ Branch 1 taken 149700 times.
|
381099 | for(i = 0; i < nridx; ++i) { |
| 104 | 231399 | dest->dim_size[i] = va_arg(ap, _index_t); | |
| 105 | 231399 | dest->index[i] = va_arg(ap, _index_t*); | |
| 106 | 231399 | dest->index_type[i] = (char) va_arg(ap,_index_t); /* char is cast to int by va_arg.*/ | |
| 107 | } | ||
| 108 | 149700 | va_end(ap); | |
| 109 | |||
| 110 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 149700 times.
|
149700 | assert(index_spec_ok(dest)); |
| 111 | 149700 | } | |
| 112 | |||
| 113 | /* Frees what create_index_spec allocated. An 'S' index came from | ||
| 114 | make_index_array; an 'A' index is the data of an integer array it borrows. */ | ||
| 115 | 433177 | void omc_index_spec_release(index_spec_t* s) | |
| 116 | { | ||
| 117 | int i; | ||
| 118 |
2/2✓ Branch 0 taken 149700 times.
✓ Branch 1 taken 283477 times.
|
433177 | if(s->index == NULL) { |
| 119 | return; | ||
| 120 | } | ||
| 121 |
2/2✓ Branch 0 taken 231399 times.
✓ Branch 1 taken 149700 times.
|
381099 | for(i = 0; i < s->ndims; ++i) { |
| 122 |
2/2✓ Branch 0 taken 81196 times.
✓ Branch 1 taken 150203 times.
|
231399 | if(s->index_type[i] == 'S') { |
| 123 | 81196 | omc_rc_release_inline(s->index[i]); | |
| 124 | } | ||
| 125 | } | ||
| 126 | 149700 | omc_rc_release_inline(s->index); | |
| 127 | 149700 | omc_rc_release_inline(s->dim_size); | |
| 128 | 149700 | omc_rc_release_inline(s->index_type); | |
| 129 | 149700 | s->index = NULL; | |
| 130 | 149700 | s->dim_size = NULL; | |
| 131 | 149700 | s->index_type = NULL; | |
| 132 | 149700 | s->ndims = 0; | |
| 133 | } | ||
| 134 | |||
| 135 | /* make_index_array | ||
| 136 | * | ||
| 137 | * Creates an integer array of indices to be used by e.g. | ||
| 138 | * create_index_spec above. | ||
| 139 | */ | ||
| 140 | 81196 | _index_t* make_index_array(int nridx, ...) | |
| 141 | { | ||
| 142 | int i; | ||
| 143 | _index_t* res; | ||
| 144 | va_list ap; | ||
| 145 | 81196 | va_start(ap,nridx); | |
| 146 | |||
| 147 | 81196 | res = size_alloc(nridx); | |
| 148 |
2/2✓ Branch 0 taken 81196 times.
✓ Branch 1 taken 81196 times.
|
162392 | for(i = 0; i < nridx; ++i) { |
| 149 | 81196 | res[i] = va_arg(ap,_index_t); | |
| 150 | } | ||
| 151 | |||
| 152 | 81196 | return res; | |
| 153 | } | ||
| 154 | |||
| 155 | ✗ | void print_size_array(int size, const size_t* arr) | |
| 156 | { | ||
| 157 | int i; | ||
| 158 | printf("{"); | ||
| 159 | ✗ | for(i = 0; i < size-1; ++i) { | |
| 160 | ✗ | printf("%d,", (int) arr[i]); | |
| 161 | } | ||
| 162 | ✗ | printf("%d}\n", (int) arr[i]); | |
| 163 | ✗ | } | |
| 164 | |||
| 165 | /* Calculates the next index for copying subscripted array. | ||
| 166 | * ndims - dimension size of indices. | ||
| 167 | * idx - updated with the the next index | ||
| 168 | * size - size of each index dimension | ||
| 169 | * The function returns 0 if new index is calculated and 1 if no more indices | ||
| 170 | * are available (all indices traversed). | ||
| 171 | */ | ||
| 172 | 4419589 | int next_index(int ndims, _index_t* idx, const _index_t* size) | |
| 173 | { | ||
| 174 | 4419589 | int d = ndims - 1; | |
| 175 | |||
| 176 | 4419589 | idx[d]++; | |
| 177 | |||
| 178 |
2/2✓ Branch 0 taken 316175 times.
✓ Branch 1 taken 4269889 times.
|
4586064 | while(idx[d] >= size[d]) { |
| 179 | 316175 | idx[d] = 0; | |
| 180 |
2/2✓ Branch 0 taken 166475 times.
✓ Branch 1 taken 149700 times.
|
316175 | if(d == 0) { |
| 181 | return 1; | ||
| 182 | } | ||
| 183 | 166475 | d--; | |
| 184 | 166475 | idx[d]++; | |
| 185 | } | ||
| 186 | |||
| 187 | return 0; | ||
| 188 | } | ||
| 189 | |||
| 190 | ✗ | void print_index_spec(const index_spec_t* spec) | |
| 191 | { | ||
| 192 | int i,k; | ||
| 193 | printf("["); | ||
| 194 | ✗ | for(i = 0; i < spec->ndims; ++i) { | |
| 195 | ✗ | switch (spec->index_type[i]) { | |
| 196 | ✗ | case 'S': | |
| 197 | ✗ | printf("%d", (int) *spec->index[i]); | |
| 198 | break; | ||
| 199 | case 'A': | ||
| 200 | printf("{"); | ||
| 201 | ✗ | for(k = 0; k < spec->dim_size[i]-1; ++k) { | |
| 202 | ✗ | printf("%d,", (int) spec->index[i][k]); | |
| 203 | } | ||
| 204 | ✗ | if(0 < spec->dim_size[i]) { | |
| 205 | ✗ | printf("%d", (int) spec->index[i][0]); | |
| 206 | } | ||
| 207 | printf("}"); | ||
| 208 | break; | ||
| 209 | case 'W': | ||
| 210 | printf(":"); | ||
| 211 | break; | ||
| 212 | ✗ | default: | |
| 213 | printf("INVALID TYPE %c.", spec->index_type[i]); | ||
| 214 | break; | ||
| 215 | } | ||
| 216 | ✗ | if(i != (spec->ndims - 1)) { | |
| 217 | printf(", "); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | printf("]"); | ||
| 221 | ✗ | } | |
| 222 |