mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
refactor: break change, rename constructor of set (NewSet->New, NewSetFromSlice->FromSlice)
This commit is contained in:
@@ -7,15 +7,15 @@ package datastructure
|
|||||||
// Set is a data container, like slice, but element of set is not duplicate.
|
// Set is a data container, like slice, but element of set is not duplicate.
|
||||||
type Set[T comparable] map[T]struct{}
|
type Set[T comparable] map[T]struct{}
|
||||||
|
|
||||||
// NewSet return a instance of set
|
// New create a instance of set from given values.
|
||||||
func NewSet[T comparable](items ...T) Set[T] {
|
func New[T comparable](items ...T) Set[T] {
|
||||||
set := make(Set[T])
|
set := make(Set[T])
|
||||||
set.Add(items...)
|
set.Add(items...)
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSetFromSlice create a set from slice
|
// FromSlice create a set from given slice.
|
||||||
func NewSetFromSlice[T comparable](items []T) Set[T] {
|
func FromSlice[T comparable](items []T) Set[T] {
|
||||||
set := make(Set[T])
|
set := make(Set[T])
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
set.Add(item)
|
set.Add(item)
|
||||||
@@ -77,7 +77,7 @@ func (s Set[T]) ContainAll(other Set[T]) bool {
|
|||||||
|
|
||||||
// Clone return a copy of set
|
// Clone return a copy of set
|
||||||
func (s Set[T]) Clone() Set[T] {
|
func (s Set[T]) Clone() Set[T] {
|
||||||
set := NewSet[T]()
|
set := New[T]()
|
||||||
set.Add(s.Values()...)
|
set.Add(s.Values()...)
|
||||||
return set
|
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
|
// 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] {
|
func (s Set[T]) Intersection(other Set[T]) Set[T] {
|
||||||
set := NewSet[T]()
|
set := New[T]()
|
||||||
s.Iterate(func(value T) {
|
s.Iterate(func(value T) {
|
||||||
if other.Contain(value) {
|
if other.Contain(value) {
|
||||||
set.Add(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
|
// 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] {
|
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
|
||||||
set := NewSet[T]()
|
set := New[T]()
|
||||||
s.Iterate(func(value T) {
|
s.Iterate(func(value T) {
|
||||||
if !other.Contain(value) {
|
if !other.Contain(value) {
|
||||||
set.Add(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
|
// 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] {
|
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
||||||
set := NewSet[T]()
|
set := New[T]()
|
||||||
|
|
||||||
s.Iterate(func(value T) {
|
s.Iterate(func(value T) {
|
||||||
if !comparedSet.Contain(value) {
|
if !comparedSet.Contain(value) {
|
||||||
|
|||||||
@@ -6,18 +6,18 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSet_NewSetFromSlice(t *testing.T) {
|
func TestSet_FromSlice(t *testing.T) {
|
||||||
t.Parallel()
|
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(3, s1.Size())
|
||||||
assert.Equal(true, s1.Contain(1))
|
assert.Equal(true, s1.Contain(1))
|
||||||
assert.Equal(true, s1.Contain(2))
|
assert.Equal(true, s1.Contain(2))
|
||||||
assert.Equal(true, s1.Contain(3))
|
assert.Equal(true, s1.Contain(3))
|
||||||
|
|
||||||
s2 := NewSetFromSlice([]int{})
|
s2 := FromSlice([]int{})
|
||||||
assert.Equal(0, s2.Size())
|
assert.Equal(0, s2.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,10 +26,10 @@ func TestSet_Add(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Add")
|
assert := internal.NewAssert(t, "TestSet_Add")
|
||||||
|
|
||||||
set := NewSet[int]()
|
set := New[int]()
|
||||||
set.Add(1, 2, 3)
|
set.Add(1, 2, 3)
|
||||||
|
|
||||||
cmpSet := NewSet(1, 2, 3)
|
cmpSet := New(1, 2, 3)
|
||||||
|
|
||||||
assert.Equal(true, set.Equal(cmpSet))
|
assert.Equal(true, set.Equal(cmpSet))
|
||||||
}
|
}
|
||||||
@@ -39,12 +39,12 @@ func TestSet_AddIfNotExist(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_AddIfNotExist")
|
assert := internal.NewAssert(t, "TestSet_AddIfNotExist")
|
||||||
|
|
||||||
set := NewSet[int]()
|
set := New[int]()
|
||||||
set.Add(1, 2, 3)
|
set.Add(1, 2, 3)
|
||||||
|
|
||||||
assert.Equal(false, set.AddIfNotExist(1))
|
assert.Equal(false, set.AddIfNotExist(1))
|
||||||
assert.Equal(true, set.AddIfNotExist(4))
|
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) {
|
func TestSet_AddIfNotExistBy(t *testing.T) {
|
||||||
@@ -52,7 +52,7 @@ func TestSet_AddIfNotExistBy(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_AddIfNotExistBy")
|
assert := internal.NewAssert(t, "TestSet_AddIfNotExistBy")
|
||||||
|
|
||||||
set := NewSet[int]()
|
set := New[int]()
|
||||||
set.Add(1, 2)
|
set.Add(1, 2)
|
||||||
|
|
||||||
ok := set.AddIfNotExistBy(3, func(val int) bool {
|
ok := set.AddIfNotExistBy(3, func(val int) bool {
|
||||||
@@ -75,7 +75,7 @@ func TestSet_Contain(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Contain")
|
assert := internal.NewAssert(t, "TestSet_Contain")
|
||||||
|
|
||||||
set := NewSet[int]()
|
set := New[int]()
|
||||||
set.Add(1, 2, 3)
|
set.Add(1, 2, 3)
|
||||||
|
|
||||||
assert.Equal(true, set.Contain(1))
|
assert.Equal(true, set.Contain(1))
|
||||||
@@ -87,9 +87,9 @@ func TestSet_ContainAll(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_ContainAll")
|
assert := internal.NewAssert(t, "TestSet_ContainAll")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := NewSet(1, 2)
|
set2 := New(1, 2)
|
||||||
set3 := NewSet(1, 2, 3, 4)
|
set3 := New(1, 2, 3, 4)
|
||||||
|
|
||||||
assert.Equal(true, set1.ContainAll(set2))
|
assert.Equal(true, set1.ContainAll(set2))
|
||||||
assert.Equal(false, set1.ContainAll(set3))
|
assert.Equal(false, set1.ContainAll(set3))
|
||||||
@@ -100,7 +100,7 @@ func TestSet_Clone(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Clone")
|
assert := internal.NewAssert(t, "TestSet_Clone")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := set1.Clone()
|
set2 := set1.Clone()
|
||||||
|
|
||||||
assert.Equal(true, set1.Size() == set2.Size())
|
assert.Equal(true, set1.Size() == set2.Size())
|
||||||
@@ -112,11 +112,11 @@ func TestSet_Delete(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Delete")
|
assert := internal.NewAssert(t, "TestSet_Delete")
|
||||||
|
|
||||||
set := NewSet[int]()
|
set := New[int]()
|
||||||
set.Add(1, 2, 3)
|
set.Add(1, 2, 3)
|
||||||
set.Delete(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) {
|
func TestSet_Equal(t *testing.T) {
|
||||||
@@ -124,9 +124,9 @@ func TestSet_Equal(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Equal")
|
assert := internal.NewAssert(t, "TestSet_Equal")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := NewSet(1, 2, 3)
|
set2 := New(1, 2, 3)
|
||||||
set3 := NewSet(1, 2, 3, 4)
|
set3 := New(1, 2, 3, 4)
|
||||||
|
|
||||||
assert.Equal(true, set1.Equal(set2))
|
assert.Equal(true, set1.Equal(set2))
|
||||||
assert.Equal(false, set1.Equal(set3))
|
assert.Equal(false, set1.Equal(set3))
|
||||||
@@ -137,7 +137,7 @@ func TestSet_Iterate(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Iterate")
|
assert := internal.NewAssert(t, "TestSet_Iterate")
|
||||||
|
|
||||||
set := NewSet(1, 2, 3)
|
set := New(1, 2, 3)
|
||||||
arr := []int{}
|
arr := []int{}
|
||||||
set.Iterate(func(value int) {
|
set.Iterate(func(value int) {
|
||||||
arr = append(arr, value)
|
arr = append(arr, value)
|
||||||
@@ -151,7 +151,7 @@ func TestSet_IsEmpty(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_IsEmpty")
|
assert := internal.NewAssert(t, "TestSet_IsEmpty")
|
||||||
|
|
||||||
set := NewSet[int]()
|
set := New[int]()
|
||||||
assert.Equal(true, set.IsEmpty())
|
assert.Equal(true, set.IsEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,7 +160,7 @@ func TestSet_Size(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Size")
|
assert := internal.NewAssert(t, "TestSet_Size")
|
||||||
|
|
||||||
set := NewSet(1, 2, 3)
|
set := New(1, 2, 3)
|
||||||
assert.Equal(3, set.Size())
|
assert.Equal(3, set.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ func TestSet_Values(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Values")
|
assert := internal.NewAssert(t, "TestSet_Values")
|
||||||
|
|
||||||
set := NewSet(1, 2, 3)
|
set := New(1, 2, 3)
|
||||||
values := set.Values()
|
values := set.Values()
|
||||||
|
|
||||||
assert.Equal(3, len(values))
|
assert.Equal(3, len(values))
|
||||||
@@ -180,12 +180,12 @@ func TestSet_Union(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Union")
|
assert := internal.NewAssert(t, "TestSet_Union")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := NewSet(2, 3, 4, 5)
|
set2 := New(2, 3, 4, 5)
|
||||||
|
|
||||||
unionSet := set1.Union(set2)
|
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) {
|
func TestSet_Intersection(t *testing.T) {
|
||||||
@@ -193,11 +193,11 @@ func TestSet_Intersection(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Intersection")
|
assert := internal.NewAssert(t, "TestSet_Intersection")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := NewSet(2, 3, 4, 5)
|
set2 := New(2, 3, 4, 5)
|
||||||
intersectionSet := set1.Intersection(set2)
|
intersectionSet := set1.Intersection(set2)
|
||||||
|
|
||||||
assert.Equal(NewSet(2, 3), intersectionSet)
|
assert.Equal(New(2, 3), intersectionSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSet_SymmetricDifference(t *testing.T) {
|
func TestSet_SymmetricDifference(t *testing.T) {
|
||||||
@@ -205,10 +205,10 @@ func TestSet_SymmetricDifference(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_SymmetricDifference")
|
assert := internal.NewAssert(t, "TestSet_SymmetricDifference")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := NewSet(2, 3, 4, 5)
|
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) {
|
func TestSet_Minus(t *testing.T) {
|
||||||
@@ -216,16 +216,16 @@ func TestSet_Minus(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSet_Minus")
|
assert := internal.NewAssert(t, "TestSet_Minus")
|
||||||
|
|
||||||
set1 := NewSet(1, 2, 3)
|
set1 := New(1, 2, 3)
|
||||||
set2 := NewSet(2, 3, 4, 5)
|
set2 := New(2, 3, 4, 5)
|
||||||
set3 := NewSet(2, 3)
|
set3 := New(2, 3)
|
||||||
|
|
||||||
assert.Equal(NewSet(1), set1.Minus(set2))
|
assert.Equal(New(1), set1.Minus(set2))
|
||||||
assert.Equal(NewSet(4, 5), set2.Minus(set3))
|
assert.Equal(New(4, 5), set2.Minus(set3))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEachWithBreak(t *testing.T) {
|
func TestEachWithBreak(t *testing.T) {
|
||||||
// s := NewSet(1, 2, 3, 4, 5)
|
// s := New(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
// var sum int
|
// var sum int
|
||||||
|
|
||||||
@@ -244,7 +244,7 @@ func TestEachWithBreak(t *testing.T) {
|
|||||||
// func TestPop(t *testing.T) {
|
// func TestPop(t *testing.T) {
|
||||||
// assert := internal.NewAssert(t, "TestPop")
|
// assert := internal.NewAssert(t, "TestPop")
|
||||||
|
|
||||||
// s := NewSet[int]()
|
// s := New[int]()
|
||||||
|
|
||||||
// val, ok := s.Pop()
|
// val, ok := s.Pop()
|
||||||
// assert.Equal(0, val)
|
// assert.Equal(0, val)
|
||||||
@@ -254,7 +254,7 @@ func TestEachWithBreak(t *testing.T) {
|
|||||||
// s.Add(2)
|
// s.Add(2)
|
||||||
// s.Add(3)
|
// s.Add(3)
|
||||||
|
|
||||||
// // s = NewSet(1, 2, 3, 4, 5)
|
// // s = New(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
// val, ok = s.Pop()
|
// val, ok = s.Pop()
|
||||||
// assert.Equal(3, val)
|
// assert.Equal(3, val)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Set
|
# Set
|
||||||
|
|
||||||
Set 集合数据结构,类似列表。Set 中元素不重复。
|
集合数据结构,类似列表。Set中元素不重复。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [NewSet](#NewSet)
|
- [New](#New)
|
||||||
- [NewSetFromSlice](#NewSetFromSlice)
|
- [FromSlice](#FromSlice)
|
||||||
- [Values](#Values)
|
- [Values](#Values)
|
||||||
- [Add](#Add)
|
- [Add](#Add)
|
||||||
- [AddIfNotExist](#AddIfNotExist)
|
- [AddIfNotExist](#AddIfNotExist)
|
||||||
@@ -45,7 +45,7 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
### <span id="NewSet">NewSet</span>
|
### <span id="New">New</span>
|
||||||
|
|
||||||
<p>返回Set结构体对象</p>
|
<p>返回Set结构体对象</p>
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ import (
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]bool
|
||||||
func NewSet[T comparable](items ...T) Set[T]
|
func New[T comparable](items ...T) Set[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -67,19 +67,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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
|
fmt.Println(st.Values()) //1,2,3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="NewSetFromSlice">NewSetFromSlice</span>
|
### <span id="FromSlice">FromSlice</span>
|
||||||
|
|
||||||
<p>基于切片创建集合</p>
|
<p>基于切片创建集合</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func NewSetFromSlice[T comparable](items []T) Set[T]
|
func FromSlice[T comparable](items []T) Set[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -93,7 +93,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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
|
fmt.Println(st.Values()) //1,2,3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -119,7 +119,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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
|
fmt.Println(st.Values()) //1,2,3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -145,7 +145,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
fmt.Println(st.Values()) //1,2,3
|
fmt.Println(st.Values()) //1,2,3
|
||||||
@@ -173,7 +173,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
r1 := st.AddIfNotExist(1)
|
r1 := st.AddIfNotExist(1)
|
||||||
@@ -206,7 +206,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2)
|
st.Add(1, 2)
|
||||||
|
|
||||||
ok := st.AddIfNotExistBy(3, func(val int) bool {
|
ok := st.AddIfNotExistBy(3, func(val int) bool {
|
||||||
@@ -245,7 +245,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
set.Delete(3)
|
set.Delete(3)
|
||||||
@@ -274,7 +274,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
fmt.Println(st.Contain(1)) //true
|
fmt.Println(st.Contain(1)) //true
|
||||||
@@ -303,9 +303,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(1, 2)
|
set2 := set.New(1, 2)
|
||||||
set3 := set.NewSet(1, 2, 3, 4)
|
set3 := set.New(1, 2, 3, 4)
|
||||||
|
|
||||||
fmt.Println(set1.ContainAll(set2)) //true
|
fmt.Println(set1.ContainAll(set2)) //true
|
||||||
fmt.Println(set1.ContainAll(set3)) //false
|
fmt.Println(set1.ContainAll(set3)) //false
|
||||||
@@ -333,7 +333,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
|
|
||||||
fmt.Println(set1.Size()) //3
|
fmt.Println(set1.Size()) //3
|
||||||
}
|
}
|
||||||
@@ -360,7 +360,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set1.Clone()
|
set2 := set1.Clone()
|
||||||
|
|
||||||
fmt.Println(set1.Size() == set2.Size()) //true
|
fmt.Println(set1.Size() == set2.Size()) //true
|
||||||
@@ -389,9 +389,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(1, 2, 3)
|
set2 := set.New(1, 2, 3)
|
||||||
set3 := set.NewSet(1, 2, 3, 4)
|
set3 := set.New(1, 2, 3, 4)
|
||||||
|
|
||||||
fmt.Println(set1.Equal(set2)) //true
|
fmt.Println(set1.Equal(set2)) //true
|
||||||
fmt.Println(set1.Equal(set3)) //false
|
fmt.Println(set1.Equal(set3)) //false
|
||||||
@@ -419,7 +419,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
arr := []int{}
|
arr := []int{}
|
||||||
set.Iterate(func(item int) {
|
set.Iterate(func(item int) {
|
||||||
arr = append(arr, item)
|
arr = append(arr, item)
|
||||||
@@ -450,7 +450,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := set.NewSet(1, 2, 3, 4, 5)
|
s := set.New(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
var sum int
|
var sum int
|
||||||
|
|
||||||
@@ -487,8 +487,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet()
|
set2 := set.New()
|
||||||
|
|
||||||
fmt.Println(set1.IsEmpty()) //false
|
fmt.Println(set1.IsEmpty()) //false
|
||||||
fmt.Println(set2.IsEmpty()) //true
|
fmt.Println(set2.IsEmpty()) //true
|
||||||
@@ -516,8 +516,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set1.Union(set2)
|
set3 := set1.Union(set2)
|
||||||
|
|
||||||
fmt.Println(set3.Values()) //1,2,3,4,5
|
fmt.Println(set3.Values()) //1,2,3,4,5
|
||||||
@@ -545,8 +545,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set1.Intersection(set2)
|
set3 := set1.Intersection(set2)
|
||||||
|
|
||||||
fmt.Println(set3.Values()) //2,3
|
fmt.Println(set3.Values()) //2,3
|
||||||
@@ -574,8 +574,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set1.SymmetricDifference(set2)
|
set3 := set1.SymmetricDifference(set2)
|
||||||
|
|
||||||
fmt.Println(set3.Values()) //1,4,5
|
fmt.Println(set3.Values()) //1,4,5
|
||||||
@@ -603,9 +603,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set.NewSet(2, 3)
|
set3 := set.New(2, 3)
|
||||||
|
|
||||||
res1 := set1.Minus(set2)
|
res1 := set1.Minus(set2)
|
||||||
fmt.Println(res1.Values()) //1
|
fmt.Println(res1.Values()) //1
|
||||||
@@ -636,7 +636,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := set.NewSet[int]()
|
s := set.New[int]()
|
||||||
s.Add(1)
|
s.Add(1)
|
||||||
s.Add(2)
|
s.Add(2)
|
||||||
s.Add(3)
|
s.Add(3)
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [NewSet](#NewSet)
|
- [New](#New)
|
||||||
- [NewSetFromSlice](#NewSetFromSlice)
|
- [FromSlice](#FromSlice)
|
||||||
- [Values](#Values)
|
- [Values](#Values)
|
||||||
- [Add](#Add)
|
- [Add](#Add)
|
||||||
- [AddIfNotExist](#AddIfNotExist)
|
- [AddIfNotExist](#AddIfNotExist)
|
||||||
@@ -46,7 +46,7 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
### <span id="NewSet">NewSet</span>
|
### <span id="New">New</span>
|
||||||
|
|
||||||
<p>Create a set instance</p>
|
<p>Create a set instance</p>
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ import (
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]bool
|
||||||
func NewSet[T comparable](items ...T) Set[T]
|
func New[T comparable](items ...T) Set[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -68,19 +68,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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
|
fmt.Println(st.Values()) //1,2,3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="NewSetFromSlice">NewSetFromSlice</span>
|
### <span id="FromSlice">FromSlice</span>
|
||||||
|
|
||||||
<p>Create a set from slice</p>
|
<p>Create a set from slice</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func NewSetFromSlice[T comparable](items []T) Set[T]
|
func FromSlice[T comparable](items []T) Set[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -94,7 +94,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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
|
fmt.Println(st.Values()) //1,2,3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -120,7 +120,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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
|
fmt.Println(st.Values()) //1,2,3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -146,7 +146,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
fmt.Println(st.Values()) //1,2,3
|
fmt.Println(st.Values()) //1,2,3
|
||||||
@@ -174,7 +174,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
r1 := st.AddIfNotExist(1)
|
r1 := st.AddIfNotExist(1)
|
||||||
@@ -207,7 +207,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2)
|
st.Add(1, 2)
|
||||||
|
|
||||||
ok := st.AddIfNotExistBy(3, func(val int) bool {
|
ok := st.AddIfNotExistBy(3, func(val int) bool {
|
||||||
@@ -246,7 +246,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
set.Delete(3)
|
set.Delete(3)
|
||||||
@@ -275,7 +275,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
st := set.NewSet[int]()
|
st := set.New[int]()
|
||||||
st.Add(1, 2, 3)
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
fmt.Println(st.Contain(1)) //true
|
fmt.Println(st.Contain(1)) //true
|
||||||
@@ -304,9 +304,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(1, 2)
|
set2 := set.New(1, 2)
|
||||||
set3 := set.NewSet(1, 2, 3, 4)
|
set3 := set.New(1, 2, 3, 4)
|
||||||
|
|
||||||
fmt.Println(set1.ContainAll(set2)) //true
|
fmt.Println(set1.ContainAll(set2)) //true
|
||||||
fmt.Println(set1.ContainAll(set3)) //false
|
fmt.Println(set1.ContainAll(set3)) //false
|
||||||
@@ -334,7 +334,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
|
|
||||||
fmt.Println(set1.Size()) //3
|
fmt.Println(set1.Size()) //3
|
||||||
}
|
}
|
||||||
@@ -361,7 +361,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set1.Clone()
|
set2 := set1.Clone()
|
||||||
|
|
||||||
fmt.Println(set1.Size() == set2.Size()) //true
|
fmt.Println(set1.Size() == set2.Size()) //true
|
||||||
@@ -390,9 +390,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(1, 2, 3)
|
set2 := set.New(1, 2, 3)
|
||||||
set3 := set.NewSet(1, 2, 3, 4)
|
set3 := set.New(1, 2, 3, 4)
|
||||||
|
|
||||||
fmt.Println(set1.Equal(set2)) //true
|
fmt.Println(set1.Equal(set2)) //true
|
||||||
fmt.Println(set1.Equal(set3)) //false
|
fmt.Println(set1.Equal(set3)) //false
|
||||||
@@ -420,7 +420,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
arr := []int{}
|
arr := []int{}
|
||||||
set.Iterate(func(item int) {
|
set.Iterate(func(item int) {
|
||||||
arr = append(arr, item)
|
arr = append(arr, item)
|
||||||
@@ -451,7 +451,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := set.NewSet(1, 2, 3, 4, 5)
|
s := set.New(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
var sum int
|
var sum int
|
||||||
|
|
||||||
@@ -488,8 +488,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet()
|
set2 := set.New()
|
||||||
|
|
||||||
fmt.Println(set1.IsEmpty()) //false
|
fmt.Println(set1.IsEmpty()) //false
|
||||||
fmt.Println(set2.IsEmpty()) //true
|
fmt.Println(set2.IsEmpty()) //true
|
||||||
@@ -517,8 +517,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set1.Union(set2)
|
set3 := set1.Union(set2)
|
||||||
|
|
||||||
fmt.Println(set3.Values()) //1,2,3,4,5
|
fmt.Println(set3.Values()) //1,2,3,4,5
|
||||||
@@ -546,8 +546,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set1.Intersection(set2)
|
set3 := set1.Intersection(set2)
|
||||||
|
|
||||||
fmt.Println(set3.Values()) //2,3
|
fmt.Println(set3.Values()) //2,3
|
||||||
@@ -575,8 +575,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set1.SymmetricDifference(set2)
|
set3 := set1.SymmetricDifference(set2)
|
||||||
|
|
||||||
fmt.Println(set3.Values()) //1,4,5
|
fmt.Println(set3.Values()) //1,4,5
|
||||||
@@ -604,9 +604,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.New(1, 2, 3)
|
||||||
set2 := set.NewSet(2, 3, 4, 5)
|
set2 := set.New(2, 3, 4, 5)
|
||||||
set3 := set.NewSet(2, 3)
|
set3 := set.New(2, 3)
|
||||||
|
|
||||||
res1 := set1.Minus(set2)
|
res1 := set1.Minus(set2)
|
||||||
fmt.Println(res1.Values()) //1
|
fmt.Println(res1.Values()) //1
|
||||||
@@ -637,7 +637,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := set.NewSet[int]()
|
s := set.New[int]()
|
||||||
s.Add(1)
|
s.Add(1)
|
||||||
s.Add(2)
|
s.Add(2)
|
||||||
s.Add(3)
|
s.Add(3)
|
||||||
|
|||||||
Reference in New Issue
Block a user