-
Notifications
You must be signed in to change notification settings - Fork 2
/
arrays.go
138 lines (121 loc) · 3.45 KB
/
arrays.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
package values
import (
"math/rand"
"slices"
"sort"
)
// ReverseArray reverses the array. The original array will not be modified.
//
// example:
//
// ReverseArray([]int{1, 2, 3}) => []int{3, 2, 1}
// ReverseArray([]string{"a", "b", "c"}) => []string{"c", "b", "a"}
func ReverseArray[T any](array []T) []T {
length := len(array)
if length <= 1 {
return array
}
result := make([]T, length)
for i, item := range array {
result[length-i-1] = item
}
return result
}
// ContainsArray checks if the target is in the array.
//
// example:
//
// ContainsArray([]int{1, 2, 3}, 2) => true
// ContainsArray([]string{"a", "b", "c"}, "d") => false
func ContainsArray[T comparable](array []T, target T) bool {
return slices.Contains(array, target)
}
// UniqueArray returns a new array with unique elements.
//
// example:
//
// UniqueArray([]int{1, 2, 2, 3}) => []int{1, 2, 3}
// UniqueArray([]string{"a", "b", "b", "c"}) => []string{"a", "b", "c"}
func UniqueArray[T comparable](array []T) []T {
mp := make(map[T]struct{})
result := make([]T, 0, len(array))
for _, item := range array {
if _, ok := mp[item]; !ok {
mp[item] = struct{}{}
result = append(result, item)
}
}
return slices.Clip(result)
}
// RemoveArray removes the elements from the array from [start, end).
//
// example:
//
// RemoveArray([]int{1, 2, 3}, 1, 2) => []int{1}
// RemoveArray([]string{"a", "b", "c"}, 0, 1) => []string{"c"}
func RemoveArray[T comparable](array []T, start, end int) []T {
return slices.Delete(array, start, end)
}
// IndexArray returns the index of the target in the array.
//
// example:
//
// IndexArray([]int{1, 2, 3}, 2) => 1
// IndexArray([]string{"a", "b", "c"}, "d") => -1
func IndexArray[T comparable](array []T, target T) int {
return slices.Index(array, target)
}
// ShuffleArray shuffles the array from [start, end).
//
// example:
//
// ShuffleArray([]int{1, 2, 3}, 0, 3) => []int{3, 1, 2}
// ShuffleArray([]string{"a", "b", "c"}, 0, 3) => []string{"c", "a", "b"}
func ShuffleArray[T any](array []T, start, end int) []T {
rand.Shuffle(end-start, func(i, j int) {
array[start+i], array[start+j] = array[start+j], array[start+i]
})
return array
}
// MergeArrays concatenates the arrays.
//
// example:
//
// MergeArrays([]int{1, 2}, []int{3, 4}) => []int{1, 2, 3, 4}
// MergeArrays([]string{"a", "b"}, []string{"c", "d"}) => []string{"a", "b", "c", "d"}
func MergeArrays[T any](arrays ...[]T) []T {
var result []T
for _, array := range arrays {
result = append(result, array...)
}
return result
}
// FilterArray filters the array with the filter function.
//
// example:
//
// FilterArray([]int{1, 2, 3}, func(i int) bool { return i > 1 }) => []int{2, 3}
// FilterArray([]string{"a", "b", "c"}, func(s string) bool { return s != "a" }) => []string{"b", "c"}
func FilterArray[T any](array []T, filter func(T) bool) []T {
result := make([]T, 0, len(array))
for _, item := range array {
if filter(item) {
result = append(result, item)
}
}
return result
}
// SortArray sorts the array with the compare function. The original array will not be modified.
//
// example:
//
// SortArray([]int{3, 1, 2}, func(i, j int) bool { return i < j }) => []int{1, 2, 3}
// SortArray([]string{"c", "a", "b"}, func(i, j string) bool { return i < j }) => []string{"a", "b", "c"}
func SortArray[T any](array []T, cmp func(T, T) bool) []T {
sorted := make([]T, len(array))
copy(sorted, array)
sort.SliceStable(sorted, func(i, j int) bool {
return cmp(sorted[i], sorted[j])
})
return sorted
}