diff --git a/Build/lib/fifo.ts b/Build/lib/fifo.ts deleted file mode 100644 index 7f0a9091..00000000 --- a/Build/lib/fifo.ts +++ /dev/null @@ -1,63 +0,0 @@ -class Node { - next?: Node; - - constructor(public readonly value: T) {} -} - -export default class FIFO { - private head?: Node; - private tail?: Node; - public $size = 0; - - constructor() { - this.clear(); - } - - enqueue(value: T) { - const node = new Node(value); - - if (this.head) { - this.tail!.next = node; - this.tail = node; - } else { - this.head = node; - this.tail = node; - } - - this.$size++; - } - - dequeue() { - const current = this.head; - if (!current) { - return; - } - - this.head = this.head!.next; - this.$size--; - return current.value; - } - - peek() { - return this.head?.value; - } - - clear() { - this.head = undefined; - this.tail = undefined; - this.$size = 0; - } - - get size() { - return this.$size; - } - - *[Symbol.iterator]() { - let current = this.head; - - while (current) { - yield current.value; - current = current.next; - } - } -} diff --git a/Build/lib/trie.ts b/Build/lib/trie.ts index ecc57257..62ed0c69 100644 --- a/Build/lib/trie.ts +++ b/Build/lib/trie.ts @@ -6,7 +6,7 @@ import { fastStringCompare } from './misc'; import util from 'node:util'; import { noop } from 'foxts/noop'; import { fastStringArrayJoin } from 'foxts/fast-string-array-join'; -import FIFO from './fifo'; +import FIFO from 'fast-fifo'; type TrieNode = [ boolean, /** end */ @@ -181,14 +181,14 @@ abstract class Triebase { private static bfsResults: [node: TrieNode | null, suffix: string[]] = [null, []]; private static bfs(this: void, nodeStack: FIFO>, suffixStack: FIFO) { - const node = nodeStack.dequeue()!; - const suffix = suffixStack.dequeue()!; + const node = nodeStack.shift()!; + const suffix = suffixStack.shift()!; node[3].forEach((childNode, k) => { // Pushing the child node to the stack for next iteration of DFS - nodeStack.enqueue(childNode); + nodeStack.push(childNode); - suffixStack.enqueue([k, ...suffix]); + suffixStack.push([k, ...suffix]); }); Triebase.bfsResults[0] = node; @@ -198,8 +198,8 @@ abstract class Triebase { } private static bfsWithSort(this: void, nodeStack: FIFO>, suffixStack: FIFO) { - const node = nodeStack.dequeue()!; - const suffix = suffixStack.dequeue()!; + const node = nodeStack.shift()!; + const suffix = suffixStack.shift()!; if (node[3].size) { const keys = Array.from(node[3].keys()).sort(Triebase.compare); @@ -209,8 +209,8 @@ abstract class Triebase { const childNode = node[3].get(key)!; // Pushing the child node to the stack for next iteration of DFS - nodeStack.enqueue(childNode); - suffixStack.enqueue([key, ...suffix]); + nodeStack.push(childNode); + suffixStack.push([key, ...suffix]); } } @@ -229,11 +229,11 @@ abstract class Triebase { const bfsImpl = withSort ? Triebase.bfsWithSort : Triebase.bfs; const nodeStack = new FIFO>(); - nodeStack.enqueue(initialNode); + nodeStack.push(initialNode); // Resolving initial string (begin the start of the stack) const suffixStack = new FIFO(); - suffixStack.enqueue(initialSuffix); + suffixStack.push(initialSuffix); let node: TrieNode = initialNode; let r; @@ -247,7 +247,7 @@ abstract class Triebase { if (node[0]) { onMatches(suffix, node[1], node[4]); } - } while (nodeStack.size); + } while (nodeStack.length); }; static compare(this: void, a: string, b: string) { @@ -261,17 +261,17 @@ abstract class Triebase { initialSuffix: string[] = [] ) { const nodeStack = new FIFO>(); - nodeStack.enqueue(initialNode); + nodeStack.push(initialNode); // Resolving initial string (begin the start of the stack) const suffixStack = new FIFO(); - suffixStack.enqueue(initialSuffix); + suffixStack.push(initialSuffix); let node: TrieNode = initialNode; do { - node = nodeStack.dequeue()!; - const suffix = suffixStack.dequeue()!; + node = nodeStack.shift()!; + const suffix = suffixStack.shift()!; if (node[3].size) { const keys = Array.from(node[3].keys()).sort(Triebase.compare); @@ -281,8 +281,8 @@ abstract class Triebase { const childNode = node[3].get(key)!; // Pushing the child node to the stack for next iteration of DFS - nodeStack.enqueue(childNode); - suffixStack.enqueue([key, ...suffix]); + nodeStack.push(childNode); + suffixStack.push([key, ...suffix]); } } @@ -290,7 +290,7 @@ abstract class Triebase { if (node[0]) { onMatches(suffix, node[1], node[4]); } - } while (nodeStack.size); + } while (nodeStack.length); }; protected getSingleChildLeaf(tokens: string[]): FindSingleChildLeafResult | null { diff --git a/package.json b/package.json index 89a32763..30a06f97 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "dns2": "^2.1.0", "escape-string-regexp-node": "^1.0.2", "fast-cidr-tools": "^0.3.1", + "fast-fifo": "^1.3.2", "fdir": "^6.4.2", "foxts": "1.0.7", "hash-wasm": "^4.12.0", @@ -55,6 +56,7 @@ "@types/better-sqlite3": "^7.6.12", "@types/cacache": "^17.0.2", "@types/dns2": "^2.0.9", + "@types/fast-fifo": "^1.3.0", "@types/make-fetch-happen": "^10.0.4", "@types/mocha": "^10.0.10", "@types/node": "^22.10.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7b640c4e..0f36fd15 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -44,6 +44,9 @@ importers: fast-cidr-tools: specifier: ^0.3.1 version: 0.3.1 + fast-fifo: + specifier: ^1.3.2 + version: 1.3.2 fdir: specifier: ^6.4.2 version: 6.4.2(picomatch@4.0.2) @@ -111,6 +114,9 @@ importers: '@types/dns2': specifier: ^2.0.9 version: 2.0.9 + '@types/fast-fifo': + specifier: ^1.3.0 + version: 1.3.0 '@types/make-fetch-happen': specifier: ^10.0.4 version: 10.0.4 @@ -513,6 +519,9 @@ packages: '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} + '@types/fast-fifo@1.3.0': + resolution: {integrity: sha512-xTVvla2QX5ruNmVUqCFc++xm42xl6RBaroppSleSczvM2rAwClr88MbnrgDGLIStt4RCVegeKO4z2HM5AHFOng==} + '@types/istanbul-lib-coverage@2.0.6': resolution: {integrity: sha512-2QF/t/auWm0lsy8XtKVPG19v3sSOQlJe/YHZgfjb/KBBHOGSV+J2q/S671rcq9uTBrLAXmZpqJiaQbMT+zNU1w==} @@ -2252,6 +2261,8 @@ snapshots: '@types/estree@1.0.6': {} + '@types/fast-fifo@1.3.0': {} + '@types/istanbul-lib-coverage@2.0.6': {} '@types/istanbul-lib-report@3.0.3':