mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
3 Commits
eb66d038ac
...
5692982dd1
| Author | SHA1 | Date | |
|---|---|---|---|
| 5692982dd1 | |||
| c39c8914fb | |||
| 29bdca1bd2 |
+39
-11
@@ -4,19 +4,47 @@ package datastructure
|
|||||||
type Set[T comparable] map[T]struct{}
|
type Set[T comparable] map[T]struct{}
|
||||||
|
|
||||||
// NewSet return a instance of set
|
// NewSet return a instance of set
|
||||||
func NewSet[T comparable](values ...T) Set[T] {
|
func NewSet[T comparable](items ...T) Set[T] {
|
||||||
set := make(Set[T])
|
set := make(Set[T])
|
||||||
set.Add(values...)
|
set.Add(items...)
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add value to set
|
// Add items to set
|
||||||
func (s Set[T]) Add(values ...T) {
|
func (s Set[T]) Add(items ...T) {
|
||||||
for _, v := range values {
|
for _, v := range items {
|
||||||
s[v] = struct{}{}
|
s[v] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func (s Set[T]) AddIfNotExist(value T) bool {
|
||||||
|
if !s.Contain(value) {
|
||||||
|
if _, ok := s[value]; !ok {
|
||||||
|
s[value] = struct{}{}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool {
|
||||||
|
if !s.Contain(item) {
|
||||||
|
if checker(item) {
|
||||||
|
if _, ok := s[item]; !ok {
|
||||||
|
s[item] = struct{}{}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Contain checks if set contains value or not
|
// Contain checks if set contains value or not
|
||||||
func (s Set[T]) Contain(value T) bool {
|
func (s Set[T]) Contain(value T) bool {
|
||||||
_, ok := s[value]
|
_, ok := s[value]
|
||||||
@@ -41,9 +69,9 @@ func (s Set[T]) Clone() Set[T] {
|
|||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete value of set
|
// Delete item of set
|
||||||
func (s Set[T]) Delete(values ...T) {
|
func (s Set[T]) Delete(items ...T) {
|
||||||
for _, v := range values {
|
for _, v := range items {
|
||||||
delete(s, v)
|
delete(s, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,13 +104,13 @@ func (s Set[T]) Size() int {
|
|||||||
|
|
||||||
// Values return all values of set
|
// Values return all values of set
|
||||||
func (s Set[T]) Values() []T {
|
func (s Set[T]) Values() []T {
|
||||||
values := make([]T, 0, 0)
|
result := make([]T, 0, len(s))
|
||||||
|
|
||||||
s.Iterate(func(value T) {
|
s.Iterate(func(value T) {
|
||||||
values = append(values, value)
|
result = append(result, value)
|
||||||
})
|
})
|
||||||
|
|
||||||
return values
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Union creates a new set contain all element of set s and other
|
// Union creates a new set contain all element of set s and other
|
||||||
|
|||||||
@@ -17,6 +17,38 @@ func TestSet_Add(t *testing.T) {
|
|||||||
assert.Equal(true, set.Equal(expected))
|
assert.Equal(true, set.Equal(expected))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSet_AddIfNotExist(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_AddIfNotExist")
|
||||||
|
|
||||||
|
set := NewSet[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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet_AddIfNotExistBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_AddIfNotExistBy")
|
||||||
|
|
||||||
|
set := NewSet[int]()
|
||||||
|
set.Add(1, 2)
|
||||||
|
|
||||||
|
ok := set.AddIfNotExistBy(3, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
|
||||||
|
notOk := set.AddIfNotExistBy(4, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
assert.Equal(false, notOk)
|
||||||
|
|
||||||
|
assert.Equal(true, set.Contain(3))
|
||||||
|
assert.Equal(false, set.Contain(4))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSet_Contain(t *testing.T) {
|
func TestSet_Contain(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSet_Contain")
|
assert := internal.NewAssert(t, "TestSet_Contain")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user