forked from xjdrew/lua-zset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-skiplist.c
182 lines (152 loc) · 3.8 KB
/
lua-skiplist.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
/*
* author: xjdrew
* date: 2014-06-03 20:38
*/
#include <stdio.h>
#include <stdlib.h>
#include "lua.h"
#include "lauxlib.h"
#include "skiplist.h"
static inline skiplist*
_to_skiplist(lua_State *L) {
skiplist **sl = lua_touserdata(L, 1);
if(sl==NULL) {
luaL_error(L, "must be skiplist object");
}
return *sl;
}
static int
_insert(lua_State *L) {
skiplist *sl = _to_skiplist(L);
double score = luaL_checknumber(L, 2);
luaL_checktype(L, 3, LUA_TSTRING);
size_t len;
const char* ptr = lua_tolstring(L, 3, &len);
slobj *obj = slCreateObj(ptr, len);
slInsert(sl, score, obj);
return 0;
}
static int
_delete(lua_State *L) {
skiplist *sl = _to_skiplist(L);
double score = luaL_checknumber(L, 2);
luaL_checktype(L, 3, LUA_TSTRING);
slobj obj;
obj.ptr = lua_tolstring(L, 3, &obj.length);
lua_pushboolean(L, slDelete(sl, score, &obj));
return 1;
}
static int
_get_count(lua_State *L) {
skiplist *sl = _to_skiplist(L);
lua_pushunsigned(L, sl->length);
return 1;
}
static int
_get_rank(lua_State *L) {
skiplist *sl = _to_skiplist(L);
double score = luaL_checknumber(L, 2);
luaL_checktype(L, 3, LUA_TSTRING);
slobj obj;
obj.ptr = lua_tolstring(L, 3, &obj.length);
unsigned long rank = slGetRank(sl, score, &obj);
if(rank == 0) {
return 0;
}
lua_pushunsigned(L, rank);
return 1;
}
static int
_get_rank_range(lua_State *L) {
skiplist *sl = _to_skiplist(L);
unsigned long r1 = luaL_checkunsigned(L, 2);
unsigned long r2 = luaL_checkunsigned(L, 3);
int reverse, rangelen;
if(r1 <= r2) {
reverse = 0;
rangelen = r2 - r1 + 1;
} else {
reverse = 1;
rangelen = r1 - r2 + 1;
}
skiplistNode* node = slGetNodeByRank(sl, r1);
lua_createtable(L, rangelen, 0);
int n = 0;
while(node && n < rangelen) {
n++;
lua_pushlstring(L, node->obj->ptr, node->obj->length);
lua_rawseti(L, -2, n);
node = reverse? node->backward : node->level[0].forward;
}
return 1;
}
static int
_get_score_range(lua_State *L) {
skiplist *sl = _to_skiplist(L);
double s1 = luaL_checknumber(L, 2);
double s2 = luaL_checknumber(L, 3);
int reverse;
skiplistNode *node;
if(s1 <= s2) {
reverse = 0;
node = slFirstInRange(sl, s1, s2);
} else {
reverse = 1;
node = slLastInRange(sl, s2, s1);
}
lua_newtable(L);
int n = 0;
while(node) {
if(reverse) {
if(node->score < s2) break;
} else {
if(node->score > s2) break;
}
n++;
lua_pushlstring(L, node->obj->ptr, node->obj->length);
lua_rawseti(L, -2, n);
node = reverse? node->backward:node->level[0].forward;
}
return 1;
}
static int
_dump(lua_State *L) {
skiplist *sl = _to_skiplist(L);
slDump(sl);
return 0;
}
static int
_new(lua_State *L) {
skiplist *psl = slCreate();
skiplist **sl = (skiplist**) lua_newuserdata(L, sizeof(skiplist*));
*sl = psl;
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
static int
_release(lua_State *L) {
skiplist *sl = _to_skiplist(L);
printf("collect sl:%p\n", sl);
slFree(sl);
return 0;
}
int luaopen_skiplist_c(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{"insert", _insert},
{"delete", _delete},
{"get_count", _get_count},
{"get_rank", _get_rank},
{"get_rank_range", _get_rank_range},
{"get_score_range", _get_score_range},
{"dump", _dump},
};
lua_createtable(L, 0, 2);
luaL_newlib(L, l);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, _release);
lua_setfield(L, -2, "__gc");
lua_pushcclosure(L, _new, 1);
return 1;
}