lua-users home
lua-l archive

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


Ken Rawlings <krawling@shooter.bluemarble.net> wrote:
> On Wed, 7 Jul 1999, Luiz Henrique de Figueiredo wrote:
> > Lua 3.2 (to be released in a couple of days) has a new built-in function
> > tinsert that does this. (You can try it now with 3.2 beta.)
> 
>    Thanks, tinsert and tremove are exactly what I was looking for. Looking
>    at the documentation, though, i'm a little concerned, given the
>    lua equivalent for tinsert. That source implies that tinsert copies
>    every object at a given position(after the insert) to that position + 1,
>    so that l[3] gets copied to l[4], making room for something at l[3]. 
>    Doesn't this make tinsert a O(n) operation, and thus prohibitive for
>    dynamic additions to large data sets?

I was thinking the same thing. 
Has anyone written their own list data type or list API?
Hmmm. If someone wrote a simple API like:

 list_new() returns listPointer
 list_insert(void *listPointer, lua_Object item)
 list_remove(listPointer, int index)
 list_at(listPointer, int index) returns lua_Object
 list_destroy(void *listPointer)

Then a simple lua list object would be easy to implement:

listProto = {
 listPointer = nil,
 clone = function (self) return clone(self):init() end,
 init  = function (self) self.listPointer = list_new() return self end,
 insertAt_ = function (self, o) list_insert(self.listPointer, o) end,
 removeAt_ = function (self, o) list_remove(self.listPointer, o) end,
 at_ = function (self, index) return list_at(self.listPointer, index) end,
 destroy = function (self) list_destroy(self.listPointer) end,
}

and tag listProto to call destroy before instances get garbage collected.


Steve