github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/treemap/iterator.go (about)

     1  package treemap
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/structure/containers"
     5  	"github.com/songzhibin97/go-baseutils/structure/trees/redblacktree"
     6  )
     7  
     8  // Assert Iterator implementation
     9  var _ containers.ReverseIteratorWithKey[any, any] = (*Iterator[any, any])(nil)
    10  
    11  // Iterator holding the iterator's state
    12  type Iterator[K, V any] struct {
    13  	iterator redblacktree.Iterator[K, V]
    14  }
    15  
    16  // Iterator returns a stateful iterator whose elements are key/value pairs.
    17  func (m *Map[K, V]) Iterator() Iterator[K, V] {
    18  	return Iterator[K, V]{iterator: m.tree.Iterator()}
    19  }
    20  
    21  // Next moves the iterator to the next element and returns true if there was a next element in the container.
    22  // If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
    23  // If Next() was called for the first time, then it will point the iterator to the first element if it exists.
    24  // Modifies the state of the iterator.
    25  func (iterator *Iterator[K, V]) Next() bool {
    26  	return iterator.iterator.Next()
    27  }
    28  
    29  // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
    30  // If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
    31  // Modifies the state of the iterator.
    32  func (iterator *Iterator[K, V]) Prev() bool {
    33  	return iterator.iterator.Prev()
    34  }
    35  
    36  // Value returns the current element's value.
    37  // Does not modify the state of the iterator.
    38  func (iterator *Iterator[K, V]) Value() V {
    39  	return iterator.iterator.Value()
    40  }
    41  
    42  // Key returns the current element's key.
    43  // Does not modify the state of the iterator.
    44  func (iterator *Iterator[K, V]) Key() K {
    45  	return iterator.iterator.Key()
    46  }
    47  
    48  // Begin resets the iterator to its initial state (one-before-first)
    49  // Call Next() to fetch the first element if any.
    50  func (iterator *Iterator[K, V]) Begin() {
    51  	iterator.iterator.Begin()
    52  }
    53  
    54  // End moves the iterator past the last element (one-past-the-end).
    55  // Call Prev() to fetch the last element if any.
    56  func (iterator *Iterator[K, V]) End() {
    57  	iterator.iterator.End()
    58  }
    59  
    60  // First moves the iterator to the first element and returns true if there was a first element in the container.
    61  // If First() returns true, then first element's key and value can be retrieved by Key() and Value().
    62  // Modifies the state of the iterator
    63  func (iterator *Iterator[K, V]) First() bool {
    64  	return iterator.iterator.First()
    65  }
    66  
    67  // Last moves the iterator to the last element and returns true if there was a last element in the container.
    68  // If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
    69  // Modifies the state of the iterator.
    70  func (iterator *Iterator[K, V]) Last() bool {
    71  	return iterator.iterator.Last()
    72  }
    73  
    74  // NextTo moves the iterator to the next element from current position that satisfies the condition given by the
    75  // passed function, and returns true if there was a next element in the container.
    76  // If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value().
    77  // Modifies the state of the iterator.
    78  func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
    79  	for iterator.Next() {
    80  		key, value := iterator.Key(), iterator.Value()
    81  		if f(key, value) {
    82  			return true
    83  		}
    84  	}
    85  	return false
    86  }
    87  
    88  // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
    89  // passed function, and returns true if there was a next element in the container.
    90  // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value().
    91  // Modifies the state of the iterator.
    92  func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
    93  	for iterator.Prev() {
    94  		key, value := iterator.Key(), iterator.Value()
    95  		if f(key, value) {
    96  			return true
    97  		}
    98  	}
    99  	return false
   100  }