From 301249e56702237cc02962ce272f1fcdfb3da4b7 Mon Sep 17 00:00:00 2001 From: Yun Date: Wed, 27 Dec 2023 19:05:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrayx.go | 142 +++++++++++++++++++++++++++++++++++++++++++++++++ arrayx_test.go | 51 ++++++++++++++++++ go.mod | 5 ++ go.sum | 2 + 4 files changed, 200 insertions(+) create mode 100644 arrayx.go create mode 100644 arrayx_test.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/arrayx.go b/arrayx.go new file mode 100644 index 0000000..8402a8a --- /dev/null +++ b/arrayx.go @@ -0,0 +1,142 @@ +package arrayx + +// Author: yun +// Version: 2023年10月24日15:31:41 + +import ( + "fmt" + "reflect" +) + +/** + * 判断是否在一个数组里面 + * + * 针对需要重复判断的情况,这个方法会比InArray()快一点 + * + * @param target interface{} 目标值 + * @param array interface{} 数组 + * @return bool 是否存在 + * @return int 索引 + */ + +type inArray struct { + array interface{} +} + +func NewArray(array interface{}) *inArray { + return &inArray{array: array} +} +func (i *inArray) In(target interface{}) (exists bool, index int) { + exists = false + index = -1 + switch reflect.TypeOf(i.array).Kind() { + case reflect.Slice: + s := reflect.ValueOf(i.array) + for i := 0; i < s.Len(); i++ { + if reflect.DeepEqual(target, s.Index(i).Interface()) { + index = i + exists = true + return + } + } + } + return +} + +/** + * 判断是否在一个数组里面 + * + * @param target interface{} 目标值 + * @param array interface{} 数组 + * @return bool 是否存在 + * @return int 索引 + */ +func InArray(target interface{}, array interface{}) (exists bool, index int) { + exists = false + index = -1 + switch reflect.TypeOf(array).Kind() { + case reflect.Slice: + s := reflect.ValueOf(array) + for i := 0; i < s.Len(); i++ { + if reflect.DeepEqual(target, s.Index(i).Interface()) { + index = i + exists = true + return + } + } + } + return +} + +// 判断字符串是否在字符串数组中 +// TODO:待验证 +// func InStringArray(target string, strArray []string) bool { +// // 对字符串切片进行排序 +// sort.Strings(strArray) +// index := sort.SearchStrings(strArray, target) +// // 先判断 &&左侧的条件,如果不满足则结束此处判断,不会再进行右侧的判断 +// if index < len(strArray) && strArray[index] == target { +// return true +// } +// return false +// } + +// 数组去重 +func ArrayUniqueString(target []string) []string { + result := make([]string, 0, len(target)) + temp := map[string]struct{}{} + for _, item := range target { + if _, ok := temp[item]; !ok { + temp[item] = struct{}{} + result = append(result, item) + } + } + return result +} + +func ArrayUniqueInt64(target []int64) []int64 { + result := make([]int64, 0, len(target)) + temp := map[int64]struct{}{} + for _, item := range target { + if _, ok := temp[item]; !ok { + temp[item] = struct{}{} + result = append(result, item) + } + } + return result +} + +func ArrayInt64ToString(target []int64) []string { + result := make([]string, 0) + for _, val := range target { + result = append(result, fmt.Sprintf("%d", val)) + } + return result +} + +func ArrayIntToString(target []int) []string { + result := make([]string, 0) + for _, val := range target { + result = append(result, fmt.Sprintf("%d", val)) + } + return result +} + +// 交集 +// 并集 +// 差集 +// 补集 +// 去重 +// 排序 +// 反转 +// 求和 +// 求平均值 +// 求最大值 +// 求最小值 +// 求最大值和最小值 +// 求最大值和最小值的差值 +// 求最大值和最小值的差值的绝对值 +// 求最大值和最小值的差值的绝对值的平均值 +// 求最大值和最小值的差值的绝对值的平均值的平方根 +// 求最大值和最小值的差值的绝对值的平均值的平方根的平方 +// 求最大值和最小值的差值的绝对值的平均值的平方根的平方的平均值 diff --git a/arrayx_test.go b/arrayx_test.go new file mode 100644 index 0000000..0effa31 --- /dev/null +++ b/arrayx_test.go @@ -0,0 +1,51 @@ +package arrayx_test + +import ( + "testing" + + "code.yun.ink/pkg/arrayx" +) + +func TestInArray(t *testing.T) { + arr1 := []int{1, 2, 3} + var val64 int64 = 1 + var val32 int32 = 1 + var val int = 1 + + exist, index := arrayx.InArray(val, arr1) + t.Log(exist, index) + if !exist { + t.Error("int64类型不应该在int数组里面") + } + + exist, index = arrayx.NewArray(arr1).In(val) + t.Log("2", exist, index) + if !exist { + t.Error("int64类型不应该在int数组里面") + } + + exist, index = arrayx.InArray(val32, arr1) + t.Log(exist, index) + if exist { + t.Error("int64类型不应该在int数组里面") + } + + exist, _ = arrayx.InArray(val64, arr1) + if exist { + t.Error("int64类型不应该在int数组里面") + } + arr2 := []interface{}{1, 2, 3} + exist, _ = arrayx.InArray(val64, arr2) + if exist { + t.Error("int64类型不应该在int数组里面") + } + arr3 := []interface{}{ + 1, + "huangxinyun", + 6.6, + } + exist, _ = arrayx.InArray("huangxinyun", arr3) + if !exist { + t.Error("int64类型不应该在int数组里面") + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9513ebf --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module code.yun.ink/pkg/arrayx + +go 1.19 + +require code.yun.ink/open/utils v1.0.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..820fbc5 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +code.yun.ink/open/utils v1.0.0 h1:qMVvvnLZIKkbwNZgr2+gjFdG18CYCWGOBHyLm/llScA= +code.yun.ink/open/utils v1.0.0/go.mod h1:RlmtwWDGJgBuyEhXSW6AP34+JAQFDapmazvV7zDfVoc=