From dbb753c718bdd678ab755f3f934c8958185ade47 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 1 Sep 2024 22:29:54 +0300 Subject: [PATCH] exsync: add Pop and ReplaceAll --- exsync/syncset.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/exsync/syncset.go b/exsync/syncset.go index 95090f3..879c576 100644 --- a/exsync/syncset.go +++ b/exsync/syncset.go @@ -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 { @@ -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() +}