mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12: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:
@@ -58,6 +58,34 @@ func (l *List[T]) LastIndexOf(value T) int {
|
|||||||
return index
|
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
|
// Contain checks if the value in the list or not
|
||||||
func (l *List[T]) Contain(value T) bool {
|
func (l *List[T]) Contain(value T) bool {
|
||||||
data := l.data
|
data := l.data
|
||||||
|
|||||||
@@ -36,6 +36,17 @@ func TestIndexOf(t *testing.T) {
|
|||||||
assert.Equal(-1, i)
|
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) {
|
func TestLastIndexOf(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIndexOf")
|
assert := internal.NewAssert(t, "TestIndexOf")
|
||||||
|
|
||||||
@@ -53,6 +64,23 @@ func TestLastIndexOf(t *testing.T) {
|
|||||||
assert.Equal(0, i)
|
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) {
|
func TestContain(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestContain")
|
assert := internal.NewAssert(t, "TestContain")
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ import (
|
|||||||
- [ValueOf](#ValueOf)
|
- [ValueOf](#ValueOf)
|
||||||
- [IndexOf](#IndexOf)
|
- [IndexOf](#IndexOf)
|
||||||
- [LastIndexOf](#LastIndexOf)
|
- [LastIndexOf](#LastIndexOf)
|
||||||
|
- [IndexOfFunc](#IndexOfFunc)
|
||||||
|
- [LastIndexOfFunc](#LastIndexOfFunc)
|
||||||
- [Push](#Push)
|
- [Push](#Push)
|
||||||
- [PopFirst](#PopFirst)
|
- [PopFirst](#PopFirst)
|
||||||
- [PopLast](#PopLast)
|
- [PopLast](#PopLast)
|
||||||
@@ -219,6 +221,58 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IndexOfFunc">IndexOfFunc</span>
|
||||||
|
<p> IndexOfFunc returns the first index satisfying f(v). if not found return -1</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (l *List[T]) IndexOfFunc(f func(T) bool) int
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LastIndexOfFunc">LastIndexOfFunc</span>
|
||||||
|
<p>LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i]). if not found return -1</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Push">Push</span>
|
### <span id="Push">Push</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user