diff --git a/datastructure/list/list.go b/datastructure/list/list.go index 22587a9..769fcbe 100644 --- a/datastructure/list/list.go +++ b/datastructure/list/list.go @@ -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 diff --git a/datastructure/list/list_test.go b/datastructure/list/list_test.go index 15703c3..e62bffd 100644 --- a/datastructure/list/list_test.go +++ b/datastructure/list/list_test.go @@ -36,6 +36,17 @@ func TestIndexOf(t *testing.T) { assert.Equal(-1, i) } +func TestIndexOfFunc(t *testing.T) { + assert := internal.NewAssert(t, "TestIndexOf") + + list := NewList([]int{1, 2, 3}) + i := list.IndexOfFunc(func(a int) bool { return a == 1 }) + assert.Equal(0, i) + + i = list.IndexOfFunc(func(a int) bool { return a == 4 }) + assert.Equal(-1, i) +} + func TestLastIndexOf(t *testing.T) { assert := internal.NewAssert(t, "TestIndexOf") @@ -53,6 +64,23 @@ func TestLastIndexOf(t *testing.T) { assert.Equal(0, i) } +func TestLastIndexOfFunc(t *testing.T) { + assert := internal.NewAssert(t, "TestIndexOf") + + list := NewList([]int{1, 2, 3, 3, 3, 3, 4, 5, 6, 9}) + i := list.LastIndexOfFunc(func(a int) bool { return a == 3 }) + assert.Equal(5, i) + + i = list.LastIndexOfFunc(func(a int) bool { return a == 10 }) + assert.Equal(-1, i) + + i = list.LastIndexOfFunc(func(a int) bool { return a == 4 }) + assert.Equal(6, i) + + i = list.LastIndexOfFunc(func(a int) bool { return a == 1 }) + assert.Equal(0, i) +} + func TestContain(t *testing.T) { assert := internal.NewAssert(t, "TestContain") diff --git a/docs/datastructure/list.md b/docs/datastructure/list.md index 22d382f..097c28b 100644 --- a/docs/datastructure/list.md +++ b/docs/datastructure/list.md @@ -27,6 +27,8 @@ import ( - [ValueOf](#ValueOf) - [IndexOf](#IndexOf) - [LastIndexOf](#LastIndexOf) +- [IndexOfFunc](#IndexOfFunc) +- [LastIndexOfFunc](#LastIndexOfFunc) - [Push](#Push) - [PopFirst](#PopFirst) - [PopLast](#PopLast) @@ -219,6 +221,58 @@ func main() { } ``` +### IndexOfFunc +
IndexOfFunc returns the first index satisfying f(v). if not found return -1
+ +Signature: + +```go +func (l *List[T]) IndexOfFunc(f func(T) bool) int +``` +Example: + +```go +package main + +import ( + "fmt" + list "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + li := list.NewList([]int{1, 2, 3}) + + fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 1 })) //0 + fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 0 })) //-1 +} +``` + +### LastIndexOfFunc +LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i]). if not found return -1
+ +Signature: + +```go +func (l *List[T]) LastIndexOfFunc(f func(T) bool) int +``` +Example: + +```go +package main + +import ( + "fmt" + list "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + li := list.NewList([]int{1, 2, 3, 1}) + + fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3 + fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1 +} +``` + ### Push