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