Skip to content

Commit

Permalink
feat: sher 包新增 FindInSlice 和 FindInSliceByBinary 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jan 5, 2024
1 parent 2ff360c commit 96953d7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions utils/sher/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@ package sher

import "github.com/kercylan98/minotaur/utils/generic"

// FindInSlice 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1
func FindInSlice[S ~[]V, V any](slice S, handler func(v V) bool) (i int, t V) {
if slice == nil {
return -1, t
}
for i, v := range slice {
if handler(v) {
return i, v
}
}
return -1, t
}

// FindInSliceByBinary 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1
func FindInSliceByBinary[S ~[]V, V any](slice S, handler func(v V) bool) (i int, t V) {
low := 0
high := len(slice) - 1

for low <= high {
mid := low + (high-low)/2
if handler(slice[mid]) {
return mid, slice[mid]
} else if handler(slice[mid]) {
high = mid - 1
} else {
low = mid + 1
}
}
return -1, t
}

// FindMinimumInSlice 获取切片中的最小值
func FindMinimumInSlice[S ~[]V, V generic.Number](slice S, handler ComparisonHandler[V]) (result V) {
if slice == nil {
Expand Down

0 comments on commit 96953d7

Please sign in to comment.