github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/linkmap/iterator.go (about)

     1  package linkmap
     2  
     3  import "github.com/djordje200179/extendedlibrary/misc"
     4  
     5  // Iterator is a struct for iterating over the map.
     6  type Iterator[K, V any] struct {
     7  	wrapper *Wrapper[K, V]
     8  
     9  	curr *Node[K, V]
    10  }
    11  
    12  // Valid returns true if the iterator points to a valid element.
    13  func (it *Iterator[K, V]) Valid() bool {
    14  	return it.curr != nil
    15  }
    16  
    17  // Move moves the iterator to the next element.
    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 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 current key.
    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 be moved to the next element.
    53  func (it *Iterator[K, V]) Remove() {
    54  	next := it.curr.next
    55  	it.wrapper.m.Remove(it.curr.key)
    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  }