Skip to content

Commit 3ecfa31

Browse files
committed
improve TrieMap in prep for microsoft#10583
1 parent fbdf234 commit 3ecfa31

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/vs/base/common/map.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ interface Node<E> {
310310
*/
311311
export class TrieMap<E> {
312312

313-
static PathSplitter = s => s.split(/[\\/]/);
313+
static PathSplitter = s => s.split(/[\\/]/).filter(s => !!s);
314314

315315
private _splitter: (s: string) => string[];
316316
private _root: Node<E> = { children: Object.create(null) };
@@ -345,6 +345,22 @@ export class TrieMap<E> {
345345
node.element = element;
346346
}
347347

348+
lookUp(path: string): E {
349+
const parts = this._splitter(path);
350+
351+
let {children} = this._root;
352+
let node: Node<E>;
353+
for (const part of parts) {
354+
node = children[part];
355+
if (!node) {
356+
return;
357+
}
358+
children = node.children;
359+
}
360+
361+
return node.element;
362+
}
363+
348364
findSubstr(path: string): E {
349365
const parts = this._splitter(path);
350366

@@ -367,4 +383,22 @@ export class TrieMap<E> {
367383
return lastNode.element;
368384
}
369385
}
386+
387+
findSuperstr(path: string): TrieMap<E> {
388+
const parts = this._splitter(path);
389+
390+
let {children} = this._root;
391+
let node: Node<E>;
392+
for (const part of parts) {
393+
node = children[part];
394+
if (!node) {
395+
return;
396+
}
397+
children = node.children;
398+
}
399+
400+
const result = new TrieMap<E>(this._splitter);
401+
result._root = node;
402+
return result;
403+
}
370404
}

src/vs/base/test/common/map.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,34 @@ suite('Map', () => {
290290
assert.equal(map.findSubstr('/user/foo/bar/far/boo'), 1);
291291

292292
});
293+
294+
test('TrieMap - lookup', function () {
295+
296+
const map = new TrieMap<number>(TrieMap.PathSplitter);
297+
map.insert('/user/foo/bar', 1);
298+
map.insert('/user/foo', 2);
299+
map.insert('/user/foo/flip/flop', 3);
300+
301+
assert.equal(map.lookUp('/foo'), undefined);
302+
assert.equal(map.lookUp('/user'), undefined);
303+
assert.equal(map.lookUp('/user/foo'), 2);
304+
assert.equal(map.lookUp('/user/foo/bar'), 1);
305+
assert.equal(map.lookUp('/user/foo/bar/boo'), undefined);
306+
});
307+
308+
test('TrieMap - superstr', function () {
309+
310+
const map = new TrieMap<number>(TrieMap.PathSplitter);
311+
map.insert('/user/foo/bar', 1);
312+
map.insert('/user/foo', 2);
313+
map.insert('/user/foo/flip/flop', 3);
314+
315+
const supMap = map.findSuperstr('/user');
316+
317+
assert.equal(supMap.lookUp('foo'), 2);
318+
assert.equal(supMap.lookUp('foo/bar'), 1);
319+
assert.equal(supMap.lookUp('foo/flip/flop'), 3);
320+
assert.equal(supMap.lookUp('foo/flip/flop/bar'), undefined);
321+
assert.equal(supMap.lookUp('user'), undefined);
322+
});
293323
});

0 commit comments

Comments
 (0)