| 1 | // Copyright (c) 2015, Emir Pasic. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package arraystack |
| 6 | |
| 7 | import "github.com/emirpasic/gods/v2/containers" |
| 8 | |
| 9 | // Assert Iterator implementation |
| 10 | var _ containers.ReverseIteratorWithIndex[int] = (*Iterator[int])(nil) |
| 11 | |
| 12 | // Iterator returns a stateful iterator whose values can be fetched by an index. |
| 13 | type Iterator[T comparable] struct { |
| 14 | stack *Stack[T] |
| 15 | index int |
| 16 | } |
| 17 | |
| 18 | // Iterator returns a stateful iterator whose values can be fetched by an index. |
| 19 | func (stack *Stack[T]) Iterator() *Iterator[T] { |
| 20 | return &Iterator[T]{stack: stack, index: -1} |
| 21 | } |
| 22 | |
| 23 | // Next moves the iterator to the next element and returns true if there was a next element in the container. |
| 24 | // If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 25 | // If Next() was called for the first time, then it will point the iterator to the first element if it exists. |
| 26 | // Modifies the state of the iterator. |
| 27 | func (iterator *Iterator[T]) Next() bool { |
| 28 | if iterator.index < iterator.stack.Size() { |
| 29 | iterator.index++ |
| 30 | } |
| 31 | return iterator.stack.withinRange(iterator.index) |
| 32 | } |
| 33 | |
| 34 | // Prev moves the iterator to the previous element and returns true if there was a previous element in the container. |
| 35 | // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). |
| 36 | // Modifies the state of the iterator. |
| 37 | func (iterator *Iterator[T]) Prev() bool { |
| 38 | if iterator.index >= 0 { |
| 39 | iterator.index-- |
| 40 | } |
| 41 | return iterator.stack.withinRange(iterator.index) |
| 42 | } |
| 43 | |
| 44 | // Value returns the current element's value. |
| 45 | // Does not modify the state of the iterator. |
| 46 | func (iterator *Iterator[T]) Value() T { |
| 47 | value, _ := iterator.stack.list.Get(iterator.stack.list.Size() - iterator.index - 1) // in reverse (LIFO) |
| 48 | return value |
| 49 | } |
| 50 | |
| 51 | // Index returns the current element's index. |
| 52 | // Does not modify the state of the iterator. |
| 53 | func (iterator *Iterator[T]) Index() int { |
| 54 | return iterator.index |
| 55 | } |
| 56 | |
| 57 | // Begin resets the iterator to its initial state (one-before-first) |
| 58 | // Call Next() to fetch the first element if any. |
| 59 | func (iterator *Iterator[T]) Begin() { |
| 60 | iterator.index = -1 |
| 61 | } |
| 62 | |
| 63 | // End moves the iterator past the last element (one-past-the-end). |
| 64 | // Call Prev() to fetch the last element if any. |
| 65 | func (iterator *Iterator[T]) End() { |
| 66 | iterator.index = iterator.stack.Size() |
| 67 | } |
| 68 | |
| 69 | // First moves the iterator to the first element and returns true if there was a first element in the container. |
| 70 | // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). |
| 71 | // Modifies the state of the iterator. |
| 72 | func (iterator *Iterator[T]) First() bool { |
| 73 | iterator.Begin() |
| 74 | return iterator.Next() |
| 75 | } |
| 76 | |
| 77 | // Last moves the iterator to the last element and returns true if there was a last element in the container. |
| 78 | // If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). |
| 79 | // Modifies the state of the iterator. |
| 80 | func (iterator *Iterator[T]) Last() bool { |
| 81 | iterator.End() |
| 82 | return iterator.Prev() |
| 83 | } |
| 84 | |
| 85 | // NextTo moves the iterator to the next element from current position that satisfies the condition given by the |
| 86 | // passed function, and returns true if there was a next element in the container. |
| 87 | // If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 88 | // Modifies the state of the iterator. |
| 89 | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool { |
| 90 | for iterator.Next() { |
| 91 | index, value := iterator.Index(), iterator.Value() |
| 92 | if f(index, value) { |
| 93 | return true |
| 94 | } |
| 95 | } |
| 96 | return false |
| 97 | } |
| 98 | |
| 99 | // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the |
| 100 | // passed function, and returns true if there was a next element in the container. |
| 101 | // If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 102 | // Modifies the state of the iterator. |
| 103 | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool { |
| 104 | for iterator.Prev() { |
| 105 | index, value := iterator.Index(), iterator.Value() |
| 106 | if f(index, value) { |
| 107 | return true |
| 108 | } |
| 109 | } |
| 110 | return false |
| 111 | } |