lua-users home
lua-l archive

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


2011/4/18 Adam Strzelecki <ono@java.pl>:
> Mike Pall wrote:
> Great idea. Maybe you know some trick also to copy just a reference to last VLS field into new VLS?
>
> My idea was to make:
> struct {
>   size_t offset;
>   size_t dimx, dimy;
>   double data[];
> }
>
> 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.
>
> My previous idea was to wrap make Matrix object to be a table with { size = { dimx, dimy ... }, data = <cdata>, offset = 0 }, so when I want to return the matrix row y for example I just make a new table { size = { dimx }, data = <same cdata>, offset = y * dimy } so both objects share same cdata.
>
> Of course there's a problem if you want to take out the column, then you need to do copying, but it doesn't matter so much.

The same thing you are talking about is used by GSL to represent
matrix or vector, I've adopted it for my FFI based matrix
implementation. One minor problem is that you cannot return a 'view'
to another matrix because a matrix of a view are both cdata and a
cdata cannot have a reference to another object so the GC could
dispose the underlying data when the view object is still alive. This
is why in my current implementation m[i] return a copy of the row. The
previous implementation was returning a real 'view', by reference, by
using userdata and setfenv to set a reference to the parent object.

-- 
Francesco