This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int16.go
153 lines (127 loc) · 3.83 KB
/
int16.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
package slices
import "sort"
/* generated by github.com/schigh/slices/base/base.go. do not edit. */
// Int16Slice is the alias of []int16
type Int16Slice []int16
// Value returns the aliased []int16
func (slice Int16Slice) Value() []int16 {
return []int16(slice)
}
// IndexOf returns the first index of needle, or -1 if not found
func (slice Int16Slice) IndexOf(needle int16) int {
for i := 0; i < len(slice); i++ {
if needle == slice[i] {
return i
}
}
return NotInSlice
}
// Contains returns true if the slice contains needle
func (slice Int16Slice) Contains(needle int16) bool {
return slice.IndexOf(needle) != NotInSlice
}
// SortAsc will sort the slice in ascending order
func (slice Int16Slice) SortAsc() Int16Slice {
sort.SliceStable(slice, func(i, j int) bool {
return slice[i] < slice[j]
})
return slice
}
// SortDesc will sort the slice in descending order
func (slice Int16Slice) SortDesc() Int16Slice {
sort.SliceStable(slice, func(i, j int) bool {
return slice[j] < slice[i]
})
return slice
}
// Reverse will reverse the order of the slice
func (slice Int16Slice) Reverse() Int16Slice {
l := len(slice)
for i, j := 0, l-1; i < l/2; i++ {
slice[i], slice[j] = slice[j], slice[i]
j--
}
return slice
}
// Unique filters out duplicate int16 values
func (slice Int16Slice) Unique() Int16Slice {
u := make([]int16, 0, len(slice))
m := make(map[int16]bool)
for i := 0; i < len(slice); i++ {
if _, ok := m[slice[i]]; !ok {
m[slice[i]] = true
u = append(u, slice[i])
}
}
return Int16Slice(u)
}
// Filter will return all int16 values that evaluate true in the user-supplied function
func (slice Int16Slice) Filter(f func(int16) bool) Int16Slice {
out := make([]int16, 0, len(slice))
for i := 0; i < len(slice); i++ {
if f(slice[i]) {
out = append(out, slice[i])
}
}
return Int16Slice(out)
}
// Each will apply a function to each int16 in the slice.
// This function will iterate over the slice completely. No
// items in the slice should be mutated by this operation.
func (slice Int16Slice) Each(f func(int16)) {
for i := 0; i < len(slice); i++ {
f(slice[i])
}
}
// TryEach will apply a function to each int16 in the slice.
// If the function returns an error, the iteration will stop and return
// the index of the element that caused the function to return the error.
// The second returned value will be first error returned from the
// supplied function, and nil otherwise.
// No items in the slice should be mutated by this operation.
func (slice Int16Slice) TryEach(f func(int16) error) (int, error) {
for i := 0; i < len(slice); i++ {
if err := f(slice[i]); err != nil {
return i, err
}
}
return NotInSlice, nil
}
// IfEach will apply a function to each int16 in the slice.
// If the function returns false, the iteration will stop and return
// the index of the element that caused the function to return false.
// The second returned value will be true if all members of the slice
// cause the provided function to return true, and false otherwise.
// No items in the slice should be mutated by this operation.
func (slice Int16Slice) IfEach(f func(int16) bool) (int, bool) {
for i := 0; i < len(slice); i++ {
if !f(slice[i]) {
return i, false
}
}
return NotInSlice, true
}
// Map will apply a function to each int16 in the slice and replace the previous value
func (slice Int16Slice) Map(f func(int16) int16) {
for i := 0; i < len(slice); i++ {
slice[i] = f(slice[i])
}
}
// Chunk will divide the slice of int16 into smaller slices defined by chunk length
func (slice Int16Slice) Chunk(size int) [][]int16 {
l := len(slice)
if l == 0 || size <= 0 {
return make([][]int16, 0)
}
floor := l / size
out := make([][]int16, 0, floor+1)
var k int
for i := 0; i < floor; i++ {
k = i*size + size
out = append(out, slice[i*size:k])
}
if l > k {
out = append(out, slice[k:])
}
return out
}