-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_array.h
91 lines (71 loc) · 2.31 KB
/
bool_array.h
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
//
// Created by Swift Sheng on 10/1/24.
//
// have this user defined data type working, then creating object capability
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.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 BitArray {
int size;
unsigned int values[1]; /* variable part */
} BitArray;
static int newarray (lua_State *L) {
int i;
size_t nbytes;
BitArray *a;
int n = (int)luaL_checkinteger(L, 1); /* number of bits */
luaL_argcheck(L, n >= 1, 1, "invalid size");
nbytes = sizeof(BitArray) + I_WORD(n - 1)*sizeof(unsigned int);
a = (BitArray *)lua_newuserdata(L, nbytes);
a->size = n;
for (i = 0; i <= I_WORD(n - 1); i++)
a->values[i] = 0; /* initialize array */
return 1; /* new userdata is already on the stack */
}
static int setarray (lua_State *L) {
BitArray *a = (BitArray *)lua_touserdata(L, 1);
int index = (int)luaL_checkinteger(L, 2) - 1;
luaL_argcheck(L, a != NULL, 1, "'array' expected");
luaL_argcheck(L, 0 <= index && 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); /* set bit */
else
a->values[I_WORD(index)] &= ~I_BIT(index); /* reset bit */
return 0;
}
static int getarray (lua_State *L) {
BitArray *a = (BitArray *)lua_touserdata(L, 1);
int index = (int)luaL_checkinteger(L, 2) - 1;
luaL_argcheck(L, a != NULL, 1, "'array' expected");
luaL_argcheck(L, 0 <= index && 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) {
BitArray *a = (BitArray *)lua_touserdata(L, 1);
luaL_argcheck(L, a != NULL, 1, "'array' expected");
lua_pushinteger(L, a->size);
return 1;
}
static const struct luaL_Reg arraylib [] = {
{"new", newarray},
{"set", setarray},
{"get", getarray},
{"size", getsize},
{NULL, NULL}
};
int luaopen_array (lua_State *L) {
luaL_newlib(L, arraylib);
return 1;
}