lua-users home
lua-l archive

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


> Do you mean if I do this:
> 
>     tbl = {[2]=20,[3]=30,[4]=40}
>     -- now tbl is all hash
>     
>     tbl[1]=10
> 
> At the last step an array part big enough to hold 4 elements will be
> created, and all the pairs formerly in the hash part will be removed
> from there and copied over to the array part?

No. In this case the hash part is already big enough to hold the '1', so
no rehash happens. (The size of the hash part is always a power of 2, so
the size of tbl will be 4 in that example). I meant filling the array
one by one, as here:

   for x = n,1,-1 do tbl[x] = true end

When the loop start, tbl will be a hash to accomodate its few large
values. Near the middle of the loop the hash will have to grow and will
be converted to a real array (and all hash values are 'rehashed' into
the array part). For n=4, this will happen when you add a[2].

-- Roberto