forked from xjdrew/lua-zset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
54 lines (43 loc) · 978 Bytes
/
test.lua
File metadata and controls
54 lines (43 loc) · 978 Bytes
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
local zset = require "zset"
local total = 100
local all = {}
for i=1, total do
all[#all + 1] = i
end
local function random_choose(t)
if #t == 0 then
return
end
local i = math.random(#t)
return table.remove(t, i)
end
local zs = zset.new()
while true do
local score = random_choose(all)
if not score then
break
end
local name = "a" .. score
zs:add(score, name)
end
assert(total == zs:count())
print("rank 28:", zs:rank("a28"))
print("rev rank 28:", zs:rev_rank("a28"))
local t = zs:range(1, 10)
print("rank 1-10:")
for _, name in ipairs(t) do
print(name)
end
local t = zs:rev_range(1, 10)
print("rev rank 1-10:")
for _, name in ipairs(t) do
print(name)
end
print("------------------ dump ------------------")
zs:dump()
print("------------------ dump after limit 10 ------------------")
zs:limit(10)
zs:dump()
print("------------------ dump after rev limit 5 ------------------")
zs:rev_limit(5)
zs:dump()