Skip to content

Commit 0deeea0

Browse files
committed
Update mutable list reallocation
This fixes an old issue from bugs/dan, so we remove it
1 parent 8ea2e61 commit 0deeea0

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

M2/Macaulay2/d/actors5.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ remove(x:List,i:int):Expr:= (
617617
if i < 0 then i = n + i;
618618
ret := x.v.i;
619619
for j from i to n - 2 do x.v.j = x.v.(j + 1);
620-
Ccode(void, x.v, "->len = ", n - 1);
620+
changeLength(x, n - 1);
621621
ret));
622622

623623
removefun(e:Expr):Expr := (

M2/Macaulay2/d/evaluate.d

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ export storeInDictionary(dc:DictionaryClosure,i:Code,rhs:Code):Expr := (
105105
is Error do ival
106106
else printErrorMessageE(i,"expected a string"));
107107

108+
export changeLength(x:List, newlen:int):void := (
109+
newcapacity := max(4, x.capacity);
110+
while newcapacity < newlen do newcapacity = 2 * newcapacity;
111+
while newcapacity > max(4, 4 * newlen) do newcapacity = newcapacity / 2;
112+
if newcapacity != x.capacity then (
113+
x.v = new Sequence len newcapacity do (
114+
foreach y in x.v do provide y;
115+
while true do provide nullE);
116+
x.capacity = newcapacity);
117+
Ccode(void, x.v, "->len = ", newlen));
118+
108119
assignvector(m:List,i:Code,rhs:Code):Expr := (
109120
x := m.v;
110121
ival := eval(i);
@@ -117,13 +128,8 @@ assignvector(m:List,i:Code,rhs:Code):Expr := (
117128
if k < 0 then return printErrorMessageE(i,"negative subscript out of bounds 0 .. "+tostring(length(x)-1));
118129
val := eval(rhs);
119130
when val is Error do return val else (
120-
if k >= length(x) then (
121-
x = new Sequence len k+1 do (
122-
foreach t in x do provide t;
123-
while true do provide nullE;
124-
);
125-
m.v = x; );
126-
x.k = val;
131+
if k >= length(x) then changeLength(m, k + 1);
132+
m.v.k = val;
127133
val))
128134
else printErrorMessageE(i,"expected small integer"))
129135
is Error do ival

bugs/dan/0-mutable-lists

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)