1
0

slice.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package utils
  15. import (
  16. "math/rand"
  17. "time"
  18. )
  19. type reducetype func(interface{}) interface{}
  20. type filtertype func(interface{}) bool
  21. // InSlice checks given string in string slice or not.
  22. func InSlice(v string, sl []string) bool {
  23. for _, vv := range sl {
  24. if vv == v {
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. // InSliceIface checks given interface in interface slice.
  31. func InSliceIface(v interface{}, sl []interface{}) bool {
  32. for _, vv := range sl {
  33. if vv == v {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. // SliceRandList generate an int slice from min to max.
  40. func SliceRandList(min, max int) []int {
  41. if max < min {
  42. min, max = max, min
  43. }
  44. length := max - min + 1
  45. t0 := time.Now()
  46. rand.Seed(int64(t0.Nanosecond()))
  47. list := rand.Perm(length)
  48. for index := range list {
  49. list[index] += min
  50. }
  51. return list
  52. }
  53. // SliceMerge merges interface slices to one slice.
  54. func SliceMerge(slice1, slice2 []interface{}) (c []interface{}) {
  55. c = append(slice1, slice2...)
  56. return
  57. }
  58. // SliceReduce generates a new slice after parsing every value by reduce function
  59. func SliceReduce(slice []interface{}, a reducetype) (dslice []interface{}) {
  60. for _, v := range slice {
  61. dslice = append(dslice, a(v))
  62. }
  63. return
  64. }
  65. // SliceRand returns random one from slice.
  66. func SliceRand(a []interface{}) (b interface{}) {
  67. randnum := rand.Intn(len(a))
  68. b = a[randnum]
  69. return
  70. }
  71. // SliceSum sums all values in int64 slice.
  72. func SliceSum(intslice []int64) (sum int64) {
  73. for _, v := range intslice {
  74. sum += v
  75. }
  76. return
  77. }
  78. // SliceFilter generates a new slice after filter function.
  79. func SliceFilter(slice []interface{}, a filtertype) (ftslice []interface{}) {
  80. for _, v := range slice {
  81. if a(v) {
  82. ftslice = append(ftslice, v)
  83. }
  84. }
  85. return
  86. }
  87. // SliceDiff returns diff slice of slice1 - slice2.
  88. func SliceDiff(slice1, slice2 []interface{}) (diffslice []interface{}) {
  89. for _, v := range slice1 {
  90. if !InSliceIface(v, slice2) {
  91. diffslice = append(diffslice, v)
  92. }
  93. }
  94. return
  95. }
  96. // SliceIntersect returns slice that are present in all the slice1 and slice2.
  97. func SliceIntersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
  98. for _, v := range slice1 {
  99. if InSliceIface(v, slice2) {
  100. diffslice = append(diffslice, v)
  101. }
  102. }
  103. return
  104. }
  105. // SliceChunk separates one slice to some sized slice.
  106. func SliceChunk(slice []interface{}, size int) (chunkslice [][]interface{}) {
  107. if size >= len(slice) {
  108. chunkslice = append(chunkslice, slice)
  109. return
  110. }
  111. end := size
  112. for i := 0; i <= (len(slice) - size); i += size {
  113. chunkslice = append(chunkslice, slice[i:end])
  114. end += size
  115. }
  116. return
  117. }
  118. // SliceRange generates a new slice from begin to end with step duration of int64 number.
  119. func SliceRange(start, end, step int64) (intslice []int64) {
  120. for i := start; i <= end; i += step {
  121. intslice = append(intslice, i)
  122. }
  123. return
  124. }
  125. // SlicePad prepends size number of val into slice.
  126. func SlicePad(slice []interface{}, size int, val interface{}) []interface{} {
  127. if size <= len(slice) {
  128. return slice
  129. }
  130. for i := 0; i < (size - len(slice)); i++ {
  131. slice = append(slice, val)
  132. }
  133. return slice
  134. }
  135. // SliceUnique cleans repeated values in slice.
  136. func SliceUnique(slice []interface{}) (uniqueslice []interface{}) {
  137. for _, v := range slice {
  138. if !InSliceIface(v, uniqueslice) {
  139. uniqueslice = append(uniqueslice, v)
  140. }
  141. }
  142. return
  143. }
  144. // SliceShuffle shuffles a slice.
  145. func SliceShuffle(slice []interface{}) []interface{} {
  146. for i := 0; i < len(slice); i++ {
  147. a := rand.Intn(len(slice))
  148. b := rand.Intn(len(slice))
  149. slice[a], slice[b] = slice[b], slice[a]
  150. }
  151. return slice
  152. }