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