Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Aug 27, 2024
1 parent be4ea3d commit bf76d67
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 294 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/go-pay/util

go 1.20
go 1.21
264 changes: 124 additions & 140 deletions slice.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package util

import (
"sort"
)
import "slices"

// Deprecated
// IntDeduplicate() is deprecated, please use DeduplicateSlice() instead.
// int 数组去重
func IntDeduplicate(slice []int) (result []int) {
var dupMap = make(map[int]struct{})
Expand All @@ -17,34 +17,16 @@ func IntDeduplicate(slice []int) (result []int) {
return result
}

// int 数组排序+去重
func IntSortDeduplicate(slice []int) (result []int) {
tmp := make([]int, len(slice))
copy(tmp, slice)
sort.Ints(tmp)
j := 0
for i := 1; i < len(tmp); i++ {
if tmp[j] == tmp[i] {
continue
}
j++
tmp[j] = tmp[i]
}
return tmp[:j+1]
}

// Deprecated
// IntMergeDeduplicate() is deprecated, please use MergeDeduplicateSlice() instead.
// int 数组合并+去重
func IntMergeDeduplicate(slice1, slice2 []int) (result []int) {
slice1 = append(slice1, slice2...)
return IntDeduplicate(slice1)
}

// int 数组合并排序+去重
func IntMergeSortDeduplicate(slice1, slice2 []int) (result []int) {
slice1 = append(slice1, slice2...)
return IntSortDeduplicate(slice1)
}

// Deprecated
// IntIntersect() is deprecated, please use IntersectSlice() instead.
// int 数组,slice1 和 slice2 交集
func IntIntersect(slice1, slice2 []int) (result []int) {
m := make(map[int]struct{})
Expand All @@ -60,22 +42,8 @@ func IntIntersect(slice1, slice2 []int) (result []int) {
return
}

// int 数组,slice1 和 slice2 交集并排序
func IntSortIntersect(slice1, slice2 []int) (result []int) {
m := make(map[int]struct{})
for _, v := range slice1 {
m[v] = struct{}{}
}
for _, v := range slice2 {
_, ok := m[v]
if ok {
result = append(result, v)
}
}
sort.Ints(result)
return
}

// Deprecated
// IntUnion() is deprecated, please use UnionSlice() instead.
// int 数组,slice1 和 slice2 并集
func IntUnion(slice1, slice2 []int) (result []int) {
m := make(map[int]struct{})
Expand All @@ -94,25 +62,8 @@ func IntUnion(slice1, slice2 []int) (result []int) {
return
}

// int 数组,slice1 和 slice2 并集并排序
func IntSortUnion(slice1, slice2 []int) (result []int) {
m := make(map[int]struct{})
for _, v := range slice1 {
m[v] = struct{}{}
}
for _, v := range slice2 {
_, ok := m[v]
if !ok {
m[v] = struct{}{}
}
}
for k := range m {
result = append(result, k)
}
sort.Ints(result)
return
}

// Deprecated
// IntRemoveElementByIndex() is deprecated, please use RemoveSliceElementByIndex() instead.
// int 数组,根据index移除元素
// return new slice
func IntRemoveElementByIndex(slice []int, index int) (result []int) {
Expand All @@ -123,6 +74,8 @@ func IntRemoveElementByIndex(slice []int, index int) (result []int) {
return
}

// Deprecated
// IntRemoveElement() is deprecated, please use RemoveSliceElement() instead.
// int 数组,移除元素
// If n < 0, there is no limit on the number of remove.
// return new slice
Expand All @@ -145,6 +98,8 @@ func IntRemoveElement(slice []int, elem, n int) (result []int) {
return result[:i]
}

// Deprecated
// StringDeduplicate() is deprecated, please use DeduplicateSlice() instead.
// string 数组去重
func StringDeduplicate(slice []string) (result []string) {
var dupMap = make(map[string]struct{})
Expand All @@ -158,116 +113,160 @@ func StringDeduplicate(slice []string) (result []string) {
return result
}

// string 数组排序+去重
func StringSortDeduplicate(slice []string) (result []string) {
tmp := make([]string, len(slice))
copy(tmp, slice)
sort.Strings(tmp)
j := 0
for i := 1; i < len(tmp); i++ {
if tmp[j] == tmp[i] {
continue
}
j++
tmp[j] = tmp[i]
}
return tmp[:j+1]
}

// Deprecated
// StringMergeDeduplicate() is deprecated, please use MergeDeduplicateSlice() instead.
// string 数组合并+去重
func StringMergeDeduplicate(slice1, slice2 []string) (result []string) {
slice1 = append(slice1, slice2...)
return StringDeduplicate(slice1)
}

// string 数组合并排序+去重
func StringMergeSortDeduplicate(slice1, slice2 []string) (result []string) {
slice1 = append(slice1, slice2...)
return StringSortDeduplicate(slice1)
// Deprecated
// StringRemoveElementByIndex() is deprecated, please use RemoveSliceElementByIndex() instead.
// string 数组,根据index移除元素
// return new slice
func StringRemoveElementByIndex(slice []string, index int) (result []string) {
if index < 0 || index >= len(slice) {
return slice
}
result = append(slice[:index], slice[index+1:]...)
return
}

// string 数组,slice1 和 slice2 交集
func StringIntersect(slice1, slice2 []string) (result []string) {
m := make(map[string]struct{})
for _, v := range slice1 {
m[v] = struct{}{}
// Deprecated
// string 数组,移除元素
// If n < 0, there is no limit on the number of remove.
// return new slice
func StringRemoveElement(slice []string, elem string, n int) (result []string) {
if n == 0 {
return append(result, slice...) // 返回输入切片的复制品
}
for _, v := range slice2 {
_, ok := m[v]
if ok {
// 复制输入切片到result中
result = append(result, slice...)
i, j := 0, 0
for j < len(result) {
if result[j] != elem || n == 0 {
result[i] = result[j]
i++
} else {
n--
}
j++
}
return result[:i]
}

// Deprecated
// FilterIntSlice() is deprecated, please use FilterSlice() instead.
// 过滤数组,去除src在dst中存在的item
// src[1,2,3,4,5] dst[2,4,6,8] result[1,3,5]
func FilterIntSlice(src []int, dst []int) (result []int) {
aMap := make(map[int]struct{})
result = make([]int, 0)
for _, v := range dst {
aMap[v] = struct{}{}
}
for _, v := range src {
if _, has := aMap[v]; !has {
result = append(result, v)
}
}
return
return result
}

// string 数组,slice1 和 slice2 交集并排序
func StringSortIntersect(slice1, slice2 []string) (result []string) {
m := make(map[string]struct{})
for _, v := range slice1 {
m[v] = struct{}{}
// Deprecated
// FilterStringSlice() is deprecated, please use FilterSlice() instead.
// 过滤数组,去除src在dst中存在的item
// src["a","b","c","d","e"] dst["b","d","f","h"] result["a","c","e"]
func FilterStringSlice(src []string, dst []string) (result []string) {
aMap := make(map[string]struct{})
result = make([]string, 0)
for _, v := range dst {
aMap[v] = struct{}{}
}
for _, v := range slice2 {
_, ok := m[v]
if ok {
for _, v := range src {
if _, has := aMap[v]; !has {
result = append(result, v)
}
}
sort.Strings(result)
return
return result
}

// string 数组,slice1 和 slice2 并集
func StringUnion(slice1, slice2 []string) (result []string) {
m := make(map[string]struct{})
for _, v := range slice1 {
m[v] = struct{}{}
// ==================new==================

// 数组去重
func DeduplicateSlice[T comparable](slice []T) (result []T) {
var dupMap = make(map[T]struct{})
for _, v := range slice {
length := len(dupMap)
dupMap[v] = struct{}{}
if len(dupMap) != length {
result = append(result, v)
}
}
for _, v := range slice2 {
return result
}

// 数组合并+去重
func MergeDeduplicateSlice[T comparable](slice1, slice2 []T) (result []T) {
slice1 = append(slice1, slice2...)
return DeduplicateSlice(slice1)
}

// slice1 和 slice2 交集
func IntersectSlice[T comparable](slice1, slice2 []T) (result []T) {
m := make(map[T]int)
for _, v := range slice1 {
_, ok := m[v]
if !ok {
m[v] = struct{}{}
m[v] = 1
}
}
for k := range m {
result = append(result, k)
for _, v := range slice2 {
count, ok := m[v]
if ok && count == 1 {
m[v]++
result = append(result, v)
}
}
return
}

// string 数组,slice1 和 slice2 并集
func StringSortUnion(slice1, slice2 []string) (result []string) {
m := make(map[string]struct{})
func UnionSlice[T comparable](slice1, slice2 []T) (result []T) {
m := make(map[T]struct{})
for _, v := range slice1 {
m[v] = struct{}{}
_, ok := m[v]
if !ok {
m[v] = struct{}{}
result = append(result, v)
}
}
for _, v := range slice2 {
_, ok := m[v]
if !ok {
m[v] = struct{}{}
result = append(result, v)
}
}
for k := range m {
result = append(result, k)
}
sort.Strings(result)
return
}

// string 数组,根据index移除元素
// 数组根据index移除元素
// return new slice
func StringRemoveElementByIndex(slice []string, index int) (result []string) {
if index < 0 || index >= len(slice) {
return slice
func RemoveSliceElementByIndex[T comparable](slice []T, index int) (result []T) {
clone := slices.Clone(slice)
if index < 0 || index >= len(clone) {
return clone
}
result = append(slice[:index], slice[index+1:]...)
result = append(clone[:index], clone[index+1:]...)
return
}

// string 数组,移除元素
// 数组移除元素
// If n < 0, there is no limit on the number of remove.
// return new slice
func StringRemoveElement(slice []string, elem string, n int) (result []string) {
func RemoveSliceElement[T comparable](slice []T, elem T, n int) (result []T) {
if n == 0 {
return append(result, slice...) // 返回输入切片的复制品
}
Expand All @@ -288,25 +287,10 @@ func StringRemoveElement(slice []string, elem string, n int) (result []string) {

// 过滤数组,去除src在dst中存在的item
// src[1,2,3,4,5] dst[2,4,6,8] result[1,3,5]
func FilterIntSlice(src []int, dst []int) (result []int) {
aMap := make(map[int]struct{})
result = make([]int, 0)
for _, v := range dst {
aMap[v] = struct{}{}
}
for _, v := range src {
if _, has := aMap[v]; !has {
result = append(result, v)
}
}
return result
}

// 过滤数组,去除src在dst中存在的item
// src["a","b","c","d","e"] dst["b","d","f","h"] result["a","c","e"]
func FilterStringSlice(src []string, dst []string) (result []string) {
aMap := make(map[string]struct{})
result = make([]string, 0)
func FilterSlice[T comparable](src, dst []T) (result []T) {
aMap := make(map[T]struct{})
result = make([]T, 0)
for _, v := range dst {
aMap[v] = struct{}{}
}
Expand Down
Loading

0 comments on commit bf76d67

Please sign in to comment.