lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Dec 14, 2010 at 10:39:38AM +0200, Dirk Laurie wrote:
> On Tue, Dec 14, 2010 at 06:03:00AM +0200, Keith Matthews wrote:
> > Given a table t, #t would be defined by those two rules:
> > 
> > 1 - #t is the highest positive integer index to which a value was
> > assigned in t (including nil)
> > 
> > 2 - table.remove(t, i) decrements #t by 1 if i <= #t.
> > 
> I'd prefer just Rule 1.  That's the ideal way to implement sparse vectors.
> -- sparse vector of length 1000
> a={}; a[3]=3; a[6]=7; a[101]=101; a[1000]=nil
> 
> But now table.remove(a,3) makes #a=999?  What sort of logic is that?

It would make sense if sparse arrays were compacted too!
Right now this is the behaviour:

  Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
  > t = {}
  > t[1] = 1
  > t[2] = 2
  > t[10] = 10
  > t[100] = 100
  > table.remove(t,1)
  > =t[1]
  2
  > table.remove(t,10)
  > =t[10]
  10

Maybe the proposal assumes table remove to move t[10] to t[9] too.
And if you want to just add a hole, without compacting, you just 
do t[1] = nil. 

I like (my interpretation of) that proposal ;-)

Cheers
-- 
Enrico Tassi