remove the const modifier from function arguments
[liba.git] / lua / src / tf.c
blob75ace9534c850881faaa1be1492f16503eb51bda
1 /***
2 transfer function
3 @module liba.tf
4 */
6 #include "tf.h"
7 #include "a/tf.h"
9 /***
10 destructor for transfer function
11 @tparam a.tf ctx transfer function userdata
12 @function die
14 int liba_tf_die(lua_State *L)
16 a_tf *const ctx = (a_tf *)lua_touserdata(L, 1);
17 if (ctx)
19 lua_alloc(L, ctx->num_p, 0);
20 ctx->num_p = 0;
21 ctx->num_n = 0;
22 ctx->input = 0;
23 lua_alloc(L, ctx->den_p, 0);
24 ctx->den_p = 0;
25 ctx->den_n = 0;
26 ctx->output = 0;
28 return 0;
31 /***
32 constructor for transfer function
33 @tparam table num numerator table
34 @tparam table den denominator table
35 @treturn a.tf transfer function userdata
36 @function new
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);
54 return 1;
56 return 0;
59 /***
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
65 @function init
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);
82 lua_pushvalue(L, 1);
83 return 1;
85 return 0;
88 /***
89 calculate for transfer function
90 @tparam a.tf ctx transfer function userdata
91 @tparam number x input
92 @treturn number output
93 @function iter
95 int liba_tf_iter(lua_State *L)
97 a_tf *const ctx = (a_tf *)lua_touserdata(L, 1);
98 if (ctx)
100 a_float x = (a_float)luaL_checknumber(L, 2);
101 lua_pushnumber(L, (lua_Number)a_tf_iter(ctx, x));
102 return 1;
104 return 0;
107 /***
108 zeroing for transfer function
109 @tparam a.tf ctx transfer function userdata
110 @treturn a.tf transfer function userdata
111 @function zero
113 int liba_tf_zero(lua_State *L)
115 a_tf *const ctx = (a_tf *)lua_touserdata(L, 1);
116 if (ctx)
118 a_tf_zero(ctx);
119 return 1;
121 return 0;
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);
136 break;
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);
145 break;
147 case 0xE8859EEB: // __name
148 case 0xE70C48C6: // __call
149 case 0xA65758B2: // __index
150 case 0xAEB551C6: // __newindex
151 break;
152 default:
153 lua_getmetatable(L, 1);
154 lua_replace(L, 1);
155 lua_rawset(L, 1);
157 return 0;
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);
167 break;
168 case 0x001A63A1: // den
169 lua_array_num_new(L, ctx->den_p, ctx->den_n);
170 break;
171 case 0x41FAB016: // input
172 lua_array_num_new(L, ctx->input, ctx->num_n);
173 break;
174 case 0x23C9C461: // output
175 lua_array_num_new(L, ctx->output, ctx->den_n);
176 break;
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);
181 lua_rawset(L, -3);
182 lua_pushstring(L, "den");
183 lua_array_num_new(L, ctx->den_p, ctx->den_n);
184 lua_rawset(L, -3);
185 lua_pushstring(L, "input");
186 lua_array_num_new(L, ctx->input, ctx->num_n);
187 lua_rawset(L, -3);
188 lua_pushstring(L, "output");
189 lua_array_num_new(L, ctx->output, ctx->den_n);
190 lua_rawset(L, -3);
191 break;
192 default:
193 lua_getmetatable(L, 1);
194 lua_replace(L, 1);
195 lua_rawget(L, 1);
197 return 1;
200 static int liba_tf_(lua_State *L)
202 lua_pushcfunction(L, liba_tf_new);
203 lua_replace(L, 1);
204 lua_call(L, lua_gettop(L) - 1, 1);
205 return 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);
238 return 1;