-
Notifications
You must be signed in to change notification settings - Fork 49
/
lua-crab.c
317 lines (277 loc) · 6.62 KB
/
lua-crab.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
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
#include <stdlib.h>
#include <stdint.h>
#include "lua.h"
#include "lauxlib.h"
typedef struct _TableNode {
uint32_t key;
int next;
char flag; // 0: empty, 'n': non-terminator, 'o': terminator
void* value;
} TableNode;
typedef struct _Table {
int capacity;
TableNode* node;
TableNode* lastfree;
} Table;
Table *g_dict = NULL;
inline static void
initnode(TableNode *node) {
node->next = -1;
node->flag = 0;
node->value = NULL;
}
inline static int
tisnil(TableNode* node) {
return node->flag == 0;
}
inline static TableNode*
tnode(Table *t, int index) {
return t->node + index;
}
inline static int
tindex(Table *t, TableNode *node) {
return node - t->node;
}
static TableNode*
mainposition(Table *t, uint32_t key) {
return &t->node[(key & (t->capacity -1))];
}
static TableNode*
getfreenode(Table *t) {
while(t->lastfree >= t->node) {
if(tisnil(t->lastfree)) {
return t->lastfree;
}
t->lastfree--;
}
return NULL;
}
static TableNode*
table_newkey(Table *t, uint32_t key);
static void
table_expand(Table *t) {
int capacity = t->capacity;
TableNode *node = t->node;
// init new table
t->capacity = t->capacity * 2;
t->node = calloc(t->capacity, sizeof(TableNode));
int i;
for(i=0; i<t->capacity; i++) {
initnode(t->node + i);
}
t->lastfree = t->node + (t->capacity - 1);
// reinsert old node
for(i=0; i< capacity; i++) {
TableNode *old = node + i;
if(tisnil(old)) {
continue;
}
TableNode *new = table_newkey(t, old->key);
new->flag = old->flag;
new->value = old->value;
}
// free old node
free(node);
}
/*
** inserts a new key into a hash table; first, check whether key's main
** position is free. If not, check whether colliding node is in its main
** position or not: if it is not, move colliding node to an empty place and
** put new key in its main position; otherwise (colliding node is in its main
** position), new key goes to an empty position.
*/
static TableNode*
table_newkey(Table *t, uint32_t key) {
TableNode *mp = mainposition(t, key);
if(!tisnil(mp)) {
TableNode *n = getfreenode(t);
if(n == NULL) {
table_expand(t);
return table_newkey(t, key);
}
TableNode *othern = mainposition(t, mp->key);
if (othern != mp) {
int mindex = tindex(t, mp);
while(othern->next != mindex) {
othern = tnode(t, othern->next);
}
othern->next = tindex(t, n);
*n = *mp;
initnode(mp);
} else {
n->next = mp->next;
mp->next = tindex(t, n);
mp = n;
}
}
mp->key = key;
mp->flag = 'n';
return mp;
}
static TableNode*
table_get(Table *t, uint32_t key) {
TableNode *n = mainposition(t, key);
while(!tisnil(n)) {
if(n->key == key) {
return n;
}
if(n->next < 0) {
break;
}
n = tnode(t, n->next);
}
return NULL;
}
static TableNode*
table_insert(Table *t, uint32_t key) {
TableNode *node = table_get(t, key);
if(node) {
return node;
}
return table_newkey(t, key);
}
static Table*
table_new() {
Table *t = malloc(sizeof(Table));
t->capacity = 1;
t->node = malloc(sizeof(TableNode));
initnode(t->node);
t->lastfree = t->node;
return t;
}
// deconstruct dictinory tree
static void
_dict_close(Table *t) {
if(t == NULL) {
return;
}
int i = 0;
for(i=0; i<t->capacity; i++) {
TableNode *node = t->node + i;
if(node->flag != 0) {
_dict_close(node->value);
}
}
free(t->node);
free(t);
}
static void
_dict_dump(Table *t, int indent) {
if(t == NULL) {
return;
}
int i = 0;
for(i=0; i<t->capacity; i++) {
TableNode *node = t->node + i;
printf("%*s", indent, " ");
if(node->flag != 0) {
printf("0x%x\n", node->key);
_dict_dump(node->value, indent + 8);
} else {
printf("%s\n", "nil");
}
}
}
static int
_dict_insert(lua_State *L, Table* dict) {
if(!lua_istable(L, -1)) {
return 0;
}
size_t len = lua_rawlen(L, -1);
size_t i;
uint32_t rune;
TableNode *node = NULL;
for(i=1; i<=len; i++) {
lua_rawgeti(L, -1, i);
int isnum;
rune = (uint32_t)lua_tointegerx(L, -1, &isnum);
lua_pop(L, 1);
if(!isnum) {
return 0;
}
Table *tmp;
if(node == NULL) {
tmp = dict;
} else {
if(node->value == NULL) {
node->value = table_new();
}
tmp = node->value;
}
node = table_insert(tmp, rune);
}
if(node) {
node->flag = 'o';
}
return 1;
}
static int
dict_open(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
Table *dict = table_new();
size_t len = lua_rawlen(L,1);
size_t i;
for(i=1;i<=len;i++) {
lua_rawgeti(L, 1, i);
if(!_dict_insert(L, dict)) {
_dict_close(dict);
return luaL_error(L, "illegal parameters in table index %d", i);
}
lua_pop(L, 1);
}
//_dict_dump(dict, 0);
// don't close old g_dict, avoid crash
g_dict = dict;
return 0;
}
static int
dict_filter(lua_State *L) {
if(!g_dict) {
return luaL_error(L, "need open first");
}
Table* dict = g_dict;
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_rawlen(L,1);
size_t i,j;
int flag = 0;
for(i=1;i<=len;) {
TableNode *node = NULL;
int step = 0;
for(j=i;j<=len;j++) {
lua_rawgeti(L, 1, j);
uint32_t rune = (uint32_t) lua_tointeger(L, -1);
lua_pop(L, 1);
if(node == NULL) {
node = table_get(dict, rune);
} else {
node = table_get(node->value, rune);
}
if(node && node->flag == 'o') step = j - i + 1;
if(!(node && node->value)) break;
}
if(step > 0) {
for(j=0;j<step;j++) {
lua_pushinteger(L, '*');
lua_rawseti(L, 1, i+j);
}
flag = 1;
i = i + step;
} else {
i++;
}
}
lua_pushboolean(L, flag);
return 1;
}
// interface
int
luaopen_crab_c(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{"open", dict_open},
{"filter", dict_filter},
{NULL, NULL}
};
luaL_newlib(L, l);
return 1;
}