[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to get the prev/next element of table FAST?
- From: Steve Litt <slitt@...>
- Date: Sat, 15 Jan 2011 00:45:29 -0500
On Friday 14 January 2011 21:12:50 Xavier Wang wrote:
> hi all.
>
> In C, to get prev/next element of a array is easy: we just use
> pointer arithmetic to get the index of the element. and increase/decrease
> the index to get prev/next value in array.
>
> but in Lua, the table is a mixing of array and hash-table. so we can not
> get the corresponding key for the value. so, how can I made a FAST
> function just like table.getkey() ?
>
> local t = {'a', 'b', 'c', 'd', 'e'}
> print(table.getkey(t, 'b')) --> 2
>
> I did it by associate index with value, but I must maintain the table of
> index if I insert/remove something from the table. there are some good way
> to do this?
Doubly linked list? By the way, you're trying to get the previous key knowing
only the current value. That would be just as tough in an array, because you'd
need to iterate through the array just to find the value.
SteveT
Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt