diff --git a/datastructure/set/set.go b/datastructure/set/set.go index 186d905..3cf498f 100644 --- a/datastructure/set/set.go +++ b/datastructure/set/set.go @@ -7,15 +7,15 @@ package datastructure // Set is a data container, like slice, but element of set is not duplicate. type Set[T comparable] map[T]struct{} -// NewSet return a instance of set -func NewSet[T comparable](items ...T) Set[T] { +// New create a instance of set from given values. +func New[T comparable](items ...T) Set[T] { set := make(Set[T]) set.Add(items...) return set } -// NewSetFromSlice create a set from slice -func NewSetFromSlice[T comparable](items []T) Set[T] { +// FromSlice create a set from given slice. +func FromSlice[T comparable](items []T) Set[T] { set := make(Set[T]) for _, item := range items { set.Add(item) @@ -77,7 +77,7 @@ func (s Set[T]) ContainAll(other Set[T]) bool { // Clone return a copy of set func (s Set[T]) Clone() Set[T] { - set := NewSet[T]() + set := New[T]() set.Add(s.Values()...) return set } @@ -135,7 +135,7 @@ func (s Set[T]) Union(other Set[T]) Set[T] { // Intersection creates a new set whose element both be contained in set s and other func (s Set[T]) Intersection(other Set[T]) Set[T] { - set := NewSet[T]() + set := New[T]() s.Iterate(func(value T) { if other.Contain(value) { set.Add(value) @@ -147,7 +147,7 @@ func (s Set[T]) Intersection(other Set[T]) Set[T] { // SymmetricDifference creates a new set whose element is in set1 or set2, but not in both sets func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] { - set := NewSet[T]() + set := New[T]() s.Iterate(func(value T) { if !other.Contain(value) { set.Add(value) @@ -165,7 +165,7 @@ func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] { // Minus creates an set of whose element in origin set but not in compared set func (s Set[T]) Minus(comparedSet Set[T]) Set[T] { - set := NewSet[T]() + set := New[T]() s.Iterate(func(value T) { if !comparedSet.Contain(value) { diff --git a/datastructure/set/set_test.go b/datastructure/set/set_test.go index 6556ad8..8112584 100644 --- a/datastructure/set/set_test.go +++ b/datastructure/set/set_test.go @@ -6,18 +6,18 @@ import ( "github.com/duke-git/lancet/v2/internal" ) -func TestSet_NewSetFromSlice(t *testing.T) { +func TestSet_FromSlice(t *testing.T) { t.Parallel() - assert := internal.NewAssert(t, "TestSet_NewSetFromSlice") + assert := internal.NewAssert(t, "TestSet_FromSlice") - s1 := NewSetFromSlice([]int{1, 2, 2, 3}) + s1 := FromSlice([]int{1, 2, 2, 3}) assert.Equal(3, s1.Size()) assert.Equal(true, s1.Contain(1)) assert.Equal(true, s1.Contain(2)) assert.Equal(true, s1.Contain(3)) - s2 := NewSetFromSlice([]int{}) + s2 := FromSlice([]int{}) assert.Equal(0, s2.Size()) } @@ -26,10 +26,10 @@ func TestSet_Add(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Add") - set := NewSet[int]() + set := New[int]() set.Add(1, 2, 3) - cmpSet := NewSet(1, 2, 3) + cmpSet := New(1, 2, 3) assert.Equal(true, set.Equal(cmpSet)) } @@ -39,12 +39,12 @@ func TestSet_AddIfNotExist(t *testing.T) { assert := internal.NewAssert(t, "TestSet_AddIfNotExist") - set := NewSet[int]() + set := New[int]() set.Add(1, 2, 3) assert.Equal(false, set.AddIfNotExist(1)) assert.Equal(true, set.AddIfNotExist(4)) - assert.Equal(NewSet(1, 2, 3, 4), set) + assert.Equal(New(1, 2, 3, 4), set) } func TestSet_AddIfNotExistBy(t *testing.T) { @@ -52,7 +52,7 @@ func TestSet_AddIfNotExistBy(t *testing.T) { assert := internal.NewAssert(t, "TestSet_AddIfNotExistBy") - set := NewSet[int]() + set := New[int]() set.Add(1, 2) ok := set.AddIfNotExistBy(3, func(val int) bool { @@ -75,7 +75,7 @@ func TestSet_Contain(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Contain") - set := NewSet[int]() + set := New[int]() set.Add(1, 2, 3) assert.Equal(true, set.Contain(1)) @@ -87,9 +87,9 @@ func TestSet_ContainAll(t *testing.T) { assert := internal.NewAssert(t, "TestSet_ContainAll") - set1 := NewSet(1, 2, 3) - set2 := NewSet(1, 2) - set3 := NewSet(1, 2, 3, 4) + set1 := New(1, 2, 3) + set2 := New(1, 2) + set3 := New(1, 2, 3, 4) assert.Equal(true, set1.ContainAll(set2)) assert.Equal(false, set1.ContainAll(set3)) @@ -100,7 +100,7 @@ func TestSet_Clone(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Clone") - set1 := NewSet(1, 2, 3) + set1 := New(1, 2, 3) set2 := set1.Clone() assert.Equal(true, set1.Size() == set2.Size()) @@ -112,11 +112,11 @@ func TestSet_Delete(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Delete") - set := NewSet[int]() + set := New[int]() set.Add(1, 2, 3) set.Delete(3) - assert.Equal(true, set.Equal(NewSet(1, 2))) + assert.Equal(true, set.Equal(New(1, 2))) } func TestSet_Equal(t *testing.T) { @@ -124,9 +124,9 @@ func TestSet_Equal(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Equal") - set1 := NewSet(1, 2, 3) - set2 := NewSet(1, 2, 3) - set3 := NewSet(1, 2, 3, 4) + set1 := New(1, 2, 3) + set2 := New(1, 2, 3) + set3 := New(1, 2, 3, 4) assert.Equal(true, set1.Equal(set2)) assert.Equal(false, set1.Equal(set3)) @@ -137,7 +137,7 @@ func TestSet_Iterate(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Iterate") - set := NewSet(1, 2, 3) + set := New(1, 2, 3) arr := []int{} set.Iterate(func(value int) { arr = append(arr, value) @@ -151,7 +151,7 @@ func TestSet_IsEmpty(t *testing.T) { assert := internal.NewAssert(t, "TestSet_IsEmpty") - set := NewSet[int]() + set := New[int]() assert.Equal(true, set.IsEmpty()) } @@ -160,7 +160,7 @@ func TestSet_Size(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Size") - set := NewSet(1, 2, 3) + set := New(1, 2, 3) assert.Equal(3, set.Size()) } @@ -169,7 +169,7 @@ func TestSet_Values(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Values") - set := NewSet(1, 2, 3) + set := New(1, 2, 3) values := set.Values() assert.Equal(3, len(values)) @@ -180,12 +180,12 @@ func TestSet_Union(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Union") - set1 := NewSet(1, 2, 3) - set2 := NewSet(2, 3, 4, 5) + set1 := New(1, 2, 3) + set2 := New(2, 3, 4, 5) unionSet := set1.Union(set2) - assert.Equal(NewSet(1, 2, 3, 4, 5), unionSet) + assert.Equal(New(1, 2, 3, 4, 5), unionSet) } func TestSet_Intersection(t *testing.T) { @@ -193,11 +193,11 @@ func TestSet_Intersection(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Intersection") - set1 := NewSet(1, 2, 3) - set2 := NewSet(2, 3, 4, 5) + set1 := New(1, 2, 3) + set2 := New(2, 3, 4, 5) intersectionSet := set1.Intersection(set2) - assert.Equal(NewSet(2, 3), intersectionSet) + assert.Equal(New(2, 3), intersectionSet) } func TestSet_SymmetricDifference(t *testing.T) { @@ -205,10 +205,10 @@ func TestSet_SymmetricDifference(t *testing.T) { assert := internal.NewAssert(t, "TestSet_SymmetricDifference") - set1 := NewSet(1, 2, 3) - set2 := NewSet(2, 3, 4, 5) + set1 := New(1, 2, 3) + set2 := New(2, 3, 4, 5) - assert.Equal(NewSet(1, 4, 5), set1.SymmetricDifference(set2)) + assert.Equal(New(1, 4, 5), set1.SymmetricDifference(set2)) } func TestSet_Minus(t *testing.T) { @@ -216,16 +216,16 @@ func TestSet_Minus(t *testing.T) { assert := internal.NewAssert(t, "TestSet_Minus") - set1 := NewSet(1, 2, 3) - set2 := NewSet(2, 3, 4, 5) - set3 := NewSet(2, 3) + set1 := New(1, 2, 3) + set2 := New(2, 3, 4, 5) + set3 := New(2, 3) - assert.Equal(NewSet(1), set1.Minus(set2)) - assert.Equal(NewSet(4, 5), set2.Minus(set3)) + assert.Equal(New(1), set1.Minus(set2)) + assert.Equal(New(4, 5), set2.Minus(set3)) } func TestEachWithBreak(t *testing.T) { - // s := NewSet(1, 2, 3, 4, 5) + // s := New(1, 2, 3, 4, 5) // var sum int @@ -244,7 +244,7 @@ func TestEachWithBreak(t *testing.T) { // func TestPop(t *testing.T) { // assert := internal.NewAssert(t, "TestPop") -// s := NewSet[int]() +// s := New[int]() // val, ok := s.Pop() // assert.Equal(0, val) @@ -254,7 +254,7 @@ func TestEachWithBreak(t *testing.T) { // s.Add(2) // s.Add(3) -// // s = NewSet(1, 2, 3, 4, 5) +// // s = New(1, 2, 3, 4, 5) // val, ok = s.Pop() // assert.Equal(3, val) diff --git a/docs/api/packages/datastructure/set.md b/docs/api/packages/datastructure/set.md index f978ebf..f80c38a 100644 --- a/docs/api/packages/datastructure/set.md +++ b/docs/api/packages/datastructure/set.md @@ -1,6 +1,6 @@ # Set -Set 集合数据结构,类似列表。Set 中元素不重复。 +集合数据结构,类似列表。Set中元素不重复。
@@ -22,8 +22,8 @@ import ( ## 目录 -- [NewSet](#NewSet) -- [NewSetFromSlice](#NewSetFromSlice) +- [New](#New) +- [FromSlice](#FromSlice) - [Values](#Values) - [Add](#Add) - [AddIfNotExist](#AddIfNotExist) @@ -45,7 +45,7 @@ import ( ## 文档 -### NewSet +### New

返回Set结构体对象

@@ -53,7 +53,7 @@ import ( ```go type Set[T comparable] map[T]bool -func NewSet[T comparable](items ...T) Set[T] +func New[T comparable](items ...T) Set[T] ``` 示例: @@ -67,19 +67,19 @@ import ( ) func main() { - st := set.NewSet[int](1,2,2,3) + st := set.New[int](1,2,2,3) fmt.Println(st.Values()) //1,2,3 } ``` -### NewSetFromSlice +### FromSlice

基于切片创建集合

函数签名: ```go -func NewSetFromSlice[T comparable](items []T) Set[T] +func FromSlice[T comparable](items []T) Set[T] ``` 示例: @@ -93,7 +93,7 @@ import ( ) func main() { - st := set.NewSetFromSlice([]int{1, 2, 2, 3}) + st := set.FromSlice([]int{1, 2, 2, 3}) fmt.Println(st.Values()) //1,2,3 } ``` @@ -119,7 +119,7 @@ import ( ) func main() { - st := set.NewSet[int](1,2,2,3) + st := set.New[int](1,2,2,3) fmt.Println(st.Values()) //1,2,3 } ``` @@ -145,7 +145,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) fmt.Println(st.Values()) //1,2,3 @@ -173,7 +173,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) r1 := st.AddIfNotExist(1) @@ -206,7 +206,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2) ok := st.AddIfNotExistBy(3, func(val int) bool { @@ -245,7 +245,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) set.Delete(3) @@ -274,7 +274,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) fmt.Println(st.Contain(1)) //true @@ -303,9 +303,9 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(1, 2) - set3 := set.NewSet(1, 2, 3, 4) + set1 := set.New(1, 2, 3) + set2 := set.New(1, 2) + set3 := set.New(1, 2, 3, 4) fmt.Println(set1.ContainAll(set2)) //true fmt.Println(set1.ContainAll(set3)) //false @@ -333,7 +333,7 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) + set1 := set.New(1, 2, 3) fmt.Println(set1.Size()) //3 } @@ -360,7 +360,7 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) + set1 := set.New(1, 2, 3) set2 := set1.Clone() fmt.Println(set1.Size() == set2.Size()) //true @@ -389,9 +389,9 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(1, 2, 3) - set3 := set.NewSet(1, 2, 3, 4) + set1 := set.New(1, 2, 3) + set2 := set.New(1, 2, 3) + set3 := set.New(1, 2, 3, 4) fmt.Println(set1.Equal(set2)) //true fmt.Println(set1.Equal(set3)) //false @@ -419,7 +419,7 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) + set1 := set.New(1, 2, 3) arr := []int{} set.Iterate(func(item int) { arr = append(arr, item) @@ -450,7 +450,7 @@ import ( ) func main() { - s := set.NewSet(1, 2, 3, 4, 5) + s := set.New(1, 2, 3, 4, 5) var sum int @@ -487,8 +487,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet() + set1 := set.New(1, 2, 3) + set2 := set.New() fmt.Println(set1.IsEmpty()) //false fmt.Println(set2.IsEmpty()) //true @@ -516,8 +516,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) set3 := set1.Union(set2) fmt.Println(set3.Values()) //1,2,3,4,5 @@ -545,8 +545,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) set3 := set1.Intersection(set2) fmt.Println(set3.Values()) //2,3 @@ -574,8 +574,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) set3 := set1.SymmetricDifference(set2) fmt.Println(set3.Values()) //1,4,5 @@ -603,9 +603,9 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) - set3 := set.NewSet(2, 3) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) + set3 := set.New(2, 3) res1 := set1.Minus(set2) fmt.Println(res1.Values()) //1 @@ -636,7 +636,7 @@ import ( ) func main() { - s := set.NewSet[int]() + s := set.New[int]() s.Add(1) s.Add(2) s.Add(3) diff --git a/docs/en/api/packages/datastructure/set.md b/docs/en/api/packages/datastructure/set.md index 00e3ebb..2c0d593 100644 --- a/docs/en/api/packages/datastructure/set.md +++ b/docs/en/api/packages/datastructure/set.md @@ -22,8 +22,8 @@ import ( ## Index -- [NewSet](#NewSet) -- [NewSetFromSlice](#NewSetFromSlice) +- [New](#New) +- [FromSlice](#FromSlice) - [Values](#Values) - [Add](#Add) - [AddIfNotExist](#AddIfNotExist) @@ -46,7 +46,7 @@ import ( ## Documentation -### NewSet +### New

Create a set instance

@@ -54,7 +54,7 @@ import ( ```go type Set[T comparable] map[T]bool -func NewSet[T comparable](items ...T) Set[T] +func New[T comparable](items ...T) Set[T] ``` Example: @@ -68,19 +68,19 @@ import ( ) func main() { - st := set.NewSet[int](1,2,2,3) + st := set.New[int](1,2,2,3) fmt.Println(st.Values()) //1,2,3 } ``` -### NewSetFromSlice +### FromSlice

Create a set from slice

Signature: ```go -func NewSetFromSlice[T comparable](items []T) Set[T] +func FromSlice[T comparable](items []T) Set[T] ``` Example: @@ -94,7 +94,7 @@ import ( ) func main() { - st := set.NewSetFromSlice([]int{1, 2, 2, 3}) + st := set.FromSlice([]int{1, 2, 2, 3}) fmt.Println(st.Values()) //1,2,3 } ``` @@ -120,7 +120,7 @@ import ( ) func main() { - st := set.NewSet[int](1,2,2,3) + st := set.New[int](1,2,2,3) fmt.Println(st.Values()) //1,2,3 } ``` @@ -146,7 +146,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) fmt.Println(st.Values()) //1,2,3 @@ -174,7 +174,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) r1 := st.AddIfNotExist(1) @@ -207,7 +207,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2) ok := st.AddIfNotExistBy(3, func(val int) bool { @@ -246,7 +246,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) set.Delete(3) @@ -275,7 +275,7 @@ import ( ) func main() { - st := set.NewSet[int]() + st := set.New[int]() st.Add(1, 2, 3) fmt.Println(st.Contain(1)) //true @@ -304,9 +304,9 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(1, 2) - set3 := set.NewSet(1, 2, 3, 4) + set1 := set.New(1, 2, 3) + set2 := set.New(1, 2) + set3 := set.New(1, 2, 3, 4) fmt.Println(set1.ContainAll(set2)) //true fmt.Println(set1.ContainAll(set3)) //false @@ -334,7 +334,7 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) + set1 := set.New(1, 2, 3) fmt.Println(set1.Size()) //3 } @@ -361,7 +361,7 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) + set1 := set.New(1, 2, 3) set2 := set1.Clone() fmt.Println(set1.Size() == set2.Size()) //true @@ -390,9 +390,9 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(1, 2, 3) - set3 := set.NewSet(1, 2, 3, 4) + set1 := set.New(1, 2, 3) + set2 := set.New(1, 2, 3) + set3 := set.New(1, 2, 3, 4) fmt.Println(set1.Equal(set2)) //true fmt.Println(set1.Equal(set3)) //false @@ -420,7 +420,7 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) + set1 := set.New(1, 2, 3) arr := []int{} set.Iterate(func(item int) { arr = append(arr, item) @@ -451,7 +451,7 @@ import ( ) func main() { - s := set.NewSet(1, 2, 3, 4, 5) + s := set.New(1, 2, 3, 4, 5) var sum int @@ -488,8 +488,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet() + set1 := set.New(1, 2, 3) + set2 := set.New() fmt.Println(set1.IsEmpty()) //false fmt.Println(set2.IsEmpty()) //true @@ -517,8 +517,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) set3 := set1.Union(set2) fmt.Println(set3.Values()) //1,2,3,4,5 @@ -546,8 +546,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) set3 := set1.Intersection(set2) fmt.Println(set3.Values()) //2,3 @@ -575,8 +575,8 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) set3 := set1.SymmetricDifference(set2) fmt.Println(set3.Values()) //1,4,5 @@ -604,9 +604,9 @@ import ( ) func main() { - set1 := set.NewSet(1, 2, 3) - set2 := set.NewSet(2, 3, 4, 5) - set3 := set.NewSet(2, 3) + set1 := set.New(1, 2, 3) + set2 := set.New(2, 3, 4, 5) + set3 := set.New(2, 3) res1 := set1.Minus(set2) fmt.Println(res1.Values()) //1 @@ -637,7 +637,7 @@ import ( ) func main() { - s := set.NewSet[int]() + s := set.New[int]() s.Add(1) s.Add(2) s.Add(3)