Compare commits

..

3 Commits

Author SHA1 Message Date
c228caa309
feat(set): add NewFromSlice function
All checks were successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
2024-05-28 00:50:19 +02:00
f8c0f0eddb
refactor(comments): beautify comments 2024-05-28 00:44:13 +02:00
2f56894b6a
ci(test): remove group directive 2024-05-28 00:38:28 +02:00
3 changed files with 17 additions and 13 deletions

View File

@ -5,7 +5,6 @@ when:
steps:
test:
group: test
image: golang:1.22-alpine
commands:
- go test -v ./...

View File

@ -1,6 +1,6 @@
package sets
// Union returns a union of the given sets (left right)
// Union returns a union of the given sets (left right).
func Union[T comparable](left, right *Set[T]) *Set[T] {
left.mux.RLock()
right.mux.RLock()
@ -23,7 +23,7 @@ func Union[T comparable](left, right *Set[T]) *Set[T] {
}
}
// Intersection returns an intersection of the given sets (left ∩ right)
// Intersection returns an intersection of the given sets (left ∩ right).
func Intersection[T comparable](left, right *Set[T]) *Set[T] {
left.mux.RLock()
right.mux.RLock()
@ -43,7 +43,7 @@ func Intersection[T comparable](left, right *Set[T]) *Set[T] {
}
}
// Diff returns the relative complement of sets (left right)
// Diff returns the relative complement of sets (left right).
func Diff[T comparable](left, right *Set[T]) *Set[T] {
left.mux.RLock()
right.mux.RLock()
@ -63,7 +63,7 @@ func Diff[T comparable](left, right *Set[T]) *Set[T] {
}
}
// SymmetricDiff returns the symmetric difference between sets (left ⊖ right)
// SymmetricDiff returns the symmetric difference between sets (left ⊖ right).
func SymmetricDiff[T comparable](left, right *Set[T]) *Set[T] {
left.mux.RLock()
right.mux.RLock()
@ -88,7 +88,7 @@ func SymmetricDiff[T comparable](left, right *Set[T]) *Set[T] {
}
}
// Equal checks whether the sets are equal (left = right)
// Equal checks whether the sets are equal (left = right).
func Equal[T comparable](left, right *Set[T]) bool {
left.mux.RLock()
right.mux.RLock()

19
set.go
View File

@ -4,13 +4,13 @@ import (
"sync"
)
// Set represents a set of values
// Set represents a set of values.
type Set[T comparable] struct {
store map[T]struct{}
mux sync.RWMutex
}
// New creates a new set
// New creates a new set.
func New[T comparable](data ...T) *Set[T] {
set := &Set[T]{
store: map[T]struct{}{},
@ -22,7 +22,12 @@ func New[T comparable](data ...T) *Set[T] {
return set
}
// Size returns number of elements in set
// NewFromSlice creates a new set from a slice.
func NewFromSlice[T comparable](data []T) *Set[T] {
return New[T](data...)
}
// Size returns number of elements in set.
func (s *Set[T]) Size() int {
s.mux.RLock()
defer s.mux.RUnlock()
@ -30,7 +35,7 @@ func (s *Set[T]) Size() int {
return len(s.store)
}
// Contains checks whether the value is contained in the set
// Contains checks whether the value is contained in the set.
func (s *Set[T]) Contains(val T) bool {
s.mux.RLock()
defer s.mux.RUnlock()
@ -39,7 +44,7 @@ func (s *Set[T]) Contains(val T) bool {
return ok
}
// Insert inserts a value into the set if the value was not already present
// Insert inserts a value into the set if the value was not already present.
func (s *Set[T]) Insert(val T) bool {
s.mux.Lock()
defer s.mux.Unlock()
@ -51,7 +56,7 @@ func (s *Set[T]) Insert(val T) bool {
return true
}
// Delete removes a value from the set if the value was already present
// Delete removes a value from the set if the value was already present.
func (s *Set[T]) Delete(val T) bool {
s.mux.Lock()
defer s.mux.Unlock()
@ -63,7 +68,7 @@ func (s *Set[T]) Delete(val T) bool {
return false
}
// Slice returns a slice which contains the elements from the set
// Slice returns a slice which contains the elements from the set.
func (s *Set[T]) Slice() []T {
s.mux.RLock()
defer s.mux.RUnlock()