github.com/eframework-cn/EP.GO.UTIL@v1.0.0/xcollect/int32.go (about)

     1  //-----------------------------------------------------------------------//
     2  //                     GNU GENERAL PUBLIC LICENSE                        //
     3  //                        Version 2, June 1991                           //
     4  //                                                                       //
     5  // Copyright (C) EFramework, https://eframework.cn, All rights reserved. //
     6  // Everyone is permitted to copy and distribute verbatim copies          //
     7  // of this license document, but changing it is not allowed.             //
     8  //                   SEE LICENSE.md FOR MORE DETAILS.                    //
     9  //-----------------------------------------------------------------------//
    10  
    11  package xcollect
    12  
    13  // 从数组/切片中索引元素([]int32)
    14  //	arr: 数组/切片
    15  //	ele: 元素
    16  func IndexForInt32(arr []int32, ele int32) int {
    17  	if arr != nil {
    18  		for k, v := range arr {
    19  			if v == ele {
    20  				return k
    21  			}
    22  		}
    23  	}
    24  	return -1
    25  }
    26  
    27  // 数组/切片是否存在元素([]int32)
    28  //	arr: 数组/切片
    29  //	ele: 元素
    30  func ContainsForInt32(arr []int32, ele int32) bool {
    31  	if arr != nil {
    32  		return IndexForInt32(arr, ele) >= 0
    33  	}
    34  	return false
    35  }
    36  
    37  // 从数组/切片中移除元素([]int32)
    38  //	arr: 数组/切片
    39  //	ele: 元素
    40  func RemoveForInt32(arr []int32, ele int32) []int32 {
    41  	if arr != nil {
    42  		for {
    43  			idx := IndexForInt32(arr, ele)
    44  			if idx >= 0 {
    45  				arr = append(arr[:idx], arr[idx+1:]...)
    46  			} else {
    47  				break
    48  			}
    49  		}
    50  	}
    51  	return arr
    52  }
    53  
    54  // 从数组/切片中移除元素([]int32)
    55  //	arr: 数组/切片
    56  //	index: 索引
    57  func DeleteForInt32(arr []int32, ele int) []int32 {
    58  	if arr != nil {
    59  		if ele < len(arr) {
    60  			arr = append(arr[:ele], arr[ele+1:]...)
    61  		}
    62  	}
    63  	return arr
    64  }
    65  
    66  // 在数组/切片中新增元素([]int32)
    67  //	arr: 数组/切片
    68  //	ele: 元素
    69  func AppendForInt32(arr []int32, ele int32) []int32 {
    70  	return append(arr, ele)
    71  }