lua-users home
lua-l archive

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


On Tue, 23 Mar 2004 20:37:25 +0000 (UTC), Phil Bewig wrote:

> Perhaps you are suggesting that I should implement linked lists in
> lua natively rather than in c, and indeed I have seen libraries for
> that, but that seems expensive to me compared to the c
> implementation.

If you're worried about the cost of doing something like:

t = foo.next

Then using ANY scripting language isn't going to be an option.  It's 
generally well accepted that a non-native-compiled language is going 
to be at least an order of magnitude slower than going native.

> pointer reference. In lua, getting the next item in a list means
> computing a hash using arithmetic, indexing into the array that
> holds the hash table, then chasing pointers down a linked list,
> comparing keys until you find the one you want.

Off the top of my head:

function apply( l, f )
  while l ~= nil do
      f(l)
      l = l.next
   end
end

That's similar to the C code you'd expect, and with a bit more power 
because of closures.

That said, I'm not sure if your linked list library was really 
intended to be "useful" as much as it was intended as a learning 
vehicle.  I can't imagine anyone using a linked list library in C 
though -- it's basically trying to usurp a lot of the core Lua 
internals for no appreciable gain.

Brian