Skip to content

Commit

Permalink
exsync: add Pop and ReplaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 1, 2024
1 parent 8c1e9c2 commit dbb753c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions exsync/syncset.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ func (s *Set[T]) Has(item T) bool {
return exists
}

// Pop removes the given item from the set. The return value is true if the item was in the set, or false otherwise.
func (s *Set[T]) Pop(item T) bool {
if s == nil {
return false
}
s.l.Lock()
_, exists := s.m[item]
if exists {
delete(s.m, item)
}
s.l.Unlock()
return exists
}

// Remove removes the given item from the set.
func (s *Set[T]) Remove(item T) {
if s == nil {
Expand All @@ -81,3 +95,17 @@ func (s *Set[T]) Remove(item T) {
delete(s.m, item)
s.l.Unlock()
}

// ReplaceAll replaces this set with the given set. If the given set is nil, the set is cleared.
func (s *Set[T]) ReplaceAll(newSet *Set[T]) {
if s == nil {
return
}
s.l.Lock()
if newSet == nil {
s.m = make(map[T]empty)
} else {
s.m = newSet.m
}
s.l.Unlock()
}

0 comments on commit dbb753c

Please sign in to comment.