| 96 | } |
| 97 | |
| 98 | class TrieIterator<in out V, out T> implements IterableIterator<T> { |
| 99 | stack: Array<[Node<V>, string, boolean]> = [] |
| 100 | firstEntry: Entry<V> | undefined |
| 101 | readonly f: TraversalMap<string, V, T> |
| 102 | readonly prefix: string |
| 103 | |
| 104 | constructor( |
| 105 | trie: TrieImpl<V>, |
| 106 | f: TraversalMap<string, V, T>, |
| 107 | prefix = "" |
| 108 | ) { |
| 109 | this.f = f |
| 110 | this.prefix = prefix |
| 111 | let root = trie._root |
| 112 | if (prefix === "") { |
| 113 | this.firstEntry = trie._empty |
| 114 | } else { |
| 115 | const match = findNode(root, prefix) |
| 116 | this.firstEntry = match?.value |
| 117 | root = match?.mid |
| 118 | } |
| 119 | if (root !== undefined) { |
| 120 | this.stack.push([root, prefix, false]) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | next(): IteratorResult<T> { |
| 125 | const first = this.firstEntry |
| 126 | if (first !== undefined) { |
| 127 | this.firstEntry = undefined |
| 128 | return { done: false, value: this.f(this.prefix, first.value) } |
| 129 | } |
| 130 | while (this.stack.length > 0) { |
| 131 | const [node, keyString, isAdded] = this.stack.pop()! |
| 132 | |
| 133 | if (isAdded) { |
| 134 | const value = node.value |
| 135 | if (value !== undefined) { |
| 136 | const key = keyString + node.key |
| 137 | return { done: false, value: this.f(key, value.value) } |
| 138 | } |
| 139 | } else { |
| 140 | this.addToStack(node, keyString) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return { done: true, value: undefined } |
| 145 | } |
| 146 | |
| 147 | addToStack(node: Node<V>, keyString: string) { |
| 148 | if (node.right !== undefined) { |
| 149 | this.stack.push([node.right, keyString, false]) |
| 150 | } |
| 151 | if (node.mid !== undefined) { |
| 152 | this.stack.push([node.mid, keyString + node.key, false]) |
| 153 | } |
| 154 | this.stack.push([node, keyString, true]) |
| 155 | if (node.left !== undefined) { |
nothing calls this directly
no outgoing calls
no test coverage detected