release 0.1.13
[liba.git] / lua / src / a.c
bloba19855964d55992339effcc0254b4789794b0fdd
1 /***
2 algorithm library
3 @module liba
4 */
6 #include "a.h"
8 /***
9 a hash function whose prime number is 131
10 @tparam string str string to be processed
11 @tparam[opt] integer initial value
12 @treturn string hash value
13 @function hash_bkdr
15 static int liba_hash_bkdr(lua_State *L)
17 a_u32 val = 0;
18 int top = lua_gettop(L);
19 if (top > 1) { val = lua_u32_get(L, 2); }
20 if (top > 0) { val = a_hash_bkdr(luaL_checklstring(L, 1, A_NULL), val); }
21 lua_u32_new(L, val);
22 return 1;
25 /***
26 a hash function whose prime number is 65599
27 @tparam string str string to be processed
28 @tparam[opt] integer initial value
29 @treturn string hash value
30 @function hash_sdbm
32 static int liba_hash_sdbm(lua_State *L)
34 a_u32 val = 0;
35 int top = lua_gettop(L);
36 if (top > 1) { val = lua_u32_get(L, 2); }
37 if (top > 0) { val = a_hash_sdbm(luaL_checklstring(L, 1, A_NULL), val); }
38 lua_u32_new(L, val);
39 return 1;
42 #include "a/math.h"
44 /***
45 square root of an unsigned integer
46 @tparam integer ... independent variables
47 @treturn integer calculated result
48 @function isqrt
50 static int liba_isqrt(lua_State *L)
52 int const Ln = lua_gettop(L);
53 for (int Li = 1; Li <= Ln; ++Li)
55 lua_Integer x = luaL_checkinteger(L, Li);
56 #if A_SIZE_MAX == A_U32_MAX
57 x = (lua_Integer)a_u32_sqrt((a_u32)x);
58 #else /* !A_SIZE_MAX */
59 x = (lua_Integer)a_u64_sqrt((a_u64)x);
60 #endif /* A_SIZE_MAX */
61 lua_pushinteger(L, x);
63 return Ln;
66 /***
67 reciprocal of square-root
68 @tparam number ... independent variables
69 @treturn number calculated result
70 @function rsqrt
72 static int liba_rsqrt(lua_State *L)
74 int const Ln = lua_gettop(L);
75 for (int Li = 1; Li <= Ln; ++Li)
77 lua_Number x = luaL_checknumber(L, Li);
78 #if A_FLOAT_TYPE + 0 == A_FLOAT_DOUBLE
79 x = (lua_Number)a_f64_rsqrt((a_f64)x);
80 #elif A_FLOAT_TYPE + 0 == A_FLOAT_SINGLE
81 x = (lua_Number)a_f32_rsqrt((a_f32)x);
82 #else /* !A_FLOAT_TYPE */
83 x = (lua_Number)(1 / sqrt((double)x));
84 #endif /* A_FLOAT_TYPE */
85 lua_pushnumber(L, x);
87 return Ln;
90 #if !defined A_VERSION
91 #include "a/version.h"
92 #endif /* A_VERSION */
94 int luaopen_liba(lua_State *L)
96 luaL_checkversion(L);
97 /***
98 algorithm library
99 @field VERSION algorithm library version string
100 @table liba
102 static lua_fun const funcs[] = {
103 {"hash_bkdr", liba_hash_bkdr},
104 {"hash_sdbm", liba_hash_sdbm},
105 {"isqrt", liba_isqrt},
106 {"rsqrt", liba_rsqrt},
108 #if !defined LUA_VERSION_NUM || (LUA_VERSION_NUM <= 501)
109 lua_fun func = {NULL, NULL};
110 luaL_register(L, "liba", &func);
111 #else /* !LUA_VERSION_NUM */
112 lua_createtable(L, 0, A_LEN(funcs) + 1);
113 #endif /* LUA_VERSION_NUM */
114 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
115 lua_str_set(L, -1, "VERSION", A_VERSION);
117 lua_pushstring(L, "complex");
118 luaopen_liba_complex(L);
119 lua_rawset(L, -3);
121 lua_pushstring(L, "crc16");
122 luaopen_liba_crc16(L);
123 lua_rawset(L, -3);
125 lua_pushstring(L, "crc32");
126 luaopen_liba_crc32(L);
127 lua_rawset(L, -3);
129 lua_pushstring(L, "crc64");
130 luaopen_liba_crc64(L);
131 lua_rawset(L, -3);
133 lua_pushstring(L, "crc8");
134 luaopen_liba_crc8(L);
135 lua_rawset(L, -3);
137 lua_pushstring(L, "hpf");
138 luaopen_liba_hpf(L);
139 lua_rawset(L, -3);
141 lua_pushstring(L, "lpf");
142 luaopen_liba_lpf(L);
143 lua_rawset(L, -3);
145 lua_pushstring(L, "mf");
146 luaopen_liba_mf(L);
147 lua_rawset(L, -3);
149 lua_pushstring(L, "pid");
150 luaopen_liba_pid(L);
151 lua_rawset(L, -3);
153 lua_pushstring(L, "pid_fuzzy");
154 luaopen_liba_pid_fuzzy(L);
155 lua_rawset(L, -3);
157 lua_pushstring(L, "pid_neuro");
158 luaopen_liba_pid_neuro(L);
159 lua_rawset(L, -3);
161 lua_pushstring(L, "tf");
162 luaopen_liba_tf(L);
163 lua_rawset(L, -3);
165 lua_pushstring(L, "trajbell");
166 luaopen_liba_trajbell(L);
167 lua_rawset(L, -3);
169 lua_pushstring(L, "trajpoly3");
170 luaopen_liba_trajpoly3(L);
171 lua_rawset(L, -3);
173 lua_pushstring(L, "trajpoly5");
174 luaopen_liba_trajpoly5(L);
175 lua_rawset(L, -3);
177 lua_pushstring(L, "trajpoly7");
178 luaopen_liba_trajpoly7(L);
179 lua_rawset(L, -3);
181 lua_pushstring(L, "trajtrap");
182 luaopen_liba_trajtrap(L);
183 lua_rawset(L, -3);
185 lua_pushstring(L, "version");
186 luaopen_liba_version(L);
187 lua_rawset(L, -3);
189 return 1;