-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip_plus.jl
204 lines (162 loc) · 5.37 KB
/
skip_plus.jl
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
using Base
using Logging
const mult = 2500
function id(x::Int)::UInt64
return hash((x + 1) * mult)
end
function props(nodes)
ids::Vector{String} = [bitstring(id(x)) for x in nodes]
return ids
end
function hash_props(nodes)
hs::Vector{Float64} = [h(x) for x in nodes]
perm = sortperm(hs)
perm⁻¹ = sortperm(perm)
return hs, perm, perm⁻¹
end
function neighbors(self::Int, nodes)
# ⇒ ids[perm_ids] := sorted array of ids
ids = props(nodes)
idsh, permh, permh⁻¹ = hash_props(nodes)
perm_ids = permh
perm_ids⁻¹ = permh⁻¹
context = (nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹)
N = Set()
levels::Dict{Int, Array{Int}} = Dict()
# maybe check why and if any neighbor could be self
left = predᵢ(nothing, 0, self, context...)
right = succᵢ(nothing, 0, self, context...)
pos = findfirst(nodes[permh] .== self)
circ = nothing
if pos == 1
circ = nodes[permh][length(nodes)]
elseif pos == length(nodes)
circ = nodes[permh][1]
end
if left !== nothing
push!(N, left)
end
if right !== nothing
push!(N, right)
end
for i in 0:length(ids[1])
r = rangeᵢ(i, self, context...)
prefix = bitstring(id(self))[1:i]
level_N = []
r1_pos = findfirst(nodes[permh] .== r[1])
r2_pos = findfirst(nodes[permh] .== r[2])
for j in r1_pos:r2_pos
idj = ids[perm_ids][j]
if idj[1:i] == prefix
push!(N, nodes[perm_ids][j])
if nodes[perm_ids][j] !== self
push!(level_N, nodes[perm_ids][j])
end
end
end
# add to levels
if !isempty(level_N)
levels[i] = level_N
end
end
delete!(N, self)
Narray = collect(N)
nids = [h(x) for x in Narray]
perm_ids = sortperm(nids)
return Narray[perm_ids], levels, circ
end
function predᵢ(i::Union{Int, Nothing}, b::Int, x::Int, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹)::Union{Int, Nothing}
"""
Returns node
x+1 := Index of node x
"""
pos = findfirst(nodes[permh] .== x)
if i === nothing
if pos == 1
return nothing
else
return nodes[permh][pos - 1]
end
end
for index in (pos - 1):-1:1
if startswith(ids[perm_ids][index], bitstring(id(x))[1:i] * string(b))
return nodes[perm_ids][index]
end
end
return nodes[perm_ids][1]
end
function succᵢ(i::Union{Int, Nothing}, b::Int, x::Int, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹)::Union{Int, Nothing}
"""
Returns node
x+1 := Index of node x
"""
pos = findfirst(nodes[permh] .== x)
if i === nothing
if pos == size(nodes, 1)
return nothing
else
return nodes[permh][pos + 1]
end
end
for index in (pos + 1):+1:size(nodes, 1)
if startswith(ids[perm_ids][index], bitstring(id(x))[1:i] * string(b))
return nodes[perm_ids][index]
end
end
return nodes[perm_ids][size(nodes, 1)]
end
function rangeᵢ(i::Union{Int, Nothing}, x::Int, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹)::Tuple{Int, Int}
pos_pred_0 = findfirst(nodes .== predᵢ(i, 0, x, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹))
pos_pred_1 = findfirst(nodes .== predᵢ(i, 1, x, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹))
pos_succ_0 = findfirst(nodes .== succᵢ(i, 0, x, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹))
pos_succ_1 = findfirst(nodes .== succᵢ(i, 1, x, nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹))
return(
nodes[perm_ids][min(perm_ids⁻¹[pos_pred_0], perm_ids⁻¹[pos_pred_1])],
nodes[perm_ids][max(perm_ids⁻¹[pos_succ_0], perm_ids⁻¹[pos_succ_1])]
)
end
function calc_neighbors(self::Int, neighbors)
push!(neighbors, self)
ids = [bitstring(id(x)) for x in neighbors]
nodes = neighbors
idsh, permh, permh⁻¹ = hash_props(neighbors)
perm_ids = permh
perm_ids⁻¹ = permh⁻¹
context = (nodes, ids, perm_ids, perm_ids⁻¹, idsh, permh, permh⁻¹)
N = Set()
levels::Dict{Int, Array{Int}} = Dict()
# maybe check why and if any neighbor could be self
left = predᵢ(nothing, 0, self, context...)
right = succᵢ(nothing, 0, self, context...)
if left !== nothing
push!(N, left)
end
if right !== nothing
push!(N, right)
end
for i in 0:length(ids[1])
r = rangeᵢ(i, self, context...)
r1_pos = findfirst(nodes .== r[1])
r2_pos = findfirst(nodes .== r[2])
prefix = bitstring(id(self))[1:i]
level_N = []
for j in perm_ids⁻¹[r1_pos]:perm_ids⁻¹[r2_pos]
idj = ids[perm_ids][j]
if idj[1:i] == prefix
push!(N, nodes[perm_ids][j])
if nodes[perm_ids][j] !== self
push!(level_N, nodes[perm_ids][j])
end
end
end
# add to levels
if !isempty(level_N)
levels[i] = level_N
end
end
delete!(N, self)
Narray = collect(N)
nids = [h(x) for x in Narray]
perm_ids = sortperm(nids)
return (Narray[perm_ids], left, right, levels)
end