github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/slice.go (about)

     1  package util
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"math/rand"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  )
    11  
    12  // SliceShuffle shuffle a slice
    13  func SliceShuffle(s []any) {
    14  	rand.NewSource(time.Now().UnixNano())
    15  	rand.Shuffle(len(s), func(i, j int) {
    16  		s[i], s[j] = s[j], s[i]
    17  	})
    18  }
    19  
    20  // SliceReverse slice 反转
    21  func SliceReverse(s []any) {
    22  	for i := len(s)/2 - 1; i >= 0; i-- {
    23  		opp := len(s) - 1 - i
    24  		s[i], s[opp] = s[opp], s[i]
    25  	}
    26  }
    27  
    28  // InSliceInt 是否在 slice
    29  func InSliceInt(s int, ss []int) bool {
    30  	for _, v := range ss {
    31  		if v == s {
    32  			return true
    33  		}
    34  	}
    35  	return false
    36  }
    37  
    38  // InSliceStr 是否在 slice
    39  func InSliceStr(s string, ss []string) bool {
    40  	for _, v := range ss {
    41  		if v == s {
    42  			return true
    43  		}
    44  	}
    45  	return false
    46  }
    47  
    48  // SliceDeleteElem 索引删除 slice 元素
    49  func SliceDeleteElem(i int, s []any) ([]any, error) {
    50  	if len(s) == 0 {
    51  		return nil, errors.New("slice is empty")
    52  	}
    53  	if i < 0 || i >= len(s) {
    54  		return nil, fmt.Errorf("index %d out of range for slice of length %d", i, len(s))
    55  	}
    56  	s[i] = s[len(s)-1]
    57  	s[len(s)-1] = nil
    58  	s = s[:len(s)-1]
    59  	return s, nil
    60  }
    61  
    62  // SliceIntJoin 整型数组拼接成字符串
    63  func SliceIntJoin(s []int, sep string) string {
    64  	if len(s) <= 1 {
    65  		return ""
    66  	}
    67  	var str strings.Builder
    68  	str.WriteString(strconv.Itoa(s[0]))
    69  	for i := 1; i < len(s); i++ {
    70  		str.WriteString(sep)
    71  		str.WriteString(strconv.Itoa(s[i]))
    72  	}
    73  
    74  	return str.String()
    75  }
    76  
    77  // SliceToInt 字符串数组转换成整型数组
    78  func SliceToInt(ss []string) (ii []int) {
    79  	for _, s := range ss {
    80  		i, _ := strconv.Atoi(s)
    81  		ii = append(ii, i)
    82  	}
    83  	return ii
    84  }
    85  
    86  // SliceBigFilter 过滤切片元素 适合大切片
    87  func SliceBigFilter(a []int, f func(v int) bool) []int {
    88  	for i := 0; i < len(a); i++ {
    89  		if !f(a[i]) {
    90  			a = append(a[:i], a[i+1:]...)
    91  			i--
    92  		}
    93  	}
    94  	return a
    95  }
    96  
    97  // SliceSmallFilter 过滤切片元素 适合小切片
    98  func SliceSmallFilter(a []int, f func(v int) bool) []int {
    99  	ret := make([]int, 0, len(a))
   100  	for _, val := range a {
   101  		if f(val) {
   102  			ret = append(ret, val)
   103  		}
   104  	}
   105  	return ret
   106  }
   107  
   108  // SliceIntDeduplication 去除重复的元素
   109  func SliceIntDeduplication(a []int) []int {
   110  	if a == nil {
   111  		return nil
   112  	}
   113  
   114  	m := make(map[int]struct{}, len(a))
   115  	j := 0
   116  	for _, v := range a {
   117  		if _, ok := m[v]; ok {
   118  			continue
   119  		}
   120  		m[v] = struct{}{}
   121  		a[j] = v
   122  		j++
   123  	}
   124  	return a[:j]
   125  }