github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/rbt/iterator.go (about) 1 package rbt 2 3 import "github.com/djordje200179/extendedlibrary/misc" 4 5 // Iterator is an iterator over a Tree. 6 type Iterator[K, V any] struct { 7 tree *Tree[K, V] 8 9 curr *Node[K, V] 10 } 11 12 // Valid returns true if it points to a valid entry. 13 func (it *Iterator[K, V]) Valid() bool { 14 return it.curr != nil 15 } 16 17 // Move moves the iterator to the next entry. 18 func (it *Iterator[K, V]) Move() { 19 if it.curr == nil { 20 return 21 } 22 23 it.curr = it.curr.Next() 24 } 25 26 // Get returns the current entry as a key-value pair. 27 func (it *Iterator[K, V]) Get() misc.Pair[K, V] { 28 return misc.MakePair(it.Key(), it.Value()) 29 } 30 31 // Key returns the key of the current entry. 32 func (it *Iterator[K, V]) Key() K { 33 return it.curr.key 34 } 35 36 // Value returns the value of the current entry. 37 func (it *Iterator[K, V]) Value() V { 38 return it.curr.Value 39 } 40 41 // ValueRef returns a reference to the value of the current entry. 42 func (it *Iterator[K, V]) ValueRef() *V { 43 return &it.curr.Value 44 } 45 46 // SetValue sets the value of the current entry. 47 func (it *Iterator[K, V]) SetValue(value V) { 48 it.curr.Value = value 49 } 50 51 // Remove removes the current entry from the map. 52 // The iterator will point to the next entry afterward. 53 func (it *Iterator[K, V]) Remove() { 54 next := it.curr.Next() 55 it.tree.removeNode(it.curr) 56 it.curr = next 57 } 58 59 // Node returns the current node. 60 func (it *Iterator[K, V]) Node() *Node[K, V] { 61 return it.curr 62 }