b1374b6a by astaxie

move safemap to utils & add many helpful slice function

1 parent 19119e99
1 package beego 1 package utils
2 2
3 import ( 3 import (
4 "sync" 4 "sync"
......
1 package beego 1 package utils
2 2
3 import ( 3 import (
4 "testing" 4 "testing"
......
1 package utils 1 package utils
2 2
3 import (
4 "math/rand"
5 "time"
6 )
7
8 type reducetype func(interface{}) interface{}
9 type filtertype func(interface{}) bool
10
3 func InSlice(v string, sl []string) bool { 11 func InSlice(v string, sl []string) bool {
4 for _, vv := range sl { 12 for _, vv := range sl {
5 if vv == v { 13 if vv == v {
...@@ -8,3 +16,126 @@ func InSlice(v string, sl []string) bool { ...@@ -8,3 +16,126 @@ func InSlice(v string, sl []string) bool {
8 } 16 }
9 return false 17 return false
10 } 18 }
19
20 func InSliceIface(v interface{}, sl []interface{}) bool {
21 for _, vv := range sl {
22 if vv == v {
23 return true
24 }
25 }
26 return false
27 }
28
29 func SliceRandList(min, max int) []int {
30 if max < min {
31 min, max = max, min
32 }
33 length := max - min + 1
34 t0 := time.Now()
35 rand.Seed(int64(t0.Nanosecond()))
36 list := rand.Perm(length)
37 for index, _ := range list {
38 list[index] += min
39 }
40 return list
41 }
42
43 func SliceMerge(slice1, slice2 []interface{}) (c []interface{}) {
44 c = append(slice1, slice2...)
45 return
46 }
47
48 func SliceReduce(slice []interface{}, a reducetype) (dslice []interface{}) {
49 for _, v := range slice {
50 dslice = append(dslice, a(v))
51 }
52 return
53 }
54
55 func SliceRand(a []interface{}) (b interface{}) {
56 randnum := rand.Intn(len(a))
57 b = a[randnum]
58 return
59 }
60
61 func SliceSum(intslice []int64) (sum int64) {
62 for _, v := range intslice {
63 sum += v
64 }
65 return
66 }
67
68 func SliceFilter(slice []interface{}, a filtertype) (ftslice []interface{}) {
69 for _, v := range slice {
70 if a(v) {
71 ftslice = append(ftslice, v)
72 }
73 }
74 return
75 }
76
77 func SliceDiff(slice1, slice2 []interface{}) (diffslice []interface{}) {
78 for _, v := range slice1 {
79 if !InSliceIface(v, slice2) {
80 diffslice = append(diffslice, v)
81 }
82 }
83 return
84 }
85
86 func SliceIntersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
87 for _, v := range slice1 {
88 if !InSliceIface(v, slice2) {
89 diffslice = append(diffslice, v)
90 }
91 }
92 return
93 }
94
95 func SliceChunk(slice []interface{}, size int) (chunkslice [][]interface{}) {
96 if size >= len(slice) {
97 chunkslice = append(chunkslice, slice)
98 return
99 }
100 end := size
101 for i := 0; i <= (len(slice) - size); i += size {
102 chunkslice = append(chunkslice, slice[i:end])
103 end += size
104 }
105 return
106 }
107
108 func SliceRange(start, end, step int64) (intslice []int64) {
109 for i := start; i <= end; i += step {
110 intslice = append(intslice, i)
111 }
112 return
113 }
114
115 func SlicePad(slice []interface{}, size int, val interface{}) []interface{} {
116 if size <= len(slice) {
117 return slice
118 }
119 for i := 0; i < (size - len(slice)); i++ {
120 slice = append(slice, val)
121 }
122 return slice
123 }
124
125 func SliceUnique(slice []interface{}) (uniqueslice []interface{}) {
126 for _, v := range slice {
127 if !InSliceIface(v, uniqueslice) {
128 uniqueslice = append(uniqueslice, v)
129 }
130 }
131 return
132 }
133
134 func SliceShuffle(slice []interface{}) []interface{} {
135 for i := 0; i < len(slice); i++ {
136 a := rand.Intn(len(slice))
137 b := rand.Intn(len(slice))
138 slice[a], slice[b] = slice[b], slice[a]
139 }
140 return slice
141 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!