提交
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
// 交集
|
||||
// 并集
|
||||
// 差集
|
||||
// 补集
|
||||
// 去重
|
||||
// 排序
|
||||
// 反转
|
||||
// 求和
|
||||
// 求平均值
|
||||
// 求最大值
|
||||
// 求最小值
|
||||
// 求最大值和最小值
|
||||
// 求最大值和最小值的差值
|
||||
// 求最大值和最小值的差值的绝对值
|
||||
// 求最大值和最小值的差值的绝对值的平均值
|
||||
// 求最大值和最小值的差值的绝对值的平均值的平方根
|
||||
// 求最大值和最小值的差值的绝对值的平均值的平方根的平方
|
||||
// 求最大值和最小值的差值的绝对值的平均值的平方根的平方的平均值
|
||||
@@ -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数组里面")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
module code.yun.ink/pkg/arrayx
|
||||
|
||||
go 1.19
|
||||
|
||||
require code.yun.ink/open/utils v1.0.0
|
||||
Reference in New Issue
Block a user