diff --git a/datastructure/list/copyonwritelist.go b/datastructure/list/copyonwritelist.go index 915bdbd..2878bbc 100644 --- a/datastructure/list/copyonwritelist.go +++ b/datastructure/list/copyonwritelist.go @@ -90,6 +90,35 @@ func lastIndexOf[T any](o T, e []T, start int, end int) int { return -1 } +// LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying the +// functional predicate f(T) bool +// if not found return -1. +func (l *CopyOnWriteList[T]) LastIndexOfFunc(f func(T) bool) int { + index := -1 + data := l.getList() + for i := len(data) - 1; i >= 0; i-- { + if f(data[i]) { + index = i + break + } + } + return index +} + +// IndexOfFunc returns the first index satisfying the functional predicate f(v) bool +// if not found return -1. +func (l *CopyOnWriteList[T]) IndexOfFunc(f func(T) bool) int { + index := -1 + data := l.getList() + for i, v := range data { + if f(v) { + index = i + break + } + } + return index +} + // get returns the element at the specified position in this list. func get[T any](o []T, index int) *T { return &o[index] diff --git a/datastructure/list/copyonwritelist_test.go b/datastructure/list/copyonwritelist_test.go index fdd84fd..7b90d1d 100644 --- a/datastructure/list/copyonwritelist_test.go +++ b/datastructure/list/copyonwritelist_test.go @@ -1,8 +1,9 @@ package datastructure import ( - "github.com/duke-git/lancet/v2/internal" "testing" + + "github.com/duke-git/lancet/v2/internal" ) func TestCopyOnWriteList_ValueOf(t *testing.T) { @@ -233,3 +234,35 @@ func TestCopyOnWriteList_SubList(t *testing.T) { subList = list.SubList(11, 1) assert.Equal([]int{}, subList) } + +func TestCopyOnWriteListIndexOfFunc(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestIndexOfFunc") + + list := NewCopyOnWriteList([]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 TestNewCopyOnWriteListLastIndexOfFunc(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestLastIndexOfFunc") + + list := NewCopyOnWriteList([]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) +} diff --git a/docs/api/packages/datastructure/copyonwritelist.md b/docs/api/packages/datastructure/copyonwritelist.md index 6a6d7c6..d68b3a4 100644 --- a/docs/api/packages/datastructure/copyonwritelist.md +++ b/docs/api/packages/datastructure/copyonwritelist.md @@ -26,6 +26,8 @@ import ( - [Remove](#Remove) - [IndexOf](#IndexOf) - [LastIndexOf](#LastIndexOf) +- [IndexOfFunc](#IndexOfFunc) +- [LastIndexOfFunc](#LastIndexOfFunc) - [IsEmpty](#IsEmpty) - [Contain](#Contain) - [ValueOf](#ValueOf) @@ -198,6 +200,58 @@ func main() { ``` +### IndexOfFunc +
返回第一个符合函数条件的元素的索引。如果未找到,则返回-1
+ +函数签名: + +```go +func (l *CopyOnWriteList[T]) IndexOfFunc(f func(T) bool) int +``` +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + l := list.NewCopyOnWriteList([]int{1, 2, 3}) + + fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 1 })) //0 + fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 0 })) //-1 +} +``` + +### LastIndexOfFunc +返回最后一个符合函数条件的元素的索引。如果未找到,则返回-1
+ +函数签名: + +```go +func (l *CopyOnWriteList[T]) LastIndexOfFunc(f func(T) bool) int +``` +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + l := list.NewCopyOnWriteList([]int{1, 2, 3, 1}) + + fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3 + fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1 +} +``` + ### IsEmpty 如果此列表不包含任何元素,则返回 true。 diff --git a/docs/en/api/packages/datastructure/copyonwritelist.md b/docs/en/api/packages/datastructure/copyonwritelist.md index 902d3b6..0419fb7 100644 --- a/docs/en/api/packages/datastructure/copyonwritelist.md +++ b/docs/en/api/packages/datastructure/copyonwritelist.md @@ -26,6 +26,8 @@ import ( - [Remove](#Remove) - [IndexOf](#IndexOf) - [LastIndexOf](#LastIndexOf) +- [IndexOfFunc](#IndexOfFunc) +- [LastIndexOfFunc](#LastIndexOfFunc) - [IsEmpty](#IsEmpty) - [Contain](#Contain) - [ValueOf](#ValueOf) @@ -197,6 +199,59 @@ func main() { ``` + +### IndexOfFunc +IndexOfFunc returns the first index satisfying the functional predicate f(v) bool. if not found return -1.
+ +Signature: + +```go +func (l *CopyOnWriteList[T]) IndexOfFunc(f func(T) bool) int +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + l := list.NewCopyOnWriteList([]int{1, 2, 3}) + + fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 1 })) //0 + fmt.Println(l.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 the functional predicate f(T) bool. if not found return -1.
+ +Signature: + +```go +func (l *CopyOnWriteList[T]) LastIndexOfFunc(f func(T) bool) int +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + l := list.NewCopyOnWriteList([]int{1, 2, 3, 1}) + + fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3 + fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1 +} +``` + ### IsEmpty Returns true if this list does not contain any elements.