| 12 | |
| 13 | /** @internal */ |
| 14 | export class RedBlackTreeIterator<in out K, out V> implements Iterator<[K, V]> { |
| 15 | private count = 0 |
| 16 | |
| 17 | constructor( |
| 18 | readonly self: RBT.RedBlackTree<K, V>, |
| 19 | readonly stack: Array<Node.Node<K, V>>, |
| 20 | readonly direction: RBT.RedBlackTree.Direction |
| 21 | ) {} |
| 22 | |
| 23 | /** |
| 24 | * Clones the iterator |
| 25 | */ |
| 26 | clone(): RedBlackTreeIterator<K, V> { |
| 27 | return new RedBlackTreeIterator(this.self, this.stack.slice(), this.direction) |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Reverse the traversal direction |
| 32 | */ |
| 33 | reversed(): RedBlackTreeIterator<K, V> { |
| 34 | return new RedBlackTreeIterator( |
| 35 | this.self, |
| 36 | this.stack.slice(), |
| 37 | this.direction === Direction.Forward ? Direction.Backward : Direction.Forward |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Iterator next |
| 43 | */ |
| 44 | next(): IteratorResult<[K, V], number> { |
| 45 | const entry = this.entry |
| 46 | this.count++ |
| 47 | if (this.direction === Direction.Forward) { |
| 48 | this.moveNext() |
| 49 | } else { |
| 50 | this.movePrev() |
| 51 | } |
| 52 | switch (entry._tag) { |
| 53 | case "None": { |
| 54 | return { done: true, value: this.count } |
| 55 | } |
| 56 | case "Some": { |
| 57 | return { done: false, value: entry.value } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the key |
| 64 | */ |
| 65 | get key(): Option.Option<K> { |
| 66 | if (this.stack.length > 0) { |
| 67 | return Option.some(this.stack[this.stack.length - 1]!.key) |
| 68 | } |
| 69 | return Option.none() |
| 70 | } |
| 71 |
nothing calls this directly
no outgoing calls
no test coverage detected