github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/linkedhashmap/iterator.go (about) 1 package linkedhashmap 2 3 import ( 4 "github.com/songzhibin97/go-baseutils/structure/lists/doublylinkedlist" 5 ) 6 7 // Iterator holding the iterator's state 8 type Iterator[K comparable, V any] struct { 9 iterator doublylinkedlist.Iterator[K] 10 table map[K]V 11 } 12 13 // Iterator returns a stateful iterator whose elements are key/value pairs. 14 func (m *Map[K, V]) Iterator() Iterator[K, V] { 15 return Iterator[K, V]{ 16 iterator: m.ordering.Iterator(), 17 table: m.table, 18 } 19 } 20 21 // Next moves the iterator to the next element and returns true if there was a next element in the container. 22 // If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). 23 // If Next() was called for the first time, then it will point the iterator to the first element if it exists. 24 // Modifies the state of the iterator. 25 func (iterator *Iterator[K, V]) Next() bool { 26 return iterator.iterator.Next() 27 } 28 29 // Prev moves the iterator to the previous element and returns true if there was a previous element in the container. 30 // If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value(). 31 // Modifies the state of the iterator. 32 func (iterator *Iterator[K, V]) Prev() bool { 33 return iterator.iterator.Prev() 34 } 35 36 // Value returns the current element's value. 37 // Does not modify the state of the iterator. 38 func (iterator *Iterator[K, V]) Value() V { 39 key := iterator.iterator.Value() 40 return iterator.table[key] 41 } 42 43 // Key returns the current element's key. 44 // Does not modify the state of the iterator. 45 func (iterator *Iterator[K, V]) Key() K { 46 return iterator.iterator.Value() 47 } 48 49 // Begin resets the iterator to its initial state (one-before-first) 50 // Call Next() to fetch the first element if any. 51 func (iterator *Iterator[K, V]) Begin() { 52 iterator.iterator.Begin() 53 } 54 55 // End moves the iterator past the last element (one-past-the-end). 56 // Call Prev() to fetch the last element if any. 57 func (iterator *Iterator[K, V]) End() { 58 iterator.iterator.End() 59 } 60 61 // First moves the iterator to the first element and returns true if there was a first element in the container. 62 // If First() returns true, then first element's key and value can be retrieved by Key() and Value(). 63 // Modifies the state of the iterator 64 func (iterator *Iterator[K, V]) First() bool { 65 return iterator.iterator.First() 66 } 67 68 // Last moves the iterator to the last element and returns true if there was a last element in the container. 69 // If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). 70 // Modifies the state of the iterator. 71 func (iterator *Iterator[K, V]) Last() bool { 72 return iterator.iterator.Last() 73 } 74 75 // NextTo moves the iterator to the next element from current position that satisfies the condition given by the 76 // passed function, and returns true if there was a next element in the container. 77 // If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value(). 78 // Modifies the state of the iterator. 79 func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool { 80 for iterator.Next() { 81 key, value := iterator.Key(), iterator.Value() 82 if f(key, value) { 83 return true 84 } 85 } 86 return false 87 } 88 89 // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the 90 // passed function, and returns true if there was a next element in the container. 91 // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value(). 92 // Modifies the state of the iterator. 93 func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool { 94 for iterator.Prev() { 95 key, value := iterator.Key(), iterator.Value() 96 if f(key, value) { 97 return true 98 } 99 } 100 return false 101 }