btree-core 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/LICENSE +23 -0
  2. package/b+tree.d.ts +429 -0
  3. package/b+tree.js +1545 -0
  4. package/b+tree.min.js +1 -0
  5. package/extended/bulkLoad.d.ts +14 -0
  6. package/extended/bulkLoad.js +113 -0
  7. package/extended/bulkLoad.min.js +1 -0
  8. package/extended/decompose.d.ts +1 -0
  9. package/extended/decompose.js +680 -0
  10. package/extended/decompose.min.js +1 -0
  11. package/extended/diffAgainst.d.ts +23 -0
  12. package/extended/diffAgainst.js +254 -0
  13. package/extended/diffAgainst.min.js +1 -0
  14. package/extended/forEachKeyInBoth.d.ts +19 -0
  15. package/extended/forEachKeyInBoth.js +73 -0
  16. package/extended/forEachKeyInBoth.min.js +1 -0
  17. package/extended/forEachKeyNotIn.d.ts +18 -0
  18. package/extended/forEachKeyNotIn.js +87 -0
  19. package/extended/forEachKeyNotIn.min.js +1 -0
  20. package/extended/index.d.ts +133 -0
  21. package/extended/index.js +200 -0
  22. package/extended/index.min.js +1 -0
  23. package/extended/intersect.d.ts +16 -0
  24. package/extended/intersect.js +44 -0
  25. package/extended/intersect.min.js +1 -0
  26. package/extended/parallelWalk.d.ts +1 -0
  27. package/extended/parallelWalk.js +188 -0
  28. package/extended/parallelWalk.min.js +1 -0
  29. package/extended/shared.d.ts +1 -0
  30. package/extended/shared.js +64 -0
  31. package/extended/shared.min.js +1 -0
  32. package/extended/subtract.d.ts +16 -0
  33. package/extended/subtract.js +35 -0
  34. package/extended/subtract.min.js +1 -0
  35. package/extended/union.d.ts +16 -0
  36. package/extended/union.js +36 -0
  37. package/extended/union.min.js +1 -0
  38. package/interfaces.d.ts +307 -0
  39. package/package.json +122 -0
  40. package/readme.md +420 -0
  41. package/sorted-array.d.ts +22 -0
  42. package/sorted-array.js +71 -0
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Donnameyer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and its associated documentation files (the “Software”),
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do so,
9
+ subject to the following condition:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ Disclaimer of Warranty and Limitation of Liability
15
+
16
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS
17
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT.
19
+
20
+ IN NO EVENT SHALL THE AUTHORS, COPYRIGHT HOLDERS, OR CONTRIBUTORS BE LIABLE FOR
21
+ ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT,
22
+ OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ OF OR OTHER DEALINGS IN THE SOFTWARE.
package/b+tree.d.ts ADDED
@@ -0,0 +1,429 @@
1
+ import { ISortedMap, ISortedMapF, ISortedSet } from './interfaces';
2
+ export { ISetSource, ISetSink, ISet, ISetF, ISortedSetSource, ISortedSet, ISortedSetF, IMapSource, IMapSink, IMap, IMapF, ISortedMapSource, ISortedMap, ISortedMapF } from './interfaces';
3
+ export type EditRangeResult<V, R = number> = {
4
+ value?: V;
5
+ break?: R;
6
+ delete?: boolean;
7
+ };
8
+ /**
9
+ * Types that BTree supports by default
10
+ */
11
+ export type DefaultComparable = number | string | Date | boolean | null | undefined | (number | string)[] | {
12
+ valueOf: () => number | string | Date | boolean | null | undefined | (number | string)[];
13
+ };
14
+ /**
15
+ * Compares DefaultComparables to form a strict partial ordering.
16
+ *
17
+ * Handles +/-0 and NaN like Map: NaN is equal to NaN, and -0 is equal to +0.
18
+ *
19
+ * Arrays are compared using '<' and '>', which may cause unexpected equality:
20
+ * for example [1] will be considered equal to ['1'].
21
+ *
22
+ * Two objects with equal valueOf compare the same, but compare unequal to
23
+ * primitives that have the same value.
24
+ */
25
+ export declare function defaultComparator(a: DefaultComparable, b: DefaultComparable): number;
26
+ /**
27
+ * Compares items using the < and > operators. This function is probably slightly
28
+ * faster than the defaultComparator for Dates and strings, but has not been benchmarked.
29
+ * Unlike defaultComparator, this comparator doesn't support mixed types correctly,
30
+ * i.e. use it with `BTree<string>` or `BTree<number>` but not `BTree<string|number>`.
31
+ *
32
+ * NaN is not supported.
33
+ *
34
+ * Note: null is treated like 0 when compared with numbers or Date, but in general
35
+ * null is not ordered with respect to strings (neither greater nor less), and
36
+ * undefined is not ordered with other types.
37
+ */
38
+ export declare function simpleComparator(a: string, b: string): number;
39
+ export declare function simpleComparator(a: number | null, b: number | null): number;
40
+ export declare function simpleComparator(a: Date | null, b: Date | null): number;
41
+ export declare function simpleComparator(a: (number | string)[], b: (number | string)[]): number;
42
+ /**
43
+ * A reasonably fast collection of key-value pairs with a powerful API.
44
+ * Largely compatible with the standard Map. BTree is a B+ tree data structure,
45
+ * so the collection is sorted by key.
46
+ *
47
+ * B+ trees tend to use memory more efficiently than hashtables such as the
48
+ * standard Map, especially when the collection contains a large number of
49
+ * items. However, maintaining the sort order makes them modestly slower:
50
+ * O(log size) rather than O(1). This B+ tree implementation supports O(1)
51
+ * fast cloning. It also supports freeze(), which can be used to ensure that
52
+ * a BTree is not changed accidentally.
53
+ *
54
+ * Confusingly, the ES6 Map.forEach(c) method calls c(value,key) instead of
55
+ * c(key,value), in contrast to other methods such as set() and entries()
56
+ * which put the key first. I can only assume that the order was reversed on
57
+ * the theory that users would usually want to examine values and ignore keys.
58
+ * BTree's forEach() therefore works the same way, but a second method
59
+ * `.forEachPair((key,value)=>{...})` is provided which sends you the key
60
+ * first and the value second; this method is slightly faster because it is
61
+ * the "native" for-each method for this class.
62
+ *
63
+ * Out of the box, BTree supports keys that are numbers, strings, arrays of
64
+ * numbers/strings, Date, and objects that have a valueOf() method returning a
65
+ * number or string. Other data types, such as arrays of Date or custom
66
+ * objects, require a custom comparator, which you must pass as the second
67
+ * argument to the constructor (the first argument is an optional list of
68
+ * initial items). Symbols cannot be used as keys because they are unordered
69
+ * (one Symbol is never "greater" or "less" than another).
70
+ *
71
+ * @example
72
+ * Given a {name: string, age: number} object, you can create a tree sorted by
73
+ * name and then by age like this:
74
+ *
75
+ * var tree = new BTree(undefined, (a, b) => {
76
+ * if (a.name > b.name)
77
+ * return 1; // Return a number >0 when a > b
78
+ * else if (a.name < b.name)
79
+ * return -1; // Return a number <0 when a < b
80
+ * else // names are equal (or incomparable)
81
+ * return a.age - b.age; // Return >0 when a.age > b.age
82
+ * });
83
+ *
84
+ * tree.set({name:"Bill", age:17}, "happy");
85
+ * tree.set({name:"Fran", age:40}, "busy & stressed");
86
+ * tree.set({name:"Bill", age:55}, "recently laid off");
87
+ * tree.forEachPair((k, v) => {
88
+ * console.log(`Name: ${k.name} Age: ${k.age} Status: ${v}`);
89
+ * });
90
+ *
91
+ * @description
92
+ * The "range" methods (`forEach, forRange, editRange`) will return the number
93
+ * of elements that were scanned. In addition, the callback can return {break:R}
94
+ * to stop early and return R from the outer function.
95
+ *
96
+ * - TODO: Test performance of preallocating values array at max size
97
+ * - TODO: Add fast initialization when a sorted array is provided to constructor
98
+ *
99
+ * For more documentation see https://github.com/qwertie/btree-typescript
100
+ *
101
+ * Are you a C# developer? You might like the similar data structures I made for C#:
102
+ * BDictionary, BList, etc. See http://core.loyc.net/collections/
103
+ *
104
+ * @author David Piepgrass
105
+ */
106
+ export default class BTree<K = any, V = any> implements ISortedMapF<K, V>, ISortedMap<K, V> {
107
+ private _root;
108
+ _maxNodeSize: number;
109
+ /**
110
+ * provides a total order over keys (and a strict partial order over the type K)
111
+ * @returns a negative value if a < b, 0 if a === b and a positive value if a > b
112
+ */
113
+ _compare: (a: K, b: K) => number;
114
+ /**
115
+ * Initializes an empty B+ tree.
116
+ * @param compare Custom function to compare pairs of elements in the tree.
117
+ * If not specified, defaultComparator will be used which is valid as long as K extends DefaultComparable.
118
+ * @param entries A set of key-value pairs to initialize the tree
119
+ * @param maxNodeSize Branching factor (maximum items or children per node)
120
+ * Must be in range 4..256. If undefined or <4 then default is used; if >256 then 256.
121
+ */
122
+ constructor(entries?: [K, V][], compare?: (a: K, b: K) => number, maxNodeSize?: number);
123
+ /** Gets the number of key-value pairs in the tree. */
124
+ get size(): number;
125
+ /** Gets the number of key-value pairs in the tree. */
126
+ get length(): number;
127
+ /** Returns true iff the tree contains no key-value pairs. */
128
+ get isEmpty(): boolean;
129
+ /** Releases the tree so that its size is 0. */
130
+ clear(): void;
131
+ forEach(callback: (v: V, k: K, tree: BTree<K, V>) => void, thisArg?: any): number;
132
+ /** Runs a function for each key-value pair, in order from smallest to
133
+ * largest key. The callback can return {break:R} (where R is any value
134
+ * except undefined) to stop immediately and return R from forEachPair.
135
+ * @param onFound A function that is called for each key-value pair. This
136
+ * function can return {break:R} to stop early with result R.
137
+ * The reason that you must return {break:R} instead of simply R
138
+ * itself is for consistency with editRange(), which allows
139
+ * multiple actions, not just breaking.
140
+ * @param initialCounter This is the value of the third argument of
141
+ * `onFound` the first time it is called. The counter increases
142
+ * by one each time `onFound` is called. Default value: 0
143
+ * @returns the number of pairs sent to the callback (plus initialCounter,
144
+ * if you provided one). If the callback returned {break:R} then
145
+ * the R value is returned instead. */
146
+ forEachPair<R = number>(callback: (k: K, v: V, counter: number) => {
147
+ break?: R;
148
+ } | void, initialCounter?: number): R | number;
149
+ /**
150
+ * Finds a pair in the tree and returns the associated value.
151
+ * @param defaultValue a value to return if the key was not found.
152
+ * @returns the value, or defaultValue if the key was not found.
153
+ * @description Computational complexity: O(log size)
154
+ */
155
+ get(key: K, defaultValue?: V): V | undefined;
156
+ /**
157
+ * Adds or overwrites a key-value pair in the B+ tree.
158
+ * @param key the key is used to determine the sort order of
159
+ * data in the tree.
160
+ * @param value data to associate with the key (optional)
161
+ * @param overwrite Whether to overwrite an existing key-value pair
162
+ * (default: true). If this is false and there is an existing
163
+ * key-value pair then this method has no effect.
164
+ * @returns true if a new key-value pair was added.
165
+ * @description Computational complexity: O(log size)
166
+ * Note: when overwriting a previous entry, the key is updated
167
+ * as well as the value. This has no effect unless the new key
168
+ * has data that does not affect its sort order.
169
+ */
170
+ set(key: K, value: V, overwrite?: boolean): boolean;
171
+ /**
172
+ * Returns true if the key exists in the B+ tree, false if not.
173
+ * Use get() for best performance; use has() if you need to
174
+ * distinguish between "undefined value" and "key not present".
175
+ * @param key Key to detect
176
+ * @description Computational complexity: O(log size)
177
+ */
178
+ has(key: K): boolean;
179
+ /**
180
+ * Removes a single key-value pair from the B+ tree.
181
+ * @param key Key to find
182
+ * @returns true if a pair was found and removed, false otherwise.
183
+ * @description Computational complexity: O(log size)
184
+ */
185
+ delete(key: K): boolean;
186
+ /** Returns a copy of the tree with the specified key set (the value is undefined). */
187
+ with(key: K): BTree<K, V | undefined>;
188
+ /** Returns a copy of the tree with the specified key-value pair set. */
189
+ with<V2>(key: K, value: V2, overwrite?: boolean): BTree<K, V | V2>;
190
+ /** Returns a copy of the tree with the specified key-value pairs set. */
191
+ withPairs<V2>(pairs: [K, V | V2][], overwrite: boolean): BTree<K, V | V2>;
192
+ /** Returns a copy of the tree with the specified keys present.
193
+ * @param keys The keys to add. If a key is already present in the tree,
194
+ * neither the existing key nor the existing value is modified.
195
+ * @param returnThisIfUnchanged if true, returns this if all keys already
196
+ * existed. Performance note: due to the architecture of this class, all
197
+ * node(s) leading to existing keys are cloned even if the collection is
198
+ * ultimately unchanged.
199
+ */
200
+ withKeys(keys: K[], returnThisIfUnchanged?: boolean): BTree<K, V | undefined>;
201
+ /** Returns a copy of the tree with the specified key removed.
202
+ * @param returnThisIfUnchanged if true, returns this if the key didn't exist.
203
+ * Performance note: due to the architecture of this class, node(s) leading
204
+ * to where the key would have been stored are cloned even when the key
205
+ * turns out not to exist and the collection is unchanged.
206
+ */
207
+ without(key: K, returnThisIfUnchanged?: boolean): this;
208
+ /** Returns a copy of the tree with the specified keys removed.
209
+ * @param returnThisIfUnchanged if true, returns this if none of the keys
210
+ * existed. Performance note: due to the architecture of this class,
211
+ * node(s) leading to where the key would have been stored are cloned
212
+ * even when the key turns out not to exist.
213
+ */
214
+ withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): this;
215
+ /** Returns a copy of the tree with the specified range of keys removed. */
216
+ withoutRange(low: K, high: K, includeHigh: boolean, returnThisIfUnchanged?: boolean): this;
217
+ /** Returns a copy of the tree with pairs removed whenever the callback
218
+ * function returns false. `where()` is a synonym for this method. */
219
+ filter(callback: (k: K, v: V, counter: number) => boolean, returnThisIfUnchanged?: boolean): this;
220
+ /** Returns a copy of the tree with all values altered by a callback function. */
221
+ mapValues<R>(callback: (v: V, k: K, counter: number) => R): BTree<K, R>;
222
+ /** Performs a reduce operation like the `reduce` method of `Array`.
223
+ * It is used to combine all pairs into a single value, or perform
224
+ * conversions. `reduce` is best understood by example. For example,
225
+ * `tree.reduce((P, pair) => P * pair[0], 1)` multiplies all keys
226
+ * together. It means "start with P=1, and for each pair multiply
227
+ * it by the key in pair[0]". Another example would be converting
228
+ * the tree to a Map (in this example, note that M.set returns M):
229
+ *
230
+ * var M = tree.reduce((M, pair) => M.set(pair[0],pair[1]), new Map())
231
+ *
232
+ * **Note**: the same array is sent to the callback on every iteration.
233
+ */
234
+ reduce<R>(callback: (previous: R, currentPair: [K, V], counter: number, tree: BTree<K, V>) => R, initialValue: R): R;
235
+ reduce<R>(callback: (previous: R | undefined, currentPair: [K, V], counter: number, tree: BTree<K, V>) => R): R | undefined;
236
+ /** Returns an iterator that provides items in order (ascending order if
237
+ * the collection's comparator uses ascending order, as is the default.)
238
+ * @param lowestKey First key to be iterated, or undefined to start at
239
+ * minKey(). If the specified key doesn't exist then iteration
240
+ * starts at the next higher key (according to the comparator).
241
+ * @param reusedArray Optional array used repeatedly to store key-value
242
+ * pairs, to avoid creating a new array on every iteration.
243
+ */
244
+ entries(lowestKey?: K, reusedArray?: (K | V)[]): IterableIterator<[K, V]>;
245
+ /** Returns an iterator that provides items in reversed order.
246
+ * @param highestKey Key at which to start iterating, or undefined to
247
+ * start at maxKey(). If the specified key doesn't exist then iteration
248
+ * starts at the next lower key (according to the comparator).
249
+ * @param reusedArray Optional array used repeatedly to store key-value
250
+ * pairs, to avoid creating a new array on every iteration.
251
+ * @param skipHighest Iff this flag is true and the highestKey exists in the
252
+ * collection, the pair matching highestKey is skipped, not iterated.
253
+ */
254
+ entriesReversed(highestKey?: K, reusedArray?: (K | V)[], skipHighest?: boolean): IterableIterator<[K, V]>;
255
+ private findPath;
256
+ /** Returns a new iterator for iterating the keys of each pair in ascending order.
257
+ * @param firstKey: Minimum key to include in the output. */
258
+ keys(firstKey?: K): IterableIterator<K>;
259
+ /** Returns a new iterator for iterating the values of each pair in order by key.
260
+ * @param firstKey: Minimum key whose associated value is included in the output. */
261
+ values(firstKey?: K): IterableIterator<V>;
262
+ /** Returns the maximum number of children/values before nodes will split. */
263
+ get maxNodeSize(): number;
264
+ /** Gets the lowest key in the tree. Complexity: O(log size) */
265
+ minKey(): K | undefined;
266
+ /** Gets the highest key in the tree. Complexity: O(1) */
267
+ maxKey(): K | undefined;
268
+ /** Quickly clones the tree by marking the root node as shared.
269
+ * Both copies remain editable. When you modify either copy, any
270
+ * nodes that are shared (or potentially shared) between the two
271
+ * copies are cloned so that the changes do not affect other copies.
272
+ * This is known as copy-on-write behavior, or "lazy copying". */
273
+ clone(): this;
274
+ /** Performs a greedy clone, immediately duplicating any nodes that are
275
+ * not currently marked as shared, in order to avoid marking any
276
+ * additional nodes as shared.
277
+ * @param force Clone all nodes, even shared ones.
278
+ */
279
+ greedyClone(force?: boolean): this;
280
+ /** Gets an array filled with the contents of the tree, sorted by key */
281
+ toArray(maxLength?: number): [K, V][];
282
+ /** Gets an array of all keys, sorted */
283
+ keysArray(): K[];
284
+ /** Gets an array of all values, sorted by key */
285
+ valuesArray(): V[];
286
+ /** Gets a string representing the tree's data based on toArray(). */
287
+ toString(): string;
288
+ /** Stores a key-value pair only if the key doesn't already exist in the tree.
289
+ * @returns true if a new key was added
290
+ */
291
+ setIfNotPresent(key: K, value: V): boolean;
292
+ /** Returns the next pair whose key is larger than the specified key (or undefined if there is none).
293
+ * If key === undefined, this function returns the lowest pair.
294
+ * @param key The key to search for.
295
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
296
+ * avoid creating a new array on every iteration.
297
+ */
298
+ nextHigherPair(key: K | undefined, reusedArray?: [K, V]): [K, V] | undefined;
299
+ /** Returns the next key larger than the specified key, or undefined if there is none.
300
+ * Also, nextHigherKey(undefined) returns the lowest key.
301
+ */
302
+ nextHigherKey(key: K | undefined): K | undefined;
303
+ /** Returns the next pair whose key is smaller than the specified key (or undefined if there is none).
304
+ * If key === undefined, this function returns the highest pair.
305
+ * @param key The key to search for.
306
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
307
+ * avoid creating a new array each time you call this method.
308
+ */
309
+ nextLowerPair(key: K | undefined, reusedArray?: [K, V]): [K, V] | undefined;
310
+ /** Returns the next key smaller than the specified key, or undefined if there is none.
311
+ * Also, nextLowerKey(undefined) returns the highest key.
312
+ */
313
+ nextLowerKey(key: K | undefined): K | undefined;
314
+ /** Returns the key-value pair associated with the supplied key if it exists
315
+ * or the pair associated with the next lower pair otherwise. If there is no
316
+ * next lower pair, undefined is returned.
317
+ * @param key The key to search for.
318
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
319
+ * avoid creating a new array each time you call this method.
320
+ * */
321
+ getPairOrNextLower(key: K, reusedArray?: [K, V]): [K, V] | undefined;
322
+ /** Returns the key-value pair associated with the supplied key if it exists
323
+ * or the pair associated with the next lower pair otherwise. If there is no
324
+ * next lower pair, undefined is returned.
325
+ * @param key The key to search for.
326
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
327
+ * avoid creating a new array each time you call this method.
328
+ * */
329
+ getPairOrNextHigher(key: K, reusedArray?: [K, V]): [K, V] | undefined;
330
+ /** Edits the value associated with a key in the tree, if it already exists.
331
+ * @returns true if the key existed, false if not.
332
+ */
333
+ changeIfPresent(key: K, value: V): boolean;
334
+ /**
335
+ * Builds an array of pairs from the specified range of keys, sorted by key.
336
+ * Each returned pair is also an array: pair[0] is the key, pair[1] is the value.
337
+ * @param low The first key in the array will be greater than or equal to `low`.
338
+ * @param high This method returns when a key larger than this is reached.
339
+ * @param includeHigh If the `high` key is present, its pair will be included
340
+ * in the output if and only if this parameter is true. Note: if the
341
+ * `low` key is present, it is always included in the output.
342
+ * @param maxLength Length limit. getRange will stop scanning the tree when
343
+ * the array reaches this size.
344
+ * @description Computational complexity: O(result.length + log size)
345
+ */
346
+ getRange(low: K, high: K, includeHigh?: boolean, maxLength?: number): [K, V][];
347
+ /** Adds all pairs from a list of key-value pairs.
348
+ * @param pairs Pairs to add to this tree. If there are duplicate keys,
349
+ * later pairs currently overwrite earlier ones (e.g. [[0,1],[0,7]]
350
+ * associates 0 with 7.)
351
+ * @param overwrite Whether to overwrite pairs that already exist (if false,
352
+ * pairs[i] is ignored when the key pairs[i][0] already exists.)
353
+ * @returns The number of pairs added to the collection.
354
+ * @description Computational complexity: O(pairs.length * log(size + pairs.length))
355
+ */
356
+ setPairs(pairs: [K, V][], overwrite?: boolean): number;
357
+ forRange(low: K, high: K, includeHigh: boolean, onFound?: (k: K, v: V, counter: number) => void, initialCounter?: number): number;
358
+ /**
359
+ * Scans and potentially modifies values for a subsequence of keys.
360
+ * Note: the callback `onFound` should ideally be a pure function.
361
+ * Specfically, it must not insert items, call clone(), or change
362
+ * the collection except via return value; out-of-band editing may
363
+ * cause an exception or may cause incorrect data to be sent to
364
+ * the callback (duplicate or missed items). It must not cause a
365
+ * clone() of the collection, otherwise the clone could be modified
366
+ * by changes requested by the callback.
367
+ * @param low The first key scanned will be greater than or equal to `low`.
368
+ * @param high Scanning stops when a key larger than this is reached.
369
+ * @param includeHigh If the `high` key is present, `onFound` is called for
370
+ * that final pair if and only if this parameter is true.
371
+ * @param onFound A function that is called for each key-value pair. This
372
+ * function can return `{value:v}` to change the value associated
373
+ * with the current key, `{delete:true}` to delete the current pair,
374
+ * `{break:R}` to stop early with result R, or it can return nothing
375
+ * (undefined or {}) to cause no effect and continue iterating.
376
+ * `{break:R}` can be combined with one of the other two commands.
377
+ * The third argument `counter` is the number of items iterated
378
+ * previously; it equals 0 when `onFound` is called the first time.
379
+ * @returns The number of values scanned, or R if the callback returned
380
+ * `{break:R}` to stop early.
381
+ * @description
382
+ * Computational complexity: O(number of items scanned + log size)
383
+ * Note: if the tree has been cloned with clone(), any shared
384
+ * nodes are copied before `onFound` is called. This takes O(n) time
385
+ * where n is proportional to the amount of shared data scanned.
386
+ */
387
+ editRange<R = V>(low: K, high: K, includeHigh: boolean, onFound: (k: K, v: V, counter: number) => EditRangeResult<V, R> | void, initialCounter?: number): R | number;
388
+ /** Same as `editRange` except that the callback is called for all pairs. */
389
+ editAll<R = V>(onFound: (k: K, v: V, counter: number) => EditRangeResult<V, R> | void, initialCounter?: number): R | number;
390
+ /**
391
+ * Removes a range of key-value pairs from the B+ tree.
392
+ * @param low The first key scanned will be greater than or equal to `low`.
393
+ * @param high Scanning stops when a key larger than this is reached.
394
+ * @param includeHigh Specifies whether the `high` key, if present, is deleted.
395
+ * @returns The number of key-value pairs that were deleted.
396
+ * @description Computational complexity: O(log size + number of items deleted)
397
+ */
398
+ deleteRange(low: K, high: K, includeHigh: boolean): number;
399
+ /** Deletes a series of keys from the collection. */
400
+ deleteKeys(keys: K[]): number;
401
+ /** Gets the height of the tree: the number of internal nodes between the
402
+ * BTree object and its leaf nodes (zero if there are no internal nodes). */
403
+ get height(): number;
404
+ /** Makes the object read-only to ensure it is not accidentally modified.
405
+ * Freezing does not have to be permanent; unfreeze() reverses the effect.
406
+ * This is accomplished by replacing mutator functions with a function
407
+ * that throws an Error. Compared to using a property (e.g. this.isFrozen)
408
+ * this implementation gives better performance in non-frozen BTrees.
409
+ */
410
+ freeze(): void;
411
+ /** Ensures mutations are allowed, reversing the effect of freeze(). */
412
+ unfreeze(): void;
413
+ /** Returns true if the tree appears to be frozen. */
414
+ get isFrozen(): boolean;
415
+ /** Scans the tree for signs of serious bugs (e.g. this.size doesn't match
416
+ * number of elements, internal nodes not caching max element properly...).
417
+ * Computational complexity: O(number of nodes). This method validates cached size
418
+ * information and, optionally, the ordering of keys (including leaves), which
419
+ * takes more time to check (O(size), which is technically the same big-O). */
420
+ checkValid(checkOrdering?: boolean): void;
421
+ }
422
+ /** A TypeScript helper function that simply returns its argument, typed as
423
+ * `ISortedSet<K>` if the BTree implements it, as it does if `V extends undefined`.
424
+ * If `V` cannot be `undefined`, it returns `unknown` instead. Or at least, that
425
+ * was the intention, but TypeScript is acting weird and may return `ISortedSet<K>`
426
+ * even if `V` can't be `undefined` (discussion: btree-typescript issue #14) */
427
+ export declare function asSet<K, V>(btree: BTree<K, V>): undefined extends V ? ISortedSet<K> : unknown;
428
+ /** A BTree frozen in the empty state. */
429
+ export declare const EmptyBTree: BTree<any, any>;