diff --git a/docs/datastructure/set.md b/docs/datastructure/set.md index edca04c..e19db38 100644 --- a/docs/datastructure/set.md +++ b/docs/datastructure/set.md @@ -24,6 +24,8 @@ import ( - [NewSet](#NewSet) - [Values](#Values) - [Add](#Add) +- [AddIfNotExist](#AddIfNotExist) +- [AddIfNotExistBy](#AddIfNotExistBy) - [Delete](#Delete) - [Contain](#Contain) - [ContainAll](#ContainAll) @@ -50,7 +52,7 @@ import ( ```go type Set[T comparable] map[T]bool -func NewSet[T comparable](values ...T) Set[T] +func NewSet[T comparable](items ...T) Set[T] ``` Example: @@ -99,12 +101,12 @@ func main() { ### Add -
Add value to set
+Add items to set
Signature: ```go -func (s Set[T]) Add(values ...T) +func (s Set[T]) Add(items ...T) ``` Example: @@ -125,14 +127,83 @@ func main() { ``` - -### Delete -Delete value in set
+### AddIfNotExist +AddIfNotExist checks if item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.
Signature: ```go -func (s Set[T]) Delete(values ...T) +func (s Set[T]) AddIfNotExist(item T) bool +``` +Example: + +```go +package main + +import ( + "fmt" + set "github.com/duke-git/lancet/v2/datastructure/set" +) + +func main() { + st := set.NewSet[int]() + st.Add(1, 2, 3) + + r1 := st.AddIfNotExist(1) + r2 := st.AddIfNotExist(4) + + fmt.Println(r1) // false + fmt.Println(r2) // true + fmt.Println(st.Values()) // 1,2,3,4 +} +``` + + +### AddIfNotExistBy +AddIfNotExistBy checks if item exists in the set and pass the `checker` function it adds the item to set and returns true if it does not exists in the set and function `checker` returns true, or else it does nothing and returns false.
+ +Signature: + +```go +func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool +``` +Example: + +```go +package main + +import ( + "fmt" + set "github.com/duke-git/lancet/v2/datastructure/set" +) + +func main() { + st := set.NewSet[int]() + st.Add(1, 2) + + ok := st.AddIfNotExistBy(3, func(val int) bool { + return val%2 != 0 + }) + fmt.Println(ok) // true + + + notOk := st.AddIfNotExistBy(4, func(val int) bool { + return val%2 != 0 + }) + fmt.Println(notOk) // false + + fmt.Println(st.Values()) // 1, 2, 3 +} +``` + + +### Delete +Delete item in set
+ +Signature: + +```go +func (s Set[T]) Delete(items ...T) ``` Example: @@ -156,12 +227,12 @@ func main() { ### Contain -Check if value is in set or not
+Check if item is in set or not
Signature: ```go -func (s Set[T]) Contain(value T) bool +func (s Set[T]) Contain(item T) bool ``` Example: @@ -308,7 +379,7 @@ func main() { Signature: ```go -func (s Set[T]) Iterate(fn func(value T)) +func (s Set[T]) Iterate(fn func(item T)) ``` Example: @@ -323,8 +394,8 @@ import ( func main() { set1 := set.NewSet(1, 2, 3) arr := []int{} - set.Iterate(func(value int) { - arr = append(arr, value) + set.Iterate(func(item int) { + arr = append(arr, item) }) fmt.Println(arr) //1,2,3 diff --git a/docs/datastructure/set_zh-CN.md b/docs/datastructure/set_zh-CN.md index 068ab7c..86d5e40 100644 --- a/docs/datastructure/set_zh-CN.md +++ b/docs/datastructure/set_zh-CN.md @@ -24,6 +24,8 @@ import ( - [NewSet](#NewSet) - [Values](#Values) - [Add](#Add) +- [AddIfNotExist](#AddIfNotExist) +- [AddIfNotExistBy](#AddIfNotExistBy) - [Delete](#Delete) - [Contain](#Contain) - [ContainAll](#ContainAll) @@ -50,7 +52,7 @@ import ( ```go type Set[T comparable] map[T]bool -func NewSet[T comparable](values ...T) Set[T] +func NewSet[T comparable](items ...T) Set[T] ``` 例子: @@ -104,7 +106,7 @@ func main() { 函数签名: ```go -func (s Set[T]) Add(values ...T) +func (s Set[T]) Add(items ...T) ``` 例子: @@ -125,6 +127,76 @@ func main() { ``` +### AddIfNotExist +如果集合中不存在元素,则添加该元素返回true, 如果集合中存在元素, 不做任何操作,返回false
+ +函数签名: + +```go +func (s Set[T]) AddIfNotExist(item T) bool +``` +例子: + +```go +package main + +import ( + "fmt" + set "github.com/duke-git/lancet/v2/datastructure/set" +) + +func main() { + st := set.NewSet[int]() + st.Add(1, 2, 3) + + r1 := st.AddIfNotExist(1) + r2 := st.AddIfNotExist(4) + + fmt.Println(r1) // false + fmt.Println(r2) // true + fmt.Println(st.Values()) // 1,2,3,4 +} +``` + + +### AddIfNotExistBy +根据checker函数判断元素是否在集合中,如果集合中不存在元素且checker返回true,则添加该元素返回true, 否则不做任何操作,返回false
+ +函数签名: + +```go +func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool +``` +例子: + +```go +package main + +import ( + "fmt" + set "github.com/duke-git/lancet/v2/datastructure/set" +) + +func main() { + st := set.NewSet[int]() + st.Add(1, 2) + + ok := st.AddIfNotExistBy(3, func(val int) bool { + return val%2 != 0 + }) + fmt.Println(ok) // true + + + notOk := st.AddIfNotExistBy(4, func(val int) bool { + return val%2 != 0 + }) + fmt.Println(notOk) // false + + fmt.Println(st.Values()) // 1, 2, 3 +} +``` + + ### Delete删除集合中元素
@@ -132,7 +204,7 @@ func main() { 函数签名: ```go -func (s Set[T]) Delete(values ...T) +func (s Set[T]) Delete(items ...T) ``` 例子: @@ -161,7 +233,7 @@ func main() { 函数签名: ```go -func (s Set[T]) Contain(value T) bool +func (s Set[T]) Contain(item T) bool ``` 例子: @@ -308,7 +380,7 @@ func main() { 函数签名: ```go -func (s Set[T]) Iterate(fn func(value T)) +func (s Set[T]) Iterate(fn func(item T)) ``` 例子: @@ -323,8 +395,8 @@ import ( func main() { set1 := set.NewSet(1, 2, 3) arr := []int{} - set.Iterate(func(value int) { - arr = append(arr, value) + set.Iterate(func(item int) { + arr = append(arr, item) }) fmt.Println(arr) //1,2,3 @@ -419,9 +491,6 @@ func main() { ``` - - - ### SymmetricDifference返回一个集合,其中元素在第一个集合或第二个集合中,且不同时存在于两个集合中