mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 22:52:29 +08:00
format doc
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
# CopyOnWriteList
|
||||
|
||||
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.
|
||||
|
||||
## 源码
|
||||
- [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
|
||||
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>
|
||||
|
||||
## 目录
|
||||
- [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
|
||||
|
||||
### NewCopyOnWriteList
|
||||
|
||||
Returns a CopyOnWriteList with empty slices.
|
||||
|
||||
```go
|
||||
type CopyOnWriteList[T any] struct {
|
||||
data []T
|
||||
@@ -49,7 +54,9 @@ type CopyOnWriteList[T any] struct {
|
||||
func NewCopyOnWriteList() *CopyOnWriteList
|
||||
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -62,14 +69,18 @@ func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
fmt.Println(l)
|
||||
}
|
||||
```
|
||||
|
||||
### Size
|
||||
|
||||
fmt.Println(l) }
|
||||
### Size
|
||||
Returns the length of the CopyOnWriteList.
|
||||
|
||||
```go
|
||||
func (l *CopyOnWriteList[T]) Size() int
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -86,57 +97,67 @@ func main() {
|
||||
```
|
||||
|
||||
### Get
|
||||
|
||||
Returns the element at the specified position in the list
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) Get(index int) *T
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
fmt.Println(l.Get(2))
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
fmt.Println(l.Get(2))
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Set
|
||||
|
||||
Replaces the element at the specified position in this list with the specified element.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool)
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
fmt.Println(l.Set(2, 4))
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
fmt.Println(l.Set(2, 4))
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Remove
|
||||
|
||||
### IndexOf
|
||||
|
||||
Returns the index of the value in the list, or -1 if not found.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) IndexOf(e T) int
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -153,6 +174,7 @@ func main() {
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
```go
|
||||
@@ -160,28 +182,32 @@ func (c *CopyOnWriteList[T]) LastIndexOf(e T) int
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3,1})
|
||||
fmt.Println(l.LastIndexOf(1))
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3,1})
|
||||
fmt.Println(l.LastIndexOf(1))
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### IsEmpty
|
||||
|
||||
Returns true if this list does not contain any elements.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) IsEmpty() bool
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -191,19 +217,21 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{})
|
||||
fmt.Println(l.IsEmpty())
|
||||
l := list.NewCopyOnWriteList([]int{})
|
||||
fmt.Println(l.IsEmpty())
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Contain
|
||||
|
||||
Determines if a CopyOnWriteList contains an element.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) Contain(e T) bool
|
||||
func (c *CopyOnWriteList[T]) Contain(e T) bool
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -218,14 +246,16 @@ fmt.Println(l.Contain(1))
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### ValueOf
|
||||
|
||||
Returns a pointer to the value at the index in the list
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) ValueOf(index int) []T
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -242,12 +272,15 @@ func main() {
|
||||
```
|
||||
|
||||
### Add
|
||||
|
||||
Appends the specified element to the end of the list.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) Add(e T) bool
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
|
||||
package main
|
||||
@@ -266,12 +299,15 @@ func main() {
|
||||
```
|
||||
|
||||
### AddAll
|
||||
|
||||
Appends all the elements of the specified collection to the end of this list
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) AddAll(e []T) bool
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -289,52 +325,63 @@ func main() {
|
||||
```
|
||||
|
||||
### AddByIndex
|
||||
|
||||
Inserts the specified element into the list at the specified position.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
)
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
list.AddByIndex(2, 6)
|
||||
fmt.Println(l.getList())
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
list.AddByIndex(2, 6)
|
||||
fmt.Println(l.getList())
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### DeleteAt
|
||||
|
||||
Removes the element at the specified position in this list.
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool)
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/datastructure/list"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
list.DeleteAt(2)
|
||||
fmt.Println(l.getList())
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3})
|
||||
list.DeleteAt(2)
|
||||
fmt.Println(l.getList())
|
||||
}
|
||||
```
|
||||
|
||||
### DeleteIf
|
||||
|
||||
Removes the first occurrence of the specified element from this list (if it exists).
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) DeleteIf(func(T) bool) (oldValue *T, ok bool)
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
@@ -352,11 +399,15 @@ func main() {
|
||||
```
|
||||
|
||||
### DeleteBy
|
||||
|
||||
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)
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
@@ -371,15 +422,17 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### DeleteRange
|
||||
|
||||
Deletes all elements from this list with indexes between fromIndex (included) and toIndex (not included).
|
||||
(leftCloseRightOpen)
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int)
|
||||
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int)
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
@@ -388,19 +441,22 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
|
||||
list.DeleteRange(2, 5)
|
||||
fmt.Println(l.getList())
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
|
||||
list.DeleteRange(2, 5)
|
||||
fmt.Println(l.getList())
|
||||
}
|
||||
```
|
||||
|
||||
### Equal
|
||||
|
||||
Returns true if the specified object is equal to this list
|
||||
|
||||
```go
|
||||
func (c *CopyOnWriteList[T]) Equal(e []T) bool
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
@@ -412,4 +468,4 @@ func main() {
|
||||
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
|
||||
fmt.Println(l.Equal([]int{1,2,3,4,5,6,7,8,9}))
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user