1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 01:02:28 +08:00

format doc

This commit is contained in:
dudaodong
2023-08-02 11:29:51 +08:00
parent 40cad365c0
commit 7079b1f704
2 changed files with 212 additions and 98 deletions

View File

@@ -1,14 +1,17 @@
# CopyOnWriteList # CopyOnWriteList
CopyOnWriteList is a thread-safe list implementation that uses go slicing as its base. CopyOnWriteList is a thread-safe list implementation that uses go slicing as its base.
. When writing, a new slice is copied and assigned to the original slice when writing is complete.When reading, the original slice is read directly. . When writing, a new slice is copied and assigned to the original slice when writing is complete.When reading, the original slice is read directly.
## 源码 ## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go](https://github.com/duke-git/lancet/blob/main /datastructure/list/copyonwritelist.go)
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go](https://github.com/duke-git/lancet/blob/main /datastructure/list/copyonwritelist.go)
## 用法 ## 用法
```go ```go
import ( import (
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
``` ```
@@ -16,30 +19,32 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [NewCopyOnWriteList](#NewCopyOnWriteList)
- [Size](#Size)
- [Get](#Get)
- [Set](#Set)
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
- [Add](#Add)
- [AddAll](#AddAll)
- [AddByIndex](#AddByIndex)
- [DeleteAt](#DeleteAt)
- [DeleteIf](#DeleteIf)
- [DeleteBy](#DeleteBy)
- [DeleteRange](#DeleteRange)
- [Equal](#Equal)
- [NewCopyOnWriteList](#NewCopyOnWriteList)
- [Size](#Size)
- [Get](#Get)
- [Set](#Set)
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
- [Add](#Add)
- [AddAll](#AddAll)
- [AddByIndex](#AddByIndex)
- [DeleteAt](#DeleteAt)
- [DeleteIf](#DeleteIf)
- [DeleteBy](#DeleteBy)
- [DeleteRange](#DeleteRange)
- [Equal](#Equal)
## Documentation ## Documentation
### NewCopyOnWriteList ### NewCopyOnWriteList
Returns a CopyOnWriteList with empty slices. Returns a CopyOnWriteList with empty slices.
```go ```go
type CopyOnWriteList[T any] struct { type CopyOnWriteList[T any] struct {
data []T data []T
@@ -49,7 +54,9 @@ type CopyOnWriteList[T any] struct {
func NewCopyOnWriteList() *CopyOnWriteList func NewCopyOnWriteList() *CopyOnWriteList
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -62,14 +69,18 @@ func main() {
l := list.NewCopyOnWriteList([]int{1,2,3}) l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l) fmt.Println(l)
} }
```
fmt.Println(l) }
### Size ### Size
Returns the length of the CopyOnWriteList. Returns the length of the CopyOnWriteList.
```go ```go
func (l *CopyOnWriteList[T]) Size() int func (l *CopyOnWriteList[T]) Size() int
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -86,57 +97,67 @@ func main() {
``` ```
### Get ### Get
Returns the element at the specified position in the list Returns the element at the specified position in the list
```go ```go
func (c *CopyOnWriteList[T]) Get(index int) *T func (c *CopyOnWriteList[T]) Get(index int) *T
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{1,2,3}) l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Get(2)) fmt.Println(l.Get(2))
} }
``` ```
### Set ### Set
Replaces the element at the specified position in this list with the specified element. Replaces the element at the specified position in this list with the specified element.
```go ```go
func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool) func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool)
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{1,2,3}) l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Set(2, 4)) fmt.Println(l.Set(2, 4))
} }
``` ```
### Remove ### Remove
### IndexOf ### IndexOf
Returns the index of the value in the list, or -1 if not found. Returns the index of the value in the list, or -1 if not found.
```go ```go
func (c *CopyOnWriteList[T]) IndexOf(e T) int func (c *CopyOnWriteList[T]) IndexOf(e T) int
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -153,6 +174,7 @@ func main() {
``` ```
### LastIndexOf ### LastIndexOf
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain that element. Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain that element.
```go ```go
@@ -160,28 +182,32 @@ func (c *CopyOnWriteList[T]) LastIndexOf(e T) int
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,1}) l := list.NewCopyOnWriteList([]int{1,2,3,1})
fmt.Println(l.LastIndexOf(1)) fmt.Println(l.LastIndexOf(1))
} }
``` ```
### IsEmpty ### IsEmpty
Returns true if this list does not contain any elements. Returns true if this list does not contain any elements.
```go ```go
func (c *CopyOnWriteList[T]) IsEmpty() bool func (c *CopyOnWriteList[T]) IsEmpty() bool
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -191,19 +217,21 @@ import (
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{}) l := list.NewCopyOnWriteList([]int{})
fmt.Println(l.IsEmpty()) fmt.Println(l.IsEmpty())
} }
``` ```
### Contain ### Contain
Determines if a CopyOnWriteList contains an element. Determines if a CopyOnWriteList contains an element.
```go ```go
func (c *CopyOnWriteList[T]) Contain(e T) bool func (c *CopyOnWriteList[T]) Contain(e T) bool
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -218,14 +246,16 @@ fmt.Println(l.Contain(1))
} }
``` ```
### ValueOf ### ValueOf
Returns a pointer to the value at the index in the list Returns a pointer to the value at the index in the list
```go ```go
func (c *CopyOnWriteList[T]) ValueOf(index int) []T func (c *CopyOnWriteList[T]) ValueOf(index int) []T
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -242,12 +272,15 @@ func main() {
``` ```
### Add ### Add
Appends the specified element to the end of the list. Appends the specified element to the end of the list.
```go ```go
func (c *CopyOnWriteList[T]) Add(e T) bool func (c *CopyOnWriteList[T]) Add(e T) bool
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -266,12 +299,15 @@ func main() {
``` ```
### AddAll ### AddAll
Appends all the elements of the specified collection to the end of this list Appends all the elements of the specified collection to the end of this list
```go ```go
func (c *CopyOnWriteList[T]) AddAll(e []T) bool func (c *CopyOnWriteList[T]) AddAll(e []T) bool
``` ```
#### Example #### Example
```go ```go
package main package main
@@ -289,52 +325,63 @@ func main() {
``` ```
### AddByIndex ### AddByIndex
Inserts the specified element into the list at the specified position. Inserts the specified element into the list at the specified position.
```go ```go
func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{1,2,3}) l := list.NewCopyOnWriteList([]int{1,2,3})
list.AddByIndex(2, 6) list.AddByIndex(2, 6)
fmt.Println(l.getList()) fmt.Println(l.getList())
} }
``` ```
### DeleteAt ### DeleteAt
Removes the element at the specified position in this list. Removes the element at the specified position in this list.
```go ```go
func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool) func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool)
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{1,2,3}) l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteAt(2) list.DeleteAt(2)
fmt.Println(l.getList()) fmt.Println(l.getList())
} }
``` ```
### DeleteIf ### DeleteIf
Removes the first occurrence of the specified element from this list (if it exists). Removes the first occurrence of the specified element from this list (if it exists).
```go ```go
func (c *CopyOnWriteList[T]) DeleteIf(func(T) bool) (oldValue *T, ok bool) func (c *CopyOnWriteList[T]) DeleteIf(func(T) bool) (oldValue *T, ok bool)
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
@@ -352,11 +399,15 @@ func main() {
``` ```
### DeleteBy ### DeleteBy
Deletes the first occurrence of the specified element from this list (if it exists). Deletes the first occurrence of the specified element from this list (if it exists).
```## go
```go
func (c *CopyOnWriteList[T]) DeleteBy(e T) (*T bool) func (c *CopyOnWriteList[T]) DeleteBy(e T) (*T bool)
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
@@ -371,15 +422,17 @@ func main() {
} }
``` ```
### DeleteRange ### DeleteRange
Deletes all elements from this list with indexes between fromIndex (included) and toIndex (not included). Deletes all elements from this list with indexes between fromIndex (included) and toIndex (not included).
(leftCloseRightOpen) (leftCloseRightOpen)
```go ```go
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int) func (c *CopyOnWriteList[T]) DeleteRange(start int, end int)
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (
@@ -388,19 +441,22 @@ import (
) )
func main() { func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9}) l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
list.DeleteRange(2, 5) list.DeleteRange(2, 5)
fmt.Println(l.getList()) fmt.Println(l.getList())
} }
``` ```
### Equal ### Equal
Returns true if the specified object is equal to this list Returns true if the specified object is equal to this list
```go ```go
func (c *CopyOnWriteList[T]) Equal(e []T) bool func (c *CopyOnWriteList[T]) Equal(e []T) bool
``` ```
#### Example #### Example
```go ```go
package main package main
import ( import (

View File

@@ -1,15 +1,18 @@
# CopyOnWriteList # CopyOnWriteList
CopyOnWriteList 是一个线程安全的List实现底层使用go 切片
CopyOnWriteList 是一个线程安全的 List 实现,底层使用 go 切片
.在写入时,会复制一份新的切片,写入完成后,再将新的切片赋值给原来的切片. .在写入时,会复制一份新的切片,写入完成后,再将新的切片赋值给原来的切片.
在读取时,直接读取原来的切片 在读取时,直接读取原来的切片
## 源码 ## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go](https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go)
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go](https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go)
## 用法 ## 用法
```go ```go
import ( import (
"github.com/duke-git/lancet/datastructure/list" "github.com/duke-git/lancet/datastructure/list"
) )
``` ```
@@ -17,30 +20,32 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [NewCopyOnWriteList](#NewCopyOnWriteList)
- [Size](#Size)
- [Get](#Get)
- [Set](#Set)
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
- [Add](#Add)
- [AddAll](#AddAll)
- [AddByIndex](#AddByIndex)
- [DeleteAt](#DeleteAt)
- [DeleteIf](#DeleteIf)
- [DeleteBy](#DeleteBy)
- [DeleteRange](#DeleteRange)
- [Equal](#Equal)
- [NewCopyOnWriteList](#NewCopyOnWriteList)
- [Size](#Size)
- [Get](#Get)
- [Set](#Set)
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
- [Add](#Add)
- [AddAll](#AddAll)
- [AddByIndex](#AddByIndex)
- [DeleteAt](#DeleteAt)
- [DeleteIf](#DeleteIf)
- [DeleteBy](#DeleteBy)
- [DeleteRange](#DeleteRange)
- [Equal](#Equal)
## 文档 ## 文档
### NewCopyOnWriteList ### NewCopyOnWriteList
返回一个具有空切片的CopyOnWriteList
返回一个具有空切片的 CopyOnWriteList。
```go ```go
type CopyOnWriteList[T any] struct { type CopyOnWriteList[T any] struct {
data []T data []T
@@ -50,7 +55,9 @@ type CopyOnWriteList[T any] struct {
func NewCopyOnWriteList() *CopyOnWriteList func NewCopyOnWriteList() *CopyOnWriteList
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -65,12 +72,17 @@ func main() {
} }
``` ```
### Size ### Size
返回CopyOnWriteList的长度
返回 CopyOnWriteList 的长度。
```go ```go
func (l *CopyOnWriteList[T]) Size() int func (l *CopyOnWriteList[T]) Size() int
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -87,12 +99,15 @@ func main() {
``` ```
### Get ### Get
返回列表中指定位置的元素 返回列表中指定位置的元素
```go ```go
func (c *CopyOnWriteList[T]) Get(index int) *T func (c *CopyOnWriteList[T]) Get(index int) *T
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -108,14 +123,16 @@ func main() {
``` ```
### Set ### Set
将此列表中指定位置的元素替换为指定元素。 将此列表中指定位置的元素替换为指定元素。
```go ```go
func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool) func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool)
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -130,14 +147,19 @@ func main() {
} }
``` ```
### Remove ### Remove
### IndexOf ### IndexOf
返回列表中值的索引,如果没有找到返回-1
返回列表中值的索引,如果没有找到返回-1。
```go ```go
func (c *CopyOnWriteList[T]) IndexOf(e T) int func (c *CopyOnWriteList[T]) IndexOf(e T) int
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -154,13 +176,15 @@ func main() {
``` ```
### LastIndexOf ### LastIndexOf
返回指定元素在此列表中最后出现的索引,如果此列表不包含该元素,则返回-1
返回指定元素在此列表中最后出现的索引,如果此列表不包含该元素,则返回-1。
```go ```go
func (c *CopyOnWriteList[T]) LastIndexOf(e T) int func (c *CopyOnWriteList[T]) LastIndexOf(e T) int
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -177,12 +201,15 @@ func main() {
``` ```
### IsEmpty ### IsEmpty
如果此列表不包含任何元素则返回true。
如果此列表不包含任何元素,则返回 true。
```go ```go
func (c *CopyOnWriteList[T]) IsEmpty() bool func (c *CopyOnWriteList[T]) IsEmpty() bool
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -197,14 +224,16 @@ func main() {
} }
``` ```
### Contain ### Contain
判断CopyOnWriteList是否包含某个元素
判断 CopyOnWriteList 是否包含某个元素
```go ```go
func (c *CopyOnWriteList[T]) Contain(e T) bool func (c *CopyOnWriteList[T]) Contain(e T) bool
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -219,14 +248,16 @@ func main() {
} }
``` ```
### ValueOf ### ValueOf
返回列表中索引处的值指针 返回列表中索引处的值指针
```go ```go
func (c *CopyOnWriteList[T]) ValueOf(index int) []T func (c *CopyOnWriteList[T]) ValueOf(index int) []T
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -243,12 +274,15 @@ func main() {
``` ```
### Add ### Add
将指定的元素追加到此列表的末尾。 将指定的元素追加到此列表的末尾。
```go ```go
func (c *CopyOnWriteList[T]) Add(e T) bool func (c *CopyOnWriteList[T]) Add(e T) bool
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -267,12 +301,15 @@ func main() {
``` ```
### AddAll ### AddAll
将指定集合中的所有元素追加到此列表的末尾 将指定集合中的所有元素追加到此列表的末尾
```go ```go
func (c *CopyOnWriteList[T]) AddAll(e []T) bool func (c *CopyOnWriteList[T]) AddAll(e []T) bool
``` ```
#### 示例 #### 示例
```go ```go
package main package main
@@ -290,12 +327,15 @@ func main() {
``` ```
### AddByIndex ### AddByIndex
将指定元素插入此列表中的指定位置
将指定元素插入此列表中的指定位置。
```go ```go
func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool
``` ```
#### 示例 #### 示例
```go ```go
package main package main
import ( import (
@@ -311,11 +351,15 @@ func main() {
``` ```
### DeleteAt ### DeleteAt
移除此列表中指定位置的元素。 移除此列表中指定位置的元素。
```go ```go
func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool) func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool)
``` ```
#### 示例 #### 示例
```go ```go
package main package main
import ( import (
@@ -331,11 +375,15 @@ func main() {
``` ```
### DeleteIf ### DeleteIf
从此列表中删除第一个出现的指定元素(如果该元素存在)。 从此列表中删除第一个出现的指定元素(如果该元素存在)。
```go ```go
func (c *CopyOnWriteList[T]) DeleteIf(f func(T) bool) (oldValue *T, ok bool) func (c *CopyOnWriteList[T]) DeleteIf(f func(T) bool) (oldValue *T, ok bool)
``` ```
#### 示例 #### 示例
```go ```go
package main package main
import ( import (
@@ -353,11 +401,15 @@ func main() {
``` ```
### DeleteBy ### DeleteBy
从此列表中删除第一个出现的指定元素(如果该元素存在)。 从此列表中删除第一个出现的指定元素(如果该元素存在)。
```go ```go
func (c *CopyOnWriteList[T]) DeleteBy(e T) (*T bool) func (c *CopyOnWriteList[T]) DeleteBy(e T) (*T bool)
``` ```
#### 示例 #### 示例
```go ```go
package main package main
import ( import (
@@ -372,14 +424,17 @@ func main() {
} }
``` ```
### DeleteRange ### DeleteRange
从该列表中删除索引介于fromIndex(包含)和toIndex(不包含)之间的所有元素。
(左闭右开) 从该列表中删除索引介于 fromIndex(包含)和 toIndex(不包含)之间的所有元素。
(左闭右开)。
```go ```go
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int) func (c *CopyOnWriteList[T]) DeleteRange(start int, end int)
``` ```
#### 示例 #### 示例
```go ```go
package main package main
import ( import (
@@ -395,12 +450,15 @@ func main() {
``` ```
### Equal ### Equal
如果指定的对象等于此列表则返回true
如果指定的对象等于此列表,则返回 true。
```go ```go
func (c *CopyOnWriteList[T]) Equal(e []T) bool func (c *CopyOnWriteList[T]) Equal(e []T) bool
``` ```
#### 示例 #### 示例
```go ```go
package main package main
import ( import (