-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path08_boolarray_lib.c
83 lines (62 loc) · 1.88 KB
/
08_boolarray_lib.c
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
#include <limits.h>
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#define BITS_PER_WORD (CHAR_BIT * sizeof(unsigned int))
#define I_WORD(i) ((unsigned int)(i) / BITS_PER_WORD)
#define I_BIT(i) (1 << ((unsigned int) (i) % BITS_PER_WORD))
typedef struct NumArray {
int size;
unsigned int values[1];
} NumArray;
static int newarray(lua_State *L) {
int i, n;
size_t nbytes;
NumArray *a;
n = luaL_checkinteger(L, 1);
luaL_argcheck(L, n >= 1, 1, "invalid size"); //check : n >= 1
nbytes = sizeof(NumArray) + I_WORD(n - 1) * sizeof(unsigned int);
a = (NumArray *)lua_newuserdata(L, nbytes);
a -> size = n;
for(i = 0; i <= I_WORD(n - 1); i++)
a -> values[i] = 0; //初始化数组
return 1;
}
static int setarray(lua_State *L) {
NumArray *a = (NumArray *) lua_touserdata(L, 1);
int index = (int) luaL_checkinteger(L, 2) - 1;
luaL_argcheck(L, a != NULL, 1, "'array' exception");
luaL_argcheck(L, index >= 0 && index < a->size, 2, "index out of range");
luaL_checkany(L, 3);
if(lua_toboolean(L, 3)) {
a->values[I_WORD(index)] |= I_BIT(index); //设置bit
} else {
a->values[I_WORD(index)] &= ~I_BIT(index); //重置bit
}
return 0;
}
static int getarray(lua_State *L) {
NumArray *a = (NumArray *) lua_touserdata(L, 1);
int index = (int) luaL_checkinteger(L, 2) - 1;
luaL_argcheck(L, a != NULL, 1, "'array' exception");
luaL_argcheck(L, index >= 0 && index < a->size, 2, "index out of range");
lua_pushboolean(L, a->values[I_WORD(index)] & I_BIT(index));
return 1;
}
static int getsize(lua_State *L) {
NumArray *a = (NumArray *) lua_touserdata(L, 1);
luaL_argcheck(L, a != NULL, 1, "'array' exception");
lua_pushnumber(L, a->size);
return 1;
}
static const struct luaL_Reg arraylib [] = {
{"new", newarray},
{"set", setarray},
{"get", getarray},
{"size", getsize},
{NULL, NULL}
};
int luaopen_boolarray(lua_State *L) {
luaL_newlib(L, arraylib);
return 1;
}