multidimensional data with user defined type

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

multidimensional data with user defined type

Philipp Kraus
Hello,

I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
data[i][j][n] = 123

The data can be any type (most numerical types). Also I have got std::maps with strings and cubes eg std::map <std::string, cube<doube,3> >
Is there a solution to get access to the data in LUA without copying? I would like to modify the data from the LUA script direct in the memory,
because the datablocks are very large.

I have found the user-defined type (pointer to my memory), but I have tested it with array (std::vector) only, so in my case I have complex data
structures, eg I would like to modify my std::map direct from LUA.

Hope for some ideas :-)
Thanks

Phil


Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Tim Hill
Qualified yes, depending on what you mean by "directly".

Lua cannot access userdata items directly (that is by design, its not supposed to!). However, you can write your own C/C++ functions and wire them into Lua, then call these functions from Lua, passing the userdata.

--Tim

On Jun 9, 2013, at 4:27 AM, Philipp Kraus <[hidden email]> wrote:

> Hello,
>
> I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
> data[i][j][n] = 123
>
> The data can be any type (most numerical types). Also I have got std::maps with strings and cubes eg std::map <std::string, cube<doube,3> >
> Is there a solution to get access to the data in LUA without copying? I would like to modify the data from the LUA script direct in the memory,
> because the datablocks are very large.
>
> I have found the user-defined type (pointer to my memory), but I have tested it with array (std::vector) only, so in my case I have complex data
> structures, eg I would like to modify my std::map direct from LUA.
>
> Hope for some ideas :-)
> Thanks
>
> Phil
>
>


Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Philipp Kraus
Hi Tim,

nice answer :-P
My first try is that I copy value-by-value into LUA and update my object after LUA has been finished. This very slow, so I have seen the user-defined type, if i understand it correct, it is a "native" pointer in LUA.
For arrays (continous memory block) I can use the pointer directly ( http://www.lua.org/pil/28.1.html ).
My question is, in which way would you create access to a std::map ?
I should be nice, that I can use a std::map in LUA like luamap[key] = value and I would like to create a C++ template for create a translation structure for any typ of map into LUA.
Would you create for each typo a structure like shown in the chapter 28.1?

Thanks

Phil

Am 09.06.2013 um 14:56 schrieb Tim Hill:

Qualified yes, depending on what you mean by "directly".

Lua cannot access userdata items directly (that is by design, its not supposed to!). However, you can write your own C/C++ functions and wire them into Lua, then call these functions from Lua, passing the userdata.

--Tim

On Jun 9, 2013, at 4:27 AM, Philipp Kraus <[hidden email]> wrote:

Hello,

I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
data[i][j][n] = 123

The data can be any type (most numerical types). Also I have got std::maps with strings and cubes eg std::map <std::string, cube<doube,3> >
Is there a solution to get access to the data in LUA without copying? I would like to modify the data from the LUA script direct in the memory,
because the datablocks are very large.

I have found the user-defined type (pointer to my memory), but I have tested it with array (std::vector) only, so in my case I have complex data
structures, eg I would like to modify my std::map direct from LUA.

Hope for some ideas :-)
Thanks

Phil





Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Dirk Laurie-2
In reply to this post by Philipp Kraus
2013/6/9 Philipp Kraus <[hidden email]>:

> I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
> data[i][j][n] = 123
>

Take a look at Francesco Abbate's GSL-shell.

http://www.nongnu.org/gsl-shell

Only downside is that it's LuaJIT-only.

Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Philipp Kraus

Am 09.06.2013 um 16:11 schrieb Dirk Laurie:

2013/6/9 Philipp Kraus <[hidden email]>:

I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
data[i][j][n] = 123


Take a look at Francesco Abbate's GSL-shell.

http://www.nongnu.org/gsl-shell

Only downside is that it's LuaJIT-only.

I don't understand this answer, because I don't write anything about numerical algorithms. If I would use this, I can also use Terra (http://terralang.org/).
My question is / was, how can I get access to any defined multidimensional data structur from LUA?I can also have got a structure like cube<10, std::map<std::string, std::vector> >

Phil
Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Pierre-Yves Gérardy
In reply to this post by Tim Hill
On Sun, Jun 9, 2013 at 2:56 PM, Tim Hill <[hidden email]> wrote:
> Lua cannot access userdata items directly (that is by design, its not supposed to!). However, you can write your own C/C++ functions and wire them into Lua, then call these functions from Lua, passing the userdata.
>
> On Jun 9, 2013, at 4:27 AM, Philipp Kraus <[hidden email]> wrote:
>> ...
>> I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
>> data[i][j][n] = 123

To complement Tim's answer, you'll be better off using a
`data:getElement(i, j, n)` method rather than successive
`__index(self, i)` calls, which would require some trickery.

-- Pierre-Yves

Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Philipp Kraus

Am 09.06.2013 um 18:26 schrieb Pierre-Yves Gérardy:

> On Sun, Jun 9, 2013 at 2:56 PM, Tim Hill <[hidden email]> wrote:
>> Lua cannot access userdata items directly (that is by design, its not supposed to!). However, you can write your own C/C++ functions and wire them into Lua, then call these functions from Lua, passing the userdata.
>>
>> On Jun 9, 2013, at 4:27 AM, Philipp Kraus <[hidden email]> wrote:
>>> ...
>>> I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
>>> data[i][j][n] = 123
>
> To complement Tim's answer, you'll be better off using a
> `data:getElement(i, j, n)` method rather than successive
> `__index(self, i)` calls, which would require some trickery.


Why is a getElement call bett than a __index call ?

Phil
Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Philipp Janda
Am 09.06.2013 18:52 schröbte Philipp Kraus:

>
> Am 09.06.2013 um 18:26 schrieb Pierre-Yves Gérardy:
>
>> On Sun, Jun 9, 2013 at 2:56 PM, Tim Hill <[hidden email]> wrote:
>>> Lua cannot access userdata items directly (that is by design, its not supposed to!). However, you can write your own C/C++ functions and wire them into Lua, then call these functions from Lua, passing the userdata.
>>>
>>> On Jun 9, 2013, at 4:27 AM, Philipp Kraus <[hidden email]> wrote:
>>>> ...
>>>> I have got multidimensional cubes in C++ and would like to push them into LUA. I would like to create a structure in LUA like
>>>> data[i][j][n] = 123
>>
>> To complement Tim's answer, you'll be better off using a
>> `data:getElement(i, j, n)` method rather than successive
>> `__index(self, i)` calls, which would require some trickery.
>
>
> Why is a getElement call bett than a __index call ?

__(new)index can only handle one dimension (without creating temporary
objects) ...

>
> Phil
>

Philipp



Reply | Threaded
Open this post in threaded view
|

Re: multidimensional data with user defined type

Choonster TheMage
I wrote a fairly basic example of map<string,double> objects being
manipulated in Lua here:
https://github.com/Choonster/lua_cpp_example/blob/master/lua_cpp_example/lua_cpp_example.cpp

It's not multidimensional, but hopefully it demonstrates how to use
C++ objects with Lua.

Note: This is my first attempt at C++ (I have a bit of experience with
C), so please tell me if I've done anything stupid.