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