github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/queues/priorityqueue/iterator.go (about) 1 package priorityqueue 2 3 import ( 4 "github.com/songzhibin97/go-baseutils/structure/containers" 5 "github.com/songzhibin97/go-baseutils/structure/trees/binaryheap" 6 ) 7 8 // Assert Iterator implementation 9 var _ containers.ReverseIteratorWithIndex[any] = (*Iterator[any])(nil) 10 11 // Iterator returns a stateful iterator whose values can be fetched by an index. 12 type Iterator[E any] struct { 13 iterator binaryheap.Iterator[E] 14 } 15 16 // Iterator returns a stateful iterator whose values can be fetched by an index. 17 func (queue *Queue[E]) Iterator() Iterator[E] { 18 return Iterator[E]{iterator: queue.heap.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 index and value can be retrieved by Index() 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[E]) 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 index and value can be retrieved by Index() and Value(). 31 // Modifies the state of the iterator. 32 func (iterator *Iterator[E]) 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[E]) Value() E { 39 return iterator.iterator.Value() 40 } 41 42 // Index returns the current element's index. 43 // Does not modify the state of the iterator. 44 func (iterator *Iterator[E]) Index() int { 45 return iterator.iterator.Index() 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[E]) 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[E]) 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 index and value can be retrieved by Index() and Value(). 62 // Modifies the state of the iterator. 63 func (iterator *Iterator[E]) 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 index and value can be retrieved by Index() and Value(). 69 // Modifies the state of the iterator. 70 func (iterator *Iterator[E]) 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 index and value can be retrieved by Index() and Value(). 77 // Modifies the state of the iterator. 78 func (iterator *Iterator[E]) NextTo(f func(index int, value E) bool) bool { 79 return iterator.iterator.NextTo(f) 80 } 81 82 // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the 83 // passed function, and returns true if there was a next element in the container. 84 // If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). 85 // Modifies the state of the iterator. 86 func (iterator *Iterator[E]) PrevTo(f func(index int, value E) bool) bool { 87 return iterator.iterator.PrevTo(f) 88 }