-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
350 lines (300 loc) · 8.17 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package arraylist
import (
"fmt"
"github.com/golanglibs/gocollections/comparer"
"github.com/golanglibs/gocollections/generic"
"github.com/golanglibs/gocollections/list"
)
/*
Array-based list. Uses go's native slice internally.
Implements Lister and Collectioner.
List is not thread safe
*/
type List[T any] struct {
equals func(*T, *T) bool
container []T
size int
cap int
}
/*
Creates a new instance of List with the given elements with a default equality comparer
and returns it
If no elements are given, then an empty list is created. Elements must be comparable
*/
func New[K comparable](elements ...K) List[K] {
var container []K
size := len(elements)
if size > 0 {
container = make([]K, size)
copy(container, elements)
}
return List[K]{
equals: comparer.DefaultEquals[K],
container: container,
size: size,
cap: size,
}
}
/*
Creates a new instance of List with the given elements with nil equality comparer and returns it
If no elements are given, then an empty list is created. Elements can be of any type
*/
func NewOfAny[T any](elements ...T) List[T] {
var container []T
size := len(elements)
if size > 0 {
container = append(container, elements...)
}
return List[T]{
container: container,
size: size,
cap: size,
}
}
/*
Creates a new instance of List from the given collection with a default equality comparer and returns it
Elements of the given collection must be comparable
*/
func NewFromCollection[K comparable](c generic.Collectioner[K]) List[K] {
var copiedSlice []K
c.ForEach(func(element *K) {
copiedSlice = append(copiedSlice, *element)
})
size := len(copiedSlice)
return List[K]{
equals: comparer.DefaultEquals[K],
container: copiedSlice,
size: size,
cap: size,
}
}
/*
Creates a new instance of List from the given collection with nil equality comparer and returns it
Elements of the given collection can be of any type
*/
func NewOfAnyFromCollection[T any](c generic.Collectioner[T]) List[T] {
var copiedSlice []T
c.ForEach(func(element *T) {
copiedSlice = append(copiedSlice, *element)
})
size := len(copiedSlice)
return List[T]{
container: copiedSlice,
size: size,
cap: size,
}
}
/*
Sets the equality comparer with the given equals function. Implements Lister.SetEqualityComparer
*/
func (l *List[T]) SetEqualityComparer(equals func(*T, *T) bool) {
l.equals = equals
}
/*
Retrieves and returns a reference to the element at the given index. Panics if the given index is out of range.
Implements lister.At
*/
func (l *List[T]) At(index int) *T {
if !l.isValidIndex(index) {
err := fmt.Sprintf("List.At could not retrieve element because given index %d is out of range", index)
panic(err)
}
return &l.container[index]
}
/*
Sets the given value at the given index. Panics if the given index is out of range.
Implements lister.Set
*/
func (l *List[T]) Set(index int, value T) {
if !l.isValidIndex(index) {
err := fmt.Sprintf("List.Set could not set given value because given index %d is out of range", index)
panic(err)
}
l.container[index] = value
}
/*
Returns the length of the List. Implements lister.Size
*/
func (l *List[T]) Size() int {
return l.size
}
/*
Returns true if the List is empty. Implements Lister.Empty and Collectioner.Empty
*/
func (l *List[T]) Empty() bool {
return l.size == 0
}
/*
Returns a reference to the first element (at index 0) in the list
*/
func (l *List[T]) Front() *T {
if l.size == 0 {
panic("ArrayList.Front failed because the list is empty")
}
return &l.container[0]
}
func (l *List[T]) Back() *T {
if l.size == 0 {
panic("ArrayList.Back failed because the list is empty")
}
return &l.container[l.size-1]
}
/*
Adds the given element to the end of the List. Add will always return true for List.
Implements Lister.Add and Collectioner.Add
*/
func (l *List[T]) Add(element T) bool {
if l.size == l.cap {
l.container = append(l.container, element)
l.cap++
} else {
l.container[l.size] = element
}
l.size++
return true
}
/*
Removes the last element of the list.
Implements Lister.RemoveBack
*/
func (l *List[T]) RemoveBack() {
if l.size == 0 {
panic("ArrayList.RemoveBack failed because the list is empty")
}
l.size--
}
/*
Adds the given element at the given index and return true.
If the given index is out of range, List.Insert will not add the element and return false.
Implements Lister.Insert
*/
func (l *List[T]) Insert(index int, element T) bool {
if index < 0 || index > l.size { // index == length is ok to allow for insertion at end after last index
return false
}
if index == l.size {
l.Add(element)
return true
}
l.container = append(l.container[:index+1], l.container[index:]...)
l.container[index] = element
l.size++
if l.size > l.cap {
l.cap++
}
return true
}
/*
Adds the given value to the front of the list.
Implements Lister.AddToFront
*/
func (l *List[T]) AddToFront(element T) {
l.Insert(0, element)
}
/*
Removes the first element of the list
Implements Lister.RemoveFront
*/
func (l *List[T]) RemoveFront() {
l.RemoveAt(0)
}
/*
Removes the first occurrence of the given element from the List.
Returns true if the element is found and removed. Returns false if the element to remove is not found.
Equality is determined by the equality comparer set either automatically (through constructors for comparable
elements or manually (through Lister.SetEqualityComparer).
Panics if the equality comparer "equals" method is not set.
Implements Lister.Remove and Collectioner.Remove
*/
func (l *List[T]) Remove(element T) bool {
elementIndex := l.indexOf(element)
if elementIndex == -1 {
return false
}
l.container = append(l.container[:elementIndex], l.container[elementIndex+1:]...)
l.size--
return true
}
/*
Removes the element at the given index from the List. Panics if the given index is out of range
Implements Lister.RemoveAt
*/
func (l *List[T]) RemoveAt(index int) {
if !l.isValidIndex(index) {
err := fmt.Sprintf(
"ArrayList.RemoveAt cannot remove element at index %d because it is out of range",
index,
)
panic(err)
}
l.container = append(l.container[:index], l.container[index+1:]...)
l.size--
}
/*
Returns the index of the given element.
Returns -1 if the element is not found.
Equality is determined by the equality comparer set either automatically (through constructors for comparable elements)
or manually (through Lister.SetEqualityComparer).
Implements Lister.IndexOf
*/
func (l *List[T]) IndexOf(element T) int {
return l.indexOf(element)
}
/*
Returns true if the given element exists in the List. Returns false otherwise.
Equality is determined by the equality comparer set either automatically (through constructors for comparable elements)
or manually (through Lister.SetEqualityComparer).
Implements Lister.Contains and Collectioner.Contains
*/
func (l *List[T]) Contains(element T) bool {
return l.indexOf(element) != -1
}
func (l *List[T]) indexOf(element T) int {
if l.equals == nil {
panic("Cannot compute equality of elements since equality comparer is not set")
}
for i, v := range l.container {
if l.equals(&v, &element) {
return i
}
}
return -1
}
/*
Returns a sub list of the current List from start index (inclusive) to end index (exclusive).
The returned sub list is a new, copied list of the current List.
Implements Lister.SubList
*/
func (l *List[T]) SubList(start int, end int) list.Lister[T] {
if !l.isValidIndex(start) || end > len(l.container) || start >= end {
panic("List.SubList Cannot create a sub list because invalid range was given")
}
newSize := end - start
newContainer := make([]T, newSize)
copy(newContainer, l.container[start:end])
return &List[T]{
container: newContainer,
size: newSize,
cap: newSize,
}
}
func (l *List[T]) isValidIndex(index int) bool {
return 0 <= index && index < l.size
}
/*
Empties the current List. This does not actually set the internal slice to nil. It simply sets the internal
size counter to zero.
Implements Lister.Clear and Collectioner.Clear
*/
func (l *List[T]) Clear() {
l.size = 0
}
/*
Loops through the List and executes the given "do" function on each element.
Implements Seter.ForEach and Collectioner.ForEach
*/
func (l *List[T]) ForEach(do func(*T)) {
for _, v := range l.container {
do(&v)
}
}