atomic-fns 0.3.1 → 0.3.3
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.
- package/.esdoc.json +2 -2
- package/README.md +48 -13
- package/core/collections/BTree.d.ts +229 -0
- package/core/collections/BTree.js +1165 -0
- package/core/collections/BTree.ts +1340 -0
- package/core/collections/Heap.d.ts +69 -0
- package/core/collections/Heap.js +181 -0
- package/core/collections/Heap.ts +189 -0
- package/core/collections/LRUCache.d.ts +131 -0
- package/core/collections/LRUCache.js +388 -0
- package/core/collections/LRUCache.ts +465 -0
- package/core/collections/SortedMap.d.ts +92 -0
- package/core/collections/SortedMap.js +130 -0
- package/core/collections/SortedMap.ts +146 -0
- package/core/collections/SortedSet.d.ts +63 -0
- package/core/collections/SortedSet.js +93 -0
- package/core/collections/SortedSet.ts +104 -0
- package/core/collections/SortedTree.d.ts +103 -0
- package/core/collections/SortedTree.js +141 -0
- package/core/collections/SortedTree.ts +160 -0
- package/core/collections/SplayTree.d.ts +105 -0
- package/core/collections/SplayTree.js +338 -0
- package/core/collections/SplayTree.ts +354 -0
- package/core/collections/abc.d.ts +152 -0
- package/core/collections/abc.js +39 -0
- package/core/collections/abc.ts +176 -0
- package/core/collections/deque.d.ts +110 -0
- package/core/collections/deque.js +285 -0
- package/core/collections/deque.ts +329 -0
- package/core/collections/frozenset.d.ts +19 -0
- package/core/collections/frozenset.js +33 -0
- package/core/collections/frozenset.ts +37 -0
- package/{src → core}/collections/index.d.ts +41 -162
- package/{src → core}/collections/index.js +57 -165
- package/{src → core}/collections/index.ts +61 -225
- package/core/collections/test.html +10 -0
- package/core/collections/typedArrays.d.ts +31 -0
- package/core/collections/typedArrays.js +143 -0
- package/core/collections/typedArrays.ts +163 -0
- package/core/datetime/index.d.ts +45 -0
- package/core/datetime/index.js +71 -0
- package/core/datetime/index.ts +128 -0
- package/{src → core}/functools/index.d.ts +2 -2
- package/{src → core}/functools/index.js +2 -2
- package/{src → core}/functools/index.ts +2 -2
- package/{src → core}/globals/index.d.ts +2 -7
- package/{src → core}/globals/index.js +5 -2
- package/{src → core}/globals/index.ts +6 -9
- package/{src → core}/index.d.ts +0 -1
- package/{src → core}/index.js +0 -1
- package/{src → core}/index.ts +0 -1
- package/{src → core}/itertools/index.d.ts +9 -7
- package/{src → core}/itertools/index.js +25 -9
- package/{src → core}/itertools/index.ts +39 -16
- package/{src → core}/math/index.d.ts +0 -0
- package/{src → core}/math/index.js +0 -0
- package/{src → core}/math/index.ts +0 -0
- package/core/operators/index.d.ts +148 -0
- package/core/operators/index.js +271 -0
- package/core/operators/index.ts +282 -0
- package/{src → core}/random/index.d.ts +0 -0
- package/{src → core}/random/index.js +0 -0
- package/{src → core}/random/index.ts +0 -0
- package/package.json +6 -6
- package/tests/BTree.spec.ts +143 -0
- package/tests/Cache.spec.ts +161 -0
- package/tests/Heap.spec.ts +66 -0
- package/tests/SortedMap.spec.ts +144 -0
- package/tests/SortedSet.spec.ts +84 -0
- package/tests/SortedTree.spec.ts +164 -0
- package/tests/SplayTree.spec.ts +121 -0
- package/tests/collections.spec.ts +1 -1
- package/tests/deque.spec.ts +107 -0
- package/tests/globals.spec.ts +2 -2
- package/tests/itertools.spec.ts +2 -2
- package/tests/math.spec.ts +1 -1
- package/tests/operators.spec.ts +5 -5
- package/tests/random.spec.ts +10 -10
- package/tsconfig.json +10 -10
- package/tsconfig.node.json +2 -2
- package/tsconfig.xo.json +2 -2
- package/src/datetime/index.d.ts +0 -6
- package/src/datetime/index.js +0 -6
- package/src/datetime/index.ts +0 -7
- package/src/operators/index.d.ts +0 -31
- package/src/operators/index.js +0 -171
- package/src/operators/index.ts +0 -175
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { Mapping } from './abc.js'
|
|
2
|
+
import { SplayTree } from './SplayTree.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A `SortedMap` is a map that holds key value pairs in sorted orders of keys. Any primitive value (and objects that implement a `compare` or `lt` methods) can be used as keys or values.
|
|
6
|
+
* @see {@link Operators.compare}
|
|
7
|
+
* @see {@link Operators.lt}
|
|
8
|
+
* @template K, V
|
|
9
|
+
*/
|
|
10
|
+
export class SortedMap<K, V> extends Mapping<K, V> {
|
|
11
|
+
private readonly store: SplayTree<K, V>
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new `SortedMap` object.
|
|
15
|
+
* @param {Iterable<[K, V]>} [iterable] Initialize the map with entries.
|
|
16
|
+
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
17
|
+
*/
|
|
18
|
+
constructor(iterable?: Iterable<[K, V]>, compareFn?) {
|
|
19
|
+
super()
|
|
20
|
+
this.store = new SplayTree(compareFn)
|
|
21
|
+
if (iterable) {
|
|
22
|
+
for (const pair of iterable) {
|
|
23
|
+
this.store.set(pair[0], pair[1] ?? (null as V))
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Adds a new `key` with a `null` value, if the `key` doesn't exist.
|
|
30
|
+
* @param {K} key
|
|
31
|
+
* @returns {this} Returns the Map object.
|
|
32
|
+
*/
|
|
33
|
+
add(key: K) {
|
|
34
|
+
this.store.set(key, null as V)
|
|
35
|
+
return this
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns the value associated with the `key` or `undefined`.
|
|
40
|
+
* @param {K} key
|
|
41
|
+
* @returns {?V} The key value or `undefined`.
|
|
42
|
+
*/
|
|
43
|
+
get(key: K) {
|
|
44
|
+
return this.store.get(key)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Sets the value for the given `key`.
|
|
49
|
+
* @param {K} key
|
|
50
|
+
* @param {V} value
|
|
51
|
+
* @returns {this} Returns the Map object.
|
|
52
|
+
*/
|
|
53
|
+
set(key: K, value: V) {
|
|
54
|
+
return this.store.set(key, value)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns the smallest key in the tree.
|
|
59
|
+
* @returns {?K}
|
|
60
|
+
*/
|
|
61
|
+
min() {
|
|
62
|
+
return this.store.min()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns the largest key in the tree.
|
|
67
|
+
* @returns {?K}
|
|
68
|
+
*/
|
|
69
|
+
max() {
|
|
70
|
+
return this.store.max()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns `true` if the `key` existed in the map and was removed.
|
|
75
|
+
* @param {K} key
|
|
76
|
+
* @returns {boolean} `true` if key was removed, otherwise `false`.
|
|
77
|
+
*/
|
|
78
|
+
delete(item: K) {
|
|
79
|
+
return this.store.remove(item) !== undefined
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Removes all key/value pairs in the tree.
|
|
84
|
+
*/
|
|
85
|
+
clear() {
|
|
86
|
+
return this.store.clear()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the total key/value pairs in the tree.
|
|
91
|
+
* @returns {number}
|
|
92
|
+
*/
|
|
93
|
+
get size() {
|
|
94
|
+
return this.store.size
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Check if there are no key/value pairs in the tree.
|
|
99
|
+
* @returns {boolean} `true` if is empty.
|
|
100
|
+
*/
|
|
101
|
+
empty() {
|
|
102
|
+
return this.store.empty()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns `true` if the `key` exists in the map.
|
|
107
|
+
* @param {K} key
|
|
108
|
+
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
109
|
+
*/
|
|
110
|
+
contains(key: K) {
|
|
111
|
+
return this.store.contains(key)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Returns `true` if the `key` exists in the map.
|
|
116
|
+
* @param {K} key
|
|
117
|
+
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
118
|
+
*/
|
|
119
|
+
has(key: K) {
|
|
120
|
+
return this.store.contains(key)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns a new iterator of all **ordered** keys in the map.
|
|
125
|
+
* @returns {Iterable<K>}
|
|
126
|
+
*/
|
|
127
|
+
keys() {
|
|
128
|
+
return this.store.keys()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returns a new iterator of all values **ordered** by their keys.
|
|
133
|
+
* @returns {Iterable<V>}
|
|
134
|
+
*/
|
|
135
|
+
values() {
|
|
136
|
+
return this.store.values()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Returns a new iterator of all key value pairs **ordered** by their keys.
|
|
141
|
+
* @returns {Iterable<[K, V]>}
|
|
142
|
+
*/
|
|
143
|
+
entries() {
|
|
144
|
+
return this.store.entries()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Collection } from './abc.js';
|
|
2
|
+
export declare class SortedSet<T> extends Collection {
|
|
3
|
+
private readonly store;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new `SortedSet` object.
|
|
6
|
+
* @param {Iterable<T>} [iterable] Initialize the set with entries.
|
|
7
|
+
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
8
|
+
*/
|
|
9
|
+
constructor(iterable?: Iterable<T>, compareFn?: any);
|
|
10
|
+
/**
|
|
11
|
+
* Inserts a new value in order in the `Set`, if the value doesn't exist.
|
|
12
|
+
* @param {T} value
|
|
13
|
+
* @returns {this} Returns the Set object.
|
|
14
|
+
*/
|
|
15
|
+
add(value: T): this;
|
|
16
|
+
/**
|
|
17
|
+
* Returns the smallest value in the set.
|
|
18
|
+
* @returns {?T}
|
|
19
|
+
*/
|
|
20
|
+
min(): T | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the largest value in the set.
|
|
23
|
+
* @returns {?T}
|
|
24
|
+
*/
|
|
25
|
+
max(): T | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Removes the value from the set and returns `true` if the value was found and removed.
|
|
28
|
+
* @param {T} value
|
|
29
|
+
* @returns {boolean} `true` if the value was found and removed.
|
|
30
|
+
*/
|
|
31
|
+
delete(value: T): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Removes all values in the set.
|
|
34
|
+
*/
|
|
35
|
+
clear(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the total number of elements in the set.
|
|
38
|
+
* @returns {number}
|
|
39
|
+
*/
|
|
40
|
+
get size(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Check if there are no values in the set.
|
|
43
|
+
* @returns {boolean} `true` if is empty.
|
|
44
|
+
*/
|
|
45
|
+
empty(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` if the `value` exists.
|
|
48
|
+
* @param {T} value
|
|
49
|
+
* @returns {boolean} `true` if value is found, otherwise `false`.
|
|
50
|
+
*/
|
|
51
|
+
contains(value: T): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Returns `true` if the `value` exists.
|
|
54
|
+
* @param {T} value
|
|
55
|
+
* @returns {boolean} `true` if value is found, otherwise `false`.
|
|
56
|
+
*/
|
|
57
|
+
has(value: T): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Returns a new iterator of all values **in-order**.
|
|
60
|
+
* @returns {Iterable<T>}
|
|
61
|
+
*/
|
|
62
|
+
values(): Iterable<T>;
|
|
63
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Collection } from './abc.js';
|
|
2
|
+
import { SplayTree } from './SplayTree.js';
|
|
3
|
+
export class SortedSet extends Collection {
|
|
4
|
+
store;
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new `SortedSet` object.
|
|
7
|
+
* @param {Iterable<T>} [iterable] Initialize the set with entries.
|
|
8
|
+
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
9
|
+
*/
|
|
10
|
+
constructor(iterable, compareFn) {
|
|
11
|
+
super();
|
|
12
|
+
this.store = new SplayTree(compareFn);
|
|
13
|
+
if (iterable) {
|
|
14
|
+
for (const key of iterable) {
|
|
15
|
+
this.store.add(key);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Inserts a new value in order in the `Set`, if the value doesn't exist.
|
|
21
|
+
* @param {T} value
|
|
22
|
+
* @returns {this} Returns the Set object.
|
|
23
|
+
*/
|
|
24
|
+
add(value) {
|
|
25
|
+
this.store.add(value);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns the smallest value in the set.
|
|
30
|
+
* @returns {?T}
|
|
31
|
+
*/
|
|
32
|
+
min() {
|
|
33
|
+
return this.store.min();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the largest value in the set.
|
|
37
|
+
* @returns {?T}
|
|
38
|
+
*/
|
|
39
|
+
max() {
|
|
40
|
+
return this.store.max();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Removes the value from the set and returns `true` if the value was found and removed.
|
|
44
|
+
* @param {T} value
|
|
45
|
+
* @returns {boolean} `true` if the value was found and removed.
|
|
46
|
+
*/
|
|
47
|
+
delete(value) {
|
|
48
|
+
return this.store.remove(value) !== undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Removes all values in the set.
|
|
52
|
+
*/
|
|
53
|
+
clear() {
|
|
54
|
+
return this.store.clear();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns the total number of elements in the set.
|
|
58
|
+
* @returns {number}
|
|
59
|
+
*/
|
|
60
|
+
get size() {
|
|
61
|
+
return this.store.size;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if there are no values in the set.
|
|
65
|
+
* @returns {boolean} `true` if is empty.
|
|
66
|
+
*/
|
|
67
|
+
empty() {
|
|
68
|
+
return this.store.empty();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns `true` if the `value` exists.
|
|
72
|
+
* @param {T} value
|
|
73
|
+
* @returns {boolean} `true` if value is found, otherwise `false`.
|
|
74
|
+
*/
|
|
75
|
+
contains(value) {
|
|
76
|
+
return this.store.contains(value);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Returns `true` if the `value` exists.
|
|
80
|
+
* @param {T} value
|
|
81
|
+
* @returns {boolean} `true` if value is found, otherwise `false`.
|
|
82
|
+
*/
|
|
83
|
+
has(value) {
|
|
84
|
+
return this.store.contains(value);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns a new iterator of all values **in-order**.
|
|
88
|
+
* @returns {Iterable<T>}
|
|
89
|
+
*/
|
|
90
|
+
values() {
|
|
91
|
+
return this.store.keys();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Collection } from './abc.js'
|
|
2
|
+
import { SplayTree } from './SplayTree.js'
|
|
3
|
+
|
|
4
|
+
export class SortedSet<T> extends Collection {
|
|
5
|
+
private readonly store: SplayTree<T>
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new `SortedSet` object.
|
|
8
|
+
* @param {Iterable<T>} [iterable] Initialize the set with entries.
|
|
9
|
+
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
10
|
+
*/
|
|
11
|
+
constructor(iterable?: Iterable<T>, compareFn?) {
|
|
12
|
+
super()
|
|
13
|
+
this.store = new SplayTree(compareFn)
|
|
14
|
+
if (iterable) {
|
|
15
|
+
for (const key of iterable) {
|
|
16
|
+
this.store.add(key)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Inserts a new value in order in the `Set`, if the value doesn't exist.
|
|
23
|
+
* @param {T} value
|
|
24
|
+
* @returns {this} Returns the Set object.
|
|
25
|
+
*/
|
|
26
|
+
add(value: T) {
|
|
27
|
+
this.store.add(value)
|
|
28
|
+
return this
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns the smallest value in the set.
|
|
33
|
+
* @returns {?T}
|
|
34
|
+
*/
|
|
35
|
+
min() {
|
|
36
|
+
return this.store.min()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns the largest value in the set.
|
|
41
|
+
* @returns {?T}
|
|
42
|
+
*/
|
|
43
|
+
max() {
|
|
44
|
+
return this.store.max()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes the value from the set and returns `true` if the value was found and removed.
|
|
49
|
+
* @param {T} value
|
|
50
|
+
* @returns {boolean} `true` if the value was found and removed.
|
|
51
|
+
*/
|
|
52
|
+
delete(value: T) {
|
|
53
|
+
return this.store.remove(value) !== undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Removes all values in the set.
|
|
58
|
+
*/
|
|
59
|
+
clear() {
|
|
60
|
+
return this.store.clear()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns the total number of elements in the set.
|
|
65
|
+
* @returns {number}
|
|
66
|
+
*/
|
|
67
|
+
get size() {
|
|
68
|
+
return this.store.size
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if there are no values in the set.
|
|
73
|
+
* @returns {boolean} `true` if is empty.
|
|
74
|
+
*/
|
|
75
|
+
empty() {
|
|
76
|
+
return this.store.empty()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Returns `true` if the `value` exists.
|
|
81
|
+
* @param {T} value
|
|
82
|
+
* @returns {boolean} `true` if value is found, otherwise `false`.
|
|
83
|
+
*/
|
|
84
|
+
contains(value: T) {
|
|
85
|
+
return this.store.contains(value)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns `true` if the `value` exists.
|
|
90
|
+
* @param {T} value
|
|
91
|
+
* @returns {boolean} `true` if value is found, otherwise `false`.
|
|
92
|
+
*/
|
|
93
|
+
has(value: T) {
|
|
94
|
+
return this.store.contains(value)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns a new iterator of all values **in-order**.
|
|
99
|
+
* @returns {Iterable<T>}
|
|
100
|
+
*/
|
|
101
|
+
values() {
|
|
102
|
+
return this.store.keys()
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Comparer } from '../operators/index.js';
|
|
2
|
+
import { Mapping } from './abc.js';
|
|
3
|
+
/**
|
|
4
|
+
* A `SortedTree` is a map that holds key value pairs in sorted orders of keys. Any primitive value (and objects that implement a `compare` or `lt` methods) can be used as keys or values.
|
|
5
|
+
* @see {@link Operators.compare}
|
|
6
|
+
* @see {@link Operators.lt}
|
|
7
|
+
* @template K, V
|
|
8
|
+
*/
|
|
9
|
+
export declare class SortedTree<K, V> extends Mapping<K, V> {
|
|
10
|
+
private readonly store;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new `SortedTree` object.
|
|
13
|
+
* @param {Iterable<[K, V]>} [iterable] Initialize the tree with entries.
|
|
14
|
+
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
15
|
+
*/
|
|
16
|
+
constructor(iterable?: Iterable<[K, V]>, compareFn?: Comparer);
|
|
17
|
+
/**
|
|
18
|
+
* Adds a new `key` with a `null` value.
|
|
19
|
+
* @param {K} key
|
|
20
|
+
* @returns {this} Returns the Tree object.
|
|
21
|
+
*/
|
|
22
|
+
add(key: K): this;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the value associated with the `key` or `undefined`.
|
|
25
|
+
* @param {K} key
|
|
26
|
+
* @returns {?V} The key value or `undefined`.
|
|
27
|
+
*/
|
|
28
|
+
get(key: K): V | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Sets the value for the given `key`.
|
|
31
|
+
* @param {K} key
|
|
32
|
+
* @param {V} value
|
|
33
|
+
* @returns {this} Returns the Tree object.
|
|
34
|
+
*/
|
|
35
|
+
set(key: K, value: V): this;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the smallest key in the tree.
|
|
38
|
+
* @returns {?K}
|
|
39
|
+
*/
|
|
40
|
+
min(): K | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the largest key in the tree.
|
|
43
|
+
* @returns {?K}
|
|
44
|
+
*/
|
|
45
|
+
max(): K;
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` if the `key` existed in the tree and was removed.
|
|
48
|
+
* @param {K} key
|
|
49
|
+
* @returns {boolean} `true` if key was removed, otherwise `false`.
|
|
50
|
+
*/
|
|
51
|
+
delete(key: K): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Removes all key/value pairs in the tree.
|
|
54
|
+
*/
|
|
55
|
+
clear(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Freezes the tree which prevents adding, updating or removing any keys.
|
|
58
|
+
* @returns {this}
|
|
59
|
+
*/
|
|
60
|
+
freeze(): this;
|
|
61
|
+
/**
|
|
62
|
+
* Returns a new iterator of `[K, V]` pairs in reverse order of keys.
|
|
63
|
+
* @returns {Iterable<[K, V]>}
|
|
64
|
+
*/
|
|
65
|
+
reversed(): IterableIterator<[K, V]>;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the total key/value pairs in the tree.
|
|
68
|
+
* @returns {number}
|
|
69
|
+
*/
|
|
70
|
+
get size(): number;
|
|
71
|
+
/**
|
|
72
|
+
* Check if there are no key/value pairs in the tree.
|
|
73
|
+
* @returns {boolean} `true` if is empty.
|
|
74
|
+
*/
|
|
75
|
+
empty(): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Returns `true` if the `key` exists in the tree.
|
|
78
|
+
* @param {K} key
|
|
79
|
+
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
80
|
+
*/
|
|
81
|
+
contains(key: K): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Returns `true` if the `key` exists in the tree.
|
|
84
|
+
* @param {K} key
|
|
85
|
+
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
86
|
+
*/
|
|
87
|
+
has(key: K): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Returns a new iterator of all **ordered** keys in the tree.
|
|
90
|
+
* @returns {Iterable<K>}
|
|
91
|
+
*/
|
|
92
|
+
keys(): IterableIterator<K>;
|
|
93
|
+
/**
|
|
94
|
+
* Returns a new iterator of all values **ordered** by their keys.
|
|
95
|
+
* @returns {Iterable<V>}
|
|
96
|
+
*/
|
|
97
|
+
values(): IterableIterator<V>;
|
|
98
|
+
/**
|
|
99
|
+
* Returns a new iterator of all key value pairs **ordered** by their keys.
|
|
100
|
+
* @returns {Iterable<[K, V]>}
|
|
101
|
+
*/
|
|
102
|
+
entries(): IterableIterator<[K, V]>;
|
|
103
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Mapping } from './abc.js';
|
|
2
|
+
import { BTree } from './BTree.js';
|
|
3
|
+
/**
|
|
4
|
+
* A `SortedTree` is a map that holds key value pairs in sorted orders of keys. Any primitive value (and objects that implement a `compare` or `lt` methods) can be used as keys or values.
|
|
5
|
+
* @see {@link Operators.compare}
|
|
6
|
+
* @see {@link Operators.lt}
|
|
7
|
+
* @template K, V
|
|
8
|
+
*/
|
|
9
|
+
export class SortedTree extends Mapping {
|
|
10
|
+
store;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new `SortedTree` object.
|
|
13
|
+
* @param {Iterable<[K, V]>} [iterable] Initialize the tree with entries.
|
|
14
|
+
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
15
|
+
*/
|
|
16
|
+
constructor(iterable, compareFn) {
|
|
17
|
+
super();
|
|
18
|
+
this.store = new BTree(iterable, compareFn);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Adds a new `key` with a `null` value.
|
|
22
|
+
* @param {K} key
|
|
23
|
+
* @returns {this} Returns the Tree object.
|
|
24
|
+
*/
|
|
25
|
+
add(key) {
|
|
26
|
+
this.store.set(key, null);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the value associated with the `key` or `undefined`.
|
|
31
|
+
* @param {K} key
|
|
32
|
+
* @returns {?V} The key value or `undefined`.
|
|
33
|
+
*/
|
|
34
|
+
get(key) {
|
|
35
|
+
return this.store.get(key);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Sets the value for the given `key`.
|
|
39
|
+
* @param {K} key
|
|
40
|
+
* @param {V} value
|
|
41
|
+
* @returns {this} Returns the Tree object.
|
|
42
|
+
*/
|
|
43
|
+
set(key, value) {
|
|
44
|
+
this.store.set(key, value);
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns the smallest key in the tree.
|
|
49
|
+
* @returns {?K}
|
|
50
|
+
*/
|
|
51
|
+
min() {
|
|
52
|
+
return this.store.minKey();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns the largest key in the tree.
|
|
56
|
+
* @returns {?K}
|
|
57
|
+
*/
|
|
58
|
+
max() {
|
|
59
|
+
return this.store.maxKey();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns `true` if the `key` existed in the tree and was removed.
|
|
63
|
+
* @param {K} key
|
|
64
|
+
* @returns {boolean} `true` if key was removed, otherwise `false`.
|
|
65
|
+
*/
|
|
66
|
+
delete(key) {
|
|
67
|
+
return this.store.remove(key);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Removes all key/value pairs in the tree.
|
|
71
|
+
*/
|
|
72
|
+
clear() {
|
|
73
|
+
return this.store.clear();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Freezes the tree which prevents adding, updating or removing any keys.
|
|
77
|
+
* @returns {this}
|
|
78
|
+
*/
|
|
79
|
+
freeze() {
|
|
80
|
+
this.store.freeze();
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns a new iterator of `[K, V]` pairs in reverse order of keys.
|
|
85
|
+
* @returns {Iterable<[K, V]>}
|
|
86
|
+
*/
|
|
87
|
+
reversed() {
|
|
88
|
+
return this.store.reversed();
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Returns the total key/value pairs in the tree.
|
|
92
|
+
* @returns {number}
|
|
93
|
+
*/
|
|
94
|
+
get size() {
|
|
95
|
+
return this.store.size;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if there are no key/value pairs in the tree.
|
|
99
|
+
* @returns {boolean} `true` if is empty.
|
|
100
|
+
*/
|
|
101
|
+
empty() {
|
|
102
|
+
return this.store.empty();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns `true` if the `key` exists in the tree.
|
|
106
|
+
* @param {K} key
|
|
107
|
+
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
108
|
+
*/
|
|
109
|
+
contains(key) {
|
|
110
|
+
return this.store.contains(key);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns `true` if the `key` exists in the tree.
|
|
114
|
+
* @param {K} key
|
|
115
|
+
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
116
|
+
*/
|
|
117
|
+
has(key) {
|
|
118
|
+
return this.store.contains(key);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns a new iterator of all **ordered** keys in the tree.
|
|
122
|
+
* @returns {Iterable<K>}
|
|
123
|
+
*/
|
|
124
|
+
keys() {
|
|
125
|
+
return this.store.keys();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns a new iterator of all values **ordered** by their keys.
|
|
129
|
+
* @returns {Iterable<V>}
|
|
130
|
+
*/
|
|
131
|
+
values() {
|
|
132
|
+
return this.store.values();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns a new iterator of all key value pairs **ordered** by their keys.
|
|
136
|
+
* @returns {Iterable<[K, V]>}
|
|
137
|
+
*/
|
|
138
|
+
entries() {
|
|
139
|
+
return this.store.entries();
|
|
140
|
+
}
|
|
141
|
+
}
|