10 destructor for transfer function
11 @tparam a.tf ctx transfer function userdata
14 int liba_tf_die(lua_State
*L
)
16 a_tf
*const ctx
= (a_tf
*)lua_touserdata(L
, 1);
19 lua_alloc(L
, ctx
->num_p
, 0);
23 lua_alloc(L
, ctx
->den_p
, 0);
32 constructor for transfer function
33 @tparam table num numerator table
34 @tparam table den denominator table
35 @treturn a.tf transfer function userdata
38 int liba_tf_new(lua_State
*L
)
40 if (lua_gettop(L
) > 1)
42 luaL_checktype(L
, 1, LUA_TTABLE
);
43 luaL_checktype(L
, 2, LUA_TTABLE
);
44 unsigned int const num_n
= (unsigned int)lua_rawlen(L
, 1);
45 a_float
*const num_p
= (a_float
*)lua_alloc(L
, NULL
, sizeof(a_float
) * num_n
* 2);
46 lua_array_num_get(L
, 1, num_p
, num_n
);
47 unsigned int const den_n
= (unsigned int)lua_rawlen(L
, 2);
48 a_float
*const den_p
= (a_float
*)lua_alloc(L
, NULL
, sizeof(a_float
) * den_n
* 2);
49 lua_array_num_get(L
, 2, den_p
, den_n
);
50 a_tf
*const ctx
= lua_newclass(L
, a_tf
);
51 lua_registry_get(L
, liba_tf_new
);
52 lua_setmetatable(L
, -2);
53 a_tf_init(ctx
, num_n
, num_p
, num_p
+ num_n
, den_n
, den_p
, den_p
+ den_n
);
60 initialize for transfer function
61 @tparam a.tf ctx transfer function userdata
62 @tparam table num numerator table
63 @tparam table den denominator table
64 @treturn a.tf transfer function userdata
67 int liba_tf_init(lua_State
*L
)
69 if (lua_gettop(L
) > 2)
71 luaL_checktype(L
, 1, LUA_TUSERDATA
);
72 luaL_checktype(L
, 2, LUA_TTABLE
);
73 luaL_checktype(L
, 3, LUA_TTABLE
);
74 a_tf
*const ctx
= (a_tf
*)lua_touserdata(L
, 1);
75 unsigned int const num_n
= (unsigned int)lua_rawlen(L
, 2);
76 a_float
*const num_p
= (a_float
*)lua_alloc(L
, ctx
->num_p
, sizeof(a_float
) * num_n
* 2);
77 lua_array_num_get(L
, 2, num_p
, num_n
);
78 unsigned int const den_n
= (unsigned int)lua_rawlen(L
, 3);
79 a_float
*const den_p
= (a_float
*)lua_alloc(L
, ctx
->den_p
, sizeof(a_float
) * den_n
* 2);
80 lua_array_num_get(L
, 3, den_p
, den_n
);
81 a_tf_init(ctx
, num_n
, num_p
, num_p
+ num_n
, den_n
, den_p
, den_p
+ den_n
);
89 calculate for transfer function
90 @tparam a.tf ctx transfer function userdata
91 @tparam number x input
92 @treturn number output
95 int liba_tf_iter(lua_State
*L
)
97 a_tf
*const ctx
= (a_tf
*)lua_touserdata(L
, 1);
100 a_float x
= (a_float
)luaL_checknumber(L
, 2);
101 lua_pushnumber(L
, (lua_Number
)a_tf_iter(ctx
, x
));
108 zeroing for transfer function
109 @tparam a.tf ctx transfer function userdata
110 @treturn a.tf transfer function userdata
113 int liba_tf_zero(lua_State
*L
)
115 a_tf
*const ctx
= (a_tf
*)lua_touserdata(L
, 1);
124 static int liba_tf_set(lua_State
*L
)
126 a_tf
*const ctx
= (a_tf
*)lua_touserdata(L
, 1);
127 switch ((a_u32
)a_hash_bkdr(lua_tostring(L
, 2), 0))
129 case 0x001D0A2A: // num
131 luaL_checktype(L
, 3, LUA_TTABLE
);
132 unsigned int const num_n
= (unsigned int)lua_rawlen(L
, 3);
133 a_float
*const num_p
= (a_float
*)lua_alloc(L
, ctx
->num_p
, sizeof(a_float
) * num_n
* 2);
134 lua_array_num_get(L
, 3, num_p
, num_n
);
135 a_tf_set_num(ctx
, num_n
, num_p
, num_p
+ num_n
);
138 case 0x001A63A1: // den
140 luaL_checktype(L
, 3, LUA_TTABLE
);
141 unsigned int const den_n
= (unsigned int)lua_rawlen(L
, 3);
142 a_float
*const den_p
= (a_float
*)lua_alloc(L
, ctx
->den_p
, sizeof(a_float
) * den_n
* 2);
143 lua_array_num_get(L
, 3, den_p
, den_n
);
144 a_tf_set_den(ctx
, den_n
, den_p
, den_p
+ den_n
);
147 case 0xE8859EEB: // __name
148 case 0xE70C48C6: // __call
149 case 0xA65758B2: // __index
150 case 0xAEB551C6: // __newindex
153 lua_getmetatable(L
, 1);
160 static int liba_tf_get(lua_State
*L
)
162 a_tf
const *const ctx
= (a_tf
const *)lua_touserdata(L
, 1);
163 switch ((a_u32
)a_hash_bkdr(lua_tostring(L
, 2), 0))
165 case 0x001D0A2A: // num
166 lua_array_num_new(L
, ctx
->num_p
, ctx
->num_n
);
168 case 0x001A63A1: // den
169 lua_array_num_new(L
, ctx
->den_p
, ctx
->den_n
);
171 case 0x41FAB016: // input
172 lua_array_num_new(L
, ctx
->input
, ctx
->num_n
);
174 case 0x23C9C461: // output
175 lua_array_num_new(L
, ctx
->output
, ctx
->den_n
);
177 case 0xA65758B2: // __index
178 lua_registry_get(L
, liba_tf_new
);
179 lua_pushstring(L
, "num");
180 lua_array_num_new(L
, ctx
->num_p
, ctx
->num_n
);
182 lua_pushstring(L
, "den");
183 lua_array_num_new(L
, ctx
->den_p
, ctx
->den_n
);
185 lua_pushstring(L
, "input");
186 lua_array_num_new(L
, ctx
->input
, ctx
->num_n
);
188 lua_pushstring(L
, "output");
189 lua_array_num_new(L
, ctx
->output
, ctx
->den_n
);
193 lua_getmetatable(L
, 1);
200 static int liba_tf_(lua_State
*L
)
202 lua_pushcfunction(L
, liba_tf_new
);
204 lua_call(L
, lua_gettop(L
) - 1, 1);
208 int luaopen_liba_tf(lua_State
*L
)
210 static lua_fun
const funcs
[] = {
211 {"init", liba_tf_init
},
212 {"iter", liba_tf_iter
},
213 {"zero", liba_tf_zero
},
214 {"new", liba_tf_new
},
215 {"die", liba_tf_die
},
217 lua_createtable(L
, 0, A_LEN(funcs
));
218 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
219 lua_createtable(L
, 0, 1);
220 lua_fun_set(L
, -1, "__call", liba_tf_
);
221 lua_setmetatable(L
, -2);
223 static lua_fun
const metas
[] = {
224 {"__newindex", liba_tf_set
},
225 {"__index", liba_tf_get
},
226 #if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM > 503)
227 {"__close", liba_tf_die
},
228 #endif /* LUA_VERSION_NUM */
229 {"__call", liba_tf_iter
},
230 {"__gc", liba_tf_die
},
232 lua_createtable(L
, 0, A_LEN(metas
) + A_LEN(funcs
) + 1);
233 lua_fun_reg(L
, -1, metas
, A_LEN(metas
));
234 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
235 lua_str_set(L
, -1, "__name", "a.tf");
236 lua_registry_set(L
, liba_tf_new
);