...
1
2
3
4
5
6 package linear
7
8 import (
9 "k8s.io/kubernetes/third_party/forked/gonum/graph"
10 )
11
12
13 type NodeStack []graph.Node
14
15
16 func (s *NodeStack) Len() int { return len(*s) }
17
18
19
20 func (s *NodeStack) Pop() graph.Node {
21 v := *s
22 v, n := v[:len(v)-1], v[len(v)-1]
23 *s = v
24 return n
25 }
26
27
28 func (s *NodeStack) Push(n graph.Node) { *s = append(*s, n) }
29
30
31 type NodeQueue struct {
32 head int
33 data []graph.Node
34 }
35
36
37 func (q *NodeQueue) Len() int { return len(q.data) - q.head }
38
39
40 func (q *NodeQueue) Enqueue(n graph.Node) {
41 if len(q.data) == cap(q.data) && q.head > 0 {
42 l := q.Len()
43 copy(q.data, q.data[q.head:])
44 q.head = 0
45 q.data = append(q.data[:l], n)
46 } else {
47 q.data = append(q.data, n)
48 }
49 }
50
51
52
53 func (q *NodeQueue) Dequeue() graph.Node {
54 if q.Len() == 0 {
55 panic("queue: empty queue")
56 }
57
58 var n graph.Node
59 n, q.data[q.head] = q.data[q.head], nil
60 q.head++
61
62 if q.Len() == 0 {
63 q.head = 0
64 q.data = q.data[:0]
65 }
66
67 return n
68 }
69
70
71 func (q *NodeQueue) Reset() {
72 q.head = 0
73 q.data = q.data[:0]
74 }
75
View as plain text