UNPKG

9.44 kBTypeScriptView Raw
1import TreeIterator = require('./TreeIterator');
2import TreePosition = require('./TreePosition');
3
4declare namespace SymbolTree {
5 interface SiblingOptions<T extends object = any> {
6 /**
7 * Used to constrain the operation to a subtree.
8 *
9 * When `null`, the whole tree is walked to the real root.
10 *
11 * @default null
12 */
13 root?: T | null | undefined;
14
15 /**
16 * If set, ignore the children of `object`
17 *
18 * @default false
19 */
20 skipChildren?: boolean | undefined;
21 }
22
23 interface ToArrayOptions<T extends object = any> {
24 /**
25 * The array to initialize the operation with.
26 *
27 * @default
28 * ```ts
29 * new Array(0);
30 * ```
31 */
32 array?: T[] | undefined;
33
34 /**
35 * Function to test each object before it is added to the array.
36 * Invoked with arguments (object).
37 *
38 * Should return `true` if an object is to be included.
39 *
40 * @param object
41 */
42 filter?(object: T): any;
43
44 /** Value to use as `this` when executing `filter`. */
45 thisArg?: any;
46 }
47
48 interface IteratorOptions {
49 /**
50 * Whether to iterate in reverse tree order.
51 *
52 * @default false
53 */
54 reverse?: boolean | undefined;
55 }
56}
57
58declare class SymbolTree<T extends object = any> {
59 static readonly TreePosition: typeof TreePosition;
60
61 /**
62 * @param [description='SymbolTree data'] Description used for the Symbol
63 *
64 * **Default:** `'SymbolTree data'`
65 */
66 constructor(description?: string);
67
68 /**
69 * You can use this function to (optionally) initialize an object right after its creation,
70 * to take advantage of V8's fast properties. Also useful if you would like to
71 * freeze your object.
72 *
73 * * `O(1)`
74 */
75 initialize<O extends T | null | undefined>(object: O): O;
76
77 /**
78 * Returns `true` if the object has any children. Otherwise it returns `false`.
79 *
80 * * `O(1)`
81 */
82 hasChildren(object: T): boolean;
83
84 /**
85 * Returns the first child of the given object.
86 *
87 * * `O(1)`
88 */
89 firstChild(object: T): T | null;
90
91 /**
92 * Returns the last child of the given object.
93 *
94 * * `O(1)`
95 */
96 lastChild(object: T): T | null;
97
98 /**
99 * Returns the previous sibling of the given object.
100 *
101 * * `O(1)`
102 */
103 previousSibling(object: T): T | null;
104
105 /**
106 * Returns the next sibling of the given object.
107 *
108 * * `O(1)`
109 */
110 nextSibling(object: T): T | null;
111
112 /**
113 * Return the parent of the given object.
114 *
115 * * `O(1)`
116 */
117 parent(object: T): T | null;
118
119 /**
120 * Find the inclusive descendant that is last in tree order of the given object.
121 *
122 * * `O(n)` (worst case) where `n` is the depth of the subtree of `object`
123 */
124 lastInclusiveDescendant(object: T): T | null;
125
126 /**
127 * Find the preceding object (A) of the given object (B).
128 * An object A is preceding an object B if A and B are in the same tree
129 * and A comes before B in tree order.
130 *
131 * * `O(n)` (worst case)
132 * * `O(1)` (amortized when walking the entire tree)
133 */
134 preceding(object: T, options?: SymbolTree.SiblingOptions<T>): T | null;
135
136 /**
137 * Find the following object (A) of the given object (B).
138 * An object A is following an object B if A and B are in the same tree
139 * and A comes after B in tree order.
140 *
141 * * `O(n)` (worst case) where `n` is the amount of objects in the entire tree
142 * * `O(1)` (amortized when walking the entire tree)
143 */
144 following(object: T, options?: SymbolTree.SiblingOptions<T>): T | null;
145
146 /**
147 * Append all children of the given object to an array.
148 *
149 * * `O(n)` where `n` is the amount of children of the given `parent`
150 */
151 childrenToArray<THIS>(
152 parent: T,
153 options?: SymbolTree.ToArrayOptions<T> & {
154 thisArg: THIS;
155 filter(this: THIS, object: T): any;
156 },
157 ): T[];
158 childrenToArray(parent: T, options?: SymbolTree.ToArrayOptions<T>): T[];
159
160 /**
161 * Append all inclusive ancestors of the given object to an array.
162 *
163 * * `O(n)` where `n` is the amount of ancestors of the given `object`
164 */
165 ancestorsToArray<THIS>(
166 object: T,
167 options?: SymbolTree.ToArrayOptions<T> & {
168 thisArg: THIS;
169 filter(this: THIS, object: T): any;
170 },
171 ): T[];
172 ancestorsToArray(object: T, options?: SymbolTree.ToArrayOptions<T>): T[];
173
174 /**
175 * Append all descendants of the given object to an array (in tree order).
176 *
177 * * `O(n)` where `n` is the amount of objects in the sub-tree of the given `object`
178 */
179 treeToArray<THIS>(
180 object: T,
181 options?: SymbolTree.ToArrayOptions<T> & {
182 thisArg: THIS;
183 filter(this: THIS, object: T): any;
184 },
185 ): T[];
186 treeToArray(object: T, options?: SymbolTree.ToArrayOptions<T>): T[];
187
188 /**
189 * Iterate over all children of the given object
190 *
191 * * `O(1)` for a single iteration
192 *
193 * @return An iterable iterator (ES6)
194 */
195 childrenIterator(parent: T, options?: SymbolTree.IteratorOptions): TreeIterator<T>;
196
197 /**
198 * Iterate over all the previous siblings of the given object. (in reverse tree order)
199 *
200 * * `O(1)` for a single iteration
201 *
202 * @return An iterable iterator (ES6)
203 */
204 previousSiblingsIterator(object: T): TreeIterator<T>;
205
206 /**
207 * Iterate over all the next siblings of the given object. (in tree order)
208 *
209 * * `O(1)` for a single iteration
210 *
211 * @return An iterable iterator (ES6)
212 */
213 nextSiblingsIterator(object: T): TreeIterator<T>;
214
215 /**
216 * Iterate over all inclusive ancestors of the given object
217 *
218 * * `O(1)` for a single iteration
219 *
220 * @return An iterable iterator (ES6)
221 */
222 ancestorsIterator(object: T): TreeIterator<T>;
223
224 /**
225 * Iterate over all descendants of the given object (in tree order).
226 *
227 * Where `n` is the amount of objects in the sub-tree of the given `root`:
228 *
229 * * `O(n)` (worst case for a single iteration)
230 * * `O(n)` (amortized, when completing the iterator)
231 *
232 * @return An iterable iterator (ES6)
233 */
234 treeIterator(object: T, options?: SymbolTree.IteratorOptions): TreeIterator<T>;
235
236 /**
237 * Find the index of the given object (the number of preceding siblings).
238 *
239 * * `O(n)` where `n` is the amount of preceding siblings
240 * * `O(1)` (amortized, if the tree is not modified)
241 *
242 * @return The number of preceding siblings, or -1 if the object has no parent
243 */
244 index(object: T): number;
245
246 /**
247 * Calculate the number of children.
248 *
249 * * `O(n)` where `n` is the amount of children
250 * * `O(1)` (amortized, if the tree is not modified)
251 */
252 childrenCount(object: T): number;
253
254 /**
255 * Compare the position of an object relative to another object. A bit set is returned:
256 *
257 * <ul>
258 * <li>DISCONNECTED : 1</li>
259 * <li>PRECEDING : 2</li>
260 * <li>FOLLOWING : 4</li>
261 * <li>CONTAINS : 8</li>
262 * <li>CONTAINED_BY : 16</li>
263 * </ul>
264 *
265 * The semantics are the same as compareDocumentPosition in DOM, with the exception that
266 * DISCONNECTED never occurs with any other bit.
267 *
268 * where `n` and `m` are the amount of ancestors of `left` and `right`;
269 * where `o` is the amount of children of the lowest common ancestor of `left` and `right`:
270 *
271 * * `O(n + m + o)` (worst case)
272 * * `O(n + m)` (amortized, if the tree is not modified)
273 */
274 compareTreePosition(left: T, right: T): number;
275
276 /**
277 * Remove the object from this tree.
278 * Has no effect if already removed.
279 *
280 * * `O(1)`
281 */
282 remove<U extends T>(object: U): U;
283
284 /**
285 * Insert the given object before the reference object.
286 * `newObject` is now the previous sibling of `referenceObject`.
287 *
288 * * `O(1)`
289 *
290 * @throws {Error} If the newObject is already present in this SymbolTree
291 */
292 insertBefore<U extends T>(referenceObject: T, newObject: U): U;
293
294 /**
295 * Insert the given object after the reference object.
296 * `newObject` is now the next sibling of `referenceObject`.
297 *
298 * * `O(1)`
299 *
300 * @throws {Error} If the newObject is already present in this SymbolTree
301 */
302 insertAfter<U extends T>(referenceObject: T, newObject: U): U;
303
304 /**
305 * Insert the given object as the first child of the given reference object.
306 * `newObject` is now the first child of `referenceObject`.
307 *
308 * * `O(1)`
309 *
310 * @throws {Error} If the newObject is already present in this SymbolTree
311 */
312 prependChild<U extends T>(referenceObject: T, newObject: U): U;
313
314 /**
315 * Insert the given object as the last child of the given reference object.
316 * `newObject` is now the last child of `referenceObject`.
317 *
318 * * `O(1)`
319 *
320 * @throws {Error} If the newObject is already present in this SymbolTree
321 */
322 appendChild<U extends T>(referenceObject: T, newObject: U): U;
323}
324
325export = SymbolTree;