github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/readmap/wrapper.go (about) 1 package readmap 2 3 import ( 4 "github.com/djordje200179/extendedlibrary/datastructures/iter" 5 "github.com/djordje200179/extendedlibrary/datastructures/maps" 6 "github.com/djordje200179/extendedlibrary/misc" 7 ) 8 9 // Wrapper is a wrapper around map that provides read-only access to the map. 10 type Wrapper[K, V any] struct { 11 m maps.Map[K, V] 12 } 13 14 // From creates a new Wrapper from the given map. 15 func From[K, V any](m maps.Map[K, V]) Wrapper[K, V] { 16 return Wrapper[K, V]{m} 17 } 18 19 // Size returns the number of entries in the map. 20 func (w Wrapper[K, V]) Size() int { 21 return w.m.Size() 22 } 23 24 // Contains returns true if the map contains the given key. 25 func (w Wrapper[K, V]) Contains(key K) bool { 26 return w.m.Contains(key) 27 } 28 29 // TryGet returns the value associated with the given key and true if the key is present in the map. 30 // Otherwise, it returns the zero value for the value type and false. 31 func (w Wrapper[K, V]) TryGet(key K) (V, bool) { 32 return w.m.TryGet(key) 33 } 34 35 // Get returns the value associated with the given key. 36 func (w Wrapper[K, V]) Get(key K) V { 37 return w.m.Get(key) 38 } 39 40 // Clone returns a shallow copy of a Wrapper. 41 // Cloned Wrapper will have the same underlying map as the original Wrapper. 42 func (w Wrapper[K, V]) Clone() Wrapper[K, V] { 43 clonedMap := w.m.Clone() 44 return Wrapper[K, V]{clonedMap} 45 } 46 47 // Iterator returns an iter.Iterator over the entries in the map. 48 func (w Wrapper[K, V]) Iterator() iter.Iterator[misc.Pair[K, V]] { 49 return w.m.Iterator() 50 } 51 52 // Stream2 streams over the entries in the map. 53 func (w Wrapper[K, V]) Stream2(yield func(K, V) bool) { 54 w.m.Stream2(yield) 55 }