1 Star 0 Fork 0

zhenyangze / zlib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
slice.go 21.25 KB
一键复制 编辑 原始数据 按行查看 历史
zhenyangze 提交于 2022-10-04 23:27 . master: [feature] update package
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
package zlib
import (
"errors"
"fmt"
"reflect"
)
//
// Part 1: unique a slice, e.g. input []int32{1, 2, 2, 3} and output is []int32{1, 2, 3}.
//
func UniqueIntSlice(src []int) []int {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]int)
return v
}
func UniqueInt8Slice(src []int8) []int8 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]int8)
return v
}
func UniqueInt16Slice(src []int16) []int16 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]int16)
return v
}
func UniqueInt32Slice(src []int32) []int32 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]int32)
return v
}
func UniqueInt64Slice(src []int64) []int64 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]int64)
return v
}
func UniqueUintSlice(src []uint) []uint {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]uint)
return v
}
func UniqueUint8Slice(src []uint8) []uint8 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]uint8)
return v
}
func UniqueUint16Slice(src []uint16) []uint16 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]uint16)
return v
}
func UniqueUint32Slice(src []uint32) []uint32 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]uint32)
return v
}
func UniqueUint64Slice(src []uint64) []uint64 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]uint64)
return v
}
func UniqueFloat32Slice(src []float32) []float32 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]float32)
return v
}
func UniqueFloat64Slice(src []float64) []float64 {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]float64)
return v
}
func UniqueStrSlice(src []string) []string {
dst, _ := UniqueSliceE(src)
v, _ := dst.([]string)
return v
}
//
// Part 2: reverse a slice, e.g. input []int32{1, 2, 3} and output is []int32{3, 2, 1}.
//
func ReverseIntSlice(src []int) []int {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]int)
return v
}
func ReverseInt8Slice(src []int8) []int8 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]int8)
return v
}
func ReverseInt16Slice(src []int16) []int16 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]int16)
return v
}
func ReverseInt32Slice(src []int32) []int32 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]int32)
return v
}
func ReverseInt64Slice(src []int64) []int64 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]int64)
return v
}
func ReverseUintSlice(src []uint) []uint {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]uint)
return v
}
func ReverseUint8Slice(src []uint8) []uint8 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]uint8)
return v
}
func ReverseUint16Slice(src []uint16) []uint16 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]uint16)
return v
}
func ReverseUint32Slice(src []uint32) []uint32 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]uint32)
return v
}
func ReverseUint64Slice(src []uint64) []uint64 {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]uint64)
return v
}
func ReverseStrSlice(src []string) []string {
dst, _ := ReverseSliceE(src)
v, _ := dst.([]string)
return v
}
//
// Part 3: sum a slice, e.g. input []int32{1, 2, 3} and output is 6.
//
// SumSlice calculates the sum of slice elements
func SumSlice(slice interface{}) float64 {
v, _ := SumSliceE(slice)
return v
}
//
// Part 4: determine whether the slice contains an element.
//
// IsContains checks whether slice or array contains the target element.
// Note that if the target element is a numeric literal, please specify its type explicitly, otherwise it defaults to int.
// For example you might call like IsContains([]int32{1,2,3}, int32(1)).
func IsContains(i interface{}, target interface{}) bool {
if i == nil {
return false
}
t := reflect.TypeOf(i)
if t.Kind() != reflect.Slice && t.Kind() != reflect.Array {
return false
}
v := reflect.ValueOf(i)
for i := 0; i < v.Len(); i++ {
if target == v.Index(i).Interface() {
return true
}
}
return false
}
//
// Part 5: join slice with a separator. e.g. input []int32{1, 2, 3} and separator "#", output is a string "1#2#3"
//
// JoinSliceWithSep joins all elements in slice with a separator
func JoinSliceWithSep(slice interface{}, sep string) string {
s, _ := JoinSliceWithSepE(slice, sep)
return s
}
//
// Part 6: CRUD(Create Read Update Delete) on slice by index.
//
func InsertIntSlice(src []int, index, value int) []int {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]int)
return v
}
func InsertInt8Slice(src []int8, index int, value int8) []int8 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]int8)
return v
}
func InsertInt16Slice(src []int, index int, value int16) []int16 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]int16)
return v
}
func InsertInt32Slice(src []int, index int, value int32) []int32 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]int32)
return v
}
func InsertInt64Slice(src []int, index int, value int64) []int64 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]int64)
return v
}
func InsertUintSlice(src []int, index int, value uint) []uint {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]uint)
return v
}
func InsertUint8Slice(src []int8, index int, value uint8) []uint8 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]uint8)
return v
}
func InsertUint16Slice(src []int, index int, value uint16) []uint16 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]uint16)
return v
}
func InsertUint32Slice(src []int, index int, value uint32) []uint32 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]uint32)
return v
}
func InsertUint64Slice(src []int, index int, value uint64) []uint64 {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]uint64)
return v
}
func InsertStrSlice(src []int, index int, value string) []string {
tmp, _ := InsertSliceE(src, index, value)
v, _ := tmp.([]string)
return v
}
func UpdateIntSlice(src []int, index, value int) []int {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]int)
return v
}
func UpdateInt8Slice(src []int8, index int, value int8) []int8 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]int8)
return v
}
func UpdateInt16Slice(src []int, index int, value int16) []int16 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]int16)
return v
}
func UpdateInt32Slice(src []int, index int, value int32) []int32 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]int32)
return v
}
func UpdateInt64Slice(src []int, index int, value int64) []int64 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]int64)
return v
}
func UpdateUintSlice(src []int, index int, value uint) []uint {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]uint)
return v
}
func UpdateUint8Slice(src []int8, index int, value uint8) []uint8 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]uint8)
return v
}
func UpdateUint16Slice(src []int, index int, value uint16) []uint16 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]uint16)
return v
}
func UpdateUint32Slice(src []int, index int, value uint32) []uint32 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]uint32)
return v
}
func UpdateUint64Slice(src []int, index int, value uint64) []uint64 {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]uint64)
return v
}
func UpdateStrSlice(src []int, index int, value string) []string {
tmp, _ := UpdateSliceE(src, index, value)
v, _ := tmp.([]string)
return v
}
func GetEleIndexesSlice(slice interface{}, value interface{}) []int {
indexes, _ := GetEleIndexesSliceE(slice, value)
return indexes
}
//
// Part 7: get the min or max element of a slice.
//
func MinIntSlice(sl []int) int {
min, _ := MinSliceE(sl)
v, _ := min.(int)
return v
}
func MinInt8Slice(sl []int8) int8 {
min, _ := MinSliceE(sl)
v, _ := min.(int8)
return v
}
func MinInt16Slice(sl []int16) int16 {
min, _ := MinSliceE(sl)
v, _ := min.(int16)
return v
}
func MinInt32Slice(sl []int32) int32 {
min, _ := MinSliceE(sl)
v, _ := min.(int32)
return v
}
func MinInt64Slice(sl []int64) int64 {
min, _ := MinSliceE(sl)
v, _ := min.(int64)
return v
}
func MinUintSlice(sl []uint) uint {
min, _ := MinSliceE(sl)
v, _ := min.(uint)
return v
}
func MinUint8Slice(sl []uint8) uint8 {
min, _ := MinSliceE(sl)
v, _ := min.(uint8)
return v
}
func MinUint16Slice(sl []uint16) uint16 {
min, _ := MinSliceE(sl)
v, _ := min.(uint16)
return v
}
func MinUint32Slice(sl []uint32) uint32 {
min, _ := MinSliceE(sl)
v, _ := min.(uint32)
return v
}
func MinUint64Slice(sl []uint64) uint64 {
min, _ := MinSliceE(sl)
v, _ := min.(uint64)
return v
}
func MinFloat32Slice(sl []float32) float32 {
min, _ := MinSliceE(sl)
v, _ := min.(float32)
return v
}
func MinFloat64Slice(sl []float64) float64 {
min, _ := MinSliceE(sl)
v, _ := min.(float64)
return v
}
func MaxIntSlice(sl []int) int {
max, _ := MaxSliceE(sl)
v, _ := max.(int)
return v
}
func MaxInt8Slice(sl []int8) int8 {
max, _ := MaxSliceE(sl)
v, _ := max.(int8)
return v
}
func MaxInt16Slice(sl []int16) int16 {
max, _ := MaxSliceE(sl)
v, _ := max.(int16)
return v
}
func MaxInt32Slice(sl []int32) int32 {
max, _ := MaxSliceE(sl)
v, _ := max.(int32)
return v
}
func MaxInt64Slice(sl []int64) int64 {
max, _ := MaxSliceE(sl)
v, _ := max.(int64)
return v
}
func MaxUintSl(sl []uint) uint {
max, _ := MaxSliceE(sl)
v, _ := max.(uint)
return v
}
func MaxUint8Slice(sl []uint8) uint8 {
max, _ := MaxSliceE(sl)
v, _ := max.(uint8)
return v
}
func MaxUint16Slice(sl []uint16) uint16 {
max, _ := MaxSliceE(sl)
v, _ := max.(uint16)
return v
}
func MaxUint32Slice(sl []uint32) uint32 {
max, _ := MaxSliceE(sl)
v, _ := max.(uint32)
return v
}
func MaxUint64Slice(sl []uint64) uint64 {
max, _ := MaxSliceE(sl)
v, _ := max.(uint64)
return v
}
func MaxFloat32Slice(sl []float32) float32 {
max, _ := MaxSliceE(sl)
v, _ := max.(float32)
return v
}
func MaxFloat64Slice(sl []float64) float64 {
max, _ := MaxSliceE(sl)
v, _ := max.(float64)
return v
}
//
// Part 8: get a random element from a slice or array.
//
// GetRandomSliceElem get a random element from a slice or array.
// If the length of slice or array is zero it will panic.
func GetRandomSliceElem(i interface{}) interface{} {
v := reflect.ValueOf(i)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return i
}
idx := RandInt(0, v.Len())
return v.Index(idx).Interface()
}
//
// Part x: basic operating functions of slice.
//
// UniqueSliceE deletes repeated elements in a slice with error.
// Note that the original slice will not be modified.
func UniqueSliceE(slice interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
// unique the slice
dst := reflect.MakeSlice(reflect.TypeOf(slice), 0, v.Len())
m := make(map[interface{}]struct{})
for i := 0; i < v.Len(); i++ {
if _, ok := m[v.Index(i).Interface()]; !ok {
dst = reflect.Append(dst, v.Index(i))
m[v.Index(i).Interface()] = struct{}{}
}
}
return dst.Interface(), nil
}
// ReverseSliceE reverses the specified slice without modifying the original slice.
func ReverseSliceE(slice interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
// reverse the slice
dst := reflect.MakeSlice(reflect.TypeOf(slice), 0, v.Len())
for i := v.Len() - 1; i >= 0; i-- {
dst = reflect.Append(dst, v.Index(i))
}
return dst.Interface(), nil
}
// SumSliceE returns the sum of slice elements and an error if occurred.
func SumSliceE(slice interface{}) (float64, error) {
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return 0.0, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
var sum float64
for i := 0; i < v.Len(); i++ {
switch v := v.Index(i).Interface().(type) {
case int:
sum += float64(v)
case int8:
sum += float64(v)
case int16:
sum += float64(v)
case int32:
sum += float64(v)
case int64:
sum += float64(v)
case uint:
sum += float64(v)
case uint8:
sum += float64(v)
case uint16:
sum += float64(v)
case uint32:
sum += float64(v)
case uint64:
sum += float64(v)
case float32:
sum += float64(v)
case float64:
sum += v
default:
return 0.0, fmt.Errorf("the element %#v of slice type %T isn't numerical type", v, v)
}
}
return sum, nil
}
// MinSliceE returns the smallest element of the slice and an error if occurred.
// If slice length is zero return the zero value of the element type.
func MinSliceE(slice interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
if v.Len() == 0 {
return nil, nil
}
// get the min element
min := v.Index(0).Interface()
for i := 1; i < v.Len(); i++ {
switch v := v.Index(i).Interface().(type) {
case int:
if v < min.(int) {
min = v
}
case int8:
if v < min.(int8) {
min = v
}
case int16:
if v < min.(int16) {
min = v
}
case int32:
if v < min.(int32) {
min = v
}
case int64:
if v < min.(int64) {
min = v
}
case uint:
if v < min.(uint) {
min = v
}
case uint8:
if v < min.(uint8) {
min = v
}
case uint16:
if v < min.(uint16) {
min = v
}
case uint32:
if v < min.(uint32) {
min = v
}
case uint64:
if v < min.(uint64) {
min = v
}
case float32:
if v < min.(float32) {
min = v
}
case float64:
if v < min.(float64) {
min = v
}
default:
return nil, fmt.Errorf("the element %#v of slice type %T isn't numerical type", v, v)
}
}
return min, nil
}
// MaxSliceE returns the largest element of the slice and an error if occurred.
// If slice length is zero return the zero value of the element type.
func MaxSliceE(slice interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
if v.Len() == 0 {
return nil, nil
}
// get the max element.
max := v.Index(0).Interface()
for i := 1; i < v.Len(); i++ {
switch v := v.Index(i).Interface().(type) {
case int:
if v > max.(int) {
max = v
}
case int8:
if v > max.(int8) {
max = v
}
case int16:
if v > max.(int16) {
max = v
}
case int32:
if v > max.(int32) {
max = v
}
case int64:
if v > max.(int64) {
max = v
}
case uint:
if v > max.(uint) {
max = v
}
case uint8:
if v > max.(uint8) {
max = v
}
case uint16:
if v > max.(uint16) {
max = v
}
case uint32:
if v > max.(uint32) {
max = v
}
case uint64:
if v > max.(uint64) {
max = v
}
case float32:
if v > max.(float32) {
max = v
}
case float64:
if v > max.(float64) {
max = v
}
default:
return nil, fmt.Errorf("the element %#v of slice type %T isn't numerical type", v, v)
}
}
return max, nil
}
// JoinSliceWithSepE joins all elements in slice or array with separator and return an error if occurred.
func JoinSliceWithSepE(slice interface{}, sep string) (string, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return "", fmt.Errorf("the input %#v of type %T isn't a slice or array", slice, slice)
}
// join the slice or array
var s string
for i := 0; i < v.Len(); i++ {
if len(s) > 0 {
s += sep
}
str := InterfaceToString(v.Index(i).Interface())
s += str
}
return s, nil
}
// InsertSliceE inserts a element to slice in the specified index.
// Note that the original slice will not be modified.
func InsertSliceE(slice interface{}, index int, value interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
t := reflect.TypeOf(slice)
if index < 0 || index > v.Len() || t.Elem() != reflect.TypeOf(value) {
return nil, errors.New("param is invalid")
}
dst := reflect.MakeSlice(t, 0, v.Len()+1)
// add the element to the end of slice
if index == v.Len() {
dst = reflect.AppendSlice(dst, v)
dst = reflect.Append(dst, reflect.ValueOf(value))
return dst.Interface(), nil
}
dst = reflect.AppendSlice(dst, v.Slice(0, index+1))
dst = reflect.AppendSlice(dst, v.Slice(index, v.Len()))
dst.Index(index).Set(reflect.ValueOf(value))
return dst.Interface(), nil
}
// UpdateSliceE modifies the specified index element of slice.
// Note that the original slice will not be modified.
func UpdateSliceE(slice interface{}, index int, value interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
if index > v.Len()-1 || reflect.TypeOf(slice).Elem() != reflect.TypeOf(value) {
return nil, errors.New("param is invalid")
}
t := reflect.MakeSlice(reflect.TypeOf(slice), 0, 0)
t = reflect.AppendSlice(t, v.Slice(0, v.Len()))
t.Index(index).Set(reflect.ValueOf(value))
return t.Interface(), nil
}
// GetEleIndexesSliceE finds all indexes of the specified element in a slice.
func GetEleIndexesSliceE(slice interface{}, value interface{}) ([]int, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("the input %#v of type %T isn't a slice", slice, slice)
}
// get indexes
var indexes []int
for i := 0; i < v.Len(); i++ {
if v.Index(i).Interface() == value {
indexes = append(indexes, i)
}
}
return indexes, nil
}
// DeleteStrSlice deletes string slice elements by indexes.
func DeleteStrSlice(src []string, indexes ...int) []string {
return DeleteSlice(src, indexes...).([]string)
}
// DeleteIntSlice deletes int slice elements by indexes.
func DeleteIntSlice(src []int, indexes ...int) []int {
return DeleteSlice(src, indexes...).([]int)
}
// DeleteInt8Slice deletes int8 slice elements by indexes.
func DeleteInt8Slice(src []int8, indexes ...int) []int8 {
return DeleteSlice(src, indexes...).([]int8)
}
// DeleteInt16Slice deletes int16 slice elements by indexes.
func DeleteInt16Slice(src []int16, indexes ...int) []int16 {
return DeleteSlice(src, indexes...).([]int16)
}
// DeleteInt32Slice deletes int32 slice elements by indexes.
func DeleteInt32Slice(src []int32, indexes ...int) []int32 {
return DeleteSlice(src, indexes...).([]int32)
}
// DeleteInt64Slice deletes int64 slice elements by indexes.
func DeleteInt64Slice(src []int64, indexes ...int) []int64 {
return DeleteSlice(src, indexes...).([]int64)
}
// DeleteUintSlice deletes uint slice elements by indexes.
func DeleteUintSlice(src []int, indexes ...int) []uint {
return DeleteSlice(src, indexes...).([]uint)
}
// DeleteUint8Slice deletes uint8 slice elements by indexes.
func DeleteUint8Slice(src []int8, indexes ...int) []uint8 {
return DeleteSlice(src, indexes...).([]uint8)
}
// DeleteUint16Slice deletes uint16 slice elements by indexes.
func DeleteUint16Slice(src []int, indexes ...int) []uint16 {
return DeleteSlice(src, indexes...).([]uint16)
}
// DeleteUint32Slice deletes uint32 slice elements by indexes.
func DeleteUint32Slice(src []uint32, indexes ...int) []uint32 {
return DeleteSlice(src, indexes...).([]uint32)
}
// DeleteUint64Slice deletes uint64 slice elements by indexes.
func DeleteUint64Slice(src []uint64, indexes ...int) []uint64 {
return DeleteSlice(src, indexes...).([]uint64)
}
// DeleteSliceElms deletes the specified elements from the slice.
// Note that the original slice will not be modified.
func DeleteSliceElms(i interface{}, elms ...interface{}) interface{} {
res, _ := DeleteSliceElmsE(i, elms...)
return res
}
// DeleteSliceElmsE deletes the specified elements from the slice.
// Note that the original slice will not be modified.
func DeleteSliceElmsE(i interface{}, elms ...interface{}) (interface{}, error) {
// check params
v := reflect.ValueOf(i)
if v.Kind() != reflect.Slice {
return nil, errors.New("the input isn't a slice")
}
if v.Len() == 0 || len(elms) == 0 {
return i, nil
}
if reflect.TypeOf(i).Elem() != reflect.TypeOf(elms[0]) {
return nil, errors.New("element type is ill")
}
// convert the elements to map set
m := make(map[interface{}]struct{})
for _, v := range elms {
m[v] = struct{}{}
}
// filter out specified elements
t := reflect.MakeSlice(reflect.TypeOf(i), 0, v.Len())
for i := 0; i < v.Len(); i++ {
if _, ok := m[v.Index(i).Interface()]; !ok {
t = reflect.Append(t, v.Index(i))
}
}
return t.Interface(), nil
}
// DeleteSlice deletes the specified index element from the slice.
// Note that the original slice will not be modified.
func DeleteSlice(slice interface{}, indexes ...int) interface{} {
res, _ := DeleteSliceE(slice, indexes...)
return res
}
// DeleteSliceE deletes the specified index element from the slice with error.
// Note that the original slice will not be modified.
func DeleteSliceE(slice interface{}, indexes ...int) (interface{}, error) {
// check params
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil, errors.New("the input isn't a slice")
}
if v.Len() == 0 || len(indexes) == 0 {
return slice, nil
}
// convert the indexes to map set
m := make(map[int]struct{})
for _, i := range indexes {
m[i] = struct{}{}
}
// delete
t := reflect.MakeSlice(reflect.TypeOf(slice), 0, v.Len())
for i := 0; i < v.Len(); i++ {
if _, ok := m[i]; !ok {
t = reflect.Append(t, v.Index(i))
}
}
return t.Interface(), nil
}
1
https://gitee.com/zhenyangze/zlib.git
git@gitee.com:zhenyangze/zlib.git
zhenyangze
zlib
zlib
v1.0.2

搜索帮助