mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-03 04:21:53 +08:00
Perf: re-implement trie using Map
This commit is contained in:
@@ -56,7 +56,7 @@ describe('Trie', () => {
|
|||||||
|
|
||||||
expect(trie.delete('rate')).toBeTrue();
|
expect(trie.delete('rate')).toBeTrue();
|
||||||
expect(trie.size).toBe(1);
|
expect(trie.size).toBe(1);
|
||||||
expect(trie.delete('tar')).toBe(true);
|
expect(trie.delete('tar')).toBeTrue();
|
||||||
expect(trie.size).toBe(0);
|
expect(trie.size).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,17 +2,18 @@
|
|||||||
* Suffix Trie based on Mnemonist Trie
|
* Suffix Trie based on Mnemonist Trie
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const SENTINEL = '\u0000';
|
export const SENTINEL = Symbol('SENTINEL');
|
||||||
|
|
||||||
type TrieNode = {
|
type TrieNode = {
|
||||||
[SENTINEL]: boolean
|
[SENTINEL]: boolean
|
||||||
} & {
|
} & Map<string, TrieNode>;
|
||||||
[key: string & {}]: TrieNode | undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
const createNode = (): TrieNode => ({
|
const createNode = (): TrieNode => {
|
||||||
[SENTINEL]: false
|
const map = new Map<string, TrieNode>();
|
||||||
}) as TrieNode;
|
const node = map as TrieNode;
|
||||||
|
node[SENTINEL] = false;
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
|
||||||
export const createTrie = (from?: string[] | Set<string>) => {
|
export const createTrie = (from?: string[] | Set<string>) => {
|
||||||
let size = 0;
|
let size = 0;
|
||||||
@@ -26,31 +27,37 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
let token: string;
|
let token: string;
|
||||||
for (let i = suffix.length - 1; i >= 0; i--) {
|
for (let i = suffix.length - 1; i >= 0; i--) {
|
||||||
token = suffix[i];
|
token = suffix[i];
|
||||||
if (!(token in node)) {
|
|
||||||
node[token] = createNode();
|
if (node.has(token)) {
|
||||||
|
node = node.get(token)!;
|
||||||
|
} else {
|
||||||
|
const newNode = createNode();
|
||||||
|
node.set(token, newNode);
|
||||||
|
node = newNode;
|
||||||
}
|
}
|
||||||
node = node[token]!; // we know it is defined
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do we need to increase size?
|
// Do we need to increase size?
|
||||||
if (!node[SENTINEL]) {
|
if (!node[SENTINEL]) {
|
||||||
size++;
|
size++;
|
||||||
|
node[SENTINEL] = true;
|
||||||
}
|
}
|
||||||
node[SENTINEL] = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} suffix
|
* @param {string} suffix
|
||||||
*/
|
*/
|
||||||
const contains = (suffix: string): boolean => {
|
const contains = (suffix: string): boolean => {
|
||||||
let node: TrieNode = root;
|
let node: TrieNode | undefined = root;
|
||||||
let token: string;
|
let token: string;
|
||||||
|
|
||||||
for (let i = suffix.length - 1; i >= 0; i--) {
|
for (let i = suffix.length - 1; i >= 0; i--) {
|
||||||
token = suffix[i];
|
token = suffix[i];
|
||||||
|
|
||||||
if (!(token in node)) return false;
|
node = node.get(token);
|
||||||
node = node[token]!; // we know it is defined
|
if (!node) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -58,42 +65,39 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
/**
|
/**
|
||||||
* Method used to retrieve every item in the trie with the given prefix.
|
* Method used to retrieve every item in the trie with the given prefix.
|
||||||
*/
|
*/
|
||||||
const find = (suffix: string, /** @default true */ includeEqualWithSuffix = true): string[] => {
|
const find = (inputSuffix: string, /** @default true */ includeEqualWithSuffix = true): string[] => {
|
||||||
let node: TrieNode = root;
|
let node: TrieNode | undefined = root;
|
||||||
let token: string;
|
let token: string;
|
||||||
|
|
||||||
for (let i = suffix.length - 1; i >= 0; i--) {
|
for (let i = inputSuffix.length - 1; i >= 0; i--) {
|
||||||
token = suffix[i];
|
token = inputSuffix[i];
|
||||||
|
|
||||||
if (!(token in node)) return [];
|
node = node.get(token);
|
||||||
node = node[token]!;
|
if (!node) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const matches: string[] = [];
|
const matches: string[] = [];
|
||||||
|
|
||||||
// Performing DFS from prefix
|
// Performing DFS from prefix
|
||||||
const nodeStack: TrieNode[] = [node];
|
const nodeStack: TrieNode[] = [node];
|
||||||
|
const suffixStack: string[] = [inputSuffix];
|
||||||
const suffixStack: string[] = [suffix];
|
|
||||||
let k: string;
|
|
||||||
|
|
||||||
let $suffix: string = suffix;
|
|
||||||
|
|
||||||
while (nodeStack.length) {
|
while (nodeStack.length) {
|
||||||
$suffix = suffixStack.pop()!;
|
const suffix = suffixStack.pop()!;
|
||||||
node = nodeStack.pop()!;
|
node = nodeStack.pop()!;
|
||||||
|
|
||||||
if (node[SENTINEL]) {
|
if (node[SENTINEL]) {
|
||||||
if (includeEqualWithSuffix || $suffix !== suffix) {
|
if (includeEqualWithSuffix || suffix !== inputSuffix) {
|
||||||
matches.push($suffix);
|
matches.push(suffix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (k in node) {
|
node.forEach((childNode, k) => {
|
||||||
if (k === SENTINEL) continue;
|
nodeStack.push(childNode);
|
||||||
nodeStack.push(node[k]!);
|
suffixStack.push(k + suffix);
|
||||||
suffixStack.push(k + $suffix);
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches;
|
return matches;
|
||||||
@@ -103,7 +107,7 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
* Method used to delete a prefix from the trie.
|
* Method used to delete a prefix from the trie.
|
||||||
*/
|
*/
|
||||||
const remove = (suffix: string): boolean => {
|
const remove = (suffix: string): boolean => {
|
||||||
let node: TrieNode = root;
|
let node: TrieNode | undefined = root;
|
||||||
let toPrune: TrieNode | null = null;
|
let toPrune: TrieNode | null = null;
|
||||||
let tokenToPrune: string | null = null;
|
let tokenToPrune: string | null = null;
|
||||||
let parent: TrieNode = node;
|
let parent: TrieNode = node;
|
||||||
@@ -113,31 +117,23 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
token = suffix[i];
|
token = suffix[i];
|
||||||
parent = node;
|
parent = node;
|
||||||
|
|
||||||
// Prefix does not exist
|
node = node.get(token);
|
||||||
if (!(token in node)) return false;
|
if (!node) {
|
||||||
// if (n === true) return false
|
return false;
|
||||||
node = node[token]!; // we know it is defined
|
}
|
||||||
|
|
||||||
// Keeping track of a potential branch to prune
|
// Keeping track of a potential branch to prune
|
||||||
// If the node is to be pruned, but they are more than one token child in it, we can't prune it
|
// If the node is to be pruned, but they are more than one token child in it, we can't prune it
|
||||||
// If there is only one token child, or no child at all, we can prune it safely
|
// If there is only one token child, or no child at all, we can prune it safely
|
||||||
|
|
||||||
let onlyChild = true;
|
const onlyChild = node.size === 1 && node.has(token);
|
||||||
for (const k in node) {
|
|
||||||
if (k !== token) {
|
|
||||||
onlyChild = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toPrune !== null) {
|
if (onlyChild) {
|
||||||
if (!onlyChild) {
|
|
||||||
toPrune = null;
|
|
||||||
tokenToPrune = null;
|
|
||||||
}
|
|
||||||
} else if (onlyChild) {
|
|
||||||
toPrune = parent;
|
toPrune = parent;
|
||||||
tokenToPrune = token;
|
tokenToPrune = token;
|
||||||
|
} else if (toPrune !== null) { // not only child, retain the branch
|
||||||
|
toPrune = null;
|
||||||
|
tokenToPrune = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +142,7 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
size--;
|
size--;
|
||||||
|
|
||||||
if (tokenToPrune && toPrune) {
|
if (tokenToPrune && toPrune) {
|
||||||
delete toPrune[tokenToPrune];
|
toPrune.delete(tokenToPrune);
|
||||||
} else {
|
} else {
|
||||||
node[SENTINEL] = false;
|
node[SENTINEL] = false;
|
||||||
}
|
}
|
||||||
@@ -162,8 +158,12 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
|
|
||||||
for (let i = suffix.length - 1; i >= 0; i--) {
|
for (let i = suffix.length - 1; i >= 0; i--) {
|
||||||
const token = suffix[i];
|
const token = suffix[i];
|
||||||
if (!(token in node)) return false;
|
|
||||||
node = node[token]!; // we know it is defined
|
if (node.has(token)) {
|
||||||
|
node = node.get(token)!;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return node[SENTINEL];
|
return node[SENTINEL];
|
||||||
|
|||||||
Reference in New Issue
Block a user