github.com/astaxie/beego@v1.12.3/utils/slice.go (about)

     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  
    15  package utils
    16  
    17  import (
    18  	"math/rand"
    19  	"time"
    20  )
    21  
    22  type reducetype func(interface{}) interface{}
    23  type filtertype func(interface{}) bool
    24  
    25  // InSlice checks given string in string slice or not.
    26  func InSlice(v string, sl []string) bool {
    27  	for _, vv := range sl {
    28  		if vv == v {
    29  			return true
    30  		}
    31  	}
    32  	return false
    33  }
    34  
    35  // InSliceIface checks given interface in interface slice.
    36  func InSliceIface(v interface{}, sl []interface{}) bool {
    37  	for _, vv := range sl {
    38  		if vv == v {
    39  			return true
    40  		}
    41  	}
    42  	return false
    43  }
    44  
    45  // SliceRandList generate an int slice from min to max.
    46  func SliceRandList(min, max int) []int {
    47  	if max < min {
    48  		min, max = max, min
    49  	}
    50  	length := max - min + 1
    51  	t0 := time.Now()
    52  	rand.Seed(int64(t0.Nanosecond()))
    53  	list := rand.Perm(length)
    54  	for index := range list {
    55  		list[index] += min
    56  	}
    57  	return list
    58  }
    59  
    60  // SliceMerge merges interface slices to one slice.
    61  func SliceMerge(slice1, slice2 []interface{}) (c []interface{}) {
    62  	c = append(slice1, slice2...)
    63  	return
    64  }
    65  
    66  // SliceReduce generates a new slice after parsing every value by reduce function
    67  func SliceReduce(slice []interface{}, a reducetype) (dslice []interface{}) {
    68  	for _, v := range slice {
    69  		dslice = append(dslice, a(v))
    70  	}
    71  	return
    72  }
    73  
    74  // SliceRand returns random one from slice.
    75  func SliceRand(a []interface{}) (b interface{}) {
    76  	randnum := rand.Intn(len(a))
    77  	b = a[randnum]
    78  	return
    79  }
    80  
    81  // SliceSum sums all values in int64 slice.
    82  func SliceSum(intslice []int64) (sum int64) {
    83  	for _, v := range intslice {
    84  		sum += v
    85  	}
    86  	return
    87  }
    88  
    89  // SliceFilter generates a new slice after filter function.
    90  func SliceFilter(slice []interface{}, a filtertype) (ftslice []interface{}) {
    91  	for _, v := range slice {
    92  		if a(v) {
    93  			ftslice = append(ftslice, v)
    94  		}
    95  	}
    96  	return
    97  }
    98  
    99  // SliceDiff returns diff slice of slice1 - slice2.
   100  func SliceDiff(slice1, slice2 []interface{}) (diffslice []interface{}) {
   101  	for _, v := range slice1 {
   102  		if !InSliceIface(v, slice2) {
   103  			diffslice = append(diffslice, v)
   104  		}
   105  	}
   106  	return
   107  }
   108  
   109  // SliceIntersect returns slice that are present in all the slice1 and slice2.
   110  func SliceIntersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
   111  	for _, v := range slice1 {
   112  		if InSliceIface(v, slice2) {
   113  			diffslice = append(diffslice, v)
   114  		}
   115  	}
   116  	return
   117  }
   118  
   119  // SliceChunk separates one slice to some sized slice.
   120  func SliceChunk(slice []interface{}, size int) (chunkslice [][]interface{}) {
   121  	if size >= len(slice) {
   122  		chunkslice = append(chunkslice, slice)
   123  		return
   124  	}
   125  	end := size
   126  	for i := 0; i <= (len(slice) - size); i += size {
   127  		chunkslice = append(chunkslice, slice[i:end])
   128  		end += size
   129  	}
   130  	return
   131  }
   132  
   133  // SliceRange generates a new slice from begin to end with step duration of int64 number.
   134  func SliceRange(start, end, step int64) (intslice []int64) {
   135  	for i := start; i <= end; i += step {
   136  		intslice = append(intslice, i)
   137  	}
   138  	return
   139  }
   140  
   141  // SlicePad prepends size number of val into slice.
   142  func SlicePad(slice []interface{}, size int, val interface{}) []interface{} {
   143  	if size <= len(slice) {
   144  		return slice
   145  	}
   146  	for i := 0; i < (size - len(slice)); i++ {
   147  		slice = append(slice, val)
   148  	}
   149  	return slice
   150  }
   151  
   152  // SliceUnique cleans repeated values in slice.
   153  func SliceUnique(slice []interface{}) (uniqueslice []interface{}) {
   154  	for _, v := range slice {
   155  		if !InSliceIface(v, uniqueslice) {
   156  			uniqueslice = append(uniqueslice, v)
   157  		}
   158  	}
   159  	return
   160  }
   161  
   162  // SliceShuffle shuffles a slice.
   163  func SliceShuffle(slice []interface{}) []interface{} {
   164  	for i := 0; i < len(slice); i++ {
   165  		a := rand.Intn(len(slice))
   166  		b := rand.Intn(len(slice))
   167  		slice[a], slice[b] = slice[b], slice[a]
   168  	}
   169  	return slice
   170  }