-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
371 lines (297 loc) · 8.27 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package lists
import (
"errors"
"fmt"
"sort"
"strings"
)
type List struct {
Values []string
Separator string
Index int
}
var OutOfBounds = errors.New("index out of bounds")
// ParseArray returns a new List.
func ParseArray(values []string, separator string) (list List) {
return List{values, separator, -1}
}
// ParseString parses string to a new List.
func ParseString(values string, separator string) (list List) {
list.Values = strings.Split(values, separator)
list.Separator = separator
list.Index = -1
return
}
// ToString returns the list as string.
func (list *List) ToString() (tostr string) {
for i, l := range list.Values {
if i > 0 {
tostr += list.Separator
}
tostr += l
}
return
}
// ToArray returns the list as array.
func (list *List) ToArray() []string {
return list.Values
}
// Len returns the number of elements in the list.
func (list *List) Len() int {
return len(list.Values)
}
// First returns the element at the first position.
func (list *List) First() string {
list.Index = 0
return list.Values[list.Index]
}
// Last returns the element at the last position.
func (list *List) Last() string {
list.Index = list.Len() - 1
return list.Values[list.Index]
}
// Find returns the index of the first occurrence of a value within a list or returns -1 if no value is found.
// The search is case-sensitive.
func (list *List) Find(value string) int {
for i, v := range list.Values {
if v == value {
return i
}
}
return -1
}
// Exist returns true or false when a value is or not found.
func (list *List) Exist(value string) bool {
return list.Find(value) > -1
}
// Exist checks if there are more elements to move forward
func (list *List) c() bool {
i := list.Len() - list.Index
return i > 1
}
// HasNext checks if there are more elements to move forward
func (list *List) HasNext() bool {
i := list.Len() - list.Index
return i > 1
}
// Next returns the next element.
func (list *List) Next() (string, error) {
// check index position
if list.IsOutOfBound(list.Index) {
return "", OutOfBounds
}
// go to the next element
list.Index++
// get value
value, _ := list.GetAt(list.Index)
// return it
return value, nil
}
// MoveTo moves the index to a given position.
func (list *List) MoveTo(index int) error {
// check list size
if list.IsOutOfBound(index) {
return OutOfBounds
}
// move index
list.Index = index
return nil
}
// Rewind moves the index before the first element.
func (list *List) Rewind() {
list.Index = -1
}
// IsEmpty returns true if list has no element.
func (list *List) IsEmpty() bool {
return list.Len() == 0
}
// IsOutOfBound returns true if index is out of bound.
func (list *List) IsOutOfBound(index int) bool {
return index >= list.Len()
}
// Get returns the element at the current position.
func (list *List) Get() string {
return list.Values[list.Index]
}
// GetAt returns the element at the specified position.
func (list *List) GetAt(position int) (string, error) {
// check list size
if list.IsOutOfBound(position) {
return "", OutOfBounds
}
// return element
return list.Values[position], nil
}
// Delete removes the element at the current position.
func (list *List) Delete() error {
return list.DeleteAt(list.Index)
}
// DeleteAt returns list with element deleted at the specified position.
func (list *List) DeleteAt(position int) error {
// check list size
if list.IsOutOfBound(position) {
return OutOfBounds
}
// remove element
list.Values = append(list.Values[:position], list.Values[position+1:]...)
// ok!
return nil
}
// DeleteFirst removes the first occurrence of the given element.
func (list *List) DeleteFirst(element string) {
if i := list.Find(element); i > -1 {
list.DeleteAt(i)
}
}
// DeleteLast removes the last occurrence of the given element.
func (list *List) DeleteLast(element string) {
for i := list.Len() - 1; i >= 0; i-- {
if value, _ := list.GetAt(i); value == element {
list.DeleteAt(i)
return
}
}
}
// DeleteAll removes matching elements.
func (list *List) DeleteAll(elements ...string) {
for _, e := range elements {
if i := list.Find(e); i > -1 {
list.DeleteAt(i) // delete element
list.DeleteAll(e) // call it again
}
}
}
// Filter returns a new list with elements that match the given elements.
func (list *List) Filter(elements ...string) {
var filtered []string
for _, value := range list.Values {
for _, e := range elements {
if value == e {
filtered = append(filtered, value)
}
}
}
list.Values = filtered
}
// Range returns the subset of a list.
func (list *List) Range(start, end int) List {
return ParseArray(list.Values[start:end], list.Separator)
}
// From returns the subset of a list from an index until the end.
func (list *List) From(start int) List {
return ParseArray(list.Values[start:], list.Separator)
}
// Until returns the subset of a list from its beginning until the given position.
func (list *List) Until(end int) List {
return ParseArray(list.Values[:end], list.Separator)
}
// Set sets a new value assigned to its element based on current index.
func (list *List) Set(value string) {
list.Values[list.Index] = value
}
// SetLast sets a new value assigned to the last element.
func (list *List) SetLast(value string) {
list.Values[list.Len()-1] = value
}
// SetAt sets a new value assigned to its element based on a given position.
func (list *List) SetAt(position int, value string) error {
// check list size
if list.IsOutOfBound(position) {
return OutOfBounds
}
// set new value
list.Values[position] = value
return nil
}
// Append adds values to the end of the list.
func (list *List) Append(values ...string) {
for _, value := range values {
list.Values = append(list.Values, value)
}
}
// Insert adds a new element to the list at a specified position.
func (list *List) Insert(position int, value string) {
list.Values = append(list.Values[:position], append([]string{value}, list.Values[position:]...)...)
}
// Swap swaps the position of two elements.
func (list *List) Swap(x, y int) {
list.Values[x], list.Values[y] = list.Values[y], list.Values[x]
}
// Shift moves an element from a position to another.
func (list *List) Shift(from, to int) {
list.Values = append(list.Values[:from], append(list.Values[from+1:to], append(list.Values[from:from+1], list.Values[to:]...)...)...)
}
// Split splits a element in 'n' new elements
func (list *List) Split(position int, interval ...int) {
from := 0
elements := make([]string, len(interval))
for i := 0; i < len(interval); i++ {
elements[i] = list.Values[position][from:interval[i]]
from = interval[i]
}
list.Values = append(list.Values[:position], append(elements, list.Values[position+1:]...)...)
}
// Upper sets all elements as uppercase.
func (list *List) Upper() {
for i, value := range list.Values {
list.Values[i] = strings.ToUpper(value)
}
}
// Lower sets all elements as lowercase.
func (list *List) Lower() {
for i, value := range list.Values {
list.Values[i] = strings.ToLower(value)
}
}
// Sort sorts elements. (A to Z)
func (list *List) Sort() {
var sorter sort.StringSlice = list.Values
sorter.Sort()
list.Values = sorter[:]
}
// Reverse reverses elements. (Z to A)
func (list *List) Reverse() {
var sorter sort.StringSlice = list.Values
sort.Sort(sort.Reverse(sorter))
list.Values = sorter[:]
}
// Count counts the number of occurrences
func (list *List) Count(element string) (count int) {
for _, value := range list.Values {
if value == element {
count++
}
}
return
}
// Dedup deletes duplicated elements in the list.
func (list *List) Dedup() {
for i := list.Len() - 1; i >= 0; i-- {
value, _ := list.GetAt(i)
if count := list.Count(value); count > 1 {
list.DeleteLast(value)
}
}
}
// Quote wraps elements with a given rune.
func (list *List) Quote(r rune) {
for i := range list.Values {
list.Values[i] = fmt.Sprintf("%s%s%s", string(r), list.Values[i], string(r))
}
}
// AppendNew appends a new value to the list only if the new element does not exist.
func (list *List) AppendNew(values ...string) {
for _, value := range values {
if !list.Exist(value) {
list.Values = append(list.Values, value)
}
}
}
// Replacer replaces elements with a given map.
func (list *List) Replacer(fromTo map[string]string) {
for i, value := range list.Values {
if v, ok := fromTo[value]; ok {
list.Values[i] = v
}
}
}