lua-users home
lua-l archive

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


Here is what i do (Lua 4 code):

-- foreach replacement that sorts the indizes
function foreachsorted(t, f)
	local indizes = tkeys(t)
	sort(indizes)
	foreachi(indizes, function(_, i) %f(i, %t[i]) end)
end

-- returns all keys of the given table as values (in an array-table)
function tkeys(t)
	local n=1
	local ret={}
	for i,v	in t do
		ret[n] = i
		n = n + 1
	end
	return ret
end

as you can see, i don't care about performance. if you do, you might want to
try something else ;-)

Cheers,
Peter

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Fabio Reis Cecin
> Sent: Friday, January 31, 2003 9:02 PM
> To: Multiple recipients of list
> Subject: Re: ordered numeric set vs. tinsert()
>
>
>
> D'oh!
> I've just realized that "pos" for tinsert() is actually the key value for
> <key,value> pair in the table and it doesn't have any correlation
> whatsoever with the internal ordering of tables (in regard to next())
>
> I've done some experiments creating numeric sets and using next()
> to traverse them, and the internal ordering of tables resembles a kind
> of tree or something, it's definately not the analogous of a
> STL vector<>
>
> Any better alternatives, to solve my specific problem, than
> 1 - use foreachi() or numeric for, or
> 2 - implement my own container in C and interface it to Lua (if things
> get slow)
>
> (I'm using Lua 4)
>
> thanks!
> -Fabio
>
> On 31 Jan 2003, at 16:32, Fabio Reis Cecin wrote:
> >
> > (....)
> > "tinsert()" asks for the position where I want the
> > element inserted. so I guess that what I need is
> > a function that does a binary search. (?)
>
> --
> []'s
> Fábio R. Cecin
> frcecin@terra.com.br