lua-users home
lua-l archive

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


On Tue, Dec 14, 2010 at 3:39 AM, Dirk Laurie <dpl@sun.ac.za> 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?
>

You still need a way to change the table length if you want to use it
as a stack, and table.remove already provides it. That's the reason
for rule #2.

But yes, if you had a table.resize() function, you could use this to
change #t, and table.remove would leave the table length unchanged.
However, this would require even more changes to existing code.

- Keith