Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsorted_array.lua
More file actions
Latest commit
162 lines (141 loc) · 2.89 KB
/
Copy pathsorted_array.lua
File metadata and controls
162 lines (141 loc) · 2.89 KB
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
localbit=require("bit")
localtable=require("table")
localstring=require("string")
localfloor=require("math").floor
localfunctions_to_a(data)
locala= {}
localbytes= {string.byte(data, i, #data)}
fori=1, #data/8do
a[i] =0
forc=0, 7do
a[i] =bit.lshift(a[i], 8)
a[i] =a[i] +bytes[i*8-c]
end
end
returna
end
localfunctiona_to_s(a)
localbytes= {}
fori=1, #ado
forc=0, 7do
table.insert(bytes, bit.band(a[i], 0xff))
a[i] =bit.rshift(a[i], 8)
end
end
returnstring.char(unpack(bytes))
end
locallimit=500
localfunctionamerge(a, b)
localr= {}
localn=limit
while#a>0and#b>0andn>0do
ifa[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>0andn>0do
table.insert(r, table.remove(a, 1))
n=n-1
end
while#b>0andn>0do
table.insert(r, table.remove(b, 1))
n=n-1
end
returnr
end
localfunctionafind_ge(a, x)
if#a==0then
return1
end
localfirst, last=1, #a
localmid
repeat
mid=floor(first+ (last-first) /2)
ifx>a[mid] then
last=mid
else
first=mid+1
end
untilfirst>=last
ifa[mid] >=xthen
mid=mid+1
end
returnmid
end
localfunctionains(a, key)
key=tonumber(key)
locali=afind_ge(a, key)
ifa[i] anda[i] >=keythen
table.insert(a, i+1, key) -- next to equal or greater
else
table.insert(a, i, key)
end
while#a>limitdo
table.remove(a)
end
end
localfunctionadel(a, key)
key=tonumber(key)
locali=afind_ge(a, key)
ifa[i] ==keythen
table.remove(a, i)
end
end
localfunctionget(space, key)
localtuple=box.select(space, 0, key)
iftuplethen
returns_to_a(tuple[1])
else
return {}
end
end
localfunctionstore(space, key, a)
returnbox.replace(space, key, a_to_s(a))
end
functionbox.sa_insert(space, key, value)
locala=get(space, key)
ains(a, value)
print(unpack(a))
returnstore(space, key, a)
end
functionbox.sa_delete(space, key, ...)
locala=get(space, key)
fori, dinpairs({...}) do
adel(a, d)
end
returnstore(space, key, a)
end
functionbox.sa_select(space, key, from, limit)
locala=get(space, key)
iffrom~=nilthen
from=afind_ge(a, tonumber(from))
else
from=1
end
iflimit~=nilthen
limit=tonumber(limit)
else
limit=0
end
localr= {}
fori=from, #ado
ifa[i] ==nilthen
break
end
table.insert(r, a[i])
limit=limit-1
iflimit==0then
break
end
end
returnkey, a_to_s(r)
end
functionbox.sa_merge(space, key_a, key_b)
locala=get(space, key_a)
localb=get(space, key_b)
localr=amerge(a, b)
returna_to_s(r)
end