github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/sets/syncset/wrapper.go (about) 1 package syncset 2 3 import ( 4 "github.com/djordje200179/extendedlibrary/datastructures/iter" 5 "github.com/djordje200179/extendedlibrary/datastructures/sets" 6 "sync" 7 ) 8 9 // Wrapper is a wrapper around a set that provides thread-safe access to the set. 10 // Locking is done through read-write mutex. This means that multiple goroutines can read from the set at the same time, 11 // but only one goroutine can write to the set at the same time. 12 type Wrapper[T any] struct { 13 set sets.Set[T] 14 15 mutex sync.RWMutex 16 } 17 18 // From creates a new Wrapper from the given set. 19 func From[T any](set sets.Set[T]) *Wrapper[T] { 20 return &Wrapper[T]{set, sync.RWMutex{}} 21 } 22 23 // Size returns the number of elements in the set. 24 func (w *Wrapper[T]) Size() int { 25 w.mutex.RLock() 26 defer w.mutex.RUnlock() 27 28 return w.set.Size() 29 } 30 31 // Add adds the given value to the set. 32 func (w *Wrapper[T]) Add(value T) { 33 w.mutex.Lock() 34 defer w.mutex.Unlock() 35 36 w.set.Add(value) 37 } 38 39 // Remove removes the given value from the set. 40 func (w *Wrapper[T]) Remove(value T) { 41 w.mutex.Lock() 42 defer w.mutex.Unlock() 43 44 w.set.Remove(value) 45 } 46 47 // Contains returns true if the set contains the given value. 48 func (w *Wrapper[T]) Contains(value T) bool { 49 w.mutex.RLock() 50 defer w.mutex.RUnlock() 51 52 return w.set.Contains(value) 53 } 54 55 // Clear removes all elements from the set. 56 func (w *Wrapper[T]) Clear() { 57 w.mutex.Lock() 58 defer w.mutex.Unlock() 59 60 w.set.Clear() 61 } 62 63 // Clone returns a shallow copy of the set. 64 func (w *Wrapper[T]) Clone() sets.Set[T] { 65 w.mutex.RLock() 66 defer w.mutex.RUnlock() 67 68 clonedSet := w.set.Clone() 69 return From[T](clonedSet) 70 } 71 72 // Iterator returns an iter.Iterator over the elements in the set. 73 func (w *Wrapper[T]) Iterator() iter.Iterator[T] { 74 return w.SetIterator() 75 } 76 77 // SetIterator returns an iterator over the elements in the set. 78 func (w *Wrapper[T]) SetIterator() sets.Iterator[T] { 79 return Iterator[T]{w.set.SetIterator(), &w.mutex} 80 } 81 82 // Stream streams the elements of the Set. 83 func (w *Wrapper[T]) Stream(yield func(T) bool) { 84 w.mutex.RLock() 85 defer w.mutex.RUnlock() 86 87 w.set.Stream(yield) 88 } 89 90 // Transaction executes the given update function with the set as an argument. 91 // The set is locked for writing during the execution of the update function. 92 func (w *Wrapper[T]) Transaction(updateFunction func(set sets.Set[T])) { 93 w.mutex.Lock() 94 defer w.mutex.Unlock() 95 96 updateFunction(w.set) 97 }