-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Work towards a common Trie Interface
- Loading branch information
Showing
10 changed files
with
277 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { TrieOptions } from './TrieNode.js'; | ||
|
||
export interface ITrieNode { | ||
/** flag End of Word */ | ||
readonly eow: boolean; | ||
/** number of children */ | ||
readonly size: number; | ||
/** get keys to children */ | ||
getKeys(): readonly string[]; | ||
/** get child ITrieNode */ | ||
get(char: string): ITrieNode | undefined; | ||
/** get a child by the key index */ | ||
child(idx: number): ITrieNode | undefined; | ||
/** has child */ | ||
has(char: string): boolean; | ||
} | ||
|
||
export interface ITrieNodeRoot extends ITrieNode { | ||
options: Readonly<TrieOptions>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { ITrieNode, ITrieNodeRoot } from './ITrieNode.js'; | ||
import type { TrieNode, TrieOptions, TrieRoot } from './TrieNode.js'; | ||
|
||
export function trieRootToITrieRoot(root: TrieRoot): ITrieNodeRoot { | ||
return new ImplITrieRoot(root); | ||
} | ||
|
||
export function trieNodeToITrieNode(root: TrieNode): ITrieNode { | ||
return new ImplITrieNode(root); | ||
} | ||
|
||
const EmptyKeys: readonly string[] = Object.freeze([]); | ||
|
||
class ImplITrieNode implements ITrieNode { | ||
private keys: readonly string[] | undefined; | ||
constructor(readonly node: TrieNode) {} | ||
|
||
/** flag End of Word */ | ||
get eow(): boolean { | ||
return !!this.node.f; | ||
} | ||
|
||
/** number of children */ | ||
get size(): number { | ||
if (!this.node.c) return 0; | ||
return this.getKeys().length; | ||
} | ||
|
||
/** get keys to children */ | ||
getKeys(): readonly string[] { | ||
if (this.keys) return this.keys; | ||
const keys = this.node.c ? Object.keys(this.node.c) : EmptyKeys; | ||
this.keys = keys; | ||
return keys; | ||
} | ||
|
||
/** get child ITrieNode */ | ||
get(char: string): ITrieNode | undefined { | ||
const n = this.node.c?.[char]; | ||
if (!n) return undefined; | ||
return new ImplITrieNode(n); | ||
} | ||
|
||
has(char: string): boolean { | ||
const c = this.node.c; | ||
return (c && char in c) || false; | ||
} | ||
|
||
child(keyIdx: number): ITrieNode | undefined { | ||
const char = this.getKeys()[keyIdx]; | ||
if (!char) return undefined; | ||
return this.get(char); | ||
} | ||
} | ||
|
||
class ImplITrieRoot extends ImplITrieNode implements ITrieNodeRoot { | ||
readonly options: Readonly<TrieOptions>; | ||
|
||
constructor(readonly root: TrieRoot) { | ||
super(root); | ||
const { stripCaseAndAccentsPrefix, compoundCharacter, forbiddenWordPrefix } = root; | ||
this.options = { stripCaseAndAccentsPrefix, compoundCharacter, forbiddenWordPrefix }; | ||
} | ||
|
||
get eow(): boolean { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.