-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathslices.go
111 lines (93 loc) · 1.78 KB
/
slices.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
package slices
import (
"math/rand"
"reflect"
"sort"
)
const(
IS_LESS_THAN = iota - 1
IS_SAME_AS
IS_GREATER_THAN
)
type Nested interface {
Depth() int
}
type Flattenable interface {
Flatten()
}
type Equatable interface {
Equal(interface{}) bool
}
type Typed interface {
Type() reflect.Type
}
type Insertable interface {
Len() int
Insert(int, interface{})
}
type Container interface {
Len() int
At(int) interface{}
Set(int, interface{})
}
type Deck interface {
Len() int
Swap(i, j int)
}
type Wipeable interface {
Len() int
BlockClear(int, int)
}
var(
NESTED = reflect.TypeOf((*Nested)(nil)).Elem()
FLATTENABLE = reflect.TypeOf((*Flattenable)(nil)).Elem()
EQUATABLE = reflect.TypeOf((*Equatable)(nil)).Elem()
TYPED = reflect.TypeOf((*Typed)(nil)).Elem()
INSERTABLE = reflect.TypeOf((*Insertable)(nil)).Elem()
CONTAINER = reflect.TypeOf((*Container)(nil)).Elem()
)
func CanFlatten(s interface{}) (ok bool) {
switch s := s.(type) {
case reflect.Value:
ok = s.Kind() == reflect.Slice || s.Type().Implements(FLATTENABLE)
default:
v := reflect.ValueOf(s)
ok = v.Kind() == reflect.Slice || v.Type().Implements(FLATTENABLE)
}
return
}
func Prepend(i Insertable, value interface{}) {
i.Insert(0, value)
}
func Append(i Insertable, value interface{}) {
i.Insert(i.Len(), value)
}
func Shuffle(d Deck) {
for i, v := range rand.Perm(d.Len()) {
if v > i {
d.Swap(i, v)
}
}
}
func ClearAll(i interface{}) (r bool) {
if i, ok := i.(Wipeable); ok {
i.BlockClear(0, i.Len())
r = true
}
return
}
func Equal(e, o interface{}) (r bool) {
if e, ok := e.(Equatable); ok {
r = e.Equal(o)
} else if o, ok := o.(Equatable); ok {
r = o.Equal(e)
}
return
}
func Sort(i interface{}) (r bool) {
if i, ok := i.(sort.Interface); ok {
sort.Sort(i)
r = true
}
return
}