-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
146 lines (110 loc) · 2.89 KB
/
select.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
package loncha
import (
"errors"
"reflect"
"sort"
)
var (
ERR_SLICE_TYPE error = errors.New("parameter must be slice or pointer of slice")
ERR_POINTER_SLICE_TYPE error = errors.New("parameter must be pointer of slice")
ERR_NOT_FOUND error = errors.New("data is not found")
ERR_ELEMENT_INVALID_TYPE error = errors.New("slice element is invalid type")
ERR_INVALID_INDEX error = errors.New("invalid index")
)
type CondFunc func(idx int) bool
type CompareFunc func(i, j int) bool
func slice2Reflect(slice interface{}) (reflect.Value, error) {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Ptr {
return reflect.ValueOf(nil), ERR_SLICE_TYPE
}
if rv.Elem().Kind() != reflect.Slice {
return reflect.ValueOf(nil), ERR_SLICE_TYPE
}
return rv, nil
}
func sliceElm2Reflect(slice interface{}) (reflect.Value, error) {
rv := reflect.ValueOf(slice)
if rv.Kind() == reflect.Slice {
return rv, nil
}
if rv.Kind() != reflect.Ptr {
return reflect.ValueOf(nil), ERR_POINTER_SLICE_TYPE
}
if rv.Elem().Kind() != reflect.Slice {
return reflect.ValueOf(nil), ERR_SLICE_TYPE
}
return rv.Elem(), nil
}
// Reverse ... Transforms an array such that the first element will become the last, the second element will become the second to last, etc.
func Reverse(slice interface{}) {
sort.Slice(slice, func(i, j int) bool { return i > j })
}
// Select ... return all element on match of CondFunc
func Select(slice interface{}, fn CondFunc) (interface{}, error) {
rv, err := sliceElm2Reflect(slice)
if err != nil {
return nil, err
}
newSlice := reflect.MakeSlice(rv.Type(), rv.Len(), rv.Cap())
reflect.Copy(newSlice, rv)
ptr := reflect.New(newSlice.Type())
ptr.Elem().Set(newSlice)
filter(ptr, fn)
return ptr.Elem().Interface(), nil
}
// OldFilter ... OldFilter element with mached funcs
// Deprecated: should use FIlter
func OldFilter(slice interface{}, funcs ...CondFunc) error {
rv, err := slice2Reflect(slice)
if err != nil {
return err
}
filter(rv, funcs...)
return nil
}
// Delete ... Delete element with mached funcs
func Delete(slice interface{}, funcs ...CondFunc) error {
rv, err := slice2Reflect(slice)
if err != nil {
return err
}
innterFilter(rv, false, funcs...)
return nil
}
func filter(pRv reflect.Value, funcs ...CondFunc) {
innterFilter(pRv, true, funcs...)
}
func innterFilter(pRv reflect.Value, keep bool, funcs ...CondFunc) {
rv := pRv.Elem()
length := rv.Len()
if length == 0 {
return
}
movelist := make([]int, length)
newIdx := 0
for i := 0; i < length; i++ {
allok := (true == keep)
for _, f := range funcs {
if !f(i) {
allok = (false == keep)
}
}
if allok {
movelist[i] = newIdx
newIdx++
} else {
movelist[i] = -1
}
}
if newIdx == length {
return
}
swap := reflect.Swapper(pRv.Elem().Interface())
for i, v := range movelist {
if v != -1 {
swap(i, v)
}
}
pRv.Elem().SetLen(newIdx)
}