-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_uniq.go
196 lines (169 loc) · 4.31 KB
/
slice_uniq.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
package gofn
// IsUnique checks a slice for uniqueness
func IsUnique[T comparable, S ~[]T](s S) bool {
length := len(s)
if length <= 1 {
return true
}
seen := make(map[T]struct{}, length)
for i := 0; i < length; i++ {
v := s[i]
if _, ok := seen[v]; ok {
return false
}
seen[v] = struct{}{}
}
return true
}
// IsUniqueBy checks a slice for uniqueness using key function
func IsUniqueBy[T any, U comparable, S ~[]T](s S, keyFunc func(t T) U) bool {
length := len(s)
if length <= 1 {
return true
}
seen := make(map[U]struct{}, length)
for i := 0; i < length; i++ {
v := keyFunc(s[i])
if _, ok := seen[v]; ok {
return false
}
seen[v] = struct{}{}
}
return true
}
// Deprecated: use IsUniqueBy instead
func IsUniquePred[T any, U comparable, S ~[]T](s S, keyFunc func(t T) U) bool {
return IsUniqueBy(s, keyFunc)
}
// FindUniques finds all elements that are unique in the given slice.
func FindUniques[T comparable, S ~[]T](s S) S {
return FindUniquesBy(s, func(t T) T { return t })
}
// FindUniquesBy finds all elements that are unique in the given slice.
func FindUniquesBy[T any, U comparable, S ~[]T](s S, keyFunc func(T) U) S {
length := len(s)
if length <= 1 {
return append(S{}, s...)
}
seen := make(map[U]int, length) // Map to store elements and their first indexes
dupFlags := make([]bool, length) // Array to store flags of duplication of elements
uniqCount := length
for i := range s {
k := keyFunc(s[i])
if firstIndex, ok := seen[k]; ok {
dupFlags[firstIndex] = true
dupFlags[i] = true
uniqCount--
continue
}
seen[k] = i
}
if uniqCount == length {
return append(S{}, s...)
}
result := make(S, 0, uniqCount)
for i := 0; i < length; i++ {
if !dupFlags[i] {
result = append(result, s[i])
}
}
return result
}
// FindDuplicates finds all elements that are duplicated in the given slice.
func FindDuplicates[T comparable, S ~[]T](s S) S {
return FindDuplicatesBy(s, func(t T) T { return t })
}
// FindDuplicatesBy finds all elements that are duplicated in the given slice.
func FindDuplicatesBy[T any, U comparable, S ~[]T](s S, keyFunc func(T) U) S {
length := len(s)
if length <= 1 {
return S{}
}
seen := make(map[U]int, length) // Map to store elements and their first indexes
dupFlags := make([]bool, length) // Array to store flags of duplication of elements
dupCount := 0
for i := range s {
k := keyFunc(s[i])
if firstIndex, ok := seen[k]; ok {
dupFlags[firstIndex] = true
dupCount++
continue
}
seen[k] = i
}
if dupCount == 0 {
return S{}
}
result := make(S, 0, dupCount)
for i := 0; i < length; i++ {
if dupFlags[i] {
result = append(result, s[i])
}
}
return result
}
// ToSet calculates unique values of a slice
func ToSet[T comparable, S ~[]T](s S) S {
length := len(s)
if length <= 1 {
return append(S{}, s...)
}
seen := make(map[T]struct{}, length)
result := make(S, 0, length)
for i := 0; i < length; i++ {
v := s[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
result = append(result, v)
}
return result
}
// ToSetBy calculates unique values of a slice with custom key function
func ToSetBy[T any, K comparable, S ~[]T](s S, keyFunc func(t T) K) S {
length := len(s)
if length <= 1 {
return append(S{}, s...)
}
seen := make(map[K]struct{}, length)
result := make(S, 0, length)
for i := 0; i < length; i++ {
v := s[i]
k := keyFunc(v)
if _, ok := seen[k]; ok {
continue
}
seen[k] = struct{}{}
result = append(result, v)
}
return result
}
// Deprecated: use ToSetBy instead
func ToSetPred[T any, K comparable, S ~[]T](s S, keyFunc func(t T) K) S {
return ToSetBy(s, keyFunc)
}
// ToSetByReverse calculates unique values of a slice with custom key function.
// Unlike ToSetBy, this function iterates over the slice from the end.
func ToSetByReverse[T any, K comparable, S ~[]T](s S, keyFunc func(t T) K) S {
length := len(s)
if length <= 1 {
return append(S{}, s...)
}
seen := make(map[K]struct{}, length)
result := make(S, 0, length)
for i := length - 1; i >= 0; i-- {
v := s[i]
k := keyFunc(v)
if _, ok := seen[k]; ok {
continue
}
seen[k] = struct{}{}
result = append(result, v)
}
return result
}
// Deprecated: use ToSetByReverse instead
func ToSetPredReverse[T any, K comparable, S ~[]T](s S, keyFunc func(t T) K) S {
return ToSetByReverse(s, keyFunc)
}