github.com/mymmsc/gox@v1.3.33/util/linkedhashmap/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 linkedhashmap 6 7 import ( 8 "github.com/mymmsc/gox/util" 9 "github.com/mymmsc/gox/util/doublylinkedlist" 10 ) 11 12 func assertIteratorImplementation() { 13 var _ util.ReverseIteratorWithKey = (*Iterator)(nil) 14 } 15 16 // Iterator holding the iterator's state 17 type Iterator struct { 18 iterator doublylinkedlist.Iterator 19 table map[interface{}]interface{} 20 } 21 22 // Iterator returns a stateful iterator whose elements are key/value pairs. 23 func (m *Map) Iterator() Iterator { 24 return Iterator{ 25 iterator: m.ordering.Iterator(), 26 table: m.table} 27 } 28 29 // Next moves the iterator to the next element and returns true if there was a next element in the container. 30 // If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). 31 // If Next() was called for the first time, then it will point the iterator to the first element if it exists. 32 // Modifies the state of the iterator. 33 func (iterator *Iterator) Next() bool { 34 return iterator.iterator.Next() 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 key and value can be retrieved by Key() and Value(). 39 // Modifies the state of the iterator. 40 func (iterator *Iterator) Prev() bool { 41 return iterator.iterator.Prev() 42 } 43 44 // Value returns the current element's value. 45 // Does not modify the state of the iterator. 46 func (iterator *Iterator) Value() interface{} { 47 key := iterator.iterator.Value() 48 return iterator.table[key] 49 } 50 51 // Key returns the current element's key. 52 // Does not modify the state of the iterator. 53 func (iterator *Iterator) Key() interface{} { 54 return iterator.iterator.Value() 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.iterator.Begin() 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.iterator.End() 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 key and value can be retrieved by Key() and Value(). 71 // Modifies the state of the iterator 72 func (iterator *Iterator) First() bool { 73 return iterator.iterator.First() 74 } 75 76 // Last moves the iterator to the last element and returns true if there was a last element in the container. 77 // If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). 78 // Modifies the state of the iterator. 79 func (iterator *Iterator) Last() bool { 80 return iterator.iterator.Last() 81 }