lua-users home
lua-l archive

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

I was thinking of creating a coroutine, dump all keys onto the stack, then quick-sort the stack. The iterator will then pop a key and return it. I like your solution better though – it is a bit slower (n^2), but avoids creation of another thread.


From: [] On Behalf Of Sven Olsen
Sent: Friday, February 08, 2013 2:19 PM
To: Lua mailing list
Subject: Re: Deterministic table iteration



I can probably create my own sorted iterator spairs() that will pre-sort the keys in some way. It can be smart to handle multiple types, like a mix of strings and numbers, even some types of user data if those can be ordered deterministically. It will not be very fast but I don't expect to use it for more than a few dozen items.

In fact if the number of items is quite small I can do the sorting directly in the the iterator's stack, and not even bother creating a separate table to hold the order.


Yes, this is a familiar train of thought :)  I posted C-code for such a non-allocating spairs a few months back, under the list post "adventures with iterators".