Refactor: replace BFS w/ DFS [skip ci]

This commit is contained in:
SukkaW 2025-02-06 22:30:34 +08:00
parent 39d57cc5c1
commit 92db009066
2 changed files with 49 additions and 51 deletions

View File

@ -125,12 +125,12 @@ describe('Trie', () => {
trie.add('cdn.example.com'); trie.add('cdn.example.com');
trie.add('example.org'); trie.add('example.org');
expect(trie.find('example.com')).toStrictEqual(['example.com', 'blog.example.com', 'cdn.example.com']); expect(trie.find('example.com')).toStrictEqual(['example.com', 'cdn.example.com', 'blog.example.com']);
expect(trie.find('com')).toStrictEqual(['example.com', 'blog.example.com', 'cdn.example.com']); expect(trie.find('com')).toStrictEqual(['example.com', 'cdn.example.com', 'blog.example.com']);
expect(trie.find('.example.com')).toStrictEqual(['blog.example.com', 'cdn.example.com']); expect(trie.find('.example.com')).toStrictEqual(['cdn.example.com', 'blog.example.com']);
expect(trie.find('org')).toStrictEqual(['example.org']); expect(trie.find('org')).toStrictEqual(['example.org']);
expect(trie.find('example.net')).toStrictEqual([]); expect(trie.find('example.net')).toStrictEqual([]);
expect(trie.dump()).toStrictEqual(['example.com', 'example.org', 'blog.example.com', 'cdn.example.com']); expect(trie.dump()).toStrictEqual(['example.org', 'example.com', 'cdn.example.com', 'blog.example.com']);
}); });
it('should be possible to retrieve items matching the given prefix even with a smol trie', () => { it('should be possible to retrieve items matching the given prefix even with a smol trie', () => {
@ -147,7 +147,7 @@ describe('Trie', () => {
expect(trie.find('.example.com')).toStrictEqual(['.example.com']); expect(trie.find('.example.com')).toStrictEqual(['.example.com']);
expect(trie.find('org')).toStrictEqual(['example.org']); expect(trie.find('org')).toStrictEqual(['example.org']);
expect(trie.find('example.net')).toStrictEqual([]); expect(trie.find('example.net')).toStrictEqual([]);
expect(trie.dump()).toStrictEqual(['.example.com', 'example.org']); expect(trie.dump()).toStrictEqual(['example.org', '.example.com']);
}); });
it('should be possible to create a trie from an arbitrary iterable.', () => { it('should be possible to create a trie from an arbitrary iterable.', () => {
@ -173,14 +173,14 @@ describe('surge domainset dedupe', () => {
it('should match subdomain - 1', () => { it('should match subdomain - 1', () => {
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false); const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false);
expect(trie.find('.skk.moe')).toStrictEqual(['blog.skk.moe', 'image.cdn.skk.moe']); expect(trie.find('.skk.moe')).toStrictEqual(['image.cdn.skk.moe', 'blog.skk.moe']);
expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']); expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
}); });
it('should match subdomain - 2', () => { it('should match subdomain - 2', () => {
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false); const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false);
expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe']); expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']); expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
}); });
@ -203,13 +203,13 @@ describe('smol tree', () => {
], true); ], true);
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'skk.moe',
'anotherskk.moe',
'.cdn.local',
'blog.skk.moe',
'blog.anotherskk.moe',
'img.skk.local', 'img.skk.local',
'blog.img.skk.local' 'blog.img.skk.local',
'.cdn.local',
'anotherskk.moe',
'blog.anotherskk.moe',
'skk.moe',
'blog.skk.moe'
]); ]);
}); });
@ -221,10 +221,10 @@ describe('smol tree', () => {
], true); ], true);
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'.skk.moe', '.sub.example.com',
'www.noc.one',
'cdn.noc.one', 'cdn.noc.one',
'.sub.example.com' 'www.noc.one',
'.skk.moe'
]); ]);
}); });
@ -262,10 +262,10 @@ describe('smol tree', () => {
], true); ], true);
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'commercial.shouji.360.cn',
'cdn.creative.medialytics.com', 'cdn.creative.medialytics.com',
'act.commercial.shouji.360.cn', 'px.cdn.creative.medialytics.com',
'px.cdn.creative.medialytics.com' 'commercial.shouji.360.cn',
'act.commercial.shouji.360.cn'
]); ]);
}); });
@ -278,10 +278,10 @@ describe('smol tree', () => {
], true); ], true);
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'skk.moe',
'anotherskk.moe', 'anotherskk.moe',
'blog.skk.moe', 'blog.anotherskk.moe',
'blog.anotherskk.moe' 'skk.moe',
'blog.skk.moe'
]); ]);
}); });
@ -299,34 +299,34 @@ describe('smol tree', () => {
trie.whitelist('.skk.moe'); trie.whitelist('.skk.moe');
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'anotherskk.moe',
'.cdn.local',
'blog.anotherskk.moe',
'img.skk.local', 'img.skk.local',
'blog.img.skk.local' 'blog.img.skk.local',
'.cdn.local',
'anotherskk.moe',
'blog.anotherskk.moe'
]); ]);
trie.whitelist('anotherskk.moe'); trie.whitelist('anotherskk.moe');
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'.cdn.local',
'blog.anotherskk.moe',
'img.skk.local', 'img.skk.local',
'blog.img.skk.local' 'blog.img.skk.local',
'.cdn.local',
'blog.anotherskk.moe'
]); ]);
trie.add('anotherskk.moe'); trie.add('anotherskk.moe');
trie.whitelist('.anotherskk.moe'); trie.whitelist('.anotherskk.moe');
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'.cdn.local',
'img.skk.local', 'img.skk.local',
'blog.img.skk.local' 'blog.img.skk.local',
'.cdn.local'
]); ]);
trie.whitelist('img.skk.local'); trie.whitelist('img.skk.local');
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'.cdn.local', 'blog.img.skk.local',
'blog.img.skk.local' '.cdn.local'
]); ]);
trie.whitelist('cdn.local'); trie.whitelist('cdn.local');
@ -349,15 +349,14 @@ describe('smol tree', () => {
], true); ], true);
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'.t.co', 'cdn.example.com', 'blog.cdn.example.com',
'.skk.moe', '.skk.moe',
'cdn.example.com', 'blog.cdn.example.com' '.t.co'
]); ]);
trie.whitelist('.t.co'); trie.whitelist('.t.co');
expect(trie.dump()).toStrictEqual([ expect(trie.dump()).toStrictEqual([
'.skk.moe', 'cdn.example.com', 'blog.cdn.example.com', '.skk.moe'
'cdn.example.com', 'blog.cdn.example.com'
]); ]);
trie.whitelist('skk.moe'); trie.whitelist('skk.moe');

View File

@ -6,7 +6,6 @@ import { fastStringCompare } from './misc';
import util from 'node:util'; import util from 'node:util';
import { noop } from 'foxts/noop'; import { noop } from 'foxts/noop';
import { fastStringArrayJoin } from 'foxts/fast-string-array-join'; import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
import FIFO from 'fast-fifo';
import { deleteBit, getBit, missingBit, setBit } from 'foxts/bitwise'; import { deleteBit, getBit, missingBit, setBit } from 'foxts/bitwise';
@ -193,9 +192,9 @@ abstract class Triebase<Meta = unknown> {
private static bfsResults: [node: TrieNode | null, suffix: string[]] = [null, []]; private static bfsResults: [node: TrieNode | null, suffix: string[]] = [null, []];
private static bfs<Meta>(this: void, nodeStack: FIFO<TrieNode<Meta>>, suffixStack: FIFO<string[]>) { private static dfs<Meta>(this: void, nodeStack: Array<TrieNode<Meta>>, suffixStack: string[][]) {
const node = nodeStack.shift()!; const node = nodeStack.pop()!;
const suffix = suffixStack.shift()!; const suffix = suffixStack.pop()!;
node[2].forEach((childNode, k) => { node[2].forEach((childNode, k) => {
// Pushing the child node to the stack for next iteration of DFS // Pushing the child node to the stack for next iteration of DFS
@ -210,9 +209,9 @@ abstract class Triebase<Meta = unknown> {
return Triebase.bfsResults; return Triebase.bfsResults;
} }
private static bfsWithSort<Meta>(this: void, nodeStack: FIFO<TrieNode<Meta>>, suffixStack: FIFO<string[]>) { private static dfsWithSort<Meta>(this: void, nodeStack: Array<TrieNode<Meta>>, suffixStack: string[][]) {
const node = nodeStack.shift()!; const node = nodeStack.pop()!;
const suffix = suffixStack.shift()!; const suffix = suffixStack.pop()!;
const child = node[2]; const child = node[2];
@ -241,13 +240,13 @@ abstract class Triebase<Meta = unknown> {
initialSuffix: string[] = [], initialSuffix: string[] = [],
withSort = false withSort = false
) { ) {
const bfsImpl = withSort ? Triebase.bfsWithSort : Triebase.bfs; const bfsImpl = withSort ? Triebase.dfsWithSort : Triebase.dfs;
const nodeStack = new FIFO<TrieNode<Meta>>(); const nodeStack: Array<TrieNode<Meta>> = [];
nodeStack.push(initialNode); nodeStack.push(initialNode);
// Resolving initial string (begin the start of the stack) // Resolving initial string (begin the start of the stack)
const suffixStack = new FIFO<string[]>(); const suffixStack: string[][] = [];
suffixStack.push(initialSuffix); suffixStack.push(initialSuffix);
let node: TrieNode<Meta> = initialNode; let node: TrieNode<Meta> = initialNode;
@ -275,19 +274,19 @@ abstract class Triebase<Meta = unknown> {
initialNode = this.$root, initialNode = this.$root,
initialSuffix: string[] = [] initialSuffix: string[] = []
) { ) {
const nodeStack = new FIFO<TrieNode<Meta>>(); const nodeStack: Array<TrieNode<Meta>> = [];
nodeStack.push(initialNode); nodeStack.push(initialNode);
// Resolving initial string (begin the start of the stack) // Resolving initial string (begin the start of the stack)
const suffixStack = new FIFO<string[]>(); const suffixStack: string[][] = [];
suffixStack.push(initialSuffix); suffixStack.push(initialSuffix);
let node: TrieNode<Meta> = initialNode; let node: TrieNode<Meta> = initialNode;
let child: Map<string, TrieNode<Meta>> = node[2]; let child: Map<string, TrieNode<Meta>> = node[2];
do { do {
node = nodeStack.shift()!; node = nodeStack.pop()!;
const suffix = suffixStack.shift()!; const suffix = suffixStack.pop()!;
child = node[2]; child = node[2];
if (child.size) { if (child.size) {
const keys = Array.from(child.keys()).sort(Triebase.compare); const keys = Array.from(child.keys()).sort(Triebase.compare);