-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
205 lines (182 loc) · 4.06 KB
/
array.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
package gorray
import (
"fmt"
"log"
"strings"
)
func slicingPos[T any](arr []T, start int, end int) (int, int) {
arrLength := len(arr)
if start < 0 {
start = arrLength + start
}
if end < 0 {
end = arrLength + end
}
return start, end
}
// Get the element from array at given `index`
// Allow negative index, it'll count back
func At[T any](arr []T, index int) T {
if index < 0 {
index = len(arr) + index
}
return arr[index]
}
// `length` must be greater than 1
func Chunk[T any](arr []T, length int) [][]T {
if length < 1 {
panic("Length must be greater than 1")
}
arrLength := len(arr)
chunksLength := arrLength / length
if arrLength%length > 0 {
chunksLength++
}
chunks := make([][]T, chunksLength)
for i := 0; i < chunksLength; i++ {
start := i * length
end := start + length
if end > arrLength {
end = arrLength
}
chunks[i] = arr[start:end]
}
log.Println(len(chunks))
return chunks
}
// Fill element to the array.
//
// You can specify which start and end position where element will be filled in Array.
func Fill[T any](arr []T, value T, pos ...int) []T {
newArr := []T{}
start := 0
end := len(arr)
if len(pos) > 0 {
if len(pos) > 1 {
if pos[1] > end {
panic("end position must less than array's length")
}
end = pos[1]
}
start = pos[0]
if start > end {
panic("start position must less than array's length and end position")
}
}
for index := start; index < end; index++ {
newArr[index] = value
}
return arr
}
// Check if array has given elements
func Exist[T comparable](arr []T, elements ...T) bool {
if len(arr) < 1 {
return false
}
if len(elements) < 1 {
return true
}
for _, element := range elements {
notFound := true
for _, elem := range arr {
if elem == element {
notFound = false
break
}
}
if notFound {
return false
}
}
return true
}
// Joins all element from array into one string.
//
// Not recommended because it'll joins all element with `fmt.Sprint`, use JoinStringer instead
func Join[T any](arr []T) string {
builder := new(strings.Builder)
for _, element := range arr {
builder.WriteString(fmt.Sprint(element))
}
return builder.String()
}
// Join all element from array Stringer into one string
func JoinStringer(arr []fmt.Stringer) string {
builder := new(strings.Builder)
for _, element := range arr {
builder.WriteString(element.String())
}
return builder.String()
}
// Remove the last element on array, and return it.
func Pop[T any](arr []T) ([]T, T) {
pos := len(arr) - 1
newArr := Clone(arr[0:pos])
return newArr, arr[pos]
}
// Remove the first element on array, and return it.
func Shift[T any](arr []T) ([]T, T) {
newArr := Clone(arr[1:])
return newArr, arr[0]
}
// Add new element on array to the first position.
func Unshift[T any](arr []T, element T) []T {
return append([]T{element}, arr...)
}
// Reverse the array
func Reverse[T any](arr []T) []T {
newArr := Clone(arr)
arrLength := len(arr)
length := arrLength / 2
if arrLength%2 > 0 {
length++
}
arrLength -= 1
for i := 0; i < length; i++ {
if i == arrLength-i {
continue
}
newArr[i] = arr[arrLength-i]
newArr[arrLength-i] = arr[i]
}
return newArr
}
// Clone array tot the new array
func Clone[T any](arr []T) []T {
newArr := make([]T, len(arr))
copy(newArr, arr)
return newArr
}
// Remove element from array at specified index.
//
// The element that removed will returned.
func RemoveAt[T any](arr []T, index int) ([]T, T) {
newArray := Clone(arr[0:index])
if index < len(arr) {
newArray = append(newArray, arr[index+1:]...)
}
return newArray, arr[index]
}
// Replace element from array with given value at specified position
func Replace[T any](arr []T, value T, pos ...int) []T {
arrLength := len(arr)
var start, end int
posLength := len(pos)
if posLength > 0 {
start = pos[0]
end = start + 1
}
if posLength > 1 {
end = pos[1]
}
start, end = slicingPos(arr, start, end)
newArr := make([]T, arrLength)
for index, elem := range newArr {
if index >= start && index < end {
newArr[index] = value
continue
}
newArr[index] = elem
}
return newArr
}