-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
292 lines (266 loc) · 6.1 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
package main
import (
"fmt"
"reflect"
)
func itemToString[T any](item T) string {
return fmt.Sprintf("%v", item)
}
/**
* A generic list implementation in Go.
* This implementation uses the `any` type constraint to create a generic list.
* The `any` type constraint allows the list to store any type of data.
*
* @author: Lemon_Juiced
*/
type List[T any] struct {
items []T
}
/**
* Creates a new list.
*
* @return: A new list.
*/
func newList[T any]() *List[T] {
return &List[T]{items: []T{}}
}
/**
* Adds an item to the list.
*
* @param item: The item to add.
*/
func (l *List[T]) add(item T) {
l.items = append(l.items, item)
}
/**
* Adds multiple items to the list.
*
* @param items: The items to add.
*/
func (l *List[T]) addAll(items []T) {
l.items = append(l.items, items...)
}
/**
* Gets the size of the list.
*
* @return: The size of the list.
*/
func (l *List[T]) size() int {
return len(l.items)
}
/**
* Gets an item from the list at the given index.
*
* @param index: The index of the item to get.
* @return: The item at the given index, or throws an error if the index is out of bounds.
*/
func (l *List[T]) get(index int) T {
if index < 0 || index >= len(l.items) {
panic(formatError("Index out of bounds."))
}
return l.items[index]
}
/**
* Removes an item from the list at the given index.
*
* @param index: The index of the item to remove.
* @return: The item that was removed, or throws an error if the index is out of bounds.
*/
func (l *List[T]) remove(index int) T {
if index < 0 || index >= len(l.items) {
panic(formatError("Index out of bounds."))
}
item := l.items[index]
l.items = append(l.items[:index], l.items[index+1:]...)
return item
}
/**
* Clears the list. (Removes all items from the list)
*/
func (l *List[T]) clear() {
l.items = []T{}
}
/**
* Checks if the list contains the given item.
*
* @param item: The item to check for.
* @return: True if the list contains the item, false otherwise.
*/
func (l *List[T]) contains(item T) bool {
for _, i := range l.items {
if reflect.DeepEqual(i, item) {
return true
}
}
return false
}
/**
* Checks if the list is empty.
*
* @return: True if the list is empty, false otherwise.
*/
func (l *List[T]) isEmpty() bool {
return len(l.items) == 0
}
/**
* Gets the head of the list as if it were treated as a stack/queue.
*
* @return: The head of the list, or throws an error if the list is empty.
*/
func (l *List[T]) head() T {
if len(l.items) == 0 {
panic(formatError("List is empty."))
}
return l.items[0]
}
/**
* Gets the tail of the list as if it were treated as a stack/queue.
*
* @return: The tail of the list, or throws an error if the list is empty.
*/
func (l *List[T]) tail() T {
if len(l.items) == 0 {
panic(formatError("List is empty."))
}
return l.items[len(l.items)-1]
}
/**
* Removes the head of the list (pops) as if it were treated as a stack/queue.
*/
func (l *List[T]) pop() {
if len(l.items) == 0 {
panic(formatError("List is empty."))
}
l.items = l.items[1:]
}
/**
* Removes the tail of the list (dequeues) as if it were treated as a stack/queue.
*/
func (l *List[T]) dequeue() {
if len(l.items) == 0 {
panic(formatError("List is empty."))
}
l.items = l.items[:len(l.items)-1]
}
/**
* Converts the list to a string.
*
* @return: The string representation of the list.
*/
func (l *List[T]) asString() string {
str := "["
for i, item := range l.items {
str += itemToString(item)
if i < len(l.items)-1 {
str += ", "
}
}
str += "]"
return str
}
/**
* Converts the list to a string with a custom separator.
*
* @param separator: The separator to use.
* @return: The string representation of the list.
*/
func (l *List[T]) asStringWithSeparator(separator string) string {
str := ""
for i, item := range l.items {
str += itemToString(item)
if i < len(l.items)-1 {
str += separator
}
}
return str
}
/**
* Sets the item at the given index to the given item.
*
* @param index: The index of the item to set.
* @param item: The item to set.
*/
func (l *List[T]) set(index int, item T) {
if index < 0 || index >= len(l.items) {
panic(formatError("Index out of bounds."))
}
l.items[index] = item
}
/**
* Swaps the items at the given indices.
*
* @param index1: The first index.
* @param index2: The second index.
*/
func (l *List[T]) swap(index1 int, index2 int) {
if index1 < 0 || index1 >= len(l.items) || index2 < 0 || index2 >= len(l.items) {
panic(formatError("Index out of bounds."))
}
l.items[index1], l.items[index2] = l.items[index2], l.items[index1]
}
/**
* Reverses the list.
*/
func (l *List[T]) reverse() {
for i := 0; i < len(l.items)/2; i++ {
l.items[i], l.items[len(l.items)-i-1] = l.items[len(l.items)-i-1], l.items[i]
}
}
/**
* Sorts the list using the given comparison function.
*
* @param compare: The comparison function.
*/
func (l *List[T]) sort(compare func(T, T) bool) {
for i := 0; i < len(l.items); i++ {
for j := i + 1; j < len(l.items); j++ {
if compare(l.items[j], l.items[i]) {
l.items[i], l.items[j] = l.items[j], l.items[i]
}
}
}
}
/**
* Appends the given list to the current list.
*
* @param list: The list to append.
*/
func (l *List[T]) append(list *List[T]) {
l.items = append(l.items, list.items...)
}
/**
* Gets the union of the current list and the given list.
*
* @param list: The list to union with.
* @return: The union of the two lists.
*/
func (l *List[T]) union(list *List[T]) *List[T] {
union := newList[T]()
union.addAll(l.items)
union.addAll(list.items)
return union
}
/**
* Gets the intersection of the current list and the given list.
*
* @param list: The list to intersect with.
* @return: The intersection of the two lists.
*/
func (l *List[T]) intersection(list *List[T]) *List[T] {
intersection := newList[T]()
for _, item := range l.items {
if list.contains(item) {
intersection.add(item)
}
}
return intersection
}
/**
* Formats an error message using red text with the ANSI escape codes.
*
* @param message: The message to format.
* @return: The formatted error message.
*/
func formatError(message string) string {
return "\033[31mError: " + message + "\033[0m\n"
}