-
Notifications
You must be signed in to change notification settings - Fork 25
/
int64.go
150 lines (129 loc) · 3.57 KB
/
int64.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
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package slices
import (
"sort"
"github.com/corestoreio/errors"
)
// Int64Slice contains Map/Filter/Reduce/Sort/Unique/etc method receivers for
// []int64.
// @todo think about the necessary gen functions
// +gen slice:"Where,Count,GroupBy[int64]"
type Int64 []int64
// ToInt64 converts to type int64 slice.
func (l Int64) ToInt64() []int64 { return []int64(l) }
// Len returns the length
func (l Int64) Len() int { return len(l) }
// Less compares two slice values
func (l Int64) Less(i, j int) bool { return l[i] < l[j] }
// Swap changes the position
func (l Int64) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
// Sort is a convenience method.
func (l Int64) Sort() Int64 { sort.Stable(l); return l }
// Reverse is a convenience method.
func (l Int64) Reverse() Int64 { sort.Stable(sort.Reverse(l)); return l }
// Append adds s (variadic) to the Int64Slice
func (l *Int64) Append(s ...int64) Int64 {
*l = append(*l, s...)
return *l
}
// Update sets the int64 s on index i. If index is not found returns an fatal
// error behaviour.
func (l *Int64) Update(i int, s int64) error {
if i > l.Len() || i < 0 {
return errors.OutOfRange.Newf("[slices] Index %d is out of range", i)
}
(*l)[i] = s
return nil
}
// Delete removes index i from slice
func (l *Int64) Delete(i int) error {
if i > l.Len()-1 || i < 0 {
return errors.OutOfRange.Newf("[slices] Index %d is out of range", i)
}
*l = append((*l)[:i], (*l)[i+1:]...)
return nil
}
// Index returns -1 if not found or the current index for target t.
func (l Int64) Index(t int64) int {
for i, v := range l {
if v == t {
return i
}
}
return -1
}
// Contains returns true if the target int64 t is in the slice.
func (l Int64) Contains(t int64) bool {
return l.Index(t) >= 0
}
// Any returns true if one of the int64s in the slice satisfies the predicate f.
func (l Int64) Any(f func(int64) bool) bool {
for _, v := range l {
if f(v) {
return true
}
}
return false
}
// All returns true if all of the int64s in the slice satisfy the predicate f.
func (l Int64) All(f func(int64) bool) bool {
for _, v := range l {
if !f(v) {
return false
}
}
return true
}
// Reduce reduces itself containing all int64s in the slice that satisfy the
// predicate f.
func (l *Int64) Reduce(f func(int64) bool) Int64 {
vsf := (*l)[:0]
for _, v := range *l {
if f(v) {
vsf = append(vsf, v)
}
}
*l = vsf
return *l
}
// Map changes itself containing the results of applying the function f to each
// int64 in itself.
func (l *Int64) Map(f func(int64) int64) Int64 {
for i, v := range *l {
(*l)[i] = f(v)
}
return *l
}
// Sum returns the sum
func (l Int64) Sum() int64 {
var s int64
for _, v := range l {
s += v
}
return s
}
// Unique removes duplicate entries in O(n)
func (l *Int64) Unique() Int64 {
seen := make(map[int64]struct{}, len(*l))
unique := (*l)[:0]
for _, p := range *l {
if _, ok := seen[p]; !ok {
unique = append(unique, p)
seen[p] = struct{}{}
}
}
*l = unique
return *l
}