-
Notifications
You must be signed in to change notification settings - Fork 23
/
sorted_array.lua
162 lines (141 loc) · 2.89 KB
/
sorted_array.lua
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
local bit = require("bit")
local table = require("table")
local string = require("string")
local floor = require("math").floor
local function s_to_a(data)
local a = {}
local bytes = {string.byte(data, i, #data)}
for i = 1, #data / 8 do
a[i] = 0
for c = 0, 7 do
a[i] = bit.lshift(a[i], 8)
a[i] = a[i] + bytes[i * 8 - c]
end
end
return a
end
local function a_to_s(a)
local bytes = {}
for i = 1, #a do
for c = 0, 7 do
table.insert(bytes, bit.band(a[i], 0xff))
a[i] = bit.rshift(a[i], 8)
end
end
return string.char(unpack(bytes))
end
local limit = 500
local function amerge(a, b)
local r = {}
local n = limit
while #a > 0 and #b > 0 and n > 0 do
if a[1] > b[1] then
table.insert(r, table.remove(a, 1))
else
table.insert(r, table.remove(b, 1))
end
n = n - 1
end
while #a > 0 and n > 0 do
table.insert(r, table.remove(a, 1))
n = n - 1
end
while #b > 0 and n > 0 do
table.insert(r, table.remove(b, 1))
n = n - 1
end
return r
end
local function afind_ge(a, x)
if #a == 0 then
return 1
end
local first, last = 1, #a
local mid
repeat
mid = floor(first + (last - first) / 2)
if x > a[mid] then
last = mid
else
first = mid + 1
end
until first >= last
if a[mid] >= x then
mid = mid + 1
end
return mid
end
local function ains(a, key)
key = tonumber(key)
local i = afind_ge(a, key)
if a[i] and a[i] >= key then
table.insert(a, i + 1, key) -- next to equal or greater
else
table.insert(a, i, key)
end
while #a > limit do
table.remove(a)
end
end
local function adel(a, key)
key = tonumber(key)
local i = afind_ge(a, key)
if a[i] == key then
table.remove(a, i)
end
end
local function get(space, key)
local tuple = box.select(space, 0, key)
if tuple then
return s_to_a(tuple[1])
else
return {}
end
end
local function store(space, key, a)
return box.replace(space, key, a_to_s(a))
end
function box.sa_insert(space, key, value)
local a = get(space, key)
ains(a, value)
print(unpack(a))
return store(space, key, a)
end
function box.sa_delete(space, key, ...)
local a = get(space, key)
for i, d in pairs({...}) do
adel(a, d)
end
return store(space, key, a)
end
function box.sa_select(space, key, from, limit)
local a = get(space, key)
if from ~= nil then
from = afind_ge(a, tonumber(from))
else
from = 1
end
if limit ~= nil then
limit = tonumber(limit)
else
limit = 0
end
local r = {}
for i = from, #a do
if a[i] == nil then
break
end
table.insert(r, a[i])
limit = limit - 1
if limit == 0 then
break
end
end
return key, a_to_s(r)
end
function box.sa_merge(space, key_a, key_b)
local a = get(space, key_a)
local b = get(space, key_b)
local r = amerge(a, b)
return a_to_s(r)
end