-
Notifications
You must be signed in to change notification settings - Fork 191
/
list.go
216 lines (177 loc) · 3.55 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
package arrutil
import (
"sort"
"strings"
"github.com/gookit/goutil/comdef"
)
// Ints type
type Ints[T comdef.Integer] []T
// String to string
func (is Ints[T]) String() string {
return ToString(is)
}
// Has given element
func (is Ints[T]) Has(i T) bool {
for _, iv := range is {
if i == iv {
return true
}
}
return false
}
// First element value.
func (is Ints[T]) First(defVal ...T) T {
if len(is) > 0 {
return is[0]
}
if len(defVal) > 0 {
return defVal[0]
}
panic("empty integer slice")
}
// Last element value.
func (is Ints[T]) Last(defVal ...T) T {
if len(is) > 0 {
return is[len(is)-1]
}
if len(defVal) > 0 {
return defVal[0]
}
panic("empty integer slice")
}
// Sort the int slice
func (is Ints[T]) Sort() {
sort.Sort(is)
}
// Len get length
func (is Ints[T]) Len() int {
return len(is)
}
// Less compare two elements
func (is Ints[T]) Less(i, j int) bool {
return is[i] < is[j]
}
// Swap elements by indexes
func (is Ints[T]) Swap(i, j int) {
is[i], is[j] = is[j], is[i]
}
// Strings type
type Strings []string
// String to string
func (ss Strings) String() string {
return strings.Join(ss, ",")
}
// Join to string
func (ss Strings) Join(sep string) string {
return strings.Join(ss, sep)
}
// Has given element
func (ss Strings) Has(sub string) bool {
return ss.Contains(sub)
}
// Contains given element
func (ss Strings) Contains(sub string) bool {
for _, s := range ss {
if s == sub {
return true
}
}
return false
}
// First element value.
func (ss Strings) First(defVal ...string) string {
if len(ss) > 0 {
return ss[0]
}
if len(defVal) > 0 {
return defVal[0]
}
panic("empty string list")
}
// Last element value.
func (ss Strings) Last(defVal ...string) string {
if len(ss) > 0 {
return ss[len(ss)-1]
}
if len(defVal) > 0 {
return defVal[0]
}
panic("empty string list")
}
// Sort the string slice
func (ss Strings) Sort() {
sort.Strings(ss)
}
// SortedList definition for compared type
type SortedList[T comdef.Compared] []T
// Len get length
func (ls SortedList[T]) Len() int {
return len(ls)
}
// Less compare two elements
func (ls SortedList[T]) Less(i, j int) bool {
return ls[i] < ls[j]
}
// Swap elements by indexes
func (ls SortedList[T]) Swap(i, j int) {
ls[i], ls[j] = ls[j], ls[i]
}
// IsEmpty check
func (ls SortedList[T]) IsEmpty() bool {
return len(ls) == 0
}
// String to string
func (ls SortedList[T]) String() string {
return ToString(ls)
}
// Has given element
func (ls SortedList[T]) Has(el T) bool {
return ls.Contains(el)
}
// Contains given element
func (ls SortedList[T]) Contains(el T) bool {
for _, v := range ls {
if v == el {
return true
}
}
return false
}
// First element value.
func (ls SortedList[T]) First(defVal ...T) T {
if len(ls) > 0 {
return ls[0]
}
if len(defVal) > 0 {
return defVal[0]
}
panic("empty list")
}
// Last element value.
func (ls SortedList[T]) Last(defVal ...T) T {
if ln := len(ls); ln > 0 {
return ls[ln-1]
}
if len(defVal) > 0 {
return defVal[0]
}
panic("empty list")
}
// Remove given element
func (ls SortedList[T]) Remove(el T) SortedList[T] {
return Filter(ls, func(v T) bool {
return v != el
})
}
// Filter the slice, default will filter zero value.
func (ls SortedList[T]) Filter(filter ...comdef.MatchFunc[T]) SortedList[T] {
return Filter(ls, filter...)
}
// Map the slice to new slice. TODO syntax ERROR: Method cannot have type parameters
// func (ls SortedList[T]) Map[V any](mapFn MapFn[T, V]) SortedList[V] {
// return Map(ls, mapFn)
// }
// Sort the slice
func (ls SortedList[T]) Sort() {
sort.Sort(ls)
}