[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Low level table routines (Was: ipairs deprecated?)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 12 Mar 2013 22:06:35 +0200
2013/3/12 John Hind <john.hind@zen.co.uk>:
> Dirk Laurie and I had a go at re-writing the Table library, but
> eventually came to the conclusion that we ought to be working in Lua
> not C. Everything except Sort and Unpack can be done as well or even
> better in Lua and even Sort and Unpack might be better as a C core
> wrapped in Lua for handling optional parameters and the like.
We had all sorts of interesting ideas on how the table library could be
enhanced, inspired by posts on this list. Eventually we settled on the
principle that the C core routines should only do simple tasks that
can take advantage of the stack and of fast C loops, and do have
expolicit limits, not implicit (neither 1 nor #tbl). For example:
insert = function(tbl,pos,item)
-- All the business about #tbl and whether the arguments are valid; the
-- two-argument case is disposed of; implementation, if you want it, of
-- negative pos to mean counting backward as in the string library.
move(tbl,first,last,tofirst,tolast) -- C routine for efficiency
rawset(tbl,first,item)
end
If you want to generalize `insert` to do multiple inserts, i.e.
insert(tbl,pos,...), that is easy too.
The routine "move" needs four parameters because it can also do this:
reverse = function(tbl)
-- Identify first and last as before.
move(tbl,first,last,last,first)
end
It is not essential to use a separate Lua file: one can call Lua code
from the routine that opens the library via `luaL_dostring`.