lua-users home
lua-l archive

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


Adam Strzelecki wrote:
> Mike Pall wrote:
> > Use a variable-length struct (VLS), with the array as the last
> > element. Store the dimensions in the struct elements.
> 
> Great idea. Maybe you know some trick also to copy just a
> reference to last VLS field into new VLS?

That doesn't work. You need to use two different struct types.

> And provide method matrix:row(y) so for { 0, {dimx, dimy}, data }
> it will return { y * dimx, {dimx, 1}, data } with updated
> offset but same data, instead doing full memory a copying.

You can drop the offset field and just shift up the pointer to the
data instead.

Note that e.g. Matrix and MatrixView may still share the same
metamethods, except for __gc. But you have to keep the original
struct alive, independently of the views.

However don't use that for simple two-dimensional indexing, as the
intermediate objects are too complex to be eliminated.

> Luiz Henrique wrote:
> > How about using __call and writing m(i,j)?
> 
> Good question. Gonna try that too. Wonder it this call will be
> somehow inlined by LuaJIT or we need a table lookup per each
> call?

That's why the ctype metatable and the __index table must not be
modified. The compiler considers all ctype (meta)methods as
immutable, which allows for zero-cost dispatch.

--Mike