OMCompiler/Compiler/Util/DoubleEnded.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 package DoubleEnded<T> | ||
| 37 | "Implementation of a mutable double-ended list. O(1) push_front, push_back, pop_front, toListAndClear" | ||
| 38 | public | ||
| 39 | import Mutable; | ||
| 40 | uniontype MutableList<T> | ||
| 41 | record LIST | ||
| 42 | Mutable<Integer> length; | ||
| 43 | Mutable<list<T>> front; | ||
| 44 | Mutable<list<T>> back; | ||
| 45 | end LIST; | ||
| 46 | end MutableList; | ||
| 47 | |||
| 48 | protected | ||
| 49 | import GCExt; | ||
| 50 | import MetaModelica.Dangerous; | ||
| 51 | |||
| 52 | public impure function new<T> | ||
| 53 | input T first; | ||
| 54 | output MutableList<T> delst; | ||
| 55 | protected | ||
| 56 | list<T> lst = {first}; | ||
| 57 | algorithm | ||
| 58 | ✗ | delst := LIST(Mutable.create(1),Mutable.create(lst),Mutable.create(lst)); | |
| 59 | end new; | ||
| 60 | |||
| 61 | public impure function fromList<T> | ||
| 62 | input list<T> lst; | ||
| 63 | output MutableList<T> delst; | ||
| 64 | protected | ||
| 65 | list<T> head,tail,tmp; | ||
| 66 | Integer length; | ||
| 67 | T t; | ||
| 68 | algorithm | ||
| 69 |
1/2✓ Branch 0 taken 201374 times.
✗ Branch 1 not taken.
|
201374 | if listEmpty(lst) then |
| 70 | 201374 | delst := LIST(Mutable.create(0),Mutable.create({}),Mutable.create({})); | |
| 71 | 201374 | return; | |
| 72 | end if; | ||
| 73 | ✗ | t::tmp := lst; | |
| 74 | head := {t}; | ||
| 75 | tail := head; | ||
| 76 | length := 1; | ||
| 77 | ✗ | for l in tmp loop | |
| 78 | tmp := {l}; | ||
| 79 | ✗ | Dangerous.listSetRest(tail, tmp); | |
| 80 | tail := tmp; | ||
| 81 | ✗ | length := length+1; | |
| 82 | end for; | ||
| 83 | ✗ | delst := LIST(Mutable.create(length),Mutable.create(head),Mutable.create(tail)); | |
| 84 | end fromList; | ||
| 85 | |||
| 86 | public impure function empty<T> | ||
| 87 | input T dummy; | ||
| 88 | output MutableList<T> delst; | ||
| 89 | algorithm | ||
| 90 | 501931 | delst := LIST(Mutable.create(0),Mutable.create({}),Mutable.create({})); | |
| 91 | end empty; | ||
| 92 | |||
| 93 | public function length<T> | ||
| 94 | input MutableList<T> delst; | ||
| 95 | output Integer length; | ||
| 96 | algorithm | ||
| 97 | 9971 | length := Mutable.access(delst.length); | |
| 98 | end length; | ||
| 99 | |||
| 100 | public function pop_front<T> | ||
| 101 | input MutableList<T> delst; | ||
| 102 | output T elt; | ||
| 103 | protected | ||
| 104 | Integer length = Mutable.access(delst.length); | ||
| 105 | list<T> lst; | ||
| 106 | algorithm | ||
| 107 | ✗ | true := length>0; | |
| 108 | ✗ | Mutable.update(delst.length, length-1); | |
| 109 | ✗ | elt::lst := Mutable.access(delst.front); | |
| 110 | ✗ | if length==1 then | |
| 111 | ✗ | Mutable.update(delst.front, {}); | |
| 112 | ✗ | Mutable.update(delst.back, {}); | |
| 113 | ✗ | return; | |
| 114 | end if; | ||
| 115 | ✗ | Mutable.update(delst.front, lst); | |
| 116 | end pop_front; | ||
| 117 | |||
| 118 | public function currentBackCell<T> | ||
| 119 | input MutableList<T> delst; | ||
| 120 | output list<T> last; | ||
| 121 | algorithm | ||
| 122 | 4811 | last := Mutable.access(delst.back); | |
| 123 | end currentBackCell; | ||
| 124 | |||
| 125 | public function push_front<T> | ||
| 126 | input MutableList<T> delst; | ||
| 127 | input T elt; | ||
| 128 | protected | ||
| 129 | Integer length=Mutable.access(delst.length); | ||
| 130 | list<T> lst; | ||
| 131 | algorithm | ||
| 132 | 4258 | Mutable.update(delst.length, length+1); | |
| 133 |
2/2✓ Branch 0 taken 219 times.
✓ Branch 1 taken 4039 times.
|
4258 | if length==0 then |
| 134 | lst := {elt}; | ||
| 135 | 219 | Mutable.update(delst.front, lst); | |
| 136 | 219 | Mutable.update(delst.back, lst); | |
| 137 | 219 | return; | |
| 138 | end if; | ||
| 139 | 4039 | lst := Mutable.access(delst.front); | |
| 140 | 4039 | Mutable.update(delst.front, elt::lst); | |
| 141 | end push_front; | ||
| 142 | |||
| 143 | public function push_list_front<T> | ||
| 144 | input MutableList<T> delst; | ||
| 145 | input list<T> lst; | ||
| 146 | protected | ||
| 147 | Integer length = Mutable.access(delst.length), lstLength; | ||
| 148 | list<T> work, oldHead, tmp, head; | ||
| 149 | T t; | ||
| 150 | algorithm | ||
| 151 | 205 | lstLength := listLength(lst); | |
| 152 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 201 times.
|
205 | if lstLength==0 then |
| 153 | 4 | return; | |
| 154 | end if; | ||
| 155 | 201 | Mutable.update(delst.length, length+lstLength); | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
|
201 | t::tmp := lst; |
| 157 | head := {t}; | ||
| 158 | 201 | oldHead := Mutable.access(delst.front); | |
| 159 | 201 | Mutable.update(delst.front, head); | |
| 160 |
2/2✓ Branch 1 taken 82 times.
✓ Branch 2 taken 201 times.
|
283 | for l in tmp loop |
| 161 | work := {l}; | ||
| 162 | 82 | Dangerous.listSetRest(head, work); | |
| 163 | head := work; | ||
| 164 | end for; | ||
| 165 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 115 times.
|
201 | if length==0 then |
| 166 | 86 | Mutable.update(delst.back, head); | |
| 167 | else | ||
| 168 | 115 | Dangerous.listSetRest(head, oldHead); | |
| 169 | end if; | ||
| 170 | end push_list_front; | ||
| 171 | |||
| 172 | public function push_back<T> | ||
| 173 | input MutableList<T> delst; | ||
| 174 | input T elt; | ||
| 175 | protected | ||
| 176 | Integer length = Mutable.access(delst.length); | ||
| 177 | list<T> lst; | ||
| 178 | algorithm | ||
| 179 | 2597233 | Mutable.update(delst.length, length+1); | |
| 180 |
2/2✓ Branch 0 taken 603613 times.
✓ Branch 1 taken 1993620 times.
|
2597233 | if length==0 then |
| 181 | lst := {elt}; | ||
| 182 | 603613 | Mutable.update(delst.front, lst); | |
| 183 | 603613 | Mutable.update(delst.back, lst); | |
| 184 | 603613 | return; | |
| 185 | end if; | ||
| 186 | lst := {elt}; | ||
| 187 | 1993620 | Dangerous.listSetRest(Mutable.access(delst.back), lst); | |
| 188 | 1993620 | Mutable.update(delst.back, lst); | |
| 189 | end push_back; | ||
| 190 | |||
| 191 | public function push_list_back<T> | ||
| 192 | input MutableList<T> delst; | ||
| 193 | input list<T> lst; | ||
| 194 | protected | ||
| 195 | Integer length=Mutable.access(delst.length), lstLength; | ||
| 196 | list<T> tail, tmp; | ||
| 197 | T t; | ||
| 198 | algorithm | ||
| 199 | 28660 | lstLength := listLength(lst); | |
| 200 |
2/2✓ Branch 0 taken 905 times.
✓ Branch 1 taken 27755 times.
|
28660 | if lstLength==0 then |
| 201 | 905 | return; | |
| 202 | end if; | ||
| 203 | 27755 | Mutable.update(delst.length, length+lstLength); | |
| 204 | 27755 | t := listGet(lst, 1); | |
| 205 | tmp := {t}; | ||
| 206 |
2/2✓ Branch 0 taken 2367 times.
✓ Branch 1 taken 25388 times.
|
27755 | if length==0 then |
| 207 | 2367 | Mutable.update(delst.front, tmp); | |
| 208 | else | ||
| 209 | 25388 | Dangerous.listSetRest(Mutable.access(delst.back), tmp); | |
| 210 | end if; | ||
| 211 | tail := tmp; | ||
| 212 |
2/2✓ Branch 2 taken 2756 times.
✓ Branch 3 taken 27755 times.
|
30511 | for l in listRest(lst) loop |
| 213 | tmp := {l}; | ||
| 214 | 2756 | Dangerous.listSetRest(tail, tmp); | |
| 215 | tail := tmp; | ||
| 216 | end for; | ||
| 217 | 27755 | Mutable.update(delst.back, tail); | |
| 218 | end push_list_back; | ||
| 219 | |||
| 220 | public impure function toListAndClear<T> | ||
| 221 | input MutableList<T> delst; | ||
| 222 | input list<T> prependToList = {}; | ||
| 223 | output list<T> res; | ||
| 224 | algorithm | ||
| 225 |
2/2✓ Branch 1 taken 73176 times.
✓ Branch 2 taken 601187 times.
|
674363 | if Mutable.access(delst.length)==0 then |
| 226 | res := prependToList; | ||
| 227 | 73176 | return; | |
| 228 | end if; | ||
| 229 | 601187 | res := Mutable.access(delst.front); | |
| 230 |
2/2✓ Branch 0 taken 100118 times.
✓ Branch 1 taken 501069 times.
|
601187 | if not listEmpty(prependToList) then |
| 231 | 100118 | Dangerous.listSetRest(Mutable.access(delst.back), prependToList); | |
| 232 | end if; | ||
| 233 | 601187 | Mutable.update(delst.back, {}); | |
| 234 | 601187 | Mutable.update(delst.front, {}); | |
| 235 | 601187 | Mutable.update(delst.length, 0); | |
| 236 | end toListAndClear; | ||
| 237 | |||
| 238 | public impure function toListNoCopyNoClear<T> | ||
| 239 | "Returns the working list, which may be changed later on!" | ||
| 240 | input MutableList<T> delst; | ||
| 241 | output list<T> res; | ||
| 242 | algorithm | ||
| 243 | 9741 | res := Mutable.access(delst.front); | |
| 244 | end toListNoCopyNoClear; | ||
| 245 | |||
| 246 | public impure function clear<T> | ||
| 247 | input MutableList<T> delst; | ||
| 248 | protected | ||
| 249 | list<T> lst; | ||
| 250 | algorithm | ||
| 251 | ✗ | lst := Mutable.access(delst.front); | |
| 252 | ✗ | Mutable.update(delst.back, {}); | |
| 253 | ✗ | Mutable.update(delst.front, {}); | |
| 254 | ✗ | Mutable.update(delst.length, 0); | |
| 255 | ✗ | for l in lst loop | |
| 256 | ✗ | GCExt.free(l); | |
| 257 | end for; | ||
| 258 | end clear; | ||
| 259 | |||
| 260 | public impure function mapNoCopy_1<T, ArgT1> | ||
| 261 | input MutableList<T> delst; | ||
| 262 | input MapFunc inMapFunc; | ||
| 263 | input ArgT1 inArg1; | ||
| 264 | partial function MapFunc | ||
| 265 | input T inElement; | ||
| 266 | input ArgT1 inArg1; | ||
| 267 | output T outElement; | ||
| 268 | end MapFunc; | ||
| 269 | protected | ||
| 270 | list<T> lst=Mutable.access(delst.front); | ||
| 271 | algorithm | ||
| 272 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 22830 times.
|
22943 | while not listEmpty(lst) loop |
| 273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
|
113 | Dangerous.listSetFirst(lst, inMapFunc(listGet(lst,1), inArg1)); |
| 274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
|
113 | _::lst := lst; |
| 275 | end while; | ||
| 276 | end mapNoCopy_1; | ||
| 277 | |||
| 278 | public impure function mapFoldNoCopy<T, ArgT1> | ||
| 279 | input MutableList<T> delst; | ||
| 280 | input MapFunc inMapFunc; | ||
| 281 | input output ArgT1 arg; | ||
| 282 | partial function MapFunc | ||
| 283 | input output T element; | ||
| 284 | input output ArgT1 arg; | ||
| 285 | end MapFunc; | ||
| 286 | protected | ||
| 287 | T element; | ||
| 288 | list<T> lst=Mutable.access(delst.front); | ||
| 289 | algorithm | ||
| 290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8472 times.
|
8472 | while not listEmpty(lst) loop |
| 291 | ✗ | (element,arg) := inMapFunc(listGet(lst,1), arg); | |
| 292 | ✗ | Dangerous.listSetFirst(lst, element); | |
| 293 | ✗ | _::lst := lst; | |
| 294 | end while; | ||
| 295 | end mapFoldNoCopy; | ||
| 296 | |||
| 297 | annotation(__OpenModelica_Interface="util_datatypes_basic"); | ||
| 298 | |||
| 299 | end DoubleEnded; | ||
| 300 |