Skip to content

Commit

Permalink
Fix double hash in getSafe (#12028)
Browse files Browse the repository at this point in the history
  • Loading branch information
phannebohm committed Feb 23, 2024
1 parent c7776a8 commit 11e78ec
Showing 1 changed file with 20 additions and 46 deletions.
66 changes: 20 additions & 46 deletions OMCompiler/Compiler/Util/UnorderedMap.mo
Expand Up @@ -175,9 +175,8 @@ public
input UnorderedMap<K, V> map;
protected
Hash hashfn = map.hashFn;
Integer hash;
Integer hash = intMod(hashfn(key), Vector.size(map.buckets));
algorithm
hash := intMod(hashfn(key), Vector.size(map.buckets));
addEntry(key, value, hash, map);
end addNew;

Expand Down Expand Up @@ -308,9 +307,8 @@ public
input UnorderedMap<K, V> map;
output Option<V> value;
protected
Integer index;
Integer index = find(key, map);
algorithm
index := find(key, map);
value := if index > 0 then SOME(Vector.getNoBounds(map.values, index)) else NONE();
end get;

Expand All @@ -321,9 +319,11 @@ public
input UnorderedMap<K, V> map;
input SourceInfo info;
output V value;
protected
Integer index = find(key, map);
algorithm
if contains(key, map) then
SOME(value) := get(key, map);
if index > 0 then
value := Vector.getNoBounds(map.values, index);
else
Error.addInternalError(getInstanceName() + " failed because the key did not exist.", info);
fail();
Expand All @@ -334,9 +334,7 @@ public
"Return the value associated with the given key, or fails if no such value exists."
input K key;
input UnorderedMap<K, V> map;
output V value;
algorithm
value := Vector.get(map.values, find(key, map));
output V value = Vector.get(map.values, find(key, map));
end getOrFail;

function getOrDefault
Expand All @@ -345,9 +343,8 @@ public
input V default;
output V value;
protected
Integer index;
Integer index = find(key, map);
algorithm
index := find(key, map);
value := if index > 0 then Vector.getNoBounds(map.values, index) else default;
end getOrDefault;

Expand All @@ -357,51 +354,40 @@ public
input UnorderedMap<K, V> map;
output Option<K> outKey;
protected
Integer index;
Integer index = find(key, map);
algorithm
index := find(key, map);
outKey := if index > 0 then SOME(Vector.getNoBounds(map.keys, index)) else NONE();
end getKey;

function contains
"Returns whether the given key exists in the map or not."
input K key;
input UnorderedMap<K, V> map;
output Boolean res;
algorithm
res := find(key, map) > 0;
output Boolean res = find(key, map) > 0;
end contains;

function first
"Returns the 'first' element in the map, or fails if the map is empty."
input UnorderedMap<K, V> map;
output V value;
algorithm
value := Vector.get(map.values, 1);
output V value = Vector.get(map.values, 1);
end first;

function firstKey
"Returns the 'first' key in the map, or fails if the map is empty."
input UnorderedMap<K, V> map;
output K key;
algorithm
key := Vector.get(map.keys, 1);
output K key = Vector.get(map.keys, 1);
end firstKey;

function keyAt
input UnorderedMap<K, V> map;
input Integer index;
output K key;
algorithm
key := Vector.get(map.keys, index);
output K key = Vector.get(map.keys, index);
end keyAt;

function valueAt
input UnorderedMap<K, V> map;
input Integer index;
output V value;
algorithm
value := Vector.get(map.values, index);
output V value = Vector.get(map.values, index);
end valueAt;

function toList
Expand All @@ -423,17 +409,13 @@ public
function keyList
"Returns the keys as a list."
input UnorderedMap<K, V> map;
output list<K> keys;
algorithm
keys := Vector.toList(map.keys);
output list<K> keys = Vector.toList(map.keys);
end keyList;

function valueList
"Returns the values as a list."
input UnorderedMap<K, V> map;
output list<V> values;
algorithm
values := Vector.toList(map.values);
output list<V> values = Vector.toList(map.values);
end valueList;

function toArray
Expand All @@ -457,17 +439,13 @@ public
function keyArray
"Returns the keys as an array."
input UnorderedMap<K, V> map;
output array<K> keys;
algorithm
keys := Vector.toArray(map.keys);
output array<K> keys = Vector.toArray(map.keys);
end keyArray;

function valueArray
"Returns the values as an array."
input UnorderedMap<K, V> map;
output array<V> values;
algorithm
values := Vector.toArray(map.values);
output array<V> values = Vector.toArray(map.values);
end valueArray;

function toVector
Expand All @@ -491,17 +469,13 @@ public
function keyVector
"Returns the keys as a Vector."
input UnorderedMap<K, V> map;
output Vector<K> keys;
algorithm
keys := Vector.copy(map.keys);
output Vector<K> keys = Vector.copy(map.keys);
end keyVector;

function valueVector
"Returns the values as a Vector."
input UnorderedMap<K, V> map;
output Vector<V> values;
algorithm
values := Vector.copy(map.values);
output Vector<V> values = Vector.copy(map.values);
end valueVector;

function fold<FT>
Expand Down

0 comments on commit 11e78ec

Please sign in to comment.