-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
276 lines (234 loc) · 5.59 KB
/
list.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// MIT License
// Copyright (c) 2022 Brian Reece
package list
import (
"github.com/bdreece/gollections/errors"
"github.com/bdreece/gollections/iterator"
)
type node[T any] struct {
value T
next *node[T]
prev *node[T]
}
func newNode[T any](value T, prev, next *node[T]) *node[T] {
return &node[T]{value, next, prev}
}
// List is the doubly-linked list data structure.
type List[T any] struct {
head *node[T]
tail *node[T]
length int
}
// New constructs a new List
func New[T any]() *List[T] {
return &List[T]{head: nil, tail: nil, length: 0}
}
// PushBack pushes an item onto the back
// of the doubly-linked list.
func (l *List[T]) PushBack(item T) {
l.length += 1
if l.head == nil {
l.head = newNode(item, nil, nil)
l.tail = l.head
return
}
l.tail.next = newNode(item, l.tail, nil)
l.tail = l.tail.next
}
// PopBack removes and returns an item from
// the back of the List. Returns nil, errors.Empty
// if the List is empty.
func (l *List[T]) PopBack() (*T, error) {
if l.tail == nil {
return nil, errors.Empty{}
}
value := new(T)
*value = l.tail.value
if l.tail.prev != nil {
// Recede tail
l.tail = l.tail.prev
// Unlink removed tail
l.tail.next = nil
} else {
// Truncate list
l.head = nil
l.tail = nil
}
l.length -= 1
return value, nil
}
// PeekBack returns a pointer to the back
// of the List. Returns nil, errors.Empty
// if the List is empty.
func (l *List[T]) PeekBack() (*T, error) {
if l.tail == nil {
return nil, errors.Empty{}
}
return &l.tail.value, nil
}
// PushFront pushes an item onto the front
// of the double-linked list.
func (l *List[T]) PushFront(item T) {
l.length += 1
if l.head == nil {
l.head = newNode(item, nil, nil)
l.tail = l.head
return
}
l.head.prev = newNode(item, nil, l.head)
l.head = l.head.prev
}
// PopFront removes and returns an item from
// the front of the List. Returns nil, errors.Empty
// if the List is empty.
func (l *List[T]) PopFront() (*T, error) {
if l.head == nil {
return nil, errors.Empty{}
}
value := new(T)
*value = l.head.value
if l.head.next != nil {
// Advance head
l.head = l.head.next
// Unlink deleted head
l.head.prev = nil
} else {
// Truncate list
l.head = nil
l.tail = nil
}
l.length -= 1
return value, nil
}
// PeekFront returns a pointer to the front
// of the List. Returns nil, errors.Empty
// if the List is empty.
func (l *List[T]) PeekFront() (*T, error) {
if l.head == nil {
return nil, errors.Empty{}
}
return &l.head.value, nil
}
// Enqueue appends an item to the end of
// the list.
func (l *List[T]) Enqueue(elem T) {
l.PushBack(elem)
}
// Dequeue removes and returns the first
// element in the list.
func (l *List[T]) Dequeue() (*T, error) {
return l.PopFront()
}
// Push prepends the list with an element.
func (l *List[T]) Push(elem T) {
l.PushFront(elem)
}
// Pop removes and returns the first
// element in the list.
func (l *List[T]) Pop() (*T, error) {
return l.PopFront()
}
// Peek returns the first element in
// the list without removing it.
func (l *List[T]) Peek() (*T, error) {
return l.PeekFront()
}
// Get returns a pointer to an item in the
// List located at index. Returns nil, errors.Empty
// if the List is empty. Returns nil,
// errors.IndexOutOfBounds if index < 0 or index > len(l).
func (l *List[T]) Get(index int) (*T, error) {
if l.head == nil {
return nil, errors.Empty{}
}
walk := l.head
for i := 0; i < index; i++ {
if walk.next == nil {
return nil, errors.IndexOutOfBounds{Index: index, Bounds: i}
}
walk = walk.next
}
return &walk.value, nil
}
// Set sets the item located at index in
// the List. returns errors.IndexOutOfBounds
// if index < 0 or index > len(l). Returns
// errors.Empty if index > 0 and List is empty.
func (l *List[T]) Set(index int, item T) error {
if l.head == nil {
if index == 0 {
l.head = newNode(item, nil, nil)
return nil
}
return errors.Empty{}
}
walk := l.head
for i := 0; i < index; i++ {
if walk.next == nil {
return errors.IndexOutOfBounds{Index: index, Bounds: i}
}
walk = walk.next
}
walk.value = item
return nil
}
// Ins inserts an element into the list at the specified
// index. Returns errors.IndexOutOfBounds if index < 0
// or index > len(l).
func (l *List[T]) Ins(index int, elem T) error {
if l.head == nil {
return errors.Empty{}
}
walk := l.head
for i := 0; i < index; i++ {
if walk.next == nil {
return errors.IndexOutOfBounds{Index: index, Bounds: i}
}
walk = walk.next
}
item := newNode[T](elem, walk.prev, walk)
item.prev.next = item
walk.prev = item
return nil
}
// Del removes and returns an element from the list at the
// specified index. Returns errors.IndexOutOfBounds if
// index < 0 or index > len(l).
func (l *List[T]) Del(index int) (*T, error) {
if l.head == nil {
return nil, errors.Empty{}
}
walk := l.head
for i := 0; i < index; i++ {
if walk.next == nil {
return nil, errors.IndexOutOfBounds{Index: index, Bounds: i}
}
walk = walk.next
}
value := new(T)
*value = walk.value
// Unlink neighbors
walk.prev.next = nil
walk.next.prev = nil
return value, nil
}
// Collect appends a variable number of items to the list.
func (l *List[T]) Collect(items ...T) {
for _, item := range items {
l.PushBack(item)
}
}
// IntoIterator returns an iterator over the items in the list.
func (l *List[T]) IntoIterator() iterator.Iterator[T] {
return &Iterator[T]{l.head}
}
// FromIterator collects the items from the iterator into the
// list.
func (l *List[T]) FromIterator(iter iterator.Iterator[T]) error {
if err := iterator.ForEach(iter, func(item *T) {
l.PushBack(*item)
}); err != nil {
return err
}
return nil
}