-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_queue.go
61 lines (48 loc) · 1011 Bytes
/
trie_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
package gostr
type trieQueue struct {
used, front int
elms []*Trie
}
func newTrieQueue(capacity int) *trieQueue {
if capacity < 1 {
capacity = 1 // never less than one, or the growing won't work
}
return &trieQueue{
used: 0, front: 0,
elms: make([]*Trie, capacity),
}
}
func (q *trieQueue) isEmpty() bool {
return q.used == 0
}
func (q *trieQueue) isFull() bool {
return q.used == len(q.elms)
}
// Only call this when used=cap!
func (q *trieQueue) grow(newCap int) {
newElms := make([]*Trie, newCap)
n := 0
for i := q.front; i < len(q.elms); i++ {
newElms[n] = q.elms[i]
n++
}
for i := 0; i < q.front; i++ {
newElms[n] = q.elms[i]
n++
}
q.elms = newElms
q.front = 0
}
func (q *trieQueue) enqueue(t *Trie) {
if q.isFull() {
q.grow(2 * len(q.elms)) //nolint:gomnd // doubling sizes
}
q.elms[(q.front+q.used)%len(q.elms)] = t
q.used++
}
func (q *trieQueue) dequeue() *Trie {
t := q.elms[q.front]
q.used--
q.front = (q.front + 1) % len(q.elms)
return t
}