| 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/emirpasic/gods/v2/containers" |
| 9 | "github.com/emirpasic/gods/v2/lists/doublylinkedlist" |
| 10 | ) |
| 11 | |
| 12 | // Assert Iterator implementation |
| 13 | var _ containers.ReverseIteratorWithKey[string, int] = (*Iterator[string, int])(nil) |
| 14 | |
| 15 | // Iterator holding the iterator's state |
| 16 | type Iterator[K comparable, V any] struct { |
| 17 | iterator doublylinkedlist.Iterator[K] |
| 18 | table map[K]V |
| 19 | } |
| 20 | |
| 21 | // Iterator returns a stateful iterator whose elements are key/value pairs. |
| 22 | func (m *Map[K, V]) Iterator() *Iterator[K, V] { |
| 23 | return &Iterator[K, V]{ |
| 24 | iterator: m.ordering.Iterator(), |
| 25 | table: m.table, |
| 26 | } |
| 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[K, V]) 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[K, V]) 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[K, V]) Value() V { |
| 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[K, V]) Key() K { |
| 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[K, V]) 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[K, V]) 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[K, V]) 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[K, V]) Last() bool { |
| 80 | return iterator.iterator.Last() |
| 81 | } |
| 82 | |
| 83 | // NextTo moves the iterator to the next element from current position that satisfies the condition given by the |
| 84 | // passed function, and returns true if there was a next element in the container. |
| 85 | // If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value(). |
| 86 | // Modifies the state of the iterator. |
| 87 | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool { |
| 88 | for iterator.Next() { |
| 89 | key, value := iterator.Key(), iterator.Value() |
| 90 | if f(key, value) { |
| 91 | return true |
| 92 | } |
| 93 | } |
| 94 | return false |
| 95 | } |
| 96 | |
| 97 | // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the |
| 98 | // passed function, and returns true if there was a next element in the container. |
| 99 | // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value(). |
| 100 | // Modifies the state of the iterator. |
| 101 | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool { |
| 102 | for iterator.Prev() { |
| 103 | key, value := iterator.Key(), iterator.Value() |
| 104 | if f(key, value) { |
| 105 | return true |
| 106 | } |
| 107 | } |
| 108 | return false |
| 109 | } |