Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit d12a035

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Use Mutable for the DoubleEndedList
1 parent 1224535 commit d12a035

1 file changed

Lines changed: 51 additions & 49 deletions

File tree

Compiler/Util/DoubleEndedList.mo

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
encapsulated uniontype DoubleEndedList<T> "Implementation of a mutable double-ended list. O(1) push_front, push_back, pop_front, toListAndClear"
3333

3434
record LIST
35-
array<Integer> length;
36-
array<list<T>> front, back;
35+
Mutable<Integer> length;
36+
Mutable<list<T>> front, back;
3737
end LIST;
3838

39+
import Mutable;
40+
3941
protected
4042
import GC;
4143
import MetaModelica.Dangerous;
@@ -48,7 +50,7 @@ impure function new
4850
protected
4951
list<T> lst={first};
5052
algorithm
51-
delst := LIST(arrayCreate(1,1),arrayCreate(1,lst),arrayCreate(1,lst));
53+
delst := LIST(Mutable.create(1),Mutable.create(lst),Mutable.create(lst));
5254
end new;
5355

5456
impure function fromList
@@ -60,7 +62,7 @@ protected
6062
T t;
6163
algorithm
6264
if listEmpty(lst) then
63-
delst := LIST(arrayCreate(1,0),arrayCreate(1,{}),arrayCreate(1,{}));
65+
delst := LIST(Mutable.create(0),Mutable.create({}),Mutable.create({}));
6466
return;
6567
end if;
6668
t::tmp := lst;
@@ -73,90 +75,90 @@ algorithm
7375
tail := tmp;
7476
length := length+1;
7577
end for;
76-
delst := LIST(arrayCreate(1,length),arrayCreate(1,head),arrayCreate(1,tail));
78+
delst := LIST(Mutable.create(length),Mutable.create(head),Mutable.create(tail));
7779
end fromList;
7880

7981
impure function empty
8082
input T dummy;
8183
output DoubleEndedList<T> delst;
8284
algorithm
83-
delst := LIST(arrayCreate(1,0),arrayCreate(1,{}),arrayCreate(1,{}));
85+
delst := LIST(Mutable.create(0),Mutable.create({}),Mutable.create({}));
8486
end empty;
8587

8688
function length
8789
input DoubleEndedList<T> delst;
8890
output Integer length;
8991
algorithm
90-
length := arrayGet(delst.length,1);
92+
length := Mutable.access(delst.length);
9193
end length;
9294

9395
function pop_front
9496
input DoubleEndedList<T> delst;
9597
output T elt;
9698
protected
97-
Integer length=arrayGet(delst.length,1);
99+
Integer length=Mutable.access(delst.length);
98100
list<T> lst;
99101
algorithm
100102
true := length>0;
101-
arrayUpdate(delst.length, 1, length-1);
103+
Mutable.update(delst.length, length-1);
102104
if length==1 then
103-
arrayUpdate(delst.front, 1, {});
104-
arrayUpdate(delst.back, 1, {});
105+
Mutable.update(delst.front, {});
106+
Mutable.update(delst.back, {});
105107
return;
106108
end if;
107-
elt::lst := arrayGet(delst.front,1);
108-
arrayUpdate(delst.front, 1, lst);
109+
elt::lst := Mutable.access(delst.front);
110+
Mutable.update(delst.front, lst);
109111
end pop_front;
110112

111113
function currentBackCell
112114
input DoubleEndedList<T> delst;
113115
output list<T> last;
114116
algorithm
115-
last := arrayGet(delst.back,1);
117+
last := Mutable.access(delst.back);
116118
end currentBackCell;
117119

118120
function push_front
119121
input DoubleEndedList<T> delst;
120122
input T elt;
121123
protected
122-
Integer length=arrayGet(delst.length,1);
124+
Integer length=Mutable.access(delst.length);
123125
list<T> lst;
124126
algorithm
125-
arrayUpdate(delst.length, 1, length+1);
127+
Mutable.update(delst.length, length+1);
126128
if length==0 then
127129
lst := {elt};
128-
arrayUpdate(delst.front, 1, lst);
129-
arrayUpdate(delst.back, 1, lst);
130+
Mutable.update(delst.front, lst);
131+
Mutable.update(delst.back, lst);
130132
return;
131133
end if;
132-
lst := arrayGet(delst.front,1);
133-
arrayUpdate(delst.front, 1, elt::lst);
134+
lst := Mutable.access(delst.front);
135+
Mutable.update(delst.front, elt::lst);
134136
end push_front;
135137

136138
function push_list_front
137139
input DoubleEndedList<T> delst;
138140
input list<T> lst;
139141
protected
140-
Integer length=arrayGet(delst.length,1), lstLength;
142+
Integer length=Mutable.access(delst.length), lstLength;
141143
list<T> work, oldHead, tmp, head;
142144
T t;
143145
algorithm
144146
lstLength := listLength(lst);
145147
if lstLength==0 then
146148
return;
147149
end if;
148-
arrayUpdate(delst.length, 1, length+lstLength);
150+
Mutable.update(delst.length, length+lstLength);
149151
t::tmp := lst;
150152
head := {t};
151-
oldHead := arrayGet(delst.front, 1);
152-
arrayUpdate(delst.front, 1, head);
153+
oldHead := Mutable.access(delst.front);
154+
Mutable.update(delst.front, head);
153155
for l in tmp loop
154156
work := {l};
155157
Dangerous.listSetRest(head, work);
156158
head := work;
157159
end for;
158160
if length==0 then
159-
arrayUpdate(delst.back, 1, head);
161+
Mutable.update(delst.back, head);
160162
else
161163
Dangerous.listSetRest(head, oldHead);
162164
end if;
@@ -166,84 +168,84 @@ function push_back<T>
166168
input DoubleEndedList<T> delst;
167169
input T elt;
168170
protected
169-
Integer length=arrayGet(delst.length,1);
171+
Integer length=Mutable.access(delst.length);
170172
list<T> lst;
171173
algorithm
172-
arrayUpdate(delst.length, 1, length+1);
174+
Mutable.update(delst.length, length+1);
173175
if length==0 then
174176
lst := {elt};
175-
arrayUpdate(delst.front, 1, lst);
176-
arrayUpdate(delst.back, 1, lst);
177+
Mutable.update(delst.front, lst);
178+
Mutable.update(delst.back, lst);
177179
return;
178180
end if;
179181
lst := {elt};
180-
Dangerous.listSetRest(arrayGet(delst.back,1), lst);
181-
arrayUpdate(delst.back, 1, lst);
182+
Dangerous.listSetRest(Mutable.access(delst.back), lst);
183+
Mutable.update(delst.back, lst);
182184
end push_back;
183185

184186
function push_list_back
185187
input DoubleEndedList<T> delst;
186188
input list<T> lst;
187189
protected
188-
Integer length=arrayGet(delst.length,1), lstLength;
190+
Integer length=Mutable.access(delst.length), lstLength;
189191
list<T> tail, tmp;
190192
T t;
191193
algorithm
192194
lstLength := listLength(lst);
193195
if lstLength==0 then
194196
return;
195197
end if;
196-
arrayUpdate(delst.length, 1, length+lstLength);
198+
Mutable.update(delst.length, length+lstLength);
197199
t := listGet(lst, 1);
198200
tmp := {t};
199201
if length==0 then
200-
arrayUpdate(delst.front, 1, tmp);
202+
Mutable.update(delst.front, tmp);
201203
else
202-
Dangerous.listSetRest(arrayGet(delst.back, 1), tmp);
204+
Dangerous.listSetRest(Mutable.access(delst.back), tmp);
203205
end if;
204206
tail := tmp;
205207
for l in listRest(lst) loop
206208
tmp := {l};
207209
Dangerous.listSetRest(tail, tmp);
208210
tail := tmp;
209211
end for;
210-
arrayUpdate(delst.back, 1, tail);
212+
Mutable.update(delst.back, tail);
211213
end push_list_back;
212214

213215
impure function toListAndClear
214216
input DoubleEndedList<T> delst;
215217
input list<T> prependToList={};
216218
output list<T> res;
217219
algorithm
218-
if arrayGet(delst.length,1)==0 then
220+
if Mutable.access(delst.length)==0 then
219221
res := prependToList;
220222
return;
221223
end if;
222-
res := arrayGet(delst.front,1);
224+
res := Mutable.access(delst.front);
223225
if not listEmpty(prependToList) then
224-
Dangerous.listSetRest(arrayGet(delst.back,1), prependToList);
226+
Dangerous.listSetRest(Mutable.access(delst.back), prependToList);
225227
end if;
226-
arrayUpdate(delst.back, 1, {});
227-
arrayUpdate(delst.front, 1, {});
228-
arrayUpdate(delst.length, 1, 0);
228+
Mutable.update(delst.back, {});
229+
Mutable.update(delst.front, {});
230+
Mutable.update(delst.length, 0);
229231
end toListAndClear;
230232

231233
impure function toListNoCopyNoClear "Returns the working list, which may be changed later on!"
232234
input DoubleEndedList<T> delst;
233235
output list<T> res;
234236
algorithm
235-
res := arrayGet(delst.front,1);
237+
res := Mutable.access(delst.front);
236238
end toListNoCopyNoClear;
237239

238240
impure function clear
239241
input DoubleEndedList<T> delst;
240242
protected
241243
list<T> lst;
242244
algorithm
243-
lst := arrayGet(delst.front,1);
244-
arrayUpdate(delst.back, 1, {});
245-
arrayUpdate(delst.front, 1, {});
246-
arrayUpdate(delst.length, 1, 0);
245+
lst := Mutable.access(delst.front);
246+
Mutable.update(delst.back, {});
247+
Mutable.update(delst.front, {});
248+
Mutable.update(delst.length, 0);
247249
for l in lst loop
248250
GC.free(l);
249251
end for;
@@ -259,7 +261,7 @@ impure function mapNoCopy_1<ArgT1>
259261
output T outElement;
260262
end MapFunc;
261263
protected
262-
list<T> lst=arrayGet(delst.front,1);
264+
list<T> lst=Mutable.access(delst.front);
263265
algorithm
264266
while not listEmpty(lst) loop
265267
Dangerous.listSetFirst(lst, inMapFunc(listGet(lst,1), inArg1));
@@ -277,7 +279,7 @@ impure function mapFoldNoCopy<ArgT1>
277279
end MapFunc;
278280
protected
279281
T element;
280-
list<T> lst=arrayGet(delst.front,1);
282+
list<T> lst=Mutable.access(delst.front);
281283
algorithm
282284
while not listEmpty(lst) loop
283285
(element,arg) := inMapFunc(listGet(lst,1), arg);

0 commit comments

Comments
 (0)