Changeset 11b8174 in OpenModelica


Ignore:
Timestamp:
2020-10-21T15:27:25+02:00 (4 years ago)
Author:
Adrian Pop <adrian.pop@…>
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)
Message:

Update UnorderedSet.

  • Fix size of the set when removing elements with UnorderedSet.remove.
  • Add functions copy, first, all, any, none and isEmpty.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Compiler/Util/UnorderedSet.mo

    rc9471b8 r11b8174  
    6767    "Creates a new set given a hash function, equality function, and optional
    6868     desired bucket count. An approriate bucket count is
    69      Util.nextPrime(number of values that will be added), but starting with a
    70      low bucket count is also fine if the number of values is unknown since the
     69     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
    7171     set rehashes as needed."
    7272    input Hash hash;
     
    8080    set := UNORDERED_SET(buckets, Mutable.create(0), hash, keyEq);
    8181  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;
    8295
    8396  function add
     
    145158    if isSome(okey) then
    146159      arrayUpdateNoBoundsChecking(buckets, hash + 1, bucket);
     160      Mutable.update(set.size, Mutable.access(set.size) - 1);
    147161    end if;
    148162  end remove;
     
    206220
    207221  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."
    209223    input UnorderedSet<T> set;
    210224    output list<T> outList = {};
     
    218232
    219233  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."
    221235    input UnorderedSet<T> set;
    222236    output array<T> outArray;
     
    254268  end fold;
    255269
     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
    256357  function size
    257     "Returns the number of values the set contains."
     358    "Returns the number of elements the set contains."
    258359    input UnorderedSet<T> set;
    259360    output Integer size = Mutable.access(set.size);
    260361  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;
    261368
    262369  function bucketCount
     
    275382  function rehash
    276383    "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."
    278385    input UnorderedSet<T> set;
    279386  protected
     
    378485    end if;
    379486
    380     // Add the key and its index in the value array to the bucket indicated by the hash.
     487    // Add the key to the bucket indicated by the hash.
    381488    arrayUpdate(buckets, h + 1, key :: arrayGet(buckets, h + 1));
    382489    // Update the size of the set.
Note: See TracChangeset for help on using the changeset viewer.