-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.go
173 lines (156 loc) · 3.82 KB
/
deque.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
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
package gods
import (
"errors"
"math"
)
const (
defaultCap = 8
)
//Deque represents an abstract double-ended queue supporting push, pop, and peek operations on both ends.
type Deque[T any] struct {
buf []T
front, back, len, cap, min int
}
//expand expands the slice backing the deque using the golden ratio. Yes, this is a personal quirk and not the most
//efficient way to expand.
func (d *Deque[T]) expand() {
newCap := int(math.Round(float64(d.cap) * math.Phi))
newBuf := make([]T, newCap)
i := 0
for j := d.front; j < d.front+d.len; j++ {
newBuf[i] = d.buf[j%d.cap]
i++
}
d.buf = newBuf
d.cap = newCap
d.front = 0
d.back = d.len - 1
}
//contract contracts the slice backing the deque using the golden ratio. Yes, this is a personal quirk and not the most
//efficient way to contract.
func (d *Deque[T]) contract() {
newCap := int(math.Round(float64(d.cap) / math.Phi))
if newCap < d.min {
return
}
newBuf := make([]T, newCap)
i := 0
for j := d.front; j < d.front+d.len; j++ {
newBuf[i] = d.buf[j%d.cap]
i++
}
d.buf = newBuf
d.cap = newCap
d.front = 0
d.back = d.len - 1
}
//NewDeque returns a pointer to an initialized deque with zero elements and a minimum capacity of 8.
func NewDeque[T any]() *Deque[T] {
return NewDequeWithCap[T](defaultCap)
}
//NewDequeWithCap returns a pointer to an initialized deque with zero elements and a minimum capacity of `cap`. If cap
//is less than 1, NewDequeWithCap will panic.
func NewDequeWithCap[T any](cap int) *Deque[T] {
if cap < 1 {
panic("attempt to create deque with capacity < 1")
}
deque := &Deque[T]{
cap: cap,
min: cap,
front: -1,
back: -1,
}
deque.buf = make([]T, cap)
return deque
}
//ErrDequeEmpty is returned when a pop or peek operation is performed on an empty deque.
var ErrDequeEmpty = errors.New("cannot pop from an empty deque")
//PeekBack returns the item at the back of the deque without removing it. Returns `ErrDequeEmpty` if the deque is empty.
func (d *Deque[T]) PeekBack() (T, error) {
if d.len == 0 {
var t T
return t, ErrDequeEmpty
}
return d.buf[d.back], nil
}
//PeekFront returns the item at the front of the deque without removing it. Returns `ErrDequeEmpty` if the deque is
//empty.
func (d *Deque[T]) PeekFront() (T, error) {
if d.len == 0 {
var t T
return t, ErrDequeEmpty
}
return d.buf[d.front], nil
}
//PushBack pushes the provided value to the back of the deque.
func (d *Deque[T]) PushBack(val T) {
if d.len == d.cap {
d.expand()
}
if d.len == 0 {
d.front = 0
d.back = 0
} else {
d.back = (d.back + 1) % d.cap
}
d.buf[d.back] = val
d.len += 1
}
//PushFront pushes the provided value to the front of the deque.
func (d *Deque[T]) PushFront(val T) {
if d.len == d.cap {
d.expand()
}
if d.len == 0 {
d.front = 0
d.back = 0
} else {
d.front = (d.front - 1) % d.cap
if d.front < 0 {
d.front = d.cap + d.front
}
}
d.buf[d.front] = val
d.len += 1
}
//PopBack removes the value at the back of the deque and returns it. Returns `ErrDequeEmpty` if the deque is empty.
func (d *Deque[T]) PopBack() (T, error) {
if d.len == 0 {
var t T
return t, ErrDequeEmpty
}
last := d.buf[d.back]
if d.len == 1 {
d.front = -1
d.back = -1
} else {
d.back = (d.back - 1) % d.cap
if d.back < 0 {
d.back = d.cap + d.back
}
}
d.len--
if d.len > 0 && float64(d.cap)/float64(d.len) > (1.0+math.Sqrt2) {
d.contract()
}
return last, nil
}
//PopFront removes the value at the front of the deque and returns it. Returns `ErrDequeEmpty` if the deque is empty.
func (d *Deque[T]) PopFront() (T, error) {
if d.len == 0 {
var t T
return t, ErrDequeEmpty
}
first := d.buf[d.front]
if d.len == 1 {
d.front = -1
d.back = -1
} else {
d.front = (d.front + 1) % d.cap
}
d.len--
if d.len > 0 && float64(d.cap)/float64(d.len) > (1.0+math.Sqrt2) {
d.contract()
}
return first, nil
}