lua-users home
lua-l archive

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


> But the "n" is not the size of the table. It's a key in a
> particular type of
> table which is used to provide the functionality of vectors.



The computation time saved by the usage of this .n field in my programs is surely much less than the time that it took me to
discover some very-hard-to-find bugs caused by this field.

I believe that everyone would agree that it would be better if this .n field wasn't there. (If I remember correctly, the only
purpose of a .n field was to speed up the insertion operation, wasn't it?)

I don't know too much about garbage collection issues, but a nearly perfect solution would be if these 3 functions could share a
table. Something like this:

N_TABLE = {}
function getn(t)
	if (N_TABLE[t] == nil) then
		N_TABLE[t] = count_elements(t)
	end
	return N_TABLE[t]
end

and equivalent for tinsert and tremove.


Possibly, to ease flexibility, another function might have to be introduced: setn(t)

The question is: none of the tables stored in the N_TABLE could be collected, could them?