-
Notifications
You must be signed in to change notification settings - Fork 588
/
LookupByPath.ts
268 lines (247 loc) · 7.62 KB
/
LookupByPath.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
/**
* A node in the path tree used in LookupByPath
*/
interface IPathTreeNode<TItem> {
/**
* The value that exactly matches the current relative path
*/
value: TItem | undefined;
/**
* Child nodes by subfolder
*/
children: Map<string, IPathTreeNode<TItem>> | undefined;
}
interface IPrefixEntry {
prefix: string;
index: number;
}
/**
* Object containing both the matched item and the start index of the remainder of the query.
*
* @beta
*/
export interface IPrefixMatch<TItem> {
value: TItem;
index: number;
}
/**
* This class is used to associate POSIX relative paths, such as those returned by `git` commands,
* with entities that correspond with ancestor folders, such as Rush Projects.
*
* It is optimized for efficiently locating the nearest ancestor path with an associated value.
*
* @example
* ```ts
* const tree = new LookupByPath([['foo', 1], ['bar', 2], ['foo/bar', 3]]);
* tree.findChildPath('foo'); // returns 1
* tree.findChildPath('foo/baz'); // returns 1
* tree.findChildPath('baz'); // returns undefined
* tree.findChildPath('foo/bar/baz'); returns 3
* tree.findChildPath('bar/foo/bar'); returns 2
* ```
* @beta
*/
export class LookupByPath<TItem> {
/**
* The delimiter used to split paths
*/
public readonly delimiter: string;
/**
* The root node of the tree, corresponding to the path ''
*/
private readonly _root: IPathTreeNode<TItem>;
/**
* Constructs a new `LookupByPath`
*
* @param entries - Initial path-value pairs to populate the tree.
*/
public constructor(entries?: Iterable<[string, TItem]>, delimiter?: string) {
this._root = {
value: undefined,
children: undefined
};
this.delimiter = delimiter ?? '/';
if (entries) {
for (const [path, item] of entries) {
this.setItem(path, item);
}
}
}
/**
* Iterates over the segments of a serialized path.
*
* @example
*
* `LookupByPath.iteratePathSegments('foo/bar/baz')` yields 'foo', 'bar', 'baz'
*
* `LookupByPath.iteratePathSegments('foo\\bar\\baz', '\\')` yields 'foo', 'bar', 'baz'
*/
public static *iteratePathSegments(serializedPath: string, delimiter: string = '/'): Iterable<string> {
for (const prefixMatch of this._iteratePrefixes(serializedPath, delimiter)) {
yield prefixMatch.prefix;
}
}
private static *_iteratePrefixes(input: string, delimiter: string = '/'): Iterable<IPrefixEntry> {
if (!input) {
return;
}
let previousIndex: number = 0;
let nextIndex: number = input.indexOf(delimiter);
// Leading segments
while (nextIndex >= 0) {
yield {
prefix: input.slice(previousIndex, nextIndex),
index: nextIndex
};
previousIndex = nextIndex + 1;
nextIndex = input.indexOf(delimiter, previousIndex);
}
// Last segment
if (previousIndex < input.length) {
yield {
prefix: input.slice(previousIndex, input.length),
index: input.length
};
}
}
/**
* Associates the value with the specified serialized path.
* If a value is already associated, will overwrite.
*
* @returns this, for chained calls
*/
public setItem(serializedPath: string, value: TItem): this {
return this.setItemFromSegments(LookupByPath.iteratePathSegments(serializedPath, this.delimiter), value);
}
/**
* Associates the value with the specified path.
* If a value is already associated, will overwrite.
*
* @returns this, for chained calls
*/
public setItemFromSegments(pathSegments: Iterable<string>, value: TItem): this {
let node: IPathTreeNode<TItem> = this._root;
for (const segment of pathSegments) {
if (!node.children) {
node.children = new Map();
}
let child: IPathTreeNode<TItem> | undefined = node.children.get(segment);
if (!child) {
node.children.set(
segment,
(child = {
value: undefined,
children: undefined
})
);
}
node = child;
}
node.value = value;
return this;
}
/**
* Searches for the item associated with `childPath`, or the nearest ancestor of that path that
* has an associated item.
*
* @returns the found item, or `undefined` if no item was found
*
* @example
* ```ts
* const tree = new LookupByPath([['foo', 1], ['foo/bar', 2]]);
* tree.findChildPath('foo/baz'); // returns 1
* tree.findChildPath('foo/bar/baz'); // returns 2
* ```
*/
public findChildPath(childPath: string): TItem | undefined {
return this.findChildPathFromSegments(LookupByPath.iteratePathSegments(childPath, this.delimiter));
}
/**
* Searches for the item for which the recorded prefix is the longest matching prefix of `query`.
* Obtains both the item and the length of the matched prefix, so that the remainder of the path can be
* extracted.
*
* @returns the found item and the length of the matched prefix, or `undefined` if no item was found
*
* @example
* ```ts
* const tree = new LookupByPath([['foo', 1], ['foo/bar', 2]]);
* tree.findLongestPrefixMatch('foo/baz'); // returns { item: 1, index: 3 }
* tree.findLongestPrefixMatch('foo/bar/baz'); // returns { item: 2, index: 7 }
* ```
*/
public findLongestPrefixMatch(query: string): IPrefixMatch<TItem> | undefined {
return this._findLongestPrefixMatch(LookupByPath._iteratePrefixes(query, this.delimiter));
}
/**
* Searches for the item associated with `childPathSegments`, or the nearest ancestor of that path that
* has an associated item.
*
* @returns the found item, or `undefined` if no item was found
*
* @example
* ```ts
* const tree = new LookupByPath([['foo', 1], ['foo/bar', 2]]);
* tree.findChildPathFromSegments(['foo', 'baz']); // returns 1
* tree.findChildPathFromSegments(['foo','bar', 'baz']); // returns 2
* ```
*/
public findChildPathFromSegments(childPathSegments: Iterable<string>): TItem | undefined {
let node: IPathTreeNode<TItem> = this._root;
let best: TItem | undefined = node.value;
// Trivial cases
if (node.children) {
for (const segment of childPathSegments) {
const child: IPathTreeNode<TItem> | undefined = node.children.get(segment);
if (!child) {
break;
}
node = child;
best = node.value ?? best;
if (!node.children) {
break;
}
}
}
return best;
}
/**
* Iterates through progressively longer prefixes of a given string and returns as soon
* as the number of candidate items that match the prefix are 1 or 0.
*
* If a match is present, returns the matched itme and the length of the matched prefix.
*
* @returns the found item, or `undefined` if no item was found
*/
private _findLongestPrefixMatch(prefixes: Iterable<IPrefixEntry>): IPrefixMatch<TItem> | undefined {
let node: IPathTreeNode<TItem> = this._root;
let best: IPrefixMatch<TItem> | undefined = node.value
? {
value: node.value,
index: 0
}
: undefined;
// Trivial cases
if (node.children) {
for (const { prefix: hash, index } of prefixes) {
const child: IPathTreeNode<TItem> | undefined = node.children.get(hash);
if (!child) {
break;
}
node = child;
if (node.value !== undefined) {
best = {
value: node.value,
index
};
}
if (!node.children) {
break;
}
}
}
return best;
}
}