OMCompiler/Compiler/Util/SBMultiInterval.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated uniontype SBMultiInterval | ||
| 37 | import SBInterval; | ||
| 38 | import UnorderedSet; | ||
| 39 | |||
| 40 | protected | ||
| 41 | import Array; | ||
| 42 | import List; | ||
| 43 | import MetaModelica.Dangerous.*; | ||
| 44 | |||
| 45 | public | ||
| 46 | record MULTI_INTERVAL | ||
| 47 | array<SBInterval> intervals; | ||
| 48 | Integer ndim; | ||
| 49 | end MULTI_INTERVAL; | ||
| 50 | |||
| 51 | function newEmpty | ||
| 52 | output SBMultiInterval mi; | ||
| 53 | algorithm | ||
| 54 | 6292 | mi := MULTI_INTERVAL(listArray({}), 0); | |
| 55 | end newEmpty; | ||
| 56 | |||
| 57 | function copy | ||
| 58 | input SBMultiInterval mi; | ||
| 59 | output SBMultiInterval outMI; | ||
| 60 | algorithm | ||
| 61 | 18294 | outMI := MULTI_INTERVAL(arrayCopy(mi.intervals), mi.ndim); | |
| 62 | end copy; | ||
| 63 | |||
| 64 | function fromList | ||
| 65 | input list<SBInterval> ints; | ||
| 66 | output SBMultiInterval outMI; | ||
| 67 | algorithm | ||
| 68 | ✗ | if List.any(ints, SBInterval.isEmpty) then | |
| 69 | ✗ | outMI := newEmpty(); | |
| 70 | else | ||
| 71 | ✗ | outMI := MULTI_INTERVAL(listArray(ints), listLength(ints)); | |
| 72 | end if; | ||
| 73 | end fromList; | ||
| 74 | |||
| 75 | function fromArray | ||
| 76 | input array<SBInterval> ints; | ||
| 77 | output SBMultiInterval outMI; | ||
| 78 | algorithm | ||
| 79 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7767 times.
|
7767 | if Array.any(ints, SBInterval.isEmpty) then |
| 80 | ✗ | outMI := newEmpty(); | |
| 81 | else | ||
| 82 | 7767 | outMI := MULTI_INTERVAL(arrayCopy(ints), arrayLength(ints)); | |
| 83 | end if; | ||
| 84 | end fromArray; | ||
| 85 | |||
| 86 | function isEmpty | ||
| 87 | input SBMultiInterval mi; | ||
| 88 | output Boolean empty; | ||
| 89 | algorithm | ||
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26597 times.
|
26597 | empty := arrayEmpty(mi.intervals); |
| 91 | end isEmpty; | ||
| 92 | |||
| 93 | function contains | ||
| 94 | input array<Integer> vals; | ||
| 95 | input SBMultiInterval mi; | ||
| 96 | output Boolean res; | ||
| 97 | algorithm | ||
| 98 | ✗ | if arrayLength(vals) <> mi.ndim then | |
| 99 | res := false; | ||
| 100 | else | ||
| 101 | ✗ | res := Array.isEqualOnTrue(vals, mi.intervals, SBInterval.contains); | |
| 102 | end if; | ||
| 103 | end contains; | ||
| 104 | |||
| 105 | function intersection | ||
| 106 | input SBMultiInterval mi1; | ||
| 107 | input SBMultiInterval mi2; | ||
| 108 | output SBMultiInterval outMI; | ||
| 109 | protected | ||
| 110 | array<SBInterval> ints; | ||
| 111 | SBInterval ires; | ||
| 112 | algorithm | ||
| 113 |
3/4✓ Branch 0 taken 10733 times.
✓ Branch 1 taken 293 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10733 times.
|
11026 | if mi1.ndim <> mi2.ndim or isEmpty(mi1) then |
| 114 | 293 | outMI := newEmpty(); | |
| 115 | 293 | return; | |
| 116 | end if; | ||
| 117 | |||
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10733 times.
|
21466 | ints := arrayCreateNoInit(mi1.ndim, arrayGet(mi1.intervals, 1)); |
| 119 | |||
| 120 |
1/2✓ Branch 0 taken 10733 times.
✗ Branch 1 not taken.
|
16785 | for i in 1:arrayLength(ints) loop |
| 121 | 11465 | ires := SBInterval.intersection( | |
| 122 | arrayGet(mi1.intervals, i), arrayGet(mi2.intervals, i)); | ||
| 123 | |||
| 124 |
2/2✓ Branch 1 taken 5413 times.
✓ Branch 2 taken 6052 times.
|
11465 | if SBInterval.isEmpty(ires) then |
| 125 | 5413 | outMI := newEmpty(); | |
| 126 | 5413 | return; | |
| 127 | end if; | ||
| 128 | |||
| 129 | arrayUpdateNoBoundsChecking(ints, i, ires); | ||
| 130 | end for; | ||
| 131 | |||
| 132 | 5320 | outMI := fromArray(ints); | |
| 133 | end intersection; | ||
| 134 | |||
| 135 | function complement | ||
| 136 | input SBMultiInterval mi1; | ||
| 137 | input SBMultiInterval mi2; | ||
| 138 | output UnorderedSet<SBMultiInterval> res; | ||
| 139 | protected | ||
| 140 | SBMultiInterval tmp_mi; | ||
| 141 | UnorderedSet<SBInterval> dummys = dummys; | ||
| 142 | array<UnorderedSet<SBInterval>> diffs; | ||
| 143 | Integer count, mi1_size; | ||
| 144 | array<SBInterval> resi; | ||
| 145 | |||
| 146 | function add_interval | ||
| 147 | input SBInterval i; | ||
| 148 | input Integer count; | ||
| 149 | input Integer size; | ||
| 150 | input array<SBInterval> ints1; | ||
| 151 | input array<SBInterval> ints2; | ||
| 152 | input output UnorderedSet<SBMultiInterval> res; | ||
| 153 | protected | ||
| 154 | SBInterval dummyi = dummyi; | ||
| 155 | array<SBInterval> resi; | ||
| 156 | algorithm | ||
| 157 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if not SBInterval.isEmpty(i) then |
| 158 | 12 | resi := arrayCreateNoInit(size, dummyi); | |
| 159 | 12 | Array.copyN(ints1, resi, count); | |
| 160 | 12 | resi[count + 1] := i; | |
| 161 | 12 | Array.copyN(ints2, resi, arrayLength(ints2) - count - 1, count + 1, count + 1); | |
| 162 | 12 | UnorderedSet.add(fromArray(resi), res); | |
| 163 | end if; | ||
| 164 | end add_interval; | ||
| 165 | algorithm | ||
| 166 | 293 | res := UnorderedSet.new(hash, isEqual); | |
| 167 | |||
| 168 |
2/4✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 293 times.
|
293 | if isEmpty(mi1) or mi1.ndim <> mi2.ndim then |
| 169 | ✗ | return; | |
| 170 | end if; | ||
| 171 | |||
| 172 | 293 | tmp_mi := intersection(mi1, mi2); | |
| 173 | |||
| 174 |
2/2✓ Branch 1 taken 73 times.
✓ Branch 2 taken 220 times.
|
293 | if isEmpty(tmp_mi) then |
| 175 | 73 | UnorderedSet.add(mi1, res); | |
| 176 | 73 | return; | |
| 177 | end if; | ||
| 178 | |||
| 179 |
2/2✓ Branch 1 taken 208 times.
✓ Branch 2 taken 12 times.
|
220 | if isEqual(mi1, tmp_mi) then |
| 180 | 208 | return; | |
| 181 | end if; | ||
| 182 | |||
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | mi1_size := arrayLength(mi1.intervals); |
| 184 | 12 | diffs := arrayCreateNoInit(mi1_size, dummys); | |
| 185 | |||
| 186 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
29 | for i in 1:mi1_size loop |
| 187 | 17 | diffs[i] := SBInterval.complement(arrayGetNoBoundsChecking(mi1.intervals, i), | |
| 188 | arrayGet(tmp_mi.intervals, i)); | ||
| 189 | end for; | ||
| 190 | |||
| 191 | count := 0; | ||
| 192 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 2 taken 12 times.
|
29 | for vdiff in diffs loop |
| 193 | 17 | UnorderedSet.fold( | |
| 194 | vdiff, | ||
| 195 | function add_interval( | ||
| 196 | count = count, | ||
| 197 | size = mi1_size, | ||
| 198 | ints1 = tmp_mi.intervals, | ||
| 199 | ints2 = mi1.intervals), | ||
| 200 | res); | ||
| 201 | |||
| 202 | 17 | count := count + 1; | |
| 203 | end for; | ||
| 204 | end complement; | ||
| 205 | |||
| 206 | function crossProd | ||
| 207 | input SBMultiInterval mi1; | ||
| 208 | input SBMultiInterval mi2; | ||
| 209 | output SBMultiInterval res; | ||
| 210 | protected | ||
| 211 | array<SBInterval> ints; | ||
| 212 | algorithm | ||
| 213 | ✗ | ints := Array.join(mi1.intervals, mi2.intervals); | |
| 214 | ✗ | res := MULTI_INTERVAL(ints, arrayLength(ints)); | |
| 215 | end crossProd; | ||
| 216 | |||
| 217 | function cardinality | ||
| 218 | input SBMultiInterval mi; | ||
| 219 | output Integer card = 0; | ||
| 220 | algorithm | ||
| 221 | ✗ | for i in 1:mi.ndim loop | |
| 222 | ✗ | card := card + SBInterval.cardinality(mi.intervals[i]); | |
| 223 | end for; | ||
| 224 | end cardinality; | ||
| 225 | |||
| 226 | function intervals | ||
| 227 | input SBMultiInterval mi; | ||
| 228 | output array<SBInterval> ints = mi.intervals; | ||
| 229 | end intervals; | ||
| 230 | |||
| 231 | function ndim | ||
| 232 | input SBMultiInterval mi; | ||
| 233 | output Integer ndim = arrayLength(mi.intervals); | ||
| 234 | end ndim; | ||
| 235 | |||
| 236 | function minElem | ||
| 237 | input SBMultiInterval mi; | ||
| 238 | output array<Integer> res; | ||
| 239 | algorithm | ||
| 240 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
✓ Branch 3 taken 183 times.
✓ Branch 4 taken 148 times.
|
479 | for i in mi.intervals loop |
| 241 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 183 times.
|
183 | if SBInterval.isEmpty(i) then |
| 242 | ✗ | res := listArray({}); | |
| 243 | ✗ | return; | |
| 244 | end if; | ||
| 245 | end for; | ||
| 246 | |||
| 247 | 148 | res := Array.map(mi.intervals, SBInterval.lowerBound); | |
| 248 | end minElem; | ||
| 249 | |||
| 250 | function replace | ||
| 251 | input SBInterval i; | ||
| 252 | input Integer dim; | ||
| 253 | input SBMultiInterval mi; | ||
| 254 | output SBMultiInterval res; | ||
| 255 | protected | ||
| 256 | array<SBInterval> ints; | ||
| 257 | algorithm | ||
| 258 | 1 | ints := arrayCopy(mi.intervals); | |
| 259 | 1 | ints[dim] := i; | |
| 260 | 1 | res := fromArray(ints); | |
| 261 | end replace; | ||
| 262 | |||
| 263 | function isEqual | ||
| 264 | input SBMultiInterval mi1; | ||
| 265 | input SBMultiInterval mi2; | ||
| 266 | output Boolean equal; | ||
| 267 | algorithm | ||
| 268 | 341 | equal := Array.isEqualOnTrue(mi1.intervals, mi2.intervals, SBInterval.isEqual); | |
| 269 | end isEqual; | ||
| 270 | |||
| 271 | function hash | ||
| 272 | input SBMultiInterval mi; | ||
| 273 | output Integer res = 0; | ||
| 274 | protected | ||
| 275 | // Small enough that res * 31 + p fits in a 32-bit Integer. | ||
| 276 | constant Integer p = 33554393; | ||
| 277 | algorithm | ||
| 278 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 7880 times.
✓ Branch 3 taken 8979 times.
✓ Branch 4 taken 7880 times.
|
24739 | for i in mi.intervals loop |
| 279 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8979 times.
|
17958 | res := intMod(res * 31 + intMod(SBInterval.lowerBound(i), p), p); |
| 280 | 17958 | res := intMod(res * 31 + intMod(SBInterval.upperBound(i), p), p); | |
| 281 | end for; | ||
| 282 | end hash; | ||
| 283 | |||
| 284 | function size | ||
| 285 | input SBMultiInterval mi; | ||
| 286 | output Integer sz = 1; | ||
| 287 | algorithm | ||
| 288 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 46 times.
|
143 | for i in mi.intervals loop |
| 289 | 51 | sz := sz * SBInterval.size(i); | |
| 290 | end for; | ||
| 291 | end size; | ||
| 292 | |||
| 293 | function toString | ||
| 294 | input SBMultiInterval mi; | ||
| 295 | output String str; | ||
| 296 | algorithm | ||
| 297 | ✗ | if isEmpty(mi) then | |
| 298 | str := "emptyInterval"; | ||
| 299 | else | ||
| 300 | ✗ | str := stringDelimitList(list(SBInterval.toString(i) for i in mi.intervals), "x"); | |
| 301 | end if; | ||
| 302 | end toString; | ||
| 303 | |||
| 304 | annotation(__OpenModelica_Interface="util"); | ||
| 305 | end SBMultiInterval; | ||
| 306 |