github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/hashmap/iterator.go (about) 1 package hashmap 2 3 import "github.com/djordje200179/extendedlibrary/misc" 4 5 // Iterator is an iterator over a HashMap. 6 type Iterator[K comparable, V any] struct { 7 m Map[K, V] 8 9 keys []K 10 index int 11 } 12 13 // Valid returns true if it points to a valid entry. 14 func (it *Iterator[K, V]) Valid() bool { 15 return it.index < len(it.keys) 16 } 17 18 // Move moves the iterator to the next entry. 19 func (it *Iterator[K, V]) Move() { 20 it.index++ 21 } 22 23 // Get returns the current entry as a key-value pair. 24 func (it *Iterator[K, V]) Get() misc.Pair[K, V] { 25 return misc.MakePair(it.Key(), it.Value()) 26 } 27 28 // Key returns the key of the current entry. 29 func (it *Iterator[K, V]) Key() K { 30 return it.keys[it.index] 31 } 32 33 // Value returns the value of the current entry. 34 func (it *Iterator[K, V]) Value() V { 35 return it.m.Get(it.Key()) 36 } 37 38 // ValueRef returns a reference to the value of the current entry. 39 func (it *Iterator[K, V]) ValueRef() *V { 40 return it.m.GetRef(it.Key()) 41 } 42 43 // SetValue sets the value of the current entry. 44 func (it *Iterator[K, V]) SetValue(value V) { 45 it.m.Set(it.Key(), value) 46 } 47 48 // Remove removes the current entry from the map. 49 // The iterator will point to the next entry afterward. 50 func (it *Iterator[K, V]) Remove() { 51 it.m.Remove(it.Key()) 52 }