Saving values of object types

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

Saving values of object types

Dmitry Maluka
Hello!

Briefly,
In modern versions of Lua, host application works with Lua functions
(and, more generally, tables and threads) only via stack (am I right?).
Is there a way to store their values somewhere for future use?

More specifically,
I work on a computationally complex problem of constraint satisfaction
(specifically, it's a university timetabling problem). I'm going to use
Lua interface for constraints specifying.

E.g., a call like

        hard({Lecture, Lecture}, function(lec, lec2)
                                   return (lec1.time ~= lec2.time
                                        or lec1.room ~= lec2.room)
                                 end)

creates a hard constraint that forbids placing 2 different events of
type Lecture at the same room at the same time. It specifies a Lua
function that checks the constraint.

Such functions are called at each step of the search algorithm. Is
there a way to call them immediately, without retrieving from somewhere
every time?


Reply | Threaded
Open this post in threaded view
|

Re: Saving values of object types

Thomas Harning Jr.
On Sat, Mar 21, 2009 at 10:27 AM, Dmitry Maluka <[hidden email]> wrote:
> Hello!
>
> Briefly,
> In modern versions of Lua, host application works with Lua functions
> (and, more generally, tables and threads) only via stack (am I right?).
> Is there a way to store their values somewhere for future use?
>
> More specifically,
> I work on a computationally complex problem of constraint satisfaction
> (specifically, it's a university timetabling problem). I'm going to use
> Lua interface for constraints specifying.
>
> E.g., a call like
>
> Â Â Â Âhard({Lecture, Lecture}, function(lec, lec2)
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return (lec1.time ~= lec2.time
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âor lec1.room ~= lec2.room)
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â end)
>
> creates a hard constraint that forbids placing 2 different events of
> type Lecture at the same room at the same time. It specifies a Lua
> function that checks the constraint.
>
> Such functions are called at each step of the search algorithm. Is
> there a way to call them immediately, without retrieving from somewhere
> every time?
You can store them in the registry and pull them back using the
reference value retrieved.

/* STORE MECHANISM */
  /* Get or create function such that it's at top of stack */
 /* Get an integer reference into the registry table, any table is
valid, but the registry is a simple-to-access table for this */
 int reference = luaL_ref(L, LUA_REGISTRYINDEX);

/* GET FUNCTION */
 /* Get the value from the registry table to call */
 lua_rawgeti(L, LUA_REGISTRYINDEX, reference);
 /* Operate on the value */

/* REMOVE MECHANISM */
 /* Remove the value from the registry and mark the reference as re-usable */
luaL_unref(L, LUA_REGISTRYINDEX, reference);

Documentation on Registry Table:
  http://www.lua.org/manual/5.1/manual.html#3.5
luaL_ref : http://www.lua.org/manual/5.1/manual.html#luaL_ref
-- 
Thomas Harning Jr.


Reply | Threaded
Open this post in threaded view
|

Re: Saving values of object types

Dmitry Maluka
Thanks a lot!


Reply | Threaded
Open this post in threaded view
|

Re: Saving values of object types

Asko Kauppi

You can also use a unique pointer (as lightuserdata) as the key to registry table. Matter of taste, which one is more convenient.

-asko


Dmitry Maluka kirjoitti 22.3.2009 kello 22:28:

Thanks a lot!