queue-typed 2.4.0 → 2.4.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.
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +12 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +12 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +12 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
- package/dist/umd/queue-typed.js +12 -0
- package/dist/umd/queue-typed.js.map +1 -1
- package/dist/umd/queue-typed.min.js +1 -1
- package/dist/umd/queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +45 -21
- package/src/data-structures/binary-tree/bst.ts +85 -10
- package/src/data-structures/binary-tree/index.ts +3 -3
- package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
- package/src/data-structures/binary-tree/tree-map.ts +439 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
- package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
- package/src/data-structures/binary-tree/tree-set.ts +407 -0
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/index.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
- package/src/data-structures/binary-tree/tree-counter.ts +0 -575
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
- package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TreeSet (ordered set) — a restricted, native-like API backed by RedBlackTree.
|
|
3
|
+
*
|
|
4
|
+
* Design goals:
|
|
5
|
+
* - No node exposure (no node inputs/outputs)
|
|
6
|
+
* - Native Set-like surface + Java NavigableSet-like helpers
|
|
7
|
+
* - Strict default comparator (number/string/Date), otherwise require comparator
|
|
8
|
+
*/
|
|
9
|
+
import type { Comparator } from '../../types';
|
|
10
|
+
import type { TreeSetElementCallback, TreeSetOptions, TreeSetRangeOptions, TreeSetReduceCallback } from '../../types';
|
|
11
|
+
/**
|
|
12
|
+
* An ordered Set backed by a red-black tree.
|
|
13
|
+
*
|
|
14
|
+
* - Iteration order is ascending by key.
|
|
15
|
+
* - No node exposure: all APIs use keys only.
|
|
16
|
+
*/
|
|
17
|
+
export declare class TreeSet<K = any, R = K> implements Iterable<K> {
|
|
18
|
+
#private;
|
|
19
|
+
/**
|
|
20
|
+
* Create a TreeSet from an iterable of keys or raw elements.
|
|
21
|
+
*
|
|
22
|
+
* @param elements - Iterable of keys, or raw elements if `toElementFn` is provided.
|
|
23
|
+
* @param options - Configuration options including optional `toElementFn` to transform raw elements.
|
|
24
|
+
* @throws {TypeError} When using the default comparator and encountering unsupported key types,
|
|
25
|
+
* or invalid keys (e.g. `NaN`, invalid `Date`).
|
|
26
|
+
* @example
|
|
27
|
+
* // Standard usage with keys
|
|
28
|
+
* const set = new TreeSet([3, 1, 2]);
|
|
29
|
+
*
|
|
30
|
+
* // Using toElementFn to transform raw objects
|
|
31
|
+
* const users = [{ id: 3, name: 'Alice' }, { id: 1, name: 'Bob' }];
|
|
32
|
+
* const set = new TreeSet<number, User>(users, { toElementFn: u => u.id });
|
|
33
|
+
*/
|
|
34
|
+
constructor(elements?: Iterable<R> | Iterable<K>, options?: TreeSetOptions<K, R>);
|
|
35
|
+
/**
|
|
36
|
+
* Create the strict default comparator.
|
|
37
|
+
*
|
|
38
|
+
* Supports:
|
|
39
|
+
* - `number` (rejects `NaN`; treats `-0` and `0` as equal)
|
|
40
|
+
* - `string`
|
|
41
|
+
* - `Date` (orders by `getTime()`, rejects invalid dates)
|
|
42
|
+
*
|
|
43
|
+
* For other key types, a custom comparator must be provided.
|
|
44
|
+
*/
|
|
45
|
+
static createDefaultComparator<K>(): Comparator<K>;
|
|
46
|
+
/**
|
|
47
|
+
* Number of elements in the set.
|
|
48
|
+
*/
|
|
49
|
+
get size(): number;
|
|
50
|
+
/**
|
|
51
|
+
* Whether the set is empty.
|
|
52
|
+
*/
|
|
53
|
+
isEmpty(): boolean;
|
|
54
|
+
private _validateKey;
|
|
55
|
+
/**
|
|
56
|
+
* Add a key to the set (no-op if already present).
|
|
57
|
+
* @remarks Expected time O(log n)
|
|
58
|
+
*/
|
|
59
|
+
add(key: K): this;
|
|
60
|
+
/**
|
|
61
|
+
* Test whether a key exists.
|
|
62
|
+
* @remarks Expected time O(log n)
|
|
63
|
+
*/
|
|
64
|
+
has(key: K): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Delete a key.
|
|
67
|
+
* @returns `true` if the key existed; otherwise `false`.
|
|
68
|
+
* @remarks Expected time O(log n)
|
|
69
|
+
*/
|
|
70
|
+
delete(key: K): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Remove all keys.
|
|
73
|
+
*/
|
|
74
|
+
clear(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Iterate over keys in ascending order.
|
|
77
|
+
*/
|
|
78
|
+
keys(): IterableIterator<K>;
|
|
79
|
+
/**
|
|
80
|
+
* Iterate over values in ascending order.
|
|
81
|
+
*
|
|
82
|
+
* Note: for Set-like containers, `values()` is the same as `keys()`.
|
|
83
|
+
*/
|
|
84
|
+
values(): IterableIterator<K>;
|
|
85
|
+
/**
|
|
86
|
+
* Iterate over `[value, value]` pairs (native Set convention).
|
|
87
|
+
*
|
|
88
|
+
* Note: TreeSet stores only keys internally; `[k, k]` is created on-the-fly during iteration.
|
|
89
|
+
*/
|
|
90
|
+
entries(): IterableIterator<[K, K]>;
|
|
91
|
+
[Symbol.iterator](): IterableIterator<K>;
|
|
92
|
+
/**
|
|
93
|
+
* Visit each value in ascending order.
|
|
94
|
+
*
|
|
95
|
+
* Callback follows native Set convention: `(value, value2, set)`.
|
|
96
|
+
*/
|
|
97
|
+
forEach(cb: (value: K, value2: K, set: TreeSet<K>) => void, thisArg?: any): void;
|
|
98
|
+
/**
|
|
99
|
+
* Create a new TreeSet by mapping each value to a new key.
|
|
100
|
+
*
|
|
101
|
+
* This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
|
|
102
|
+
* @remarks Time O(n log n) expected, Space O(n)
|
|
103
|
+
*/
|
|
104
|
+
map<MK>(callbackfn: TreeSetElementCallback<K, MK, TreeSet<K>>, options?: Omit<TreeSetOptions<MK>, 'toElementFn'> & {
|
|
105
|
+
comparator?: (a: MK, b: MK) => number;
|
|
106
|
+
}, thisArg?: unknown): TreeSet<MK>;
|
|
107
|
+
/**
|
|
108
|
+
* Create a new TreeSet containing only values that satisfy the predicate.
|
|
109
|
+
* @remarks Time O(n log n) expected, Space O(n)
|
|
110
|
+
*/
|
|
111
|
+
filter(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): TreeSet<K>;
|
|
112
|
+
/**
|
|
113
|
+
* Reduce values into a single accumulator.
|
|
114
|
+
* @remarks Time O(n), Space O(1)
|
|
115
|
+
*/
|
|
116
|
+
reduce<A>(callbackfn: TreeSetReduceCallback<K, A, TreeSet<K>>, initialValue: A): A;
|
|
117
|
+
/**
|
|
118
|
+
* Test whether all values satisfy a predicate.
|
|
119
|
+
* @remarks Time O(n), Space O(1)
|
|
120
|
+
*/
|
|
121
|
+
every(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Test whether any value satisfies a predicate.
|
|
124
|
+
* @remarks Time O(n), Space O(1)
|
|
125
|
+
*/
|
|
126
|
+
some(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Find the first value that satisfies a predicate.
|
|
129
|
+
* @remarks Time O(n), Space O(1)
|
|
130
|
+
*/
|
|
131
|
+
find(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): K | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* Materialize the set into an array of keys.
|
|
134
|
+
* @remarks Time O(n), Space O(n)
|
|
135
|
+
*/
|
|
136
|
+
toArray(): K[];
|
|
137
|
+
/**
|
|
138
|
+
* Print a human-friendly representation.
|
|
139
|
+
* @remarks Time O(n), Space O(n)
|
|
140
|
+
*/
|
|
141
|
+
print(): void;
|
|
142
|
+
/**
|
|
143
|
+
* Smallest key in the set.
|
|
144
|
+
*/
|
|
145
|
+
first(): K | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* Largest key in the set.
|
|
148
|
+
*/
|
|
149
|
+
last(): K | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* Remove and return the smallest key.
|
|
152
|
+
*/
|
|
153
|
+
pollFirst(): K | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Remove and return the largest key.
|
|
156
|
+
*/
|
|
157
|
+
pollLast(): K | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* Smallest key that is >= the given key.
|
|
160
|
+
*/
|
|
161
|
+
ceiling(key: K): K | undefined;
|
|
162
|
+
/**
|
|
163
|
+
* Largest key that is <= the given key.
|
|
164
|
+
*/
|
|
165
|
+
floor(key: K): K | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Smallest key that is > the given key.
|
|
168
|
+
*/
|
|
169
|
+
higher(key: K): K | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* Largest key that is < the given key.
|
|
172
|
+
*/
|
|
173
|
+
lower(key: K): K | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* Return all keys in a given range.
|
|
176
|
+
*
|
|
177
|
+
* @param range `[low, high]`
|
|
178
|
+
* @param options Inclusive/exclusive bounds (defaults to inclusive).
|
|
179
|
+
*/
|
|
180
|
+
rangeSearch(range: [K, K], options?: TreeSetRangeOptions): K[];
|
|
181
|
+
}
|
|
@@ -11,7 +11,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
11
11
|
readonly isMapMode: boolean;
|
|
12
12
|
iterationType: IterationType;
|
|
13
13
|
readonly NIL: BinaryTreeNode<K, V>;
|
|
14
|
-
readonly store: Map<K, V
|
|
14
|
+
readonly store: Map<K, BinaryTreeNode<K, V>>;
|
|
15
15
|
readonly toEntryFn?: ToEntryFn<K, V, R>;
|
|
16
16
|
readonly isDuplicate: boolean;
|
|
17
17
|
createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
|
|
@@ -19,7 +19,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
19
19
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
20
20
|
set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
21
21
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
22
|
-
delete(keyNodeEntryRawOrPredicate:
|
|
22
|
+
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
23
23
|
clear(): void;
|
|
24
24
|
isEmpty(): boolean;
|
|
25
25
|
get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
|
|
@@ -2,8 +2,8 @@ export * from './binary-tree';
|
|
|
2
2
|
export * from './bst';
|
|
3
3
|
export * from './avl-tree';
|
|
4
4
|
export * from './segment-tree';
|
|
5
|
-
export * from './avl-tree-multi-map';
|
|
6
5
|
export * from './red-black-tree';
|
|
7
6
|
export * from './tree-multi-map';
|
|
8
|
-
export * from './tree-
|
|
9
|
-
export * from './
|
|
7
|
+
export * from './tree-set';
|
|
8
|
+
export * from './tree-map';
|
|
9
|
+
export * from './tree-multi-set';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Comparator } from '../../common';
|
|
2
|
+
export interface TreeMapOptions<K, V, R = [K, V]> {
|
|
3
|
+
comparator?: Comparator<K>;
|
|
4
|
+
/**
|
|
5
|
+
* Pass-through to the underlying RedBlackTree/BST `isMapMode` option.
|
|
6
|
+
*
|
|
7
|
+
* - `true` (default in core): store values in an internal key→value store.
|
|
8
|
+
* - `false`: store values on tree nodes (Node Mode).
|
|
9
|
+
*/
|
|
10
|
+
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Transform raw elements into `[key, value]` entries.
|
|
13
|
+
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<[K, V]>`.
|
|
14
|
+
*/
|
|
15
|
+
toEntryFn?: (rawElement: R) => [K, V];
|
|
16
|
+
}
|
|
17
|
+
export type TreeMapRangeOptions = {
|
|
18
|
+
lowInclusive?: boolean;
|
|
19
|
+
highInclusive?: boolean;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Callback used by TreeMap entry-wise utilities.
|
|
23
|
+
*
|
|
24
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
25
|
+
* Implementations (e.g. `TreeMap<K, V>`) should bind `SELF` at use sites.
|
|
26
|
+
*/
|
|
27
|
+
export type TreeMapEntryCallback<K, V, R, SELF> = (value: V | undefined, key: K, index: number, map: SELF) => R;
|
|
28
|
+
/**
|
|
29
|
+
* Reducer callback used by TreeMap.
|
|
30
|
+
*
|
|
31
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
32
|
+
*/
|
|
33
|
+
export type TreeMapReduceCallback<K, V, A, SELF> = (acc: A, value: V | undefined, key: K, index: number, map: SELF) => A;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Comparator } from '../../common';
|
|
2
|
+
export interface TreeMultiSetOptions<K, R = K> {
|
|
3
|
+
comparator?: Comparator<K>;
|
|
4
|
+
/**
|
|
5
|
+
* Pass-through to the underlying RedBlackTree/BST `isMapMode` option.
|
|
6
|
+
*
|
|
7
|
+
* - `true` (recommended): MapMode store uses key→node index for fast lookups.
|
|
8
|
+
* - `false`: Node Mode.
|
|
9
|
+
*/
|
|
10
|
+
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Transform raw elements into keys.
|
|
13
|
+
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
|
|
14
|
+
*/
|
|
15
|
+
toElementFn?: (rawElement: R) => K;
|
|
16
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Comparator } from '../../common';
|
|
2
|
+
export interface TreeSetOptions<K, R = K> {
|
|
3
|
+
comparator?: Comparator<K>;
|
|
4
|
+
/**
|
|
5
|
+
* Pass-through to the underlying RedBlackTree/BST `isMapMode` option.
|
|
6
|
+
*
|
|
7
|
+
* - `true` (default in core): store values in an internal key→value store.
|
|
8
|
+
* - `false`: store values on tree nodes (Node Mode).
|
|
9
|
+
*/
|
|
10
|
+
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Transform raw elements into keys.
|
|
13
|
+
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
|
|
14
|
+
*/
|
|
15
|
+
toElementFn?: (rawElement: R) => K;
|
|
16
|
+
}
|
|
17
|
+
export type TreeSetRangeOptions = {
|
|
18
|
+
lowInclusive?: boolean;
|
|
19
|
+
highInclusive?: boolean;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Callback used by TreeSet element-wise utilities.
|
|
23
|
+
*
|
|
24
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
25
|
+
* Implementations (e.g. `TreeSet<K>`) should bind `SELF` at use sites.
|
|
26
|
+
*/
|
|
27
|
+
export type TreeSetElementCallback<K, R, SELF> = (value: K, index: number, set: SELF) => R;
|
|
28
|
+
/**
|
|
29
|
+
* Reducer callback used by TreeSet.
|
|
30
|
+
*
|
|
31
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
32
|
+
*/
|
|
33
|
+
export type TreeSetReduceCallback<K, A, SELF> = (acc: A, value: K, index: number, set: SELF) => A;
|
package/dist/umd/queue-typed.js
CHANGED
|
@@ -536,6 +536,12 @@ var queueTyped = (() => {
|
|
|
536
536
|
}
|
|
537
537
|
return -1;
|
|
538
538
|
}
|
|
539
|
+
/**
|
|
540
|
+
* Concatenate lists/elements preserving order.
|
|
541
|
+
* @param items - Elements or `LinearBase` instances.
|
|
542
|
+
* @returns New list with combined elements (`this` type).
|
|
543
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
544
|
+
*/
|
|
539
545
|
concat(...items) {
|
|
540
546
|
const newList = this.clone();
|
|
541
547
|
for (const item of items) {
|
|
@@ -2337,6 +2343,12 @@ var queueTyped = (() => {
|
|
|
2337
2343
|
*/
|
|
2338
2344
|
_setBucketSize(size) {
|
|
2339
2345
|
this._bucketSize = size;
|
|
2346
|
+
if (this._length === 0) {
|
|
2347
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
2348
|
+
this._bucketCount = 1;
|
|
2349
|
+
this._bucketFirst = this._bucketLast = 0;
|
|
2350
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2351
|
+
}
|
|
2340
2352
|
}
|
|
2341
2353
|
/**
|
|
2342
2354
|
* (Protected) Iterate elements from front to back.
|