-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue.go
84 lines (75 loc) · 1.76 KB
/
queue.go
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
// Copyright 2021 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package geometry
// Priority Queue ordered by dist (smallest to largest)
type qkind int
const (
qrect qkind = 1
qseg qkind = 2
)
type qnode struct {
dist float64 // distance to rect or segment
kind qkind // rect or segment
pos int // segment index or rect addr
a, b Point // segment or rect points
}
// cmp compares two nodes. Returns -1, 0, +1
func (qn qnode) cmp(other qnode) int {
// The comparison priority of 'dist' then 'kind' then 'pos' is important
// for stable ordering.
var cmp int
if qn.dist < other.dist {
cmp = -1
} else if qn.dist > other.dist {
cmp = 1
} else if qn.kind < other.kind {
cmp = -1
} else if qn.kind > other.kind {
cmp = 1
} else if qn.pos < other.pos {
cmp = -1
} else if qn.pos > other.pos {
cmp = 1
}
return cmp
}
type queue []qnode
func (q *queue) push(node qnode) {
*q = append(*q, node)
nodes := *q
i := len(nodes) - 1
parent := (i - 1) / 2
for ; i != 0 && nodes[parent].cmp(nodes[i]) > 0; parent = (i - 1) / 2 {
nodes[parent], nodes[i] = nodes[i], nodes[parent]
i = parent
}
}
func (q *queue) pop() (qnode, bool) {
nodes := *q
if len(nodes) == 0 {
return qnode{}, false
}
var n qnode
n, nodes[0] = nodes[0], nodes[len(*q)-1]
nodes = nodes[:len(nodes)-1]
*q = nodes
i := 0
for {
smallest := i
left := i*2 + 1
right := i*2 + 2
if left < len(nodes) && nodes[left].cmp(nodes[smallest]) <= 0 {
smallest = left
}
if right < len(nodes) && nodes[right].cmp(nodes[smallest]) <= 0 {
smallest = right
}
if smallest == i {
break
}
nodes[smallest], nodes[i] = nodes[i], nodes[smallest]
i = smallest
}
return n, true
}