From 44238191feb7ed38b3792594e3a019e43874ff50 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sat, 2 Mar 2019 07:21:16 -0700 Subject: [PATCH] Fix sorted map iterator initialization. --- immutable.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/immutable.go b/immutable.go index f9be606..2a8acb1 100644 --- a/immutable.go +++ b/immutable.go @@ -1585,31 +1585,37 @@ func (itr *SortedMapIterator) Done() bool { // First moves the iterator to the first key/value pair. func (itr *SortedMapIterator) First() { - if itr.m.root != nil { - itr.stack[0] = sortedMapIteratorElem{node: itr.m.root} - itr.depth = 0 - itr.first() + if itr.m.root == nil { + itr.depth = -1 + return } + itr.stack[0] = sortedMapIteratorElem{node: itr.m.root} + itr.depth = 0 + itr.first() } // Last moves the iterator to the last key/value pair. func (itr *SortedMapIterator) Last() { - if itr.m.root != nil { - itr.stack[0] = sortedMapIteratorElem{node: itr.m.root} - itr.depth = 0 - itr.last() + if itr.m.root == nil { + itr.depth = -1 + return } + itr.stack[0] = sortedMapIteratorElem{node: itr.m.root} + itr.depth = 0 + itr.last() } // Seek moves the iterator position to the given key in the map. // If the key does not exist then the next key is used. If no more keys exist // then the iteartor is marked as done. func (itr *SortedMapIterator) Seek(key interface{}) { - if itr.m.root != nil { - itr.stack[0] = sortedMapIteratorElem{node: itr.m.root} - itr.depth = 0 - itr.seek(key) + if itr.m.root == nil { + itr.depth = -1 + return } + itr.stack[0] = sortedMapIteratorElem{node: itr.m.root} + itr.depth = 0 + itr.seek(key) } // Next returns the current key/value pair and moves the iterator forward.