Skip to content

Commit

Permalink
don't use recursion for _entries, #124723
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jun 7, 2021
1 parent 692ffc0 commit 229f018
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
33 changes: 20 additions & 13 deletions src/vs/base/common/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,20 +498,27 @@ export class TernarySearchTree<K, V> {
}

private *_entries(node: TernarySearchTreeNode<K, V> | undefined): IterableIterator<[K, V]> {
if (node) {
// left
yield* this._entries(node.left);

// node
if (node.value) {
// callback(node.value, this._iter.join(parts));
yield [node.key, node.value];
// DFS
if (!node) {
return;
}
const stack = [node];
while (stack.length > 0) {
const node = stack.pop();
if (node) {
if (node.value) {
yield [node.key, node.value];
}
if (node.left) {
stack.push(node.left);
}
if (node.mid) {
stack.push(node.mid);
}
if (node.right) {
stack.push(node.right);
}
}
// mid
yield* this._entries(node.mid);

// right
yield* this._entries(node.right);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/vs/base/test/common/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ suite('Map', () => {

iter = map.findSuperstr(URI.file('/'))!;
item = iter.next();
assert.strictEqual(item.value[1], 4);
assert.strictEqual(item.done, false);
item = iter.next();
assert.strictEqual(item.value[1], 2);
assert.strictEqual(item.done, false);
item = iter.next();
Expand All @@ -771,9 +774,6 @@ suite('Map', () => {
assert.strictEqual(item.value[1], 3);
assert.strictEqual(item.done, false);
item = iter.next();
assert.strictEqual(item.value[1], 4);
assert.strictEqual(item.done, false);
item = iter.next();
assert.strictEqual(item.value, undefined);
assert.strictEqual(item.done, true);

Expand Down

0 comments on commit 229f018

Please sign in to comment.