github.com/mymmsc/gox@v1.3.33/util/arraystack/iterator.go (about) 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 ( 8 "github.com/mymmsc/gox/util" 9 ) 10 11 func assertIteratorImplementation() { 12 var _ util.ReverseIteratorWithIndex = (*Iterator)(nil) 13 } 14 15 // Iterator returns a stateful iterator whose values can be fetched by an index. 16 type Iterator struct { 17 stack *Stack 18 index int 19 } 20 21 // Iterator returns a stateful iterator whose values can be fetched by an index. 22 func (stack *Stack) Iterator() Iterator { 23 return Iterator{stack: stack, index: -1} 24 } 25 26 // Next moves the iterator to the next element and returns true if there was a next element in the container. 27 // If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). 28 // If Next() was called for the first time, then it will point the iterator to the first element if it exists. 29 // Modifies the state of the iterator. 30 func (iterator *Iterator) Next() bool { 31 if iterator.index < iterator.stack.Size() { 32 iterator.index++ 33 } 34 return iterator.stack.withinRange(iterator.index) 35 } 36 37 // Prev moves the iterator to the previous element and returns true if there was a previous element in the container. 38 // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). 39 // Modifies the state of the iterator. 40 func (iterator *Iterator) Prev() bool { 41 if iterator.index >= 0 { 42 iterator.index-- 43 } 44 return iterator.stack.withinRange(iterator.index) 45 } 46 47 // Value returns the current element's value. 48 // Does not modify the state of the iterator. 49 func (iterator *Iterator) Value() interface{} { 50 value, _ := iterator.stack.list.Get(iterator.stack.list.Size() - iterator.index - 1) // in reverse (LIFO) 51 return value 52 } 53 54 // Index returns the current element's index. 55 // Does not modify the state of the iterator. 56 func (iterator *Iterator) Index() int { 57 return iterator.index 58 } 59 60 // Begin resets the iterator to its initial state (one-before-first) 61 // Call Next() to fetch the first element if any. 62 func (iterator *Iterator) Begin() { 63 iterator.index = -1 64 } 65 66 // End moves the iterator past the last element (one-past-the-end). 67 // Call Prev() to fetch the last element if any. 68 func (iterator *Iterator) End() { 69 iterator.index = iterator.stack.Size() 70 } 71 72 // First moves the iterator to the first element and returns true if there was a first element in the container. 73 // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). 74 // Modifies the state of the iterator. 75 func (iterator *Iterator) First() bool { 76 iterator.Begin() 77 return iterator.Next() 78 } 79 80 // Last moves the iterator to the last element and returns true if there was a last element in the container. 81 // If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). 82 // Modifies the state of the iterator. 83 func (iterator *Iterator) Last() bool { 84 iterator.End() 85 return iterator.Prev() 86 }