github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/database/internal/treap/treapiter.go (about) 1 // Copyright (c) 2015-2016 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package treap 7 8 import "bytes" 9 10 // Iterator represents an iterator for forwards and backwards iteration over 11 // the contents of a treap (mutable or immutable). 12 type Iterator struct { 13 t *Mutable // Mutable treap iterator is associated with or nil 14 root *treapNode // Root node of treap iterator is associated with 15 node *treapNode // The node the iterator is positioned at 16 parents parentStack // The stack of parents needed to iterate 17 isNew bool // Whether the iterator has been positioned 18 seekKey []byte // Used to handle dynamic updates for mutable treap 19 startKey []byte // Used to limit the iterator to a range 20 limitKey []byte // Used to limit the iterator to a range 21 } 22 23 // limitIterator clears the current iterator node if it is outside of the range 24 // specified when the iterator was created. It returns whether the iterator is 25 // valid. 26 func (iter *Iterator) limitIterator() bool { 27 if iter.node == nil { 28 return false 29 } 30 31 node := iter.node 32 if iter.startKey != nil && bytes.Compare(node.key, iter.startKey) < 0 { 33 iter.node = nil 34 return false 35 } 36 37 if iter.limitKey != nil && bytes.Compare(node.key, iter.limitKey) >= 0 { 38 iter.node = nil 39 return false 40 } 41 42 return true 43 } 44 45 // seek moves the iterator based on the provided key and flags. 46 // 47 // When the exact match flag is set, the iterator will either be moved to first 48 // key in the treap that exactly matches the provided key, or the one 49 // before/after it depending on the greater flag. 50 // 51 // When the exact match flag is NOT set, the iterator will be moved to the first 52 // key in the treap before/after the provided key depending on the greater flag. 53 // 54 // In all cases, the limits specified when the iterator was created are 55 // respected. 56 func (iter *Iterator) seek(key []byte, exactMatch bool, greater bool) bool { 57 iter.node = nil 58 iter.parents = parentStack{} 59 var selectedNodeDepth int 60 for node := iter.root; node != nil; { 61 iter.parents.Push(node) 62 63 // Traverse left or right depending on the result of the 64 // comparison. Also, set the iterator to the node depending on 65 // the flags so the iterator is positioned properly when an 66 // exact match isn't found. 67 compareResult := bytes.Compare(key, node.key) 68 if compareResult < 0 { 69 if greater { 70 iter.node = node 71 selectedNodeDepth = iter.parents.Len() - 1 72 } 73 node = node.left 74 continue 75 } 76 if compareResult > 0 { 77 if !greater { 78 iter.node = node 79 selectedNodeDepth = iter.parents.Len() - 1 80 } 81 node = node.right 82 continue 83 } 84 85 // The key is an exact match. Set the iterator and return now 86 // when the exact match flag is set. 87 if exactMatch { 88 iter.node = node 89 iter.parents.Pop() 90 return iter.limitIterator() 91 } 92 93 // The key is an exact match, but the exact match is not set, so 94 // choose which direction to go based on whether the larger or 95 // smaller key was requested. 96 if greater { 97 node = node.right 98 } else { 99 node = node.left 100 } 101 } 102 103 // There was either no exact match or there was an exact match but the 104 // exact match flag was not set. In any case, the parent stack might 105 // need to be adjusted to only include the parents up to the selected 106 // node. Also, ensure the selected node's key does not exceed the 107 // allowed range of the iterator. 108 for i := iter.parents.Len(); i > selectedNodeDepth; i-- { 109 iter.parents.Pop() 110 } 111 return iter.limitIterator() 112 } 113 114 // First moves the iterator to the first key/value pair. When there is only a 115 // single key/value pair both First and Last will point to the same pair. 116 // Returns false if there are no key/value pairs. 117 func (iter *Iterator) First() bool { 118 // Seek the start key if the iterator was created with one. This will 119 // result in either an exact match, the first greater key, or an 120 // exhausted iterator if no such key exists. 121 iter.isNew = false 122 if iter.startKey != nil { 123 return iter.seek(iter.startKey, true, true) 124 } 125 126 // The smallest key is in the left-most node. 127 iter.parents = parentStack{} 128 for node := iter.root; node != nil; node = node.left { 129 if node.left == nil { 130 iter.node = node 131 return true 132 } 133 iter.parents.Push(node) 134 } 135 return false 136 } 137 138 // Last moves the iterator to the last key/value pair. When there is only a 139 // single key/value pair both First and Last will point to the same pair. 140 // Returns false if there are no key/value pairs. 141 func (iter *Iterator) Last() bool { 142 // Seek the limit key if the iterator was created with one. This will 143 // result in the first key smaller than the limit key, or an exhausted 144 // iterator if no such key exists. 145 iter.isNew = false 146 if iter.limitKey != nil { 147 return iter.seek(iter.limitKey, false, false) 148 } 149 150 // The highest key is in the right-most node. 151 iter.parents = parentStack{} 152 for node := iter.root; node != nil; node = node.right { 153 if node.right == nil { 154 iter.node = node 155 return true 156 } 157 iter.parents.Push(node) 158 } 159 return false 160 } 161 162 // Next moves the iterator to the next key/value pair and returns false when the 163 // iterator is exhausted. When invoked on a newly created iterator it will 164 // position the iterator at the first item. 165 func (iter *Iterator) Next() bool { 166 if iter.isNew { 167 return iter.First() 168 } 169 170 if iter.node == nil { 171 return false 172 } 173 174 // Reseek the previous key without allowing for an exact match if a 175 // force seek was requested. This results in the key greater than the 176 // previous one or an exhausted iterator if there is no such key. 177 if seekKey := iter.seekKey; seekKey != nil { 178 iter.seekKey = nil 179 return iter.seek(seekKey, false, true) 180 } 181 182 // When there is no right node walk the parents until the parent's right 183 // node is not equal to the previous child. This will be the next node. 184 if iter.node.right == nil { 185 parent := iter.parents.Pop() 186 for parent != nil && parent.right == iter.node { 187 iter.node = parent 188 parent = iter.parents.Pop() 189 } 190 iter.node = parent 191 return iter.limitIterator() 192 } 193 194 // There is a right node, so the next node is the left-most node down 195 // the right sub-tree. 196 iter.parents.Push(iter.node) 197 iter.node = iter.node.right 198 for node := iter.node.left; node != nil; node = node.left { 199 iter.parents.Push(iter.node) 200 iter.node = node 201 } 202 return iter.limitIterator() 203 } 204 205 // Prev moves the iterator to the previous key/value pair and returns false when 206 // the iterator is exhausted. When invoked on a newly created iterator it will 207 // position the iterator at the last item. 208 func (iter *Iterator) Prev() bool { 209 if iter.isNew { 210 return iter.Last() 211 } 212 213 if iter.node == nil { 214 return false 215 } 216 217 // Reseek the previous key without allowing for an exact match if a 218 // force seek was requested. This results in the key smaller than the 219 // previous one or an exhausted iterator if there is no such key. 220 if seekKey := iter.seekKey; seekKey != nil { 221 iter.seekKey = nil 222 return iter.seek(seekKey, false, false) 223 } 224 225 // When there is no left node walk the parents until the parent's left 226 // node is not equal to the previous child. This will be the previous 227 // node. 228 for iter.node.left == nil { 229 parent := iter.parents.Pop() 230 for parent != nil && parent.left == iter.node { 231 iter.node = parent 232 parent = iter.parents.Pop() 233 } 234 iter.node = parent 235 return iter.limitIterator() 236 } 237 238 // There is a left node, so the previous node is the right-most node 239 // down the left sub-tree. 240 iter.parents.Push(iter.node) 241 iter.node = iter.node.left 242 for node := iter.node.right; node != nil; node = node.right { 243 iter.parents.Push(iter.node) 244 iter.node = node 245 } 246 return iter.limitIterator() 247 } 248 249 // Seek moves the iterator to the first key/value pair with a key that is 250 // greater than or equal to the given key and returns true if successful. 251 func (iter *Iterator) Seek(key []byte) bool { 252 iter.isNew = false 253 return iter.seek(key, true, true) 254 } 255 256 // Key returns the key of the current key/value pair or nil when the iterator 257 // is exhausted. The caller should not modify the contents of the returned 258 // slice. 259 func (iter *Iterator) Key() []byte { 260 if iter.node == nil { 261 return nil 262 } 263 return iter.node.key 264 } 265 266 // Value returns the value of the current key/value pair or nil when the 267 // iterator is exhausted. The caller should not modify the contents of the 268 // returned slice. 269 func (iter *Iterator) Value() []byte { 270 if iter.node == nil { 271 return nil 272 } 273 return iter.node.value 274 } 275 276 // Valid indicates whether the iterator is positioned at a valid key/value pair. 277 // It will be considered invalid when the iterator is newly created or exhausted. 278 func (iter *Iterator) Valid() bool { 279 return iter.node != nil 280 } 281 282 // ForceReseek notifies the iterator that the underlying mutable treap has been 283 // updated, so the next call to Prev or Next needs to reseek in order to allow 284 // the iterator to continue working properly. 285 // 286 // NOTE: Calling this function when the iterator is associated with an immutable 287 // treap has no effect as you would expect. 288 func (iter *Iterator) ForceReseek() { 289 // Nothing to do when the iterator is associated with an immutable 290 // treap. 291 if iter.t == nil { 292 return 293 } 294 295 // Update the iterator root to the mutable treap root in case it 296 // changed. 297 iter.root = iter.t.root 298 299 // Set the seek key to the current node. This will force the Next/Prev 300 // functions to reseek, and thus properly reconstruct the iterator, on 301 // their next call. 302 if iter.node == nil { 303 iter.seekKey = nil 304 return 305 } 306 iter.seekKey = iter.node.key 307 } 308 309 // Iterator returns a new iterator for the mutable treap. The newly returned 310 // iterator is not pointing to a valid item until a call to one of the methods 311 // to position it is made. 312 // 313 // The start key and limit key parameters cause the iterator to be limited to 314 // a range of keys. The start key is inclusive and the limit key is exclusive. 315 // Either or both can be nil if the functionality is not desired. 316 // 317 // WARNING: The ForceSeek method must be called on the returned iterator if 318 // the treap is mutated. Failure to do so will cause the iterator to return 319 // unexpected keys and/or values. 320 // 321 // For example: 322 // iter := t.Iterator(nil, nil) 323 // for iter.Next() { 324 // if someCondition { 325 // t.Delete(iter.Key()) 326 // iter.ForceReseek() 327 // } 328 // } 329 func (t *Mutable) Iterator(startKey, limitKey []byte) *Iterator { 330 iter := &Iterator{ 331 t: t, 332 root: t.root, 333 isNew: true, 334 startKey: startKey, 335 limitKey: limitKey, 336 } 337 return iter 338 } 339 340 // Iterator returns a new iterator for the immutable treap. The newly returned 341 // iterator is not pointing to a valid item until a call to one of the methods 342 // to position it is made. 343 // 344 // The start key and limit key parameters cause the iterator to be limited to 345 // a range of keys. The start key is inclusive and the limit key is exclusive. 346 // Either or both can be nil if the functionality is not desired. 347 func (t *Immutable) Iterator(startKey, limitKey []byte) *Iterator { 348 iter := &Iterator{ 349 root: t.root, 350 isNew: true, 351 startKey: startKey, 352 limitKey: limitKey, 353 } 354 return iter 355 }