1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { TreeNode, CompositeTreeNode } from './tree';
|
18 | import { ExpandableTreeNode } from './tree-expansion';
|
19 |
|
20 | export interface TreeIterator extends Iterator<TreeNode> {
|
21 | }
|
22 |
|
23 | export namespace TreeIterator {
|
24 |
|
25 | export interface Options {
|
26 | readonly pruneCollapsed: boolean
|
27 | readonly pruneSiblings: boolean
|
28 | }
|
29 |
|
30 | export const DEFAULT_OPTIONS: Options = {
|
31 | pruneCollapsed: false,
|
32 | pruneSiblings: false
|
33 | };
|
34 |
|
35 | }
|
36 |
|
37 | export abstract class AbstractTreeIterator implements TreeIterator, Iterable<TreeNode> {
|
38 |
|
39 | protected readonly delegate: IterableIterator<TreeNode>;
|
40 | protected readonly options: TreeIterator.Options;
|
41 |
|
42 | constructor(protected readonly root: TreeNode, options?: Partial<TreeIterator.Options>) {
|
43 | this.options = {
|
44 | ...TreeIterator.DEFAULT_OPTIONS,
|
45 | ...options
|
46 | };
|
47 | this.delegate = this.iterator(this.root);
|
48 | }
|
49 |
|
50 |
|
51 | [Symbol.iterator]() {
|
52 | return this.delegate;
|
53 | }
|
54 |
|
55 | next(): IteratorResult<TreeNode> {
|
56 | return this.delegate.next();
|
57 | }
|
58 |
|
59 | protected abstract iterator(node: TreeNode): IterableIterator<TreeNode>;
|
60 |
|
61 | protected children(node: TreeNode): TreeNode[] | undefined {
|
62 | if (!CompositeTreeNode.is(node)) {
|
63 | return undefined;
|
64 | }
|
65 | if (this.options.pruneCollapsed && this.isCollapsed(node)) {
|
66 | return undefined;
|
67 | }
|
68 | return node.children.slice();
|
69 | }
|
70 |
|
71 | protected isCollapsed(node: TreeNode): boolean {
|
72 | return ExpandableTreeNode.isCollapsed(node);
|
73 | }
|
74 |
|
75 | protected isEmpty(nodes: TreeNode[] | undefined): boolean {
|
76 | return nodes === undefined || nodes.length === 0;
|
77 | }
|
78 |
|
79 | }
|
80 |
|
81 | export class DepthFirstTreeIterator extends AbstractTreeIterator {
|
82 |
|
83 | protected iterator(root: TreeNode): IterableIterator<TreeNode> {
|
84 | return Iterators.depthFirst(root, this.children.bind(this));
|
85 | }
|
86 |
|
87 | }
|
88 |
|
89 | export class BreadthFirstTreeIterator extends AbstractTreeIterator {
|
90 |
|
91 | protected iterator(root: TreeNode): IterableIterator<TreeNode> {
|
92 | return Iterators.breadthFirst(root, this.children.bind(this));
|
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 | export class TopDownTreeIterator extends AbstractTreeIterator {
|
127 |
|
128 | protected iterator(root: TreeNode): IterableIterator<TreeNode> {
|
129 | const doNext = this.doNext.bind(this);
|
130 | return (function* (): IterableIterator<TreeNode> {
|
131 | let next = root;
|
132 | while (next) {
|
133 | yield next;
|
134 | next = doNext(next);
|
135 | }
|
136 | }).bind(this)();
|
137 | }
|
138 |
|
139 | protected doNext(node: TreeNode): TreeNode | undefined {
|
140 | return this.findFirstChild(node) || this.findNextSibling(node);
|
141 | }
|
142 |
|
143 | protected findFirstChild(node: TreeNode): TreeNode | undefined {
|
144 | return (this.children(node) || [])[0];
|
145 | }
|
146 |
|
147 | protected findNextSibling(node: TreeNode | undefined): TreeNode | undefined {
|
148 | if (!node) {
|
149 | return undefined;
|
150 | }
|
151 | if (this.options.pruneSiblings && node === this.root) {
|
152 | return undefined;
|
153 | }
|
154 | if (node.nextSibling) {
|
155 | return node.nextSibling;
|
156 | }
|
157 | return this.findNextSibling(node.parent);
|
158 | }
|
159 |
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | export class BottomUpTreeIterator extends AbstractTreeIterator {
|
167 |
|
168 | protected iterator(root: TreeNode): IterableIterator<TreeNode> {
|
169 | const doNext = this.doNext.bind(this);
|
170 | return (function* (): IterableIterator<TreeNode> {
|
171 | let next = root;
|
172 | while (next) {
|
173 | yield next;
|
174 | next = doNext(next);
|
175 | }
|
176 | }).bind(this)();
|
177 | }
|
178 |
|
179 | protected doNext(node: TreeNode): TreeNode | undefined {
|
180 | const previousSibling = node.previousSibling;
|
181 | const lastChild = this.lastChild(previousSibling);
|
182 | return lastChild || node.parent;
|
183 | }
|
184 |
|
185 | protected lastChild(node: TreeNode | undefined): TreeNode | undefined {
|
186 | const children = node ? this.children(node) : [];
|
187 | if (this.isEmpty(children)) {
|
188 | return node;
|
189 | }
|
190 | if (CompositeTreeNode.is(node)) {
|
191 | const lastChild = CompositeTreeNode.getLastChild(node);
|
192 | return this.lastChild(lastChild);
|
193 | }
|
194 | return undefined;
|
195 | }
|
196 |
|
197 | }
|
198 |
|
199 | export namespace Iterators {
|
200 |
|
201 | |
202 |
|
203 |
|
204 | export function* depthFirst<T>(root: T, children: (node: T) => T[] | undefined, include: (node: T) => boolean = () => true): IterableIterator<T> {
|
205 | let stack: T[] = [];
|
206 | stack.push(root);
|
207 | while (stack.length > 0) {
|
208 | const top = stack.pop()!;
|
209 | yield top;
|
210 | stack = stack.concat((children(top) || []).filter(include).reverse());
|
211 | }
|
212 | }
|
213 |
|
214 | |
215 |
|
216 |
|
217 | export function* breadthFirst<T>(root: T, children: (node: T) => T[] | undefined, include: (node: T) => boolean = () => true): IterableIterator<T> {
|
218 | let queue: T[] = [];
|
219 | queue.push(root);
|
220 | while (queue.length > 0) {
|
221 | const head = queue.shift()!;
|
222 | yield head;
|
223 | queue = queue.concat((children(head) || []).filter(include));
|
224 | }
|
225 | }
|
226 |
|
227 | |
228 |
|
229 |
|
230 | export function asIterator<T>(elements: ReadonlyArray<T>): IterableIterator<T> {
|
231 | return elements.slice()[Symbol.iterator]();
|
232 | }
|
233 |
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | export function* cycle<T>(elements: ReadonlyArray<T>, start?: T): IterableIterator<T> {
|
242 | const copy = elements.slice();
|
243 | let index = !!start ? copy.indexOf(start) : 0;
|
244 | if (index === -1) {
|
245 | throw new Error(`${start} is not contained in ${copy}.`);
|
246 | }
|
247 | while (true) {
|
248 | yield copy[index];
|
249 | index++;
|
250 | if (index === copy.length) {
|
251 | index = 0;
|
252 | }
|
253 | }
|
254 | }
|
255 |
|
256 | }
|