Skip to content

Commit fd08599

Browse files
replace linked list implementations with recorder + node
1 parent 68517e4 commit fd08599

File tree

4 files changed

+113
-67
lines changed

4 files changed

+113
-67
lines changed

src/Recorder.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { inspect, InspectOptions } from 'util'
2+
import { SubstituteNodeBase } from './SubstituteNodeBase'
3+
4+
export class Recorder {
5+
private _records: SubstituteNodeBase[]
6+
private _indexedRecords: Map<PropertyKey, SubstituteNodeBase[]>
7+
8+
constructor() {
9+
this._records = []
10+
this._indexedRecords = new Map()
11+
}
12+
13+
public get records(): SubstituteNodeBase[] {
14+
return this._records
15+
}
16+
17+
public get indexedRecords(): Map<PropertyKey, SubstituteNodeBase[]> {
18+
return this._indexedRecords
19+
}
20+
21+
public addIndexedRecord(node: SubstituteNodeBase): void {
22+
const existingNodes = this.indexedRecords.get(node.key)
23+
if (typeof existingNodes === 'undefined') this._indexedRecords.set(node.key, [node])
24+
else existingNodes.push(node)
25+
}
26+
27+
public addRecord(node: SubstituteNodeBase): void {
28+
this._records.push(node)
29+
}
30+
31+
public getSiblingsOf(node: SubstituteNodeBase): SubstituteNodeBase[] {
32+
const siblingNodes = this.indexedRecords.get(node.key) ?? []
33+
return siblingNodes.filter(siblingNode => siblingNode !== node)
34+
}
35+
36+
public [inspect.custom](_: number, options: InspectOptions): string {
37+
const entries = [...this.indexedRecords.entries()]
38+
return entries.map(([key, value]) => `\n ${key.toString()}: {\n${value.map(v => ` ${inspect(v, options)}`).join(',\n')}\n }`).join()
39+
}
40+
}

src/SubstituteNodeBase.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { SubstituteBase } from './SubstituteBase'
2+
import { Substitute } from './Substitute'
3+
4+
export abstract class SubstituteNodeBase<T extends SubstituteNodeBase = SubstituteNodeBase<any>> extends SubstituteBase {
5+
private _parent?: T = undefined
6+
private _child?: T
7+
private _head: T & { parent: undefined }
8+
private _root: Substitute
9+
10+
constructor(private _key: PropertyKey, caller: T | SubstituteBase) {
11+
super()
12+
13+
if (caller instanceof Substitute) {
14+
caller.recorder.addIndexedRecord(this)
15+
this._root = caller
16+
}
17+
18+
if (caller instanceof SubstituteNodeBase) {
19+
this._parent = caller
20+
this._head = caller.head as T & { parent: undefined }
21+
caller.child = this
22+
}
23+
24+
this.root.recorder.addRecord(this)
25+
}
26+
27+
get key(): PropertyKey {
28+
return this._key
29+
}
30+
31+
set parent(parent: T | undefined) {
32+
this._parent = parent
33+
}
34+
35+
get parent(): T | undefined {
36+
return this._parent
37+
}
38+
39+
set child(child: T) {
40+
this._child = child
41+
}
42+
43+
get child(): T {
44+
return this._child
45+
}
46+
47+
get head(): T & { parent: undefined } {
48+
return this.isHead() ? this : this._head
49+
}
50+
51+
protected get root(): Substitute {
52+
return this.head._root
53+
}
54+
55+
protected isHead(): this is T & { parent: undefined } {
56+
return typeof this._parent === 'undefined'
57+
}
58+
59+
protected isIntermediateNode(): this is T & { parent: T } {
60+
return !this.isHead()
61+
}
62+
63+
protected getAllSiblings(): T[] {
64+
return this.root.recorder.getSiblingsOf(this) as T[]
65+
}
66+
67+
protected getAllSiblingsOfHead(): T[] {
68+
return this.root.recorder.getSiblingsOf(this.head) as T[]
69+
}
70+
71+
public abstract read(): void
72+
public abstract write(value: any): void
73+
}

src/linked-list/Graph.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/linked-list/Node.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)