M2 icon indicating copy to clipboard operation
M2 copied to clipboard

growth of mutable lists

Open DanGrayson opened this issue 8 years ago • 0 comments

It looks like mutable lists grow their length by only one at a time. Perhaps the length should double when it needs to increase. Or just warn users to amortize the cost themselves.

    i36 : x = new MutableList 

    o36 = MutableList{}

    o36 : MutableList

    i37 : x#4=1

    o37 = 1

    i38 : peek x

    o38 = MutableList{null, null, null, null, 1}

    i39 : x#5=1

    o39 = 1

    i40 : peek x

    o40 = MutableList{null, null, null, null, 1, 1}

from evaluate.d:

assignvector(m:List,i:Code,rhs:Code):Expr := (
     x := m.v;
     ival := eval(i);
     when ival
     is j:ZZcell do (
	  if isInt(j)
	  then (
	       k := toInt(j);
	       if k < 0 then k = k + length(x);
	       if k < 0 then return printErrorMessageE(i,"negative subscript out of bounds 0 .. "+tostring(length(x)-1));
	       val := eval(rhs);
	       when val is Error do return val else (
		    if k >= length(x) then (
			 x = new Sequence len k+1 do (
			      foreach t in x do provide t;
			      while true do provide nullE;
			      );
			 m.v = x; );
	       	    x.k = val;
	       	    val))
	  else printErrorMessageE(i,"expected small integer"))
     is Error do ival
     else printErrorMessageE(i,"index not an integer")
     );

DanGrayson avatar Jul 27 '17 14:07 DanGrayson