liba 0.1.15

An algorithm library based on C/C++
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "a.h"

void *lua_alloc(lua_State *L, void const *ptr_, size_t siz)
{
    void *ud = (void *)(a_uptr)ptr_; // NOLINT
    void *ptr = (void *)(a_uptr)ptr_; // NOLINT
    return lua_getallocf(L, &ud)(ud, ptr, 0, siz);
}

void lua_registry_get(lua_State *L, int (*fn)(lua_State *))
{
    lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)(a_uptr)fn); // NOLINT
}

void lua_registry_set(lua_State *L, int (*fn)(lua_State *))
{
    lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)(a_uptr)fn); // NOLINT
}

void lua_fun_set(lua_State *L, int idx, char const *name, lua_CFunction func)
{
    /* table[name]=func */
    lua_pushstring(L, name);
    lua_pushcclosure(L, func, 0);
    lua_rawset(L, idx < 0 ? idx - 2 : idx);
}

void lua_fun_reg(lua_State *L, int idx, lua_fun const *tab, size_t len)
{
    for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
    {
        /* table[name]=func */
        lua_pushstring(L, tab->name);
        lua_pushcclosure(L, tab->func, 0);
        lua_rawset(L, idx);
    }
}

void lua_str_set(lua_State *L, int idx, char const *name, char const *data)
{
    /* table[name]=data */
    lua_pushstring(L, name);
    lua_pushstring(L, data);
    lua_rawset(L, idx < 0 ? idx - 2 : idx);
}

char const *lua_str_get(lua_State *L, int idx, char const *name)
{
    /* data=table[name] */
    lua_pushstring(L, name);
    lua_rawget(L, idx < 0 ? idx - 1 : idx);
    char const *s = lua_tostring(L, -1);
    lua_pop(L, 1);
    return s;
}

void lua_str_reg(lua_State *L, int idx, lua_str const *tab, size_t len)
{
    for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
    {
        /* table[name]=data */
        lua_pushstring(L, tab->name);
        lua_pushstring(L, tab->data);
        lua_rawset(L, idx);
    }
}

void lua_int_set(lua_State *L, int idx, char const *name, LUA_INT data)
{
    /* table[name]=data */
    lua_pushstring(L, name);
    lua_pushinteger(L, (lua_Integer)data);
    lua_rawset(L, idx < 0 ? idx - 2 : idx);
}

LUA_INT lua_int_get(lua_State *L, int idx, char const *name)
{
    /* data=table[name] */
    lua_pushstring(L, name);
    lua_rawget(L, idx < 0 ? idx - 1 : idx);
    LUA_INT x = (LUA_INT)lua_tointeger(L, -1);
    lua_pop(L, 1);
    return x;
}

void lua_int_reg(lua_State *L, int idx, lua_int const *tab, size_t len)
{
    for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
    {
        /* table[name]=data */
        lua_pushstring(L, tab->name);
        lua_pushinteger(L, (lua_Integer)tab->data);
        lua_rawset(L, idx);
    }
}

void lua_num_set(lua_State *L, int idx, char const *name, LUA_NUM data)
{
    /* table[name]=data */
    lua_pushstring(L, name);
    lua_pushnumber(L, (lua_Number)data);
    lua_rawset(L, idx < 0 ? idx - 2 : idx);
}

LUA_NUM lua_num_get(lua_State *L, int idx, char const *name)
{
    /* data=table[name] */
    lua_pushstring(L, name);
    lua_rawget(L, idx < 0 ? idx - 1 : idx);
    LUA_NUM x = (LUA_NUM)lua_tonumber(L, -1);
    lua_pop(L, 1);
    return x;
}

void lua_num_reg(lua_State *L, int idx, lua_num const *tab, size_t len)
{
    for (idx = idx < 0 ? idx - 2 : idx; len--; ++tab)
    {
        /* table[name]=data */
        lua_pushstring(L, tab->name);
        lua_pushnumber(L, (lua_Number)tab->data);
        lua_rawset(L, idx);
    }
}

void lua_array_str_get(lua_State *L, int idx, char const **ptr, unsigned int num)
{
    size_t len = (size_t)lua_rawlen(L, idx);
    if (num > len) { num = (unsigned int)len; }
    for (unsigned int i = num; i--; num = i)
    {
        lua_rawgeti(L, idx, (int)num);
        ptr[i] = lua_tostring(L, -1);
        lua_pop(L, 1);
    }
}

void lua_array_str_set(lua_State *L, int idx, char const *const *ptr, unsigned int num)
{
    idx = idx < 0 ? idx - 1 : idx;
    for (unsigned int i = num; i--; num = i)
    {
        lua_pushstring(L, ptr[i]);
        lua_rawseti(L, idx, (int)num);
    }
}

void lua_array_str_new(lua_State *L, char const *const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_pushstring(L, ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

void lua_array_int_get(lua_State *L, int idx, LUA_INT *ptr, unsigned int num)
{
    size_t len = (size_t)lua_rawlen(L, idx);
    if (num > len) { num = (unsigned int)len; }
    for (unsigned int i = num; i--; num = i)
    {
        lua_rawgeti(L, idx, (int)num);
        ptr[i] = (LUA_INT)lua_tointeger(L, -1);
        lua_pop(L, 1);
    }
}

void lua_array_int_set(lua_State *L, int idx, LUA_INT const *ptr, unsigned int num)
{
    idx = idx < 0 ? idx - 1 : idx;
    for (unsigned int i = num; i--; num = i)
    {
        lua_pushinteger(L, (lua_Integer)ptr[i]);
        lua_rawseti(L, idx, (int)num);
    }
}

void lua_array_int_new(lua_State *L, LUA_INT const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_pushinteger(L, (lua_Integer)ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

void lua_array_num_get(lua_State *L, int idx, LUA_NUM *ptr, unsigned int num)
{
    size_t len = (size_t)lua_rawlen(L, idx);
    if (num > len) { num = (unsigned int)len; }
    for (unsigned int i = num; i--; num = i)
    {
        lua_rawgeti(L, idx, (int)num);
        ptr[i] = (LUA_NUM)lua_tonumber(L, -1);
        lua_pop(L, 1);
    }
}

void lua_array_num_set(lua_State *L, int idx, LUA_NUM const *ptr, unsigned int num)
{
    idx = idx < 0 ? idx - 1 : idx;
    for (unsigned int i = num; i--; num = i)
    {
        lua_pushnumber(L, (lua_Number)ptr[i]);
        lua_rawseti(L, idx, (int)num);
    }
}

void lua_array_num_new(lua_State *L, LUA_NUM const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_pushnumber(L, (lua_Number)ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

size_t lua_table_num_len(lua_State *L, int idx);
size_t lua_table_num_len(lua_State *L, int idx) // NOLINT(misc-no-recursion)
{
    size_t num = 0;
    size_t n = (size_t)lua_rawlen(L, idx);
    for (unsigned int i = 0; i++ != n;)
    {
        lua_rawgeti(L, idx, (int)i);
        int e = lua_type(L, -1);
        if (e == LUA_TNUMBER) { ++num; }
        else if (e == LUA_TTABLE)
        {
            num += lua_table_num_len(L, -1);
        }
        lua_pop(L, 1);
    }
    return num;
}

LUA_NUM *lua_table_num_ptr(lua_State *L, int idx, LUA_NUM *ptr);
LUA_NUM *lua_table_num_ptr(lua_State *L, int idx, LUA_NUM *ptr) // NOLINT(misc-no-recursion)
{
    size_t n = (size_t)lua_rawlen(L, idx);
    for (unsigned int i = 0; i++ != n;)
    {
        lua_rawgeti(L, idx, (int)i);
        int e = lua_type(L, -1);
        if (e == LUA_TNUMBER)
        {
            *ptr++ = (LUA_NUM)lua_tonumber(L, -1);
        }
        else if (e == LUA_TTABLE)
        {
            ptr = lua_table_num_ptr(L, -1, ptr);
        }
        lua_pop(L, 1);
    }
    return ptr;
}

LUA_NUM *lua_table_num_get(lua_State *L, int idx, LUA_NUM const *ptr, size_t *num)
{
    LUA_NUM *ret = (LUA_NUM *)(intptr_t)ptr; // NOLINT(performance-no-int-to-ptr)
    if (lua_type(L, idx) == LUA_TTABLE)
    {
        size_t num_ = 0;
        num = num ? num : &num_;
        *num = lua_table_num_len(L, idx);
        if (*num)
        {
            ret = (LUA_NUM *)lua_alloc(L, ret, sizeof(LUA_NUM) * *num);
            lua_table_num_ptr(L, idx, ret);
        }
    }
    return ret;
}

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf sprintf_s
#endif /* _MSC_VER */

void lua_u8_new(lua_State *L, a_u8 value)
{
    lua_pushinteger(L, (lua_Integer)value);
}

a_u8 lua_u8_get(lua_State *L, int idx)
{
    a_u8 value = 0;
    switch (lua_type(L, idx))
    {
    default: A_FALLTHROUGH;
    case LUA_TNUMBER: value = (a_u8)luaL_checkinteger(L, idx); break;
    case LUA_TSTRING: value = (a_u8)strtoul(lua_tostring(L, idx), NULL, 0);
    }
    return value;
}

void lua_u16_new(lua_State *L, a_u16 value)
{
    lua_pushinteger(L, (lua_Integer)value);
}

a_u16 lua_u16_get(lua_State *L, int idx)
{
    a_u16 value = 0;
    switch (lua_type(L, idx))
    {
    default: A_FALLTHROUGH;
    case LUA_TNUMBER: value = (a_u16)luaL_checkinteger(L, idx); break;
    case LUA_TSTRING: value = (a_u16)strtoul(lua_tostring(L, idx), NULL, 0);
    }
    return value;
}

void lua_u32_new(lua_State *L, a_u32 value)
{
    char buf[8 + 3];
    (void)snprintf(buf, sizeof(buf), "0x%08" PRIX32, value);
    lua_pushstring(L, buf);
}

a_u32 lua_u32_get(lua_State *L, int idx)
{
    a_u32 value = 0;
    switch (lua_type(L, idx))
    {
    default: A_FALLTHROUGH;
    case LUA_TNUMBER: value = (a_u32)luaL_checkinteger(L, idx); break;
    case LUA_TSTRING: value = (a_u32)strtoul(lua_tostring(L, idx), NULL, 0);
    }
    return value;
}

void lua_u64_new(lua_State *L, a_u64 value)
{
    char buf[16 + 3];
    (void)snprintf(buf, sizeof(buf), "0x%016" PRIX64, value);
    lua_pushstring(L, buf);
}

a_u64 lua_u64_get(lua_State *L, int idx)
{
    a_u64 value = 0;
    switch (lua_type(L, idx))
    {
    default: A_FALLTHROUGH;
    case LUA_TNUMBER: value = (a_u64)luaL_checkinteger(L, idx); break;
#if ULONG_MAX > A_U32_MAX
    case LUA_TSTRING: value = (a_u64)strtoul(lua_tostring(L, idx), NULL, 0);
#else /* >long */
    case LUA_TSTRING: value = (a_u64)strtoull(lua_tostring(L, idx), NULL, 0);
#endif /* long */
    }
    return value;
}

void lua_array_u8_new(lua_State *L, a_u8 const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_u8_new(L, ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

void lua_array_u16_new(lua_State *L, a_u16 const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_u16_new(L, ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

void lua_array_u32_new(lua_State *L, a_u32 const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_u32_new(L, ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

void lua_array_u64_new(lua_State *L, a_u64 const *ptr, unsigned int num)
{
    lua_createtable(L, (int)num, 0);
    for (unsigned int i = num; i--; num = i)
    {
        lua_u64_new(L, ptr[i]);
        lua_rawseti(L, -2, (int)num);
    }
}

#if defined(LUA_STACK)
void lua_stack_view(lua_State *L, unsigned int line)
{
    int const n = lua_gettop(L);
    printf("%u:", line);
    for (int i = 0; i++ != n;)
    {
        putchar(' ');
        switch (lua_type(L, i))
        {
        case LUA_TBOOLEAN:
            printf("%s", lua_toboolean(L, i) ? "true" : "false");
            break;
        case LUA_TLIGHTUSERDATA:
            printf("ptr:%p", lua_topointer(L, i));
            break;
        case LUA_TNUMBER:
            printf("%g", lua_tonumber(L, i));
            break;
        case LUA_TSTRING:
            printf("\"%s\"", lua_tostring(L, i));
            break;
        case LUA_TTABLE:
            printf("tab:%p", lua_topointer(L, i));
            break;
        case LUA_TFUNCTION:
        {
            union
            {
                int (*fn)(lua_State *);
                void *p;
            } func = {lua_tocfunction(L, i)};
            printf("func:%p", func.p);
            break;
        }
        case LUA_TUSERDATA:
            printf("data:%p", lua_touserdata(L, i));
            break;
        case LUA_TTHREAD:
            printf("thrd:%p", (void *)lua_tothread(L, i));
            break;
        default: printf("%s", "nil");
        }
    }
    putchar('\n');
}
#endif /* LUA_STACK */