-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslice.go
208 lines (181 loc) · 4.1 KB
/
slice.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
package utils
import (
"strconv"
)
// ContainsInt64 tells whether a slice contains x.
func ContainsInt64(a []int64, x int64) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
// ContainsString tells whether a slice contains x.
func ContainsString(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
// SliceAtoi -> convert array of string to array of integer
func SliceAtoi(s []string) ([]int, error) {
var arr []int
for _, val := range s {
i, err := strconv.Atoi(val)
if err != nil {
return arr, err
}
arr = append(arr, i)
}
return arr, nil
}
// DifferenceString :nodoc:
func DifferenceString(slice1 []string, slice2 []string) []string {
var diff []string
// Loop two times, first to find slice1 strings not in slice2,
// second loop to find slice2 strings not in slice1
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff = append(diff, s1)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
// DifferenceInt64 :nodoc:
func DifferenceInt64(slice1 []int64, slice2 []int64) []int64 {
var diff []int64
// Loop two times, first to find slice1 int64 not in slice2,
// second loop to find slice2 int64 not in slice1
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff = append(diff, s1)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
// UniqueString :nodoc:
func UniqueString(elements []string) []string {
encountered := map[string]bool{}
result := []string{}
for idx := range elements {
if _, ok := encountered[elements[idx]]; ok {
continue
}
encountered[elements[idx]] = true
result = append(result, elements[idx])
}
return result
}
// UniqueInt64 :nodoc:
func UniqueInt64(elements []int64) []int64 {
encountered := map[int64]bool{}
result := []int64{}
for idx := range elements {
if encountered[elements[idx]] {
continue
}
encountered[elements[idx]] = true
result = append(result, elements[idx])
}
return result
}
// SlicePointerInt32PointerToSliceInt64 :nodoc:
func SlicePointerInt32PointerToSliceInt64(i *[]*int32) (result []int64) {
if i != nil {
dump := *i
for _, element := range dump {
result = append(result, int64(*element))
}
return result
}
return result
}
// PaginateSlice :nodoc:
func PaginateSlice[T comparable](data []T, page, size int64) []T {
if page < 1 || size < 1 {
return nil
}
offset := Offset(page, size)
count := len(data)
switch {
case count < int(offset):
return []T{}
case count < int(offset+size):
return data[offset:count]
default:
return data[offset : offset+size]
}
}
// FindDifferencesFromSlices find item that not exists in all slices but exists in one or more of them
func FindDifferencesFromSlices[T comparable](slices ...[]T) []T {
if len(slices) < 2 {
return nil
}
itemCountMap := map[T]int{}
for _, slice := range slices {
handledItem := map[T]bool{}
for _, item := range slice {
// handle duplicate item in one slice
if !handledItem[item] {
itemCountMap[item]++
}
}
}
var result []T
for item, count := range itemCountMap {
if count < len(slices) {
result = append(result, item)
}
}
return result
}
// IsUniqueSliceItem :nodoc:
func IsUniqueSliceItem[T comparable](data []T) bool {
mapData := make(map[T]bool, len(data))
for _, d := range data {
if _, ok := mapData[d]; ok {
return false
}
mapData[d] = true
}
return true
}
// ConvertSlice can change slice data type or even manipulate the data using converter func
func ConvertSlice[T1 any, T2 any](in []T1, converter func(T1) T2) []T2 {
var res []T2
for _, v := range in {
res = append(res, converter(v))
}
return res
}