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

     1  package array
     2  
     3  // Iterator is an iterator over an Array.
     4  type Iterator[T any] struct {
     5  	array *Array[T]
     6  	index int
     7  }
     8  
     9  // Valid returns true if the iterator is currently pointing to a valid element.
    10  func (it *Iterator[T]) Valid() bool {
    11  	return it.index < it.array.Size()
    12  }
    13  
    14  // Move moves the iterator to the next element.
    15  func (it *Iterator[T]) Move() {
    16  	it.index++
    17  }
    18  
    19  // GetRef returns a reference to the current element.
    20  func (it *Iterator[T]) GetRef() *T {
    21  	return it.array.GetRef(it.index)
    22  }
    23  
    24  // Get returns the current element.
    25  func (it *Iterator[T]) Get() T {
    26  	return it.array.Get(it.index)
    27  }
    28  
    29  // Set sets the current element.
    30  func (it *Iterator[T]) Set(value T) {
    31  	it.array.Set(it.index, value)
    32  }
    33  
    34  // InsertBefore inserts the specified element before the current element.
    35  // Iterator then points to the inserted element.
    36  func (it *Iterator[T]) InsertBefore(value T) {
    37  	it.array.Insert(it.index, value)
    38  }
    39  
    40  // InsertAfter inserts the specified element after the current element.
    41  // Iterator keeps pointing to the current element.
    42  func (it *Iterator[T]) InsertAfter(value T) {
    43  	it.array.Insert(it.index+1, value)
    44  }
    45  
    46  // Remove removes the current element.
    47  // Iterator then points to the next element.
    48  func (it *Iterator[T]) Remove() {
    49  	it.array.Remove(it.index)
    50  }
    51  
    52  // Index returns the current index.
    53  func (it *Iterator[T]) Index() int {
    54  	return it.index
    55  }