github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/syncmap/iterator.go (about) 1 package syncmap 2 3 import ( 4 "github.com/djordje200179/extendedlibrary/datastructures/maps" 5 "github.com/djordje200179/extendedlibrary/misc" 6 "sync" 7 ) 8 9 // Iterator is a wrapper around an map iterator that provides thread-safe access to the 10 // underlying map. 11 type Iterator[K, V any] struct { 12 mapIt maps.Iterator[K, V] 13 14 mutex *sync.RWMutex 15 } 16 17 // Valid returns true if the iterator is currently pointing to a valid entry. 18 func (it Iterator[K, V]) Valid() bool { 19 return it.mapIt.Valid() 20 } 21 22 // Move moves the iterator to the next entry. 23 func (it Iterator[K, V]) Move() { 24 it.mapIt.Move() 25 } 26 27 // Get returns the current entry as a key-value pair. 28 func (it Iterator[K, V]) Get() misc.Pair[K, V] { 29 it.mutex.RLock() 30 defer it.mutex.RUnlock() 31 32 return misc.MakePair(it.mapIt.Key(), it.mapIt.Value()) 33 } 34 35 // Key returns the current entry's key. 36 func (it Iterator[K, V]) Key() K { 37 it.mutex.RLock() 38 defer it.mutex.RUnlock() 39 40 return it.mapIt.Key() 41 } 42 43 // Value returns the current entry's value. 44 func (it Iterator[K, V]) Value() V { 45 it.mutex.RLock() 46 defer it.mutex.RUnlock() 47 48 return it.mapIt.Value() 49 } 50 51 // ValueRef returns a reference to the current entry's value. 52 func (it Iterator[K, V]) ValueRef() *V { 53 it.mutex.RLock() 54 defer it.mutex.RUnlock() 55 56 return it.mapIt.ValueRef() 57 } 58 59 // SetValue sets the current entry's value. 60 func (it Iterator[K, V]) SetValue(value V) { 61 it.mutex.Lock() 62 defer it.mutex.Unlock() 63 64 it.mapIt.SetValue(value) 65 } 66 67 // Remove removes the current entry. 68 func (it Iterator[K, V]) Remove() { 69 it.mutex.Lock() 70 defer it.mutex.Unlock() 71 72 it.mapIt.Remove() 73 }