Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice: 支持 Diff*, Union*, SymmetricDiff*, Intersection*, Index* 方法 #83

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
- [ekit:引入 golangci-lint 和 goimports](https://github.com/gotomicro/ekit/pull/54)
- [ekit: 实现了 TaskPool](https://github.com/gotomicro/ekit/pull/57)
- [ekit: 修复OnDemandBlockTaskPool测试不稳定](https://github.com/gotomicro/ekit/pull/70)
- [syncx: 使用泛型封装 sync.Map](https://github.com/gotomicro/ekit/pull/79)
- [syncx: 使用泛型封装 sync.Map](https://github.com/gotomicro/ekit/pull/79)
- [slice: 支持 Diff*, Intersection*, Union*, Index* 类方法](https://github.com/gotomicro/ekit/pull/83)
4 changes: 2 additions & 2 deletions .github/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

# Pre-commit configuration

RESULT=$(make check)
make check
printf "执行检查中...\n"

if [ -n "$RESULT" ]; then
if [ $? -ne 0 ]; then
echo >&2 "[ERROR]: 有文件发生变更,请将变更文件添加到本次提交中"
exit 1
fi
Expand Down
12 changes: 4 additions & 8 deletions script/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ SOURCE_PUSH=.github/pre-push
TARGET_PUSH=.git/hooks/pre-push

# copy pre-commit file if not exist.
if [ ! -f $TARGET_COMMIT ]; then
echo "设置 git pre-commit hooks..."
cp $SOURCE_COMMIT $TARGET_COMMIT
fi
echo "设置 git pre-commit hooks..."
cp $SOURCE_COMMIT $TARGET_COMMIT

# copy pre-push file if not exist.
if [ ! -f $TARGET_PUSH ]; then
echo "设置 git pre-push hooks..."
cp $SOURCE_PUSH $TARGET_PUSH
fi
echo "设置 git pre-push hooks..."
cp $SOURCE_PUSH $TARGET_PUSH

# add permission to TARGET_PUSH and TARGET_COMMIT file.
test -x $TARGET_PUSH || chmod +x $TARGET_PUSH
Expand Down
47 changes: 41 additions & 6 deletions slice/contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,68 @@ package slice

// Contains 判断 src 里面是否存在 dst
func Contains[T comparable](src []T, dst T) bool {
for _, v := range src {
if v == dst {
return true
}
}
return false
}

// ContainsFunc 判断 src 里面是否存在 dst
// 你应该优先使用 Contains
func ContainsFunc[T any](src []T, dst T, equal EqualFunc[T]) bool {
func ContainsFunc[T any](src []T, dst T, equal equalFunc[T]) bool {
// 遍历调用equal函数进行判断
for _, v := range src {
if equal(v, dst) {
return true
}
}
return false
}

// ContainsAny 判断 src 里面是否存在 dst 中的任何一个元素
func ContainsAny[T comparable](src, dst []T) bool {
srcMap := toMap[T](src)
for _, v := range dst {
if _, exist := srcMap[v]; exist {
return true
}
}
return false
}

// ContainsAnyFunc 判断 src 里面是否存在 dst 中的任何一个元素
// 你应该优先使用 ContainsAny
func ContainsAnyFunc[T any](src, dst []T, equal EqualFunc[T]) bool {
func ContainsAnyFunc[T any](src, dst []T, equal equalFunc[T]) bool {
for _, valDst := range dst {
for _, valSrc := range src {
if equal(valSrc, valDst) {
return true
}
}
}
return false
}

// ContainsAll 判断 src 里面是否存在 dst 中的所有元素
func ContainsAll[T comparable](src, dst []T) bool {
return false
srcMap := toMap[T](src)
for _, v := range dst {
if _, exist := srcMap[v]; !exist {
return false
}
}
return true
}

// ContainsAllFunc 判断 src 里面是否存在 dst 中的所有元素
// 你应该优先使用 ContainsAllFunc
func ContainsAllFunc[T any](src, dst []T, equal EqualFunc[T]) bool {
return false
// 你应该优先使用 ContainsAll
func ContainsAllFunc[T any](src, dst []T, equal equalFunc[T]) bool {
for _, valDst := range dst {
if !ContainsFunc[T](src, valDst, equal) {
return false
}
}
return true
}
Loading