From a82b5dd206ef710ba681abc0e062541a3aa9d80e Mon Sep 17 00:00:00 2001 From: donutloop Date: Thu, 21 Jul 2022 09:19:18 +0200 Subject: [PATCH] list: add LastIndexOf (#46) Add LastIndexOf method too List and fix doc LastIndexOf returns the index of the last occurrence of the value in this list. if not found return -1 --- datastructure/list/list.go | 16 +++++++++++++++- datastructure/list/list_test.go | 17 +++++++++++++++++ docs/datastructure/list.md | 29 ++++++++++++++++++++++++++++- docs/maputil.md | 2 +- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/datastructure/list/list.go b/datastructure/list/list.go index 9776c56..22587a9 100644 --- a/datastructure/list/list.go +++ b/datastructure/list/list.go @@ -31,7 +31,7 @@ func (l *List[T]) ValueOf(index int) (*T, bool) { return &l.data[index], true } -// IndexOf reture the index of value. if not found return -1 +// IndexOf returns the index of value. if not found return -1 func (l *List[T]) IndexOf(value T) int { index := -1 data := l.data @@ -44,6 +44,20 @@ func (l *List[T]) IndexOf(value T) int { return index } +// LastIndexOf returns the index of the last occurrence of the value in this list. +// if not found return -1 +func (l *List[T]) LastIndexOf(value T) int { + index := -1 + data := l.data + for i := len(data) - 1; i >= 0; i-- { + if reflect.DeepEqual(data[i], value) { + 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 ec663be..15703c3 100644 --- a/datastructure/list/list_test.go +++ b/datastructure/list/list_test.go @@ -36,6 +36,23 @@ func TestIndexOf(t *testing.T) { assert.Equal(-1, i) } +func TestLastIndexOf(t *testing.T) { + assert := internal.NewAssert(t, "TestIndexOf") + + list := NewList([]int{1, 2, 3, 3, 3, 3, 4, 5, 6, 9}) + i := list.LastIndexOf(3) + assert.Equal(5, i) + + i = list.LastIndexOf(10) + assert.Equal(-1, i) + + i = list.LastIndexOf(4) + assert.Equal(6, i) + + i = list.LastIndexOf(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 7ad3715..22d382f 100644 --- a/docs/datastructure/list.md +++ b/docs/datastructure/list.md @@ -26,6 +26,7 @@ import ( - [Data](#Data) - [ValueOf](#ValueOf) - [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) - [Push](#Push) - [PopFirst](#PopFirst) - [PopLast](#PopLast) @@ -167,7 +168,7 @@ func main() { ### IndexOf -

Reture the index of value in the list. if not found return -1

+

Returns the index of value in the list. if not found return -1

Signature: @@ -192,6 +193,32 @@ func main() { } ``` +### LastIndexOf +

Returns the index of the last occurrence of the value in this list if not found return -1

+ +Signature: + +```go +func (l *List[T]) LastIndexOf(value T) 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.LastIndexOf(1)) // 3 + fmt.Println(li.LastIndexOf(0)) //-1 +} +``` + ### Push diff --git a/docs/maputil.md b/docs/maputil.md index ca4778f..aa18039 100644 --- a/docs/maputil.md +++ b/docs/maputil.md @@ -27,7 +27,7 @@ import ( - [Merge](#Merge) - [Minus](#Minus) - [Values](#Values) -- [Values](#Values) +- [IsDisjoint](#IsDisjoint)