Changeset 11b8174 in OpenModelica
- Timestamp:
- 2020-10-21T15:27:25+02:00 (4 years ago)
- Children:
- 51a52b92
- Parents:
- db67367
- git-author:
- Per Östlund <perost86@…> (09/30/20 16:39:24)
- git-committer:
- Adrian Pop <adrian.pop@…> (10/21/20 15:27:25)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
OMCompiler/Compiler/Util/UnorderedSet.mo
rc9471b8 r11b8174 67 67 "Creates a new set given a hash function, equality function, and optional 68 68 desired bucket count. An approriate bucket count is 69 Util.nextPrime(number of values that will be added), but starting with a70 low bucket count is also fine if the number of values is unknown since the69 Util.nextPrime(number of elements that will be added), but starting with a 70 low bucket count is also fine if the number of elements is unknown since the 71 71 set rehashes as needed." 72 72 input Hash hash; … … 80 80 set := UNORDERED_SET(buckets, Mutable.create(0), hash, keyEq); 81 81 end new; 82 83 function copy 84 "Returns a copy of the given set." 85 input UnorderedSet<T> set; 86 output UnorderedSet<T> outSet; 87 algorithm 88 outSet := UNORDERED_SET( 89 Mutable.create(arrayCopy(Mutable.access(set.buckets))), 90 Mutable.create(Mutable.access(set.size)), 91 set.hashFn, 92 set.eqFn 93 ); 94 end copy; 82 95 83 96 function add … … 145 158 if isSome(okey) then 146 159 arrayUpdateNoBoundsChecking(buckets, hash + 1, bucket); 160 Mutable.update(set.size, Mutable.access(set.size) - 1); 147 161 end if; 148 162 end remove; … … 206 220 207 221 function toList 208 "Returns the values in the set as a list in no particular order."222 "Returns the elements in the set as a list in no particular order." 209 223 input UnorderedSet<T> set; 210 224 output list<T> outList = {}; … … 218 232 219 233 function toArray 220 "Returns the values in the set as an array in no particular order."234 "Returns the elements in the set as an array in no particular order." 221 235 input UnorderedSet<T> set; 222 236 output array<T> outArray; … … 254 268 end fold; 255 269 270 function all 271 "Returns true if the given function returns true for all elements in the set, 272 otherwise false." 273 input UnorderedSet<T> set; 274 input PredFn fn; 275 output Boolean res; 276 277 partial function PredFn 278 input T key; 279 output Boolean res; 280 end PredFn; 281 algorithm 282 if isEmpty(set) then 283 res := true; 284 return; 285 end if; 286 287 for b in Mutable.access(set.buckets) loop 288 for k in b loop 289 if not fn(k) then 290 res := false; 291 return; 292 end if; 293 end for; 294 end for; 295 296 res := true; 297 end all; 298 299 function any 300 "Returns true if the given function returns true for any elements in the set, 301 otherwise false." 302 input UnorderedSet<T> set; 303 input PredFn fn; 304 output Boolean res; 305 306 partial function PredFn 307 input T key; 308 output Boolean res; 309 end PredFn; 310 algorithm 311 if isEmpty(set) then 312 res := false; 313 return; 314 end if; 315 316 for b in Mutable.access(set.buckets) loop 317 for k in b loop 318 if fn(k) then 319 res := true; 320 return; 321 end if; 322 end for; 323 end for; 324 325 res := false; 326 end any; 327 328 function none 329 "Returns true if the given function returns true for none of the elements in 330 the set, otherwise false." 331 input UnorderedSet<T> set; 332 input PredFn fn; 333 output Boolean res; 334 335 partial function PredFn 336 input T key; 337 output Boolean res; 338 end PredFn; 339 algorithm 340 if isEmpty(set) then 341 res := true; 342 return; 343 end if; 344 345 for b in Mutable.access(set.buckets) loop 346 for k in b loop 347 if fn(k) then 348 res := false; 349 return; 350 end if; 351 end for; 352 end for; 353 354 res := true; 355 end none; 356 256 357 function size 257 "Returns the number of values the set contains."358 "Returns the number of elements the set contains." 258 359 input UnorderedSet<T> set; 259 360 output Integer size = Mutable.access(set.size); 260 361 end size; 362 363 function isEmpty 364 "Returns whether the set is empty or not." 365 input UnorderedSet<T> set; 366 output Boolean empty = Mutable.access(set.size) == 0; 367 end isEmpty; 261 368 262 369 function bucketCount … … 275 382 function rehash 276 383 "Changes the number of buckets to an appropriate number based on the number 277 of values in the set and rehashes all the keys."384 of elements in the set and rehashes all the keys." 278 385 input UnorderedSet<T> set; 279 386 protected … … 378 485 end if; 379 486 380 // Add the key and its index in the value arrayto the bucket indicated by the hash.487 // Add the key to the bucket indicated by the hash. 381 488 arrayUpdate(buckets, h + 1, key :: arrayGet(buckets, h + 1)); 382 489 // Update the size of the set.
Note: See TracChangeset
for help on using the changeset viewer.