-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export type EntryNodeKey = string | number | ||
|
||
export class EntryNode<T> { | ||
value: T | undefined | ||
children = new Map<string | number, EntryNode<T>>() | ||
|
||
constructor() | ||
constructor(keys: EntryNodeKey[], value: T) | ||
constructor(...args: [] | [EntryNodeKey[], T]) { | ||
if (args.length) { | ||
this.set(...args) | ||
} | ||
} | ||
|
||
/** | ||
* Sets the value while building the tree | ||
* | ||
* @param keys - key as an array | ||
* @param value - value to set | ||
*/ | ||
set(keys: EntryNodeKey[], value: T) { | ||
if (keys.length === 0) { | ||
this.value = value | ||
} else { | ||
// this.children ??= new Map<EntryNodeKey, | ||
const [top, ...otherKeys] = keys | ||
const node: EntryNode<T> | undefined = this.children.get(top) | ||
if (node) { | ||
node.set(otherKeys, value) | ||
} else { | ||
this.children.set(top, new EntryNode(otherKeys, value)) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Delete the node at the given path of keys and all its children. | ||
* | ||
* @param keys - path of keys | ||
*/ | ||
delete(keys: EntryNodeKey[]) { | ||
if (keys.length === 1) { | ||
this.children.delete(keys[0]) | ||
} else { | ||
const [top, ...otherKeys] = keys | ||
const node = this.children.get(top) | ||
if (node) { | ||
node.delete(otherKeys) | ||
} | ||
} | ||
} | ||
} |