Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { Map as ImmutableMap, OrderedSet } from 'immutable';
/**
* An immutable tree where each node requires a unique reference.
* Represented as an immutable map.
*/
export interface ImmutableTree<T> {
readonly rootRef: string,
readonly version: number,
readonly nodes: ImmutableTree.Nodes<T>,
getRef(e: T): string
}
export namespace ImmutableTree {
export interface MutableNode<T> { ref: string, value: T, version: number, parent: string, children: OrderedSet<string> }
export interface Node<T> extends Readonly<MutableNode<T>> { }
export interface Nodes<T> extends ImmutableMap<string, Node<T>> { }
class Impl<T> implements ImmutableTree<T> {
readonly rootRef: string;
readonly version: number;
readonly nodes: ImmutableTree.Nodes<T>;
readonly getRef: (e: T) => string;
constructor(rootRef: string, nodes: ImmutableTree.Nodes<T>, getRef: (e: T) => string, version: number) {
this.rootRef = rootRef;
this.nodes = nodes;
this.getRef = getRef;
this.version = version;
}
}
/**
* Create an instance of an immutable tree.
*/
export function create<T>(root: T, getRef: (t: T) => string): ImmutableTree<T> {
const ref = getRef(root);
const r: Node<T> = { ref, value: root, version: 0, parent: ref, children: OrderedSet() };
return new Impl(ref, ImmutableMap([[ref, r]]), getRef, 0);
}
export function asTransient<T>(tree: ImmutableTree<T>) {
return new Transient(tree);
}
type N = Node<any>
type Ns = Nodes<any>
type VisitorCtx = { nodes: Ns, state: any, f: (node: N, nodes: Ns, state: any) => boolean | undefined | void };
function _postOrderFunc(this: VisitorCtx, c: string | undefined) { _doPostOrder(this, this.nodes.get(c!)!); }
function _doPostOrder<T, S>(ctx: VisitorCtx, root: N) {
if (root.children.size) {
root.children.forEach(_postOrderFunc, ctx);
}
ctx.f(root, ctx.nodes, ctx.state);
}
/**
* Visit all nodes in a subtree in "post order", meaning leafs get visited first.
*/
export function doPostOrder<T, S>(tree: ImmutableTree<T>, root: Node<T>, state: S, f: (node: Node<T>, nodes: Nodes<T>, state: S) => boolean | undefined | void) {
const ctx: VisitorCtx = { nodes: tree.nodes, state, f };
_doPostOrder(ctx, root);
return ctx.state;
}
function _preOrderFunc(this: VisitorCtx, c: string | undefined) { _doPreOrder(this, this.nodes.get(c!)!); }
function _doPreOrder<T, S>(ctx: VisitorCtx, root: N) {
ctx.f(root, ctx.nodes, ctx.state);
if (root.children.size) {
root.children.forEach(_preOrderFunc, ctx);
}
}
/**
* Visit all nodes in a subtree in "pre order", meaning leafs get visited last.
*/
export function doPreOrder<T, S>(tree: ImmutableTree<T>, root: Node<T>, state: S, f: (node: Node<T>, nodes: Nodes<T>, state: S) => boolean | undefined | void) {
const ctx: VisitorCtx = { nodes: tree.nodes, state, f };
_doPreOrder(ctx, root);
return ctx.state;
}
function _subtree(n: N, nodes: Ns, subtree: N[]) { subtree.push(n); }
/**
* Get all nodes in a subtree, leafs come first.
*/
export function subtreePostOrder<T>(tree: ImmutableTree<T>, root: Node<T>) {
return doPostOrder<T, Node<T>[]>(tree, root, [], _subtree);
}
function checkSetRef(oldRef: string, newRef: string) {
if (oldRef !== newRef) {
throw new Error(`Cannot setValue of node '${oldRef}' because the new value has a different ref '${newRef}'.`);
}
}
function ensureNotPresent(nodes: Ns, ref: string) {
if (nodes.has(ref)) {
throw new Error(`Cannot add node '${ref}' because a different node with this ref already present in the tree.`);
}
}
function ensurePresent(nodes: Ns, ref: string) {
if (!nodes.has(ref)) {
throw new Error(`Node '${ref}' is not present in the tree.`);
}
}
function mutateNode(nodes: Ns, mutations: Map<string, N>, ref: string): N {
ensurePresent(nodes, ref);
if (mutations.has(ref)) {
return mutations.get(ref)!;
}
const node = nodes.get(ref)!;
const newNode: N = { ref: node.ref, value: node.value, version: node.version + 1, parent: node.parent, children: node.children.asMutable() };
mutations.set(ref, newNode);
nodes.set(ref, newNode);
return newNode;
}
export class Transient<T> implements ImmutableTree<T> {
nodes = this.tree.nodes.asMutable();
version: number = this.tree.version + 1;
private mutations: Map<string, Node<T>> = new Map();
mutate(ref: string): MutableNode<T> {
return mutateNode(this.nodes, this.mutations, ref);
}
get rootRef() { return this.tree.rootRef; }
getRef(e: T) {
return this.tree.getRef(e);
}
add(parentRef: string, value: T) {
const ref = this.getRef(value);
ensureNotPresent(this.nodes, ref);
const parent = this.mutate(parentRef);
const node: Node<T> = { ref, version: 0, value, parent: parent.ref, children: OrderedSet<string>().asMutable() };
this.mutations.set(ref, node);
parent.children.add(ref);
this.nodes.set(ref, node);
return node;
}
setValue(ref: string, value: T): Node<T> {
checkSetRef(ref, this.getRef(value));
const node = this.mutate(ref);
node.value = value;
return node;
}
remove<T>(ref: string): Node<T>[] {
const { nodes, mutations, mutate } = this;
const node = nodes.get(ref);
if (!node) return [];
const parent = nodes.get(node.parent)!;
const children = mutate(parent.ref).children;
const st = subtreePostOrder(this, node);
if (parent.ref === node.ref) {
nodes.clear();
mutations.clear();
return st;
}
children.delete(ref);
for (const n of st) {
nodes.delete(n.value.ref);
mutations.delete(n.value.ref);
}
return st;
}
removeChildren(ref: string): Node<T>[] {
const { nodes, mutations, mutate } = this;
let node = nodes.get(ref);
if (!node || !node.children.size) return [];
node = mutate(ref);
const st = subtreePostOrder(this, node);
node.children.clear();
for (const n of st) {
if (n === node) continue;
nodes.delete(n.value.ref);
mutations.delete(n.value.ref);
}
return st;
}
asImmutable() {
if (this.mutations.size === 0) return this.tree;
this.mutations.forEach(m => (m as MutableNode<T>).children = m.children.asImmutable());
return new Impl<T>(this.tree.rootRef, this.nodes.asImmutable(), this.tree.getRef, this.version);
}
constructor(private tree: ImmutableTree<T>) {
}
}
}