| 130 | >(2, (self, index) => at(self, index, Direction.Forward)) |
| 131 | |
| 132 | const at = <K, V>( |
| 133 | self: RBT.RedBlackTree<K, V>, |
| 134 | index: number, |
| 135 | direction: RBT.RedBlackTree.Direction |
| 136 | ): Iterable<[K, V]> => { |
| 137 | return { |
| 138 | [Symbol.iterator]: () => { |
| 139 | if (index < 0) { |
| 140 | return new RedBlackTreeIterator(self, [], direction) |
| 141 | } |
| 142 | let node = (self as RedBlackTreeImpl<K, V>)._root |
| 143 | const stack: Array<Node.Node<K, V>> = [] |
| 144 | while (node !== undefined) { |
| 145 | stack.push(node) |
| 146 | if (node.left !== undefined) { |
| 147 | if (index < node.left.count) { |
| 148 | node = node.left |
| 149 | continue |
| 150 | } |
| 151 | index -= node.left.count |
| 152 | } |
| 153 | if (!index) { |
| 154 | return new RedBlackTreeIterator(self, stack, direction) |
| 155 | } |
| 156 | index -= 1 |
| 157 | if (node.right !== undefined) { |
| 158 | if (index >= node.right.count) { |
| 159 | break |
| 160 | } |
| 161 | node = node.right |
| 162 | } else { |
| 163 | break |
| 164 | } |
| 165 | } |
| 166 | return new RedBlackTreeIterator(self, [], direction) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** @internal */ |
| 172 | export const findAll = dual< |