| 1 |
d. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package kdtree |
| 7 |
|
| 8 |
import ( |
| 9 |
"container/heap" |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
"sort" |
| 13 |
) |
| 14 |
|
| 15 |
type Interface interface { |
| 16 |
|
| 17 |
Index(i int) Comparable |
| 18 |
|
| 19 |
|
| 20 |
Len() int |
| 21 |
|
| 22 |
|
| 23 |
Pivot(Dim) int |
| 24 |
|
| 25 |
|
| 26 |
Slice(start, end int) Interface |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
type Bounder interface { |
| 31 |
Bounds() *Bounding |
| 32 |
} |
| 33 |
|
| 34 |
type bounder interface { |
| 35 |
Interface |
| 36 |
Bounder |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
type Dim int |
| 41 |
|
| 42 |
|
| 43 |
type Comparable interface { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
Compare(Comparable, Dim) float64 |
| 51 |
|
| 52 |
|
| 53 |
Dims() int |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Distance(Comparable) float64 |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
type Extender interface { |
| 63 |
Comparable |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Extend(*Bounding) *Bounding |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
type Bounding [2]Comparable |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
func (b *Bounding) Contains(c Comparable) bool { |
| 76 |
if b == nil { |
| 77 |
return true |
| 78 |
} |
| 79 |
for d := Dim(0); d < Dim(c.Dims()); d++ { |
| 80 |
if c.Compare(b[0], d) < 0 || c.Compare(b[1], d) > 0 { |
| 81 |
return false |
| 82 |
} |
| 83 |
} |
| 84 |
return true |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
type Node struct { |
| 89 |
Point Comparable |
| 90 |
Plane Dim |
| 91 |
Left, Right *Node |
| 92 |
*Bounding |
| 93 |
} |
| 94 |
|
| 95 |
func (n *Node) String() string { |
| 96 |
if n == nil { |
| 97 |
return "<nil>" |
| 98 |
} |
| 99 |
return fmt.Sprintf("%.3f %d", n.Point, n.Plane) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
type Tree struct { |
| 104 |
Root *Node |
| 105 |
Count int |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
func New(p Interface, bounding bool) *Tree { |
| 111 |
if p, ok := p.(bounder); ok && bounding { |
| 112 |
return &Tree{ |
| 113 |
Root: buildBounded(p, 0, bounding), |
| 114 |
Count: p.Len(), |
| 115 |
} |
| 116 |
} |
| 117 |
return &Tree{ |
| 118 |
Root: build(p, 0), |
| 119 |
Count: p.Len(), |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func build(p Interface, plane Dim) *Node { |
| 124 |
if p.Len() == 0 { |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
piv := p.Pivot(plane) |
| 129 |
d := p.Index(piv) |
| 130 |
np := (plane + 1) % Dim(d.Dims()) |
| 131 |
|
| 132 |
return &Node{ |
| 133 |
Point: d, |
| 134 |
Plane: plane, |
| 135 |
Left: build(p.Slice(0, piv), np), |
| 136 |
Right: build(p.Slice(piv+1, p.Len()), np), |
| 137 |
Bounding: nil, |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func buildBounded(p bounder, plane Dim, bounding bool) *Node { |
| 142 |
if p.Len() == 0 { |
| 143 |
return nil |
| 144 |
} |
| 145 |
|
| 146 |
piv := p.Pivot(plane) |
| 147 |
d := p.Index(piv) |
| 148 |
np := (plane + 1) % Dim(d.Dims()) |
| 149 |
|
| 150 |
b := p.Bounds() |
| 151 |
return &Node{ |
| 152 |
Point: d, |
| 153 |
Plane: plane, |
| 154 |
Left: buildBounded(p.Slice(0, piv).(bounder), np, bounding), |
| 155 |
Right: buildBounded(p.Slice(piv+1, p.Len()).(bounder), np, bounding), |
| 156 |
Bounding: b, |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
func (t *Tree) Insert(c Comparable, bounding bool) { |
| 164 |
t.Count++ |
| 165 |
if t.Root != nil { |
| 166 |
bounding = t.Root.Bounding != nil |
| 167 |
} |
| 168 |
if c, ok := c.(Extender); ok && bounding { |
| 169 |
t.Root = t.Root.insertBounded(c, 0, bounding) |
| 170 |
return |
| 171 |
} else if !ok && t.Root != nil { |
| 172 |
|
| 173 |
t.Root.Bounding = nil |
| 174 |
} |
| 175 |
t.Root = t.Root.insert(c, 0) |
| 176 |
} |
| 177 |
|
| 178 |
func (n *Node) insert(c Comparable, d Dim) *Node { |
| 179 |
if n == nil { |
| 180 |
return &Node{ |
| 181 |
Point: c, |
| 182 |
Plane: d, |
| 183 |
Bounding: nil, |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
d = (n.Plane + 1) % Dim(c.Dims()) |
| 188 |
if c.Compare(n.Point, n.Plane) <= 0 { |
| 189 |
n.Left = n.Left.insert(c, d) |
| 190 |
} else { |
| 191 |
n.Right = n.Right.insert(c, d) |
| 192 |
} |
| 193 |
|
| 194 |
return n |
| 195 |
} |
| 196 |
|
| 197 |
func (n *Node) insertBounded(c Extender, d Dim, bounding bool) *Node { |
| 198 |
if n == nil { |
| 199 |
var b *Bounding |
| 200 |
if bounding { |
| 201 |
b = c.Extend(b) |
| 202 |
} |
| 203 |
return &Node{ |
| 204 |
Point: c, |
| 205 |
Plane: d, |
| 206 |
Bounding: b, |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if bounding { |
| 211 |
n.Bounding = c.Extend(n.Bounding) |
| 212 |
} |
| 213 |
d = (n.Plane + 1) % Dim(c.Dims()) |
| 214 |
if c.Compare(n.Point, n.Plane) <= 0 { |
| 215 |
n.Left = n.Left.insertBounded(c, d, bounding) |
| 216 |
} else { |
| 217 |
n.Right = n.Right.insertBounded(c, d, bounding) |
| 218 |
} |
| 219 |
|
| 220 |
return n |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
func (t *Tree) Len() int { return t.Count } |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
func (t *Tree) Contains(c Comparable) bool { |
| 229 |
if t.Root.Bounding == nil { |
| 230 |
return true |
| 231 |
} |
| 232 |
return t.Root.Contains(c) |
| 233 |
} |
| 234 |
|
| 235 |
var inf = math.Inf(1) |
| 236 |
|
| 237 |
|
| 238 |
func (t *Tree) Nearest(q Comparable) (Comparable, float64) { |
| 239 |
if t.Root == nil { |
| 240 |
return nil, inf |
| 241 |
} |
| 242 |
n, dist := t.Root.search(q, inf) |
| 243 |
if n == nil { |
| 244 |
return nil, inf |
| 245 |
} |
| 246 |
return n.Point, dist |
| 247 |
} |
| 248 |
|
| 249 |
func (n *Node) search(q Comparable, dist float64) (*Node, float64) { |
| 250 |
if n == nil { |
| 251 |
return nil, inf |
| 252 |
} |
| 253 |
|
| 254 |
c := q.Compare(n.Point, n.Plane) |
| 255 |
dist = math.Min(dist, q.Distance(n.Point)) |
| 256 |
|
| 257 |
bn := n |
| 258 |
if c <= 0 { |
| 259 |
ln, ld := n.Left.search(q, dist) |
| 260 |
if ld < dist { |
| 261 |
dist = ld |
| 262 |
bn = ln |
| 263 |
} |
| 264 |
if c*c < dist { |
| 265 |
rn, rd := n.Right.search(q, dist) |
| 266 |
if rd < dist { |
| 267 |
bn, dist = rn, rd |
| 268 |
} |
| 269 |
} |
| 270 |
return bn, dist |
| 271 |
} |
| 272 |
rn, rd := n.Right.search(q, dist) |
| 273 |
if rd < dist { |
| 274 |
dist = rd |
| 275 |
bn = rn |
| 276 |
} |
| 277 |
if c*c < dist { |
| 278 |
ln, ld := n.Left.search(q, dist) |
| 279 |
if ld < dist { |
| 280 |
bn, dist = ln, ld |
| 281 |
} |
| 282 |
} |
| 283 |
return bn, dist |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
type ComparableDist struct { |
| 290 |
Comparable Comparable |
| 291 |
Dist float64 |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
type Heap []ComparableDist |
| 296 |
|
| 297 |
func (h *Heap) Max() ComparableDist { return (*h)[0] } |
| 298 |
func (h *Heap) Len() int { return len(*h) } |
| 299 |
func (h *Heap) Less(i, j int) bool { return (*h)[i].Comparable == nil || (*h)[i].Dist > (*h)[j].Dist } |
| 300 |
func (h *Heap) Swap(i, j int) { (*h)[i], (*h)[j] = (*h)[j], (*h)[i] } |
| 301 |
func (h *Heap) Push(x interface{}) { (*h) = append(*h, x.(ComparableDist)) } |
| 302 |
func (h *Heap) Pop() (i interface{}) { i, *h = (*h)[len(*h)-1], (*h)[:len(*h)-1]; return i } |
| 303 |
|
| 304 |
|
| 305 |
type NKeeper struct { |
| 306 |
Heap |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
func NewNKeeper(n int) *NKeeper { |
| 312 |
k := NKeeper{make(Heap, 1, n)} |
| 313 |
k.Heap[0].Dist = inf |
| 314 |
return &k |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
func (k *NKeeper) Keep(c ComparableDist) { |
| 321 |
if c.Dist < k.Heap[0].Dist { |
| 322 |
if len(k.Heap) == cap(k.Heap) { |
| 323 |
heap.Pop(k) |
| 324 |
} |
| 325 |
heap.Push(k, c) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
type DistKeeper struct { |
| 332 |
Heap |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
func NewDistKeeper(d float64) *DistKeeper { return &DistKeeper{Heap{{Dist: d}}} } |
| 337 |
|
| 338 |
|
| 339 |
func (k *DistKeeper) Keep(c ComparableDist) { |
| 340 |
if c.Dist <= k.Heap[0].Dist { |
| 341 |
heap.Push(k, c) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
type Keeper interface { |
| 348 |
Keep(ComparableDist) |
| 349 |
Max() ComparableDist |
| 350 |
heap.Interface |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
func (t *Tree) NearestSet(k Keeper, q Comparable) { |
| 358 |
if t.Root == nil { |
| 359 |
return |
| 360 |
} |
| 361 |
t.Root.searchSet(q, k) |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
removeSentinel := k.Len() != 0 && k.Max().Comparable == nil |
| 366 |
|
| 367 |
sort.Sort(sort.Reverse(k)) |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if removeSentinel { |
| 374 |
k.Pop() |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
func (n *Node) searchSet(q Comparable, k Keeper) { |
| 379 |
if n == nil { |
| 380 |
return |
| 381 |
} |
| 382 |
|
| 383 |
c := q.Compare(n.Point, n.Plane) |
| 384 |
k.Keep(ComparableDist{Comparable: n.Point, Dist: q.Distance(n.Point)}) |
| 385 |
if c <= 0 { |
| 386 |
n.Left.searchSet(q, k) |
| 387 |
if c*c <= k.Max().Dist { |
| 388 |
n.Right.searchSet(q, k) |
| 389 |
} |
| 390 |
return |
| 391 |
} |
| 392 |
n.Right.searchSet(q, k) |
| 393 |
if c*c <= k.Max().Dist { |
| 394 |
n.Left.searchSet(q, k) |
| 395 |
} |
| 396 |
return |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
type Operation func(Comparable, *Bounding, int) (done bool) |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
func (t *Tree) Do(fn Operation) bool { |
| 408 |
if t.Root == nil { |
| 409 |
return false |
| 410 |
} |
| 411 |
return t.Root.do(fn, 0) |
| 412 |
} |
| 413 |
|
| 414 |
func (n *Node) do(fn Operation, depth int) (done bool) { |
| 415 |
if n.Left != nil { |
| 416 |
done = n.Left.do(fn, depth+1) |
| 417 |
if done { |
| 418 |
return |
| 419 |
} |
| 420 |
} |
| 421 |
done = fn(n.Point, n.Bounding, depth) |
| 422 |
if done { |
| 423 |
return |
| 424 |
} |
| 425 |
if n.Right != nil { |
| 426 |
done = n.Right.do(fn, depth+1) |
| 427 |
} |
| 428 |
return |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
func (t *Tree) DoBounded(fn Operation, b *Bounding) bool { |
| 436 |
if t.Root == nil { |
| 437 |
return false |
| 438 |
} |
| 439 |
if b == nil { |
| 440 |
return t.Root.do(fn, 0) |
| 441 |
} |
| 442 |
return t.Root.doBounded(fn, b, 0) |
| 443 |
} |
| 444 |
|
| 445 |
func (n *Node) doBounded(fn Operation, b *Bounding, depth int) (done bool) { |
| 446 |
if n.Left != nil && b[0].Compare(n.Point, n.Plane) < 0 { |
| 447 |
done = n.Left.doBounded(fn, b, depth+1) |
| 448 |
if done { |
| 449 |
return |
| 450 |
} |
| 451 |
} |
| 452 |
if b.Contains(n.Point) { |
| 453 |
done = fn(n.Point, b, depth) |
| 454 |
if done { |
| 455 |
return |
| 456 |
} |
| 457 |
} |
| 458 |
if n.Right != nil && 0 < b[1].Compare(n.Point, n.Plane) { |
| 459 |
done = n.Right.doBounded(fn, b, depth+1) |
| 460 |
} |
| 461 |
return |
| 462 |
} |