Prev returns the current key/value pair and moves the iterator backward. Returns a nil key if the there are no more elements to return.
()
| 2129 | // Prev returns the current key/value pair and moves the iterator backward. |
| 2130 | // Returns a nil key if the there are no more elements to return. |
| 2131 | func (itr *SortedMapIterator[K, V]) Prev() (key K, value V, ok bool) { |
| 2132 | // Return nil key if iteration is complete. |
| 2133 | if itr.Done() { |
| 2134 | return key, value, false |
| 2135 | } |
| 2136 | |
| 2137 | // Retrieve current key/value pair. |
| 2138 | leafElem := &itr.stack[itr.depth] |
| 2139 | leafNode := leafElem.node.(*sortedMapLeafNode[K, V]) |
| 2140 | leafEntry := &leafNode.entries[leafElem.index] |
| 2141 | key, value = leafEntry.key, leafEntry.value |
| 2142 | |
| 2143 | itr.prev() |
| 2144 | return key, value, true |
| 2145 | } |
| 2146 | |
| 2147 | // prev moves to the previous key. If no keys are before then depth is set to -1. |
| 2148 | func (itr *SortedMapIterator[K, V]) prev() { |