Skip to content

Commit

Permalink
Fix ExpandableArray
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Apr 14, 2017
1 parent 3eb1be0 commit 9abe3dc
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Compiler/Util/ExpandableArray.mo
Expand Up @@ -63,12 +63,12 @@ function clear "O(n)
input output ExpandableArray<T> exarray;
protected
Integer n = Dangerous.arrayGetNoBoundsChecking(exarray.numberOfElements, 1);
Integer capacity = Dangerous.arrayGetNoBoundsChecking(exarray.capacity, 1);
Integer lastUsedIndex = Dangerous.arrayGetNoBoundsChecking(exarray.lastUsedIndex, 1);
array<Option<T>> data = Dangerous.arrayGetNoBoundsChecking(exarray.data, 1);
algorithm
Dangerous.arrayUpdateNoBoundsChecking(exarray.numberOfElements, 1, 0);
Dangerous.arrayUpdateNoBoundsChecking(exarray.lastUsedIndex, 1, 0);
for i in 1:capacity loop
for i in 1:lastUsedIndex loop
if isSome(Dangerous.arrayGetNoBoundsChecking(data, i)) then
n := n-1;
Dangerous.arrayUpdateNoBoundsChecking(data, i, NONE());
Expand Down Expand Up @@ -174,6 +174,14 @@ algorithm
if index >= 1 and index <= lastUsedIndex and isSome(Dangerous.arrayGetNoBoundsChecking(data, index)) then
arrayUpdate(data, index, NONE());
Dangerous.arrayUpdateNoBoundsChecking(exarray.numberOfElements, 1, numberOfElements-1);

if index == lastUsedIndex then
lastUsedIndex := lastUsedIndex-1;
while lastUsedIndex > 0 and isNone(Dangerous.arrayGetNoBoundsChecking(data, lastUsedIndex)) loop
lastUsedIndex := lastUsedIndex-1;
end while;
Dangerous.arrayUpdateNoBoundsChecking(exarray.lastUsedIndex, 1, lastUsedIndex);
end if;
else
fail();
end if;
Expand Down Expand Up @@ -222,7 +230,7 @@ protected
Integer lastUsedIndex = Dangerous.arrayGetNoBoundsChecking(exarray.lastUsedIndex, 1);
array<Option<T>> data = Dangerous.arrayGetNoBoundsChecking(exarray.data, 1);
algorithm
if numberOfElements == 0 then
if numberOfElements == 0 or numberOfElements == lastUsedIndex then
return;
end if;

Expand All @@ -237,7 +245,7 @@ algorithm
end if;
end for;

Dangerous.arrayUpdateNoBoundsChecking(exarray.lastUsedIndex, 1, numberOfElements);
Dangerous.arrayUpdateNoBoundsChecking(exarray.lastUsedIndex, 1, lastUsedIndex);
end compress;

function shrink "O(n)
Expand Down Expand Up @@ -298,6 +306,11 @@ function getNumberOfElements
output Integer numberOfElements = Dangerous.arrayGetNoBoundsChecking(exarray.numberOfElements, 1);
end getNumberOfElements;

function getLastUsedIndex
input ExpandableArray<T> exarray;
output Integer lastUsedIndex = Dangerous.arrayGetNoBoundsChecking(exarray.lastUsedIndex, 1);
end getLastUsedIndex;

function getCapacity
input ExpandableArray<T> exarray;
output Integer capacity = Dangerous.arrayGetNoBoundsChecking(exarray.capacity, 1);
Expand Down

0 comments on commit 9abe3dc

Please sign in to comment.