From 5446f7e33c964ea9fe5ed52ee53d0ccb50842c3f Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 28 Jul 2022 12:56:11 +0800 Subject: [PATCH] doc: add document for list methods --- docs/datastructure/list_zh-CN.md | 170 +++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/docs/datastructure/list_zh-CN.md b/docs/datastructure/list_zh-CN.md index e7cf69c..6e560e4 100644 --- a/docs/datastructure/list_zh-CN.md +++ b/docs/datastructure/list_zh-CN.md @@ -26,11 +26,15 @@ import ( - [Data](#Data) - [ValueOf](#ValueOf) - [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) +- [IndexOfFunc](#IndexOfFunc) +- [LastIndexOfFunc](#LastIndexOfFunc) - [Push](#Push) - [PopFirst](#PopFirst) - [PopLast](#PopLast) - [DeleteAt](#DeleteAt) - [InsertAt](#InsertAt) + - [UpdateAt](#UpdateAt) - [Equal](#Equal) - [IsEmpty](#IsEmpty) @@ -38,11 +42,14 @@ import ( - [Clone](#Clone) - [Merge](#Merge) - [Size](#Size) +- [Cap](#Cap) - [Swap](#Swap) - [Reverse](#Reverse) - [Unique](#Unique) - [Union](#Union) - [Intersection](#Intersection) +- [SubList](#SubList) +- [DeleteIf](#DeleteIf)
@@ -192,6 +199,85 @@ func main() { ``` +### LastIndexOf +

返回列表中最后一次出现的值的索引。如果未找到,则返回-1

+ +函数签名: + +```go +func (l *List[T]) LastIndexOf(value T) int +``` +例子: + +```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 +} +``` + +### IndexOfFunc +

返回第一个符合函数条件的元素的索引。如果未找到,则返回-1

+ +函数签名: + +```go +func (l *List[T]) IndexOfFunc(f func(T) bool) int +``` +例子: + +```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 +

返回最后一个符合函数条件的元素的索引。如果未找到,则返回-1

+ +函数签名: + +```go +func (l *List[T]) LastIndexOfFunc(f func(T) bool) int +``` +例子: + +```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

将值附加到列表末尾

@@ -566,6 +652,34 @@ func main() { +### Cap +

返回列表数据容量

+ +函数签名: + +```go +func (l *List[T]) Cap() int +``` +例子: + +```go +package main + +import ( + "fmt" + list "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + data := make([]int, 0, 100) + + li := list.NewList(data) + + fmt.Println(li.Cap()) // 100 +} +``` + + ### Swap

交换列表中两个索引位置的值

@@ -709,4 +823,60 @@ func main() { fmt.Println(li3.Data()) //4 } +``` + + + +### SubList +

SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.

+ +函数签名: + +```go +func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] +``` +例子: + +```go +package main + +import ( + "fmt" + list "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + l := list.NewList([]int{1, 2, 3, 4, 5, 6}) + + fmt.Println(l.SubList(2, 5)) // []int{3, 4, 5} +} +``` + + + + +### DeleteIf +

删除列表中所有符合函数(调用函数返回true)的元素,返回删除元素的数量

+ +函数签名: + +```go +func (l *List[T]) DeleteIf(f func(T) bool) int +``` +例子: + +```go +package main + +import ( + "fmt" + list "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1}) + + fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12 + fmt.Println(l.Data()) // []int{2, 3, 4} +} ``` \ No newline at end of file