1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-14 09:42:28 +08:00

list: Add LastIndexOfFunc and IndexOfFunc method (#48)

* LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i])
* IndexOfFunc returns the first index satisfying f(v)
This commit is contained in:
donutloop
2022-07-22 04:04:31 +02:00
committed by GitHub
parent a82b5dd206
commit ac3baac5c6
3 changed files with 110 additions and 0 deletions

View File

@@ -58,6 +58,34 @@ func (l *List[T]) LastIndexOf(value T) int {
return index
}
// IndexOfFunc returns the first index satisfying f(v)
// if not found return -1
func (l *List[T]) IndexOfFunc(f func(T) bool) int {
index := -1
data := l.data
for i, v := range data {
if f(v) {
index = i
break
}
}
return index
}
// LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i])
// if not found return -1
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int {
index := -1
data := l.data
for i := len(data) - 1; i >= 0; i-- {
if f(data[i]) {
index = i
break
}
}
return index
}
// Contain checks if the value in the list or not
func (l *List[T]) Contain(value T) bool {
data := l.data