github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/hashset/hashset_safe.go (about) 1 package hashset 2 3 import ( 4 "github.com/songzhibin97/go-baseutils/structure/sets" 5 "sync" 6 ) 7 8 var _ sets.Set[int] = (*SetSafe[int])(nil) 9 10 type SetSafe[E comparable] struct { 11 unsafe *Set[E] 12 lock sync.Mutex 13 } 14 15 func (s *SetSafe[E]) Add(items ...E) { 16 s.lock.Lock() 17 defer s.lock.Unlock() 18 s.unsafe.Add(items...) 19 20 } 21 22 func (s *SetSafe[E]) Remove(items ...E) { 23 s.lock.Lock() 24 defer s.lock.Unlock() 25 s.unsafe.Remove(items...) 26 27 } 28 29 func (s *SetSafe[E]) Contains(items ...E) bool { 30 s.lock.Lock() 31 defer s.lock.Unlock() 32 return s.unsafe.Contains(items...) 33 } 34 35 func (s *SetSafe[E]) Empty() bool { 36 s.lock.Lock() 37 defer s.lock.Unlock() 38 return s.unsafe.Empty() 39 } 40 41 func (s *SetSafe[E]) Size() int { 42 s.lock.Lock() 43 defer s.lock.Unlock() 44 return s.unsafe.Size() 45 } 46 47 func (s *SetSafe[E]) Clear() { 48 s.lock.Lock() 49 defer s.lock.Unlock() 50 s.unsafe.Clear() 51 52 } 53 54 func (s *SetSafe[E]) Values() []E { 55 s.lock.Lock() 56 defer s.lock.Unlock() 57 return s.unsafe.Values() 58 } 59 60 func (s *SetSafe[E]) String() string { 61 s.lock.Lock() 62 defer s.lock.Unlock() 63 return s.unsafe.String() 64 } 65 66 func (s *SetSafe[E]) Intersection(another *Set[E]) *Set[E] { 67 s.lock.Lock() 68 defer s.lock.Unlock() 69 return s.unsafe.Intersection(another) 70 } 71 72 func (s *SetSafe[E]) Union(another *Set[E]) *Set[E] { 73 s.lock.Lock() 74 defer s.lock.Unlock() 75 return s.unsafe.Union(another) 76 } 77 78 func (s *SetSafe[E]) Difference(another *Set[E]) *Set[E] { 79 s.lock.Lock() 80 defer s.lock.Unlock() 81 return s.unsafe.Difference(another) 82 } 83 84 func (s *SetSafe[E]) UnmarshalJSON(bytes []byte) error { 85 s.lock.Lock() 86 defer s.lock.Unlock() 87 return s.unsafe.UnmarshalJSON(bytes) 88 } 89 90 func (s *SetSafe[E]) MarshalJSON() ([]byte, error) { 91 s.lock.Lock() 92 defer s.lock.Unlock() 93 return s.unsafe.MarshalJSON() 94 }