github.com/alexandrestein/gods@v1.0.1/lists/arraylist/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 arraylist 6 7 import "github.com/alexandrestein/gods/containers" 8 9 func assertIteratorImplementation() { 10 var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) 11 } 12 13 // Iterator holding the iterator's state 14 type Iterator struct { 15 list *List 16 index int 17 } 18 19 // Iterator returns a stateful iterator whose values can be fetched by an index. 20 func (list *List) Iterator() Iterator { 21 return Iterator{list: list, index: -1} 22 } 23 24 // Next moves the iterator to the next element and returns true if there was a next element in the container. 25 // If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). 26 // If Next() was called for the first time, then it will point the iterator to the first element if it exists. 27 // Modifies the state of the iterator. 28 func (iterator *Iterator) Next() bool { 29 if iterator.index < iterator.list.size { 30 iterator.index++ 31 } 32 return iterator.list.withinRange(iterator.index) 33 } 34 35 // Prev moves the iterator to the previous element and returns true if there was a previous element in the container. 36 // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). 37 // Modifies the state of the iterator. 38 func (iterator *Iterator) Prev() bool { 39 if iterator.index >= 0 { 40 iterator.index-- 41 } 42 return iterator.list.withinRange(iterator.index) 43 } 44 45 // Value returns the current element's value. 46 // Does not modify the state of the iterator. 47 func (iterator *Iterator) Value() interface{} { 48 return iterator.list.elements[iterator.index] 49 } 50 51 // Index returns the current element's index. 52 // Does not modify the state of the iterator. 53 func (iterator *Iterator) 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) 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) End() { 66 iterator.index = iterator.list.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) 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) Last() bool { 81 iterator.End() 82 return iterator.Prev() 83 }