Skip to content

Commit

Permalink
feat: wip tree map
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 25, 2023
1 parent 45c5ade commit b87bff4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/tree-map.ts
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)
}
}
}
}

0 comments on commit b87bff4

Please sign in to comment.