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
/
uint16.go
153 lines (127 loc) · 3.88 KB
/
uint16.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. */
// UInt16Slice is the alias of []uint16
type UInt16Slice []uint16
// Value returns the aliased []uint16
func (slice UInt16Slice) Value() []uint16 {
return []uint16(slice)
}
// IndexOf returns the first index of needle, or -1 if not found
func (slice UInt16Slice) IndexOf(needle uint16) 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 UInt16Slice) Contains(needle uint16) bool {
return slice.IndexOf(needle) != NotInSlice
}
// SortAsc will sort the slice in ascending order
func (slice UInt16Slice) SortAsc() UInt16Slice {
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 UInt16Slice) SortDesc() UInt16Slice {
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 UInt16Slice) Reverse() UInt16Slice {
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 uint16 values
func (slice UInt16Slice) Unique() UInt16Slice {
u := make([]uint16, 0, len(slice))
m := make(map[uint16]bool)
for i := 0; i < len(slice); i++ {
if _, ok := m[slice[i]]; !ok {
m[slice[i]] = true
u = append(u, slice[i])
}
}
return UInt16Slice(u)
}
// Filter will return all uint16 values that evaluate true in the user-supplied function
func (slice UInt16Slice) Filter(f func(uint16) bool) UInt16Slice {
out := make([]uint16, 0, len(slice))
for i := 0; i < len(slice); i++ {
if f(slice[i]) {
out = append(out, slice[i])
}
}
return UInt16Slice(out)
}
// Each will apply a function to each uint16 in the slice.
// This function will iterate over the slice completely. No
// items in the slice should be mutated by this operation.
func (slice UInt16Slice) Each(f func(uint16)) {
for i := 0; i < len(slice); i++ {
f(slice[i])
}
}
// TryEach will apply a function to each uint16 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 UInt16Slice) TryEach(f func(uint16) 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 uint16 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 UInt16Slice) IfEach(f func(uint16) 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 uint16 in the slice and replace the previous value
func (slice UInt16Slice) Map(f func(uint16) uint16) {
for i := 0; i < len(slice); i++ {
slice[i] = f(slice[i])
}
}
// Chunk will divide the slice of uint16 into smaller slices defined by chunk length
func (slice UInt16Slice) Chunk(size int) [][]uint16 {
l := len(slice)
if l == 0 || size <= 0 {
return make([][]uint16, 0)
}
floor := l / size
out := make([][]uint16, 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
}