adding slice methods
This commit is contained in:
parent
92b5c07cfe
commit
e0132bb4bc
3
set.go
3
set.go
@ -7,8 +7,11 @@ import (
|
|||||||
|
|
||||||
type Set[T comparable] interface {
|
type Set[T comparable] interface {
|
||||||
Insert(T) (inserted bool)
|
Insert(T) (inserted bool)
|
||||||
|
InsertSlice([]T) (inserted []bool)
|
||||||
Contains(T) bool
|
Contains(T) bool
|
||||||
|
ContainsSlice([]T) []bool
|
||||||
Remove(T) (removed bool)
|
Remove(T) (removed bool)
|
||||||
|
RemoveSlice([]T) (removed []bool)
|
||||||
Len() int
|
Len() int
|
||||||
ToSlice() []T
|
ToSlice() []T
|
||||||
Clear()
|
Clear()
|
||||||
|
@ -26,12 +26,28 @@ func (s *SimpleSet[T]) Insert(t T) (inserted bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SimpleSet[T]) InsertSlice(slice []T) (inserted []bool) {
|
||||||
|
inserted = make([]bool, 0, len(slice))
|
||||||
|
for _, t := range slice {
|
||||||
|
inserted = append(inserted, s.Insert(t))
|
||||||
|
}
|
||||||
|
return inserted
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SimpleSet[T]) Contains(t T) bool {
|
func (s *SimpleSet[T]) Contains(t T) bool {
|
||||||
s.lockedInitMapIfNil()
|
s.lockedInitMapIfNil()
|
||||||
_, found := s.set[t]
|
_, found := s.set[t]
|
||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SimpleSet[T]) ContainsSlice(slice []T) []bool {
|
||||||
|
results := make([]bool, 0, len(slice))
|
||||||
|
for _, t := range slice {
|
||||||
|
results = append(results, s.Contains(t))
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SimpleSet[T]) Len() int {
|
func (s *SimpleSet[T]) Len() int {
|
||||||
s.lockedInitMapIfNil()
|
s.lockedInitMapIfNil()
|
||||||
return len(s.set)
|
return len(s.set)
|
||||||
@ -57,3 +73,11 @@ func (s *SimpleSet[T]) Remove(t T) (removed bool) {
|
|||||||
delete(s.set, t)
|
delete(s.set, t)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SimpleSet[T]) RemoveSlice(slice []T) (removed []bool) {
|
||||||
|
removed = make([]bool, 0, len(slice))
|
||||||
|
for _, t := range slice {
|
||||||
|
removed = append(removed, s.Remove(t))
|
||||||
|
}
|
||||||
|
return removed
|
||||||
|
}
|
||||||
|
@ -30,12 +30,24 @@ func (s *ThreadsafeSet[T]) Insert(t T) (inserted bool) {
|
|||||||
return s.set.Insert(t)
|
return s.set.Insert(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ThreadsafeSet[T]) InsertSlice(slice []T) (inserted []bool) {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
return s.set.InsertSlice(slice)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ThreadsafeSet[T]) Contains(t T) bool {
|
func (s *ThreadsafeSet[T]) Contains(t T) bool {
|
||||||
s.mutex.RLock()
|
s.mutex.RLock()
|
||||||
defer s.mutex.RUnlock()
|
defer s.mutex.RUnlock()
|
||||||
return s.set != nil && s.set.Contains(t)
|
return s.set != nil && s.set.Contains(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ThreadsafeSet[T]) ContainsSlice(slice []T) []bool {
|
||||||
|
s.mutex.RLock()
|
||||||
|
defer s.mutex.RUnlock()
|
||||||
|
return s.set.ContainsSlice(slice)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ThreadsafeSet[T]) Len() int {
|
func (s *ThreadsafeSet[T]) Len() int {
|
||||||
s.mutex.RLock()
|
s.mutex.RLock()
|
||||||
defer s.mutex.RUnlock()
|
defer s.mutex.RUnlock()
|
||||||
@ -67,3 +79,9 @@ func (s *ThreadsafeSet[T]) Remove(t T) (removed bool) {
|
|||||||
s.lockedInitMapIfNil()
|
s.lockedInitMapIfNil()
|
||||||
return s.set.Remove(t)
|
return s.set.Remove(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ThreadsafeSet[T]) RemoveSlice(slice []T) (removed []bool) {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
return s.set.RemoveSlice(slice)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user