ts-data-forge 1.0.0 → 1.0.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/README.md +1 -1
- package/dist/array/array-utils.d.mts +2617 -0
- package/dist/array/array-utils.d.mts.map +1 -0
- package/dist/array/array-utils.mjs +2915 -0
- package/dist/array/array-utils.mjs.map +1 -0
- package/dist/array/index.d.mts +3 -0
- package/dist/array/index.d.mts.map +1 -0
- package/dist/array/index.mjs +3 -0
- package/dist/array/index.mjs.map +1 -0
- package/dist/array/tuple-utils.d.mts +421 -0
- package/dist/array/tuple-utils.d.mts.map +1 -0
- package/dist/array/tuple-utils.mjs +391 -0
- package/dist/array/tuple-utils.mjs.map +1 -0
- package/dist/collections/imap-mapped.d.mts +445 -0
- package/dist/collections/imap-mapped.d.mts.map +1 -0
- package/dist/collections/imap-mapped.mjs +424 -0
- package/dist/collections/imap-mapped.mjs.map +1 -0
- package/dist/collections/imap.d.mts +359 -0
- package/dist/collections/imap.d.mts.map +1 -0
- package/dist/collections/imap.mjs +338 -0
- package/dist/collections/imap.mjs.map +1 -0
- package/dist/collections/index.d.mts +7 -0
- package/dist/collections/index.d.mts.map +1 -0
- package/dist/collections/index.mjs +7 -0
- package/dist/collections/index.mjs.map +1 -0
- package/dist/collections/iset-mapped.d.mts +576 -0
- package/dist/collections/iset-mapped.d.mts.map +1 -0
- package/dist/collections/iset-mapped.mjs +522 -0
- package/dist/collections/iset-mapped.mjs.map +1 -0
- package/dist/collections/iset.d.mts +426 -0
- package/dist/collections/iset.d.mts.map +1 -0
- package/dist/collections/iset.mjs +437 -0
- package/dist/collections/iset.mjs.map +1 -0
- package/dist/collections/queue.d.mts +190 -0
- package/dist/collections/queue.d.mts.map +1 -0
- package/dist/collections/queue.mjs +317 -0
- package/dist/collections/queue.mjs.map +1 -0
- package/dist/collections/stack.d.mts +210 -0
- package/dist/collections/stack.d.mts.map +1 -0
- package/dist/collections/stack.mjs +353 -0
- package/dist/collections/stack.mjs.map +1 -0
- package/dist/expect-type.d.mts +199 -0
- package/dist/expect-type.d.mts.map +1 -0
- package/dist/expect-type.mjs +201 -0
- package/dist/expect-type.mjs.map +1 -0
- package/dist/functional/index.d.mts +5 -0
- package/dist/functional/index.d.mts.map +1 -0
- package/dist/functional/index.mjs +5 -0
- package/dist/functional/index.mjs.map +1 -0
- package/dist/functional/match.d.mts +215 -0
- package/dist/functional/match.d.mts.map +1 -0
- package/dist/functional/match.mjs +139 -0
- package/dist/functional/match.mjs.map +1 -0
- package/dist/functional/optional.d.mts +517 -0
- package/dist/functional/optional.d.mts.map +1 -0
- package/dist/functional/optional.mjs +532 -0
- package/dist/functional/optional.mjs.map +1 -0
- package/dist/functional/pipe.d.mts +185 -0
- package/dist/functional/pipe.d.mts.map +1 -0
- package/dist/functional/pipe.mjs +129 -0
- package/dist/functional/pipe.mjs.map +1 -0
- package/dist/functional/result.d.mts +796 -0
- package/dist/functional/result.d.mts.map +1 -0
- package/dist/functional/result.mjs +844 -0
- package/dist/functional/result.mjs.map +1 -0
- package/dist/globals.d.mts +38 -0
- package/dist/guard/has-key.d.mts +100 -0
- package/dist/guard/has-key.d.mts.map +1 -0
- package/dist/guard/has-key.mjs +94 -0
- package/dist/guard/has-key.mjs.map +1 -0
- package/dist/guard/index.d.mts +8 -0
- package/dist/guard/index.d.mts.map +1 -0
- package/dist/guard/index.mjs +8 -0
- package/dist/guard/index.mjs.map +1 -0
- package/dist/guard/is-non-empty-string.d.mts +106 -0
- package/dist/guard/is-non-empty-string.d.mts.map +1 -0
- package/dist/guard/is-non-empty-string.mjs +108 -0
- package/dist/guard/is-non-empty-string.mjs.map +1 -0
- package/dist/guard/is-non-null-object.d.mts +105 -0
- package/dist/guard/is-non-null-object.d.mts.map +1 -0
- package/dist/guard/is-non-null-object.mjs +108 -0
- package/dist/guard/is-non-null-object.mjs.map +1 -0
- package/dist/guard/is-primitive.d.mts +146 -0
- package/dist/guard/is-primitive.d.mts.map +1 -0
- package/dist/guard/is-primitive.mjs +161 -0
- package/dist/guard/is-primitive.mjs.map +1 -0
- package/dist/guard/is-record.d.mts +151 -0
- package/dist/guard/is-record.d.mts.map +1 -0
- package/dist/guard/is-record.mjs +155 -0
- package/dist/guard/is-record.mjs.map +1 -0
- package/dist/guard/is-type.d.mts +430 -0
- package/dist/guard/is-type.d.mts.map +1 -0
- package/dist/guard/is-type.mjs +432 -0
- package/dist/guard/is-type.mjs.map +1 -0
- package/dist/guard/key-is-in.d.mts +158 -0
- package/dist/guard/key-is-in.d.mts.map +1 -0
- package/dist/guard/key-is-in.mjs +160 -0
- package/dist/guard/key-is-in.mjs.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +61 -0
- package/dist/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +2 -0
- package/dist/iterator/index.d.mts.map +1 -0
- package/dist/iterator/index.mjs +2 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/iterator/range.d.mts +97 -0
- package/dist/iterator/range.d.mts.map +1 -0
- package/dist/iterator/range.mjs +130 -0
- package/dist/iterator/range.mjs.map +1 -0
- package/dist/json/index.d.mts +2 -0
- package/dist/json/index.d.mts.map +1 -0
- package/dist/json/index.mjs +2 -0
- package/dist/json/index.mjs.map +1 -0
- package/dist/json/json.d.mts +597 -0
- package/dist/json/json.d.mts.map +1 -0
- package/dist/json/json.mjs +687 -0
- package/dist/json/json.mjs.map +1 -0
- package/dist/number/branded-types/finite-number.d.mts +291 -0
- package/dist/number/branded-types/finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/finite-number.mjs +296 -0
- package/dist/number/branded-types/finite-number.mjs.map +1 -0
- package/dist/number/branded-types/index.d.mts +27 -0
- package/dist/number/branded-types/index.d.mts.map +1 -0
- package/dist/number/branded-types/index.mjs +27 -0
- package/dist/number/branded-types/index.mjs.map +1 -0
- package/dist/number/branded-types/int.d.mts +242 -0
- package/dist/number/branded-types/int.d.mts.map +1 -0
- package/dist/number/branded-types/int.mjs +239 -0
- package/dist/number/branded-types/int.mjs.map +1 -0
- package/dist/number/branded-types/int16.d.mts +162 -0
- package/dist/number/branded-types/int16.d.mts.map +1 -0
- package/dist/number/branded-types/int16.mjs +141 -0
- package/dist/number/branded-types/int16.mjs.map +1 -0
- package/dist/number/branded-types/int32.d.mts +155 -0
- package/dist/number/branded-types/int32.d.mts.map +1 -0
- package/dist/number/branded-types/int32.mjs +142 -0
- package/dist/number/branded-types/int32.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts +165 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int16.d.mts +160 -0
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int16.mjs +138 -0
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int32.d.mts +156 -0
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int32.mjs +138 -0
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts +154 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int.d.mts +131 -0
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int.mjs +128 -0
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int16.d.mts +166 -0
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int16.mjs +145 -0
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int32.d.mts +158 -0
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int32.mjs +145 -0
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts +148 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs +145 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts +160 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts +156 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -0
- package/dist/number/branded-types/positive-finite-number.d.mts +171 -0
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/positive-finite-number.mjs +165 -0
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/positive-int.d.mts +270 -0
- package/dist/number/branded-types/positive-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int.mjs +257 -0
- package/dist/number/branded-types/positive-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-int16.d.mts +162 -0
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int16.mjs +139 -0
- package/dist/number/branded-types/positive-int16.mjs.map +1 -0
- package/dist/number/branded-types/positive-int32.d.mts +158 -0
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int32.mjs +139 -0
- package/dist/number/branded-types/positive-int32.mjs.map +1 -0
- package/dist/number/branded-types/positive-safe-int.d.mts +152 -0
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-safe-int.mjs +138 -0
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint16.d.mts +160 -0
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint16.mjs +139 -0
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint32.d.mts +156 -0
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint32.mjs +139 -0
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -0
- package/dist/number/branded-types/safe-int.d.mts +243 -0
- package/dist/number/branded-types/safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/safe-int.mjs +240 -0
- package/dist/number/branded-types/safe-int.mjs.map +1 -0
- package/dist/number/branded-types/safe-uint.d.mts +151 -0
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -0
- package/dist/number/branded-types/safe-uint.mjs +138 -0
- package/dist/number/branded-types/safe-uint.mjs.map +1 -0
- package/dist/number/branded-types/uint.d.mts +144 -0
- package/dist/number/branded-types/uint.d.mts.map +1 -0
- package/dist/number/branded-types/uint.mjs +132 -0
- package/dist/number/branded-types/uint.mjs.map +1 -0
- package/dist/number/branded-types/uint16.d.mts +157 -0
- package/dist/number/branded-types/uint16.d.mts.map +1 -0
- package/dist/number/branded-types/uint16.mjs +137 -0
- package/dist/number/branded-types/uint16.mjs.map +1 -0
- package/dist/number/branded-types/uint32.d.mts +185 -0
- package/dist/number/branded-types/uint32.d.mts.map +1 -0
- package/dist/number/branded-types/uint32.mjs +169 -0
- package/dist/number/branded-types/uint32.mjs.map +1 -0
- package/dist/number/enum/index.d.mts +3 -0
- package/dist/number/enum/index.d.mts.map +1 -0
- package/dist/number/enum/index.mjs +3 -0
- package/dist/number/enum/index.mjs.map +1 -0
- package/dist/number/enum/int8.d.mts +202 -0
- package/dist/number/enum/int8.d.mts.map +1 -0
- package/dist/number/enum/int8.mjs +296 -0
- package/dist/number/enum/int8.mjs.map +1 -0
- package/dist/number/enum/uint8.d.mts +128 -0
- package/dist/number/enum/uint8.d.mts.map +1 -0
- package/dist/number/enum/uint8.mjs +251 -0
- package/dist/number/enum/uint8.mjs.map +1 -0
- package/dist/number/index.d.mts +5 -0
- package/dist/number/index.d.mts.map +1 -0
- package/dist/number/index.mjs +31 -0
- package/dist/number/index.mjs.map +1 -0
- package/dist/number/num.d.mts +515 -0
- package/dist/number/num.d.mts.map +1 -0
- package/dist/number/num.mjs +513 -0
- package/dist/number/num.mjs.map +1 -0
- package/dist/number/refined-number-utils.d.mts +191 -0
- package/dist/number/refined-number-utils.d.mts.map +1 -0
- package/dist/number/refined-number-utils.mjs +179 -0
- package/dist/number/refined-number-utils.mjs.map +1 -0
- package/dist/object/index.d.mts +2 -0
- package/dist/object/index.d.mts.map +1 -0
- package/dist/object/index.mjs +2 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/object/object.d.mts +296 -0
- package/dist/object/object.d.mts.map +1 -0
- package/dist/object/object.mjs +295 -0
- package/dist/object/object.mjs.map +1 -0
- package/dist/others/cast-mutable.d.mts +110 -0
- package/dist/others/cast-mutable.d.mts.map +1 -0
- package/dist/others/cast-mutable.mjs +114 -0
- package/dist/others/cast-mutable.mjs.map +1 -0
- package/dist/others/cast-readonly.d.mts +189 -0
- package/dist/others/cast-readonly.d.mts.map +1 -0
- package/dist/others/cast-readonly.mjs +193 -0
- package/dist/others/cast-readonly.mjs.map +1 -0
- package/dist/others/if-then.d.mts +98 -0
- package/dist/others/if-then.d.mts.map +1 -0
- package/dist/others/if-then.mjs +100 -0
- package/dist/others/if-then.mjs.map +1 -0
- package/dist/others/index.d.mts +8 -0
- package/dist/others/index.d.mts.map +1 -0
- package/dist/others/index.mjs +8 -0
- package/dist/others/index.mjs.map +1 -0
- package/dist/others/map-nullable.d.mts +151 -0
- package/dist/others/map-nullable.d.mts.map +1 -0
- package/dist/others/map-nullable.mjs +159 -0
- package/dist/others/map-nullable.mjs.map +1 -0
- package/dist/others/memoize-function.d.mts +173 -0
- package/dist/others/memoize-function.d.mts.map +1 -0
- package/dist/others/memoize-function.mjs +189 -0
- package/dist/others/memoize-function.mjs.map +1 -0
- package/dist/others/tuple.d.mts +159 -0
- package/dist/others/tuple.d.mts.map +1 -0
- package/dist/others/tuple.mjs +161 -0
- package/dist/others/tuple.mjs.map +1 -0
- package/dist/others/unknown-to-string.d.mts +180 -0
- package/dist/others/unknown-to-string.d.mts.map +1 -0
- package/dist/others/unknown-to-string.mjs +211 -0
- package/dist/others/unknown-to-string.mjs.map +1 -0
- package/dist/tsconfig.json +1 -0
- package/package.json +16 -14
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import '../functional/optional.mjs';
|
|
2
|
+
import { Result } from '../functional/result.mjs';
|
|
3
|
+
import '../number/branded-types/finite-number.mjs';
|
|
4
|
+
import '../number/branded-types/int.mjs';
|
|
5
|
+
import '../number/branded-types/int16.mjs';
|
|
6
|
+
import '../number/branded-types/int32.mjs';
|
|
7
|
+
import '../number/branded-types/non-negative-finite-number.mjs';
|
|
8
|
+
import '../number/branded-types/non-negative-int16.mjs';
|
|
9
|
+
import '../number/branded-types/non-negative-int32.mjs';
|
|
10
|
+
import '../number/branded-types/non-zero-finite-number.mjs';
|
|
11
|
+
import '../number/branded-types/non-zero-int.mjs';
|
|
12
|
+
import '../number/branded-types/non-zero-int16.mjs';
|
|
13
|
+
import '../number/branded-types/non-zero-int32.mjs';
|
|
14
|
+
import '../number/branded-types/non-zero-safe-int.mjs';
|
|
15
|
+
import '../number/branded-types/non-zero-uint16.mjs';
|
|
16
|
+
import '../number/branded-types/non-zero-uint32.mjs';
|
|
17
|
+
import '../number/branded-types/positive-finite-number.mjs';
|
|
18
|
+
import '../number/branded-types/positive-int.mjs';
|
|
19
|
+
import '../number/branded-types/positive-int16.mjs';
|
|
20
|
+
import '../number/branded-types/positive-int32.mjs';
|
|
21
|
+
import '../number/branded-types/positive-safe-int.mjs';
|
|
22
|
+
import '../number/branded-types/positive-uint16.mjs';
|
|
23
|
+
import '../number/branded-types/positive-uint32.mjs';
|
|
24
|
+
import '../number/branded-types/safe-int.mjs';
|
|
25
|
+
import '../number/branded-types/safe-uint.mjs';
|
|
26
|
+
import '../number/branded-types/uint.mjs';
|
|
27
|
+
import '../number/branded-types/uint16.mjs';
|
|
28
|
+
import { asUint32 } from '../number/branded-types/uint32.mjs';
|
|
29
|
+
import '../number/enum/int8.mjs';
|
|
30
|
+
import '../number/enum/uint8.mjs';
|
|
31
|
+
import '../number/num.mjs';
|
|
32
|
+
import '../number/refined-number-utils.mjs';
|
|
33
|
+
import { unknownToString } from '../others/unknown-to-string.mjs';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Provides utility functions for ISet.
|
|
37
|
+
*/
|
|
38
|
+
var ISet;
|
|
39
|
+
(function (ISet) {
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new ISet instance from an iterable of elements.
|
|
42
|
+
*
|
|
43
|
+
* This factory function accepts any iterable of elements, including arrays,
|
|
44
|
+
* JavaScript Sets, other ISets, or custom iterables. Duplicate elements in the
|
|
45
|
+
* input iterable will be automatically deduplicated in the resulting set.
|
|
46
|
+
*
|
|
47
|
+
* **Performance:** O(n) where n is the number of elements in the iterable.
|
|
48
|
+
*
|
|
49
|
+
* @template K The type of the elements. Must extend MapSetKeyType.
|
|
50
|
+
* @param iterable An iterable of elements (e.g., Array, Set, ISet, etc.)
|
|
51
|
+
* @returns A new ISet instance containing all unique elements from the iterable.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* // From array (duplicates automatically removed)
|
|
56
|
+
* const uniqueIds = ISet.create([1, 2, 3, 2, 1]); // Contains: 1, 2, 3
|
|
57
|
+
* console.log(uniqueIds.size); // Output: 3
|
|
58
|
+
*
|
|
59
|
+
* // From JavaScript Set
|
|
60
|
+
* const jsSet = new Set(["red", "green", "blue"]);
|
|
61
|
+
* const colors = ISet.create(jsSet);
|
|
62
|
+
* console.log(colors.has("red")); // Output: true
|
|
63
|
+
*
|
|
64
|
+
* // From another ISet (creates a copy)
|
|
65
|
+
* const originalTags = ISet.create(["typescript", "immutable"]);
|
|
66
|
+
* const copiedTags = ISet.create(originalTags);
|
|
67
|
+
* console.log(copiedTags.size); // Output: 2
|
|
68
|
+
*
|
|
69
|
+
* // Empty set
|
|
70
|
+
* const emptyPermissions = ISet.create<string>([]);
|
|
71
|
+
* console.log(emptyPermissions.isEmpty); // Output: true
|
|
72
|
+
*
|
|
73
|
+
* // Fluent operations
|
|
74
|
+
* const processedNumbers = ISet.create([1, 2, 3, 4, 5])
|
|
75
|
+
* .filter(x => x % 2 === 0) // Keep even numbers: 2, 4
|
|
76
|
+
* .add(6) // Add 6: 2, 4, 6
|
|
77
|
+
* .delete(2); // Remove 2: 4, 6
|
|
78
|
+
* console.log(processedNumbers.toArray().sort()); // Output: [4, 6]
|
|
79
|
+
*
|
|
80
|
+
* // From generator function
|
|
81
|
+
* function* generatePrimes(): Generator<number> {
|
|
82
|
+
* yield 2; yield 3; yield 5; yield 7;
|
|
83
|
+
* }
|
|
84
|
+
* const primes = ISet.create(generatePrimes());
|
|
85
|
+
* console.log(primes.size); // Output: 4
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
ISet.create = (iterable) => new ISetClass(iterable);
|
|
89
|
+
/**
|
|
90
|
+
* Checks if two ISet instances are structurally equal.
|
|
91
|
+
*
|
|
92
|
+
* Two ISets are considered equal if they have the same size and contain exactly the same
|
|
93
|
+
* elements. The order of elements does not matter for equality comparison since sets are
|
|
94
|
+
* unordered collections. Elements are compared using JavaScript's `===` operator.
|
|
95
|
+
*
|
|
96
|
+
* **Performance:** O(n) where n is the size of the smaller set.
|
|
97
|
+
*
|
|
98
|
+
* @template K The type of the elements.
|
|
99
|
+
* @param a The first ISet instance to compare.
|
|
100
|
+
* @param b The second ISet instance to compare.
|
|
101
|
+
* @returns `true` if the sets contain exactly the same elements, `false` otherwise.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* // Basic equality comparison
|
|
106
|
+
* const permissions1 = ISet.create(["read", "write", "execute"]);
|
|
107
|
+
* const permissions2 = ISet.create(["execute", "read", "write"]); // Order doesn't matter
|
|
108
|
+
* const permissions3 = ISet.create(["read", "write"]);
|
|
109
|
+
*
|
|
110
|
+
* console.log(ISet.equal(permissions1, permissions2)); // true
|
|
111
|
+
* console.log(ISet.equal(permissions1, permissions3)); // false (different sizes)
|
|
112
|
+
*
|
|
113
|
+
* // With different element types
|
|
114
|
+
* const numbers1 = ISet.create([1, 2, 3]);
|
|
115
|
+
* const numbers2 = ISet.create([3, 1, 2]);
|
|
116
|
+
* const numbers3 = ISet.create([1, 2, 4]); // Different element
|
|
117
|
+
*
|
|
118
|
+
* console.log(ISet.equal(numbers1, numbers2)); // true
|
|
119
|
+
* console.log(ISet.equal(numbers1, numbers3)); // false
|
|
120
|
+
*
|
|
121
|
+
* // Empty sets
|
|
122
|
+
* const empty1 = ISet.create<string>([]);
|
|
123
|
+
* const empty2 = ISet.create<string>([]);
|
|
124
|
+
* console.log(ISet.equal(empty1, empty2)); // true
|
|
125
|
+
*
|
|
126
|
+
* // Single element sets
|
|
127
|
+
* const single1 = ISet.create(["unique"]);
|
|
128
|
+
* const single2 = ISet.create(["unique"]);
|
|
129
|
+
* const single3 = ISet.create(["different"]);
|
|
130
|
+
*
|
|
131
|
+
* console.log(ISet.equal(single1, single2)); // true
|
|
132
|
+
* console.log(ISet.equal(single1, single3)); // false
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
ISet.equal = (a, b) => a.size === b.size && a.every((e) => b.has(e));
|
|
136
|
+
/**
|
|
137
|
+
* Computes the difference between two ISet instances, identifying added and deleted elements.
|
|
138
|
+
*
|
|
139
|
+
* This function performs a set difference operation to determine what elements were added
|
|
140
|
+
* and what elements were deleted when transitioning from the old set to the new set.
|
|
141
|
+
* This is useful for change detection, state management, and synchronization scenarios.
|
|
142
|
+
*
|
|
143
|
+
* **Performance:** O(n + m) where n and m are the sizes of the old and new sets respectively.
|
|
144
|
+
*
|
|
145
|
+
* @template K The type of the elements.
|
|
146
|
+
* @param oldSet The original set representing the previous state.
|
|
147
|
+
* @param newSet The new set representing the current state.
|
|
148
|
+
* @returns An object with `added` and `deleted` properties, each containing an ISet
|
|
149
|
+
* of elements that were added or removed respectively.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // User permission changes
|
|
154
|
+
* const oldPermissions = ISet.create(["read", "write", "delete"]);
|
|
155
|
+
* const newPermissions = ISet.create(["read", "write", "execute", "admin"]);
|
|
156
|
+
*
|
|
157
|
+
* const permissionDiff = ISet.diff(oldPermissions, newPermissions);
|
|
158
|
+
*
|
|
159
|
+
* console.log("Permissions removed:", permissionDiff.deleted.toArray());
|
|
160
|
+
* // Output: ["delete"]
|
|
161
|
+
*
|
|
162
|
+
* console.log("Permissions added:", permissionDiff.added.toArray());
|
|
163
|
+
* // Output: ["execute", "admin"]
|
|
164
|
+
*
|
|
165
|
+
* // No changes
|
|
166
|
+
* const unchanged1 = ISet.create(["a", "b", "c"]);
|
|
167
|
+
* const unchanged2 = ISet.create(["a", "b", "c"]);
|
|
168
|
+
* const noDiff = ISet.diff(unchanged1, unchanged2);
|
|
169
|
+
*
|
|
170
|
+
* console.log(noDiff.added.isEmpty); // true
|
|
171
|
+
* console.log(noDiff.deleted.isEmpty); // true
|
|
172
|
+
*
|
|
173
|
+
* // Complete replacement
|
|
174
|
+
* const oldTags = ISet.create(["javascript", "react"]);
|
|
175
|
+
* const newTags = ISet.create(["typescript", "vue"]);
|
|
176
|
+
* const tagDiff = ISet.diff(oldTags, newTags);
|
|
177
|
+
*
|
|
178
|
+
* console.log(tagDiff.deleted.toArray()); // ["javascript", "react"]
|
|
179
|
+
* console.log(tagDiff.added.toArray()); // ["typescript", "vue"]
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
ISet.diff = (oldSet, newSet) => ({
|
|
183
|
+
deleted: oldSet.subtract(newSet),
|
|
184
|
+
added: newSet.subtract(oldSet),
|
|
185
|
+
});
|
|
186
|
+
/**
|
|
187
|
+
* Computes the intersection of two ISet instances.
|
|
188
|
+
*
|
|
189
|
+
* Returns a new set containing only the elements that are present in both input sets.
|
|
190
|
+
* This operation is commutative: `intersection(a, b) === intersection(b, a)`.
|
|
191
|
+
*
|
|
192
|
+
* **Performance:** O(min(n, m)) where n and m are the sizes of the input sets.
|
|
193
|
+
*
|
|
194
|
+
* @template K The type of the elements.
|
|
195
|
+
* @param a The first set.
|
|
196
|
+
* @param b The second set.
|
|
197
|
+
* @returns A new ISet instance containing elements common to both sets.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* // Finding common permissions between user and role
|
|
202
|
+
* const userPermissions = ISet.create(["read", "write", "delete", "admin"]);
|
|
203
|
+
* const rolePermissions = ISet.create(["read", "write", "execute"]);
|
|
204
|
+
*
|
|
205
|
+
* const commonPermissions = ISet.intersection(userPermissions, rolePermissions);
|
|
206
|
+
* console.log(commonPermissions.toArray()); // ["read", "write"]
|
|
207
|
+
*
|
|
208
|
+
* // No common elements
|
|
209
|
+
* const setA = ISet.create([1, 2, 3]);
|
|
210
|
+
* const setB = ISet.create([4, 5, 6]);
|
|
211
|
+
* const noCommon = ISet.intersection(setA, setB);
|
|
212
|
+
* console.log(noCommon.isEmpty); // true
|
|
213
|
+
*
|
|
214
|
+
* // Complete overlap
|
|
215
|
+
* const identical1 = ISet.create(["a", "b", "c"]);
|
|
216
|
+
* const identical2 = ISet.create(["a", "b", "c"]);
|
|
217
|
+
* const completeOverlap = ISet.intersection(identical1, identical2);
|
|
218
|
+
* console.log(ISet.equal(completeOverlap, identical1)); // true
|
|
219
|
+
*
|
|
220
|
+
* // Intersection with empty set
|
|
221
|
+
* const nonEmpty = ISet.create([1, 2, 3]);
|
|
222
|
+
* const empty = ISet.create<number>([]);
|
|
223
|
+
* const withEmpty = ISet.intersection(nonEmpty, empty);
|
|
224
|
+
* console.log(withEmpty.isEmpty); // true
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
ISet.intersection = (a, b) => a.intersect(b);
|
|
228
|
+
/**
|
|
229
|
+
* Computes the union of two ISet instances.
|
|
230
|
+
*
|
|
231
|
+
* Returns a new set containing all elements that are present in either input set.
|
|
232
|
+
* Duplicate elements are automatically handled since sets only contain unique values.
|
|
233
|
+
* This operation is commutative: `union(a, b) === union(b, a)`.
|
|
234
|
+
*
|
|
235
|
+
* **Performance:** O(n + m) where n and m are the sizes of the input sets.
|
|
236
|
+
*
|
|
237
|
+
* @template K1 The type of elements in the first set.
|
|
238
|
+
* @template K2 The type of elements in the second set.
|
|
239
|
+
* @param a The first set.
|
|
240
|
+
* @param b The second set.
|
|
241
|
+
* @returns A new ISet instance containing all elements from both sets.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* // Combining permissions from multiple sources
|
|
246
|
+
* const userPermissions = ISet.create(["read", "write"]);
|
|
247
|
+
* const rolePermissions = ISet.create(["write", "execute", "admin"]);
|
|
248
|
+
*
|
|
249
|
+
* const allPermissions = ISet.union(userPermissions, rolePermissions);
|
|
250
|
+
* console.log(allPermissions.toArray().sort());
|
|
251
|
+
* // Output: ["admin", "execute", "read", "write"]
|
|
252
|
+
*
|
|
253
|
+
* // Union with different types (type widening)
|
|
254
|
+
* const numbers = ISet.create([1, 2, 3]);
|
|
255
|
+
* const strings = ISet.create(["a", "b"]);
|
|
256
|
+
* const mixed = ISet.union(numbers, strings); // ISet<number | string>
|
|
257
|
+
* console.log(mixed.size); // 5
|
|
258
|
+
*
|
|
259
|
+
* // Union with empty set
|
|
260
|
+
* const nonEmpty = ISet.create(["item1", "item2"]);
|
|
261
|
+
* const empty = ISet.create<string>([]);
|
|
262
|
+
* const withEmpty = ISet.union(nonEmpty, empty);
|
|
263
|
+
* console.log(ISet.equal(withEmpty, nonEmpty)); // true
|
|
264
|
+
*
|
|
265
|
+
* // Overlapping sets
|
|
266
|
+
* const featuresA = ISet.create(["feature1", "feature2", "feature3"]);
|
|
267
|
+
* const featuresB = ISet.create(["feature2", "feature3", "feature4"]);
|
|
268
|
+
* const allFeatures = ISet.union(featuresA, featuresB);
|
|
269
|
+
* console.log(allFeatures.size); // 4 (duplicates removed)
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
ISet.union = (a, b) => a.union(b);
|
|
273
|
+
})(ISet || (ISet = {}));
|
|
274
|
+
/**
|
|
275
|
+
* Internal class implementation for ISet providing immutable set operations.
|
|
276
|
+
*
|
|
277
|
+
* This class implements the ISet interface using JavaScript's native Set as the underlying
|
|
278
|
+
* storage mechanism for optimal performance. All mutation operations create new instances
|
|
279
|
+
* rather than modifying the existing set, ensuring immutability.
|
|
280
|
+
*
|
|
281
|
+
* **Implementation Details:**
|
|
282
|
+
* - Uses ReadonlySet<K> internally for type safety and performance
|
|
283
|
+
* - Implements copy-on-write semantics for efficiency
|
|
284
|
+
* - Provides optional debug messaging for development
|
|
285
|
+
*
|
|
286
|
+
* @template K The type of the elements. Must extend MapSetKeyType.
|
|
287
|
+
* @implements ISet
|
|
288
|
+
* @implements Iterable
|
|
289
|
+
* @internal This class should not be used directly. Use ISet.create() instead.
|
|
290
|
+
*/
|
|
291
|
+
class ISetClass {
|
|
292
|
+
#set;
|
|
293
|
+
#showNotFoundMessage;
|
|
294
|
+
/**
|
|
295
|
+
* Constructs an ISetClass instance with the given elements.
|
|
296
|
+
*
|
|
297
|
+
* @param iterable An iterable of elements to populate the set.
|
|
298
|
+
* @param showNotFoundMessage Whether to log warning messages when operations
|
|
299
|
+
* are performed on non-existent elements. Useful for debugging.
|
|
300
|
+
* Defaults to false for production use.
|
|
301
|
+
* @internal Use ISet.create() instead of calling this constructor directly.
|
|
302
|
+
*/
|
|
303
|
+
constructor(iterable, showNotFoundMessage = false) {
|
|
304
|
+
this.#set = new Set(iterable);
|
|
305
|
+
this.#showNotFoundMessage = showNotFoundMessage;
|
|
306
|
+
}
|
|
307
|
+
/** @inheritdoc */
|
|
308
|
+
get size() {
|
|
309
|
+
return asUint32(this.#set.size);
|
|
310
|
+
}
|
|
311
|
+
/** @inheritdoc */
|
|
312
|
+
get isEmpty() {
|
|
313
|
+
return this.size === 0;
|
|
314
|
+
}
|
|
315
|
+
/** @inheritdoc */
|
|
316
|
+
has(key) {
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
318
|
+
return this.#set.has(key);
|
|
319
|
+
}
|
|
320
|
+
/** @inheritdoc */
|
|
321
|
+
every(predicate) {
|
|
322
|
+
for (const key of this.values()) {
|
|
323
|
+
if (!predicate(key))
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
/** @inheritdoc */
|
|
329
|
+
some(predicate) {
|
|
330
|
+
for (const key of this.values()) {
|
|
331
|
+
if (predicate(key))
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
/** @inheritdoc */
|
|
337
|
+
add(key) {
|
|
338
|
+
if (this.has(key))
|
|
339
|
+
return this;
|
|
340
|
+
return ISet.create([...this.#set, key]);
|
|
341
|
+
}
|
|
342
|
+
/** @inheritdoc */
|
|
343
|
+
delete(key) {
|
|
344
|
+
if (!this.has(key)) {
|
|
345
|
+
if (this.#showNotFoundMessage) {
|
|
346
|
+
const keyStr = unknownToString(key);
|
|
347
|
+
console.warn(`ISet.delete: key not found: ${Result.isOk(keyStr) ? keyStr.value : '<error converting key to string>'}`);
|
|
348
|
+
}
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
return ISet.create(Array.from(this.#set).filter((k) => !Object.is(k, key)));
|
|
352
|
+
}
|
|
353
|
+
/** @inheritdoc */
|
|
354
|
+
withMutations(actions) {
|
|
355
|
+
const mut_result = new Set(this.#set);
|
|
356
|
+
for (const action of actions) {
|
|
357
|
+
switch (action.type) {
|
|
358
|
+
case 'delete':
|
|
359
|
+
mut_result.delete(action.key);
|
|
360
|
+
break;
|
|
361
|
+
case 'add':
|
|
362
|
+
mut_result.add(action.key);
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return ISet.create(mut_result);
|
|
367
|
+
}
|
|
368
|
+
/** @inheritdoc */
|
|
369
|
+
map(mapFn) {
|
|
370
|
+
return ISet.create(this.toArray().map(mapFn));
|
|
371
|
+
}
|
|
372
|
+
/** @inheritdoc */
|
|
373
|
+
filter(predicate) {
|
|
374
|
+
return ISet.create(this.toArray().filter(predicate));
|
|
375
|
+
}
|
|
376
|
+
/** @inheritdoc */
|
|
377
|
+
filterNot(predicate) {
|
|
378
|
+
return ISet.create(this.toArray().filter((e) => !predicate(e)));
|
|
379
|
+
}
|
|
380
|
+
/** @inheritdoc */
|
|
381
|
+
forEach(callbackfn) {
|
|
382
|
+
for (const v of this.#set.values()) {
|
|
383
|
+
callbackfn(v);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
/** @inheritdoc */
|
|
387
|
+
isSubsetOf(set) {
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
389
|
+
return this.every((k) => set.has(k));
|
|
390
|
+
}
|
|
391
|
+
/** @inheritdoc */
|
|
392
|
+
isSupersetOf(set) {
|
|
393
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
394
|
+
return set.every((k) => this.has(k));
|
|
395
|
+
}
|
|
396
|
+
/** @inheritdoc */
|
|
397
|
+
subtract(set) {
|
|
398
|
+
return ISet.create(this.toArray().filter((k) => !set.has(k)));
|
|
399
|
+
}
|
|
400
|
+
/** @inheritdoc */
|
|
401
|
+
intersect(set) {
|
|
402
|
+
return ISet.create(this.toArray().filter((k) => set.has(k)));
|
|
403
|
+
}
|
|
404
|
+
/** @inheritdoc */
|
|
405
|
+
union(set) {
|
|
406
|
+
return ISet.create([...this, ...set]);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* @inheritdoc
|
|
410
|
+
*/
|
|
411
|
+
[Symbol.iterator]() {
|
|
412
|
+
return this.#set[Symbol.iterator]();
|
|
413
|
+
}
|
|
414
|
+
/** @inheritdoc */
|
|
415
|
+
keys() {
|
|
416
|
+
return this.#set.keys();
|
|
417
|
+
}
|
|
418
|
+
/** @inheritdoc */
|
|
419
|
+
values() {
|
|
420
|
+
return this.#set.values();
|
|
421
|
+
}
|
|
422
|
+
/** @inheritdoc */
|
|
423
|
+
entries() {
|
|
424
|
+
return this.#set.entries();
|
|
425
|
+
}
|
|
426
|
+
/** @inheritdoc */
|
|
427
|
+
toArray() {
|
|
428
|
+
return Array.from(this.values());
|
|
429
|
+
}
|
|
430
|
+
/** @inheritdoc */
|
|
431
|
+
toRawSet() {
|
|
432
|
+
return this.#set;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export { ISet };
|
|
437
|
+
//# sourceMappingURL=iset.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iset.mjs","sources":["../../src/collections/iset.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0NA;;AAEG;AACG,IAAW;AAAjB,CAAA,UAAiB,IAAI,EAAA;AACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CG;IACU,IAAA,CAAA,MAAM,GAAG,CACpB,QAAqB,KACT,IAAI,SAAS,CAAI,QAAQ,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CG;AACU,IAAA,IAAA,CAAA,KAAK,GAAG,CACnB,CAAU,EACV,CAAU,K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
|
2
|
+
/**
|
|
3
|
+
* Interface for a high-performance queue with FIFO (First-In, First-Out) behavior.
|
|
4
|
+
*
|
|
5
|
+
* This interface defines a mutable queue data structure where elements are added to the back
|
|
6
|
+
* and removed from the front, maintaining the order in which they were added. The implementation
|
|
7
|
+
* uses a circular buffer for optimal performance, providing O(1) operations for both enqueue
|
|
8
|
+
* and dequeue operations.
|
|
9
|
+
*
|
|
10
|
+
* **FIFO Behavior:**
|
|
11
|
+
* - **enqueue**: Adds elements to the back of the queue
|
|
12
|
+
* - **dequeue**: Removes and returns elements from the front of the queue
|
|
13
|
+
* - Elements are processed in the exact order they were added
|
|
14
|
+
*
|
|
15
|
+
* **Performance Characteristics:**
|
|
16
|
+
* - enqueue: O(1) amortized (O(n) when buffer needs resizing)
|
|
17
|
+
* - dequeue: O(1) always
|
|
18
|
+
* - size/isEmpty: O(1) always
|
|
19
|
+
* - Memory efficient with automatic garbage collection of removed elements
|
|
20
|
+
*
|
|
21
|
+
* **Use Cases:**
|
|
22
|
+
* - Task scheduling and job queues
|
|
23
|
+
* - Breadth-first search algorithms
|
|
24
|
+
* - Event processing systems
|
|
25
|
+
* - Producer-consumer patterns
|
|
26
|
+
* - Buffer management for streaming data
|
|
27
|
+
*
|
|
28
|
+
* @template T The type of elements stored in the queue.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { createQueue, Queue } from './queue';
|
|
33
|
+
*
|
|
34
|
+
* // Example 1: Basic FIFO operations
|
|
35
|
+
* const messageQueue: Queue<string> = createQueue<string>();
|
|
36
|
+
*
|
|
37
|
+
* messageQueue.enqueue("first message"); // Add to back
|
|
38
|
+
* messageQueue.enqueue("second message"); // Add to back
|
|
39
|
+
* messageQueue.enqueue("third message"); // Add to back
|
|
40
|
+
*
|
|
41
|
+
* console.log(messageQueue.size); // Output: 3
|
|
42
|
+
*
|
|
43
|
+
* // Process messages in FIFO order
|
|
44
|
+
* console.log(messageQueue.dequeue().unwrap()); // "first message" (first in, first out)
|
|
45
|
+
* console.log(messageQueue.dequeue().unwrap()); // "second message"
|
|
46
|
+
* console.log(messageQueue.size); // Output: 1
|
|
47
|
+
*
|
|
48
|
+
* // Example 2: Task processing system
|
|
49
|
+
* type Task = { id: number; priority: string; action: () => void };
|
|
50
|
+
* const taskQueue: Queue<Task> = createQueue<Task>();
|
|
51
|
+
*
|
|
52
|
+
* taskQueue.enqueue({ id: 1, priority: "high", action: () => console.log("Task 1") });
|
|
53
|
+
* taskQueue.enqueue({ id: 2, priority: "low", action: () => console.log("Task 2") });
|
|
54
|
+
*
|
|
55
|
+
* // Process tasks in order
|
|
56
|
+
* while (!taskQueue.isEmpty) {
|
|
57
|
+
* const task = taskQueue.dequeue().unwrap();
|
|
58
|
+
* console.log(`Processing task ${task.id} with ${task.priority} priority`);
|
|
59
|
+
* task.action();
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export type Queue<T> = Readonly<{
|
|
64
|
+
/** Checks if the queue is empty. */
|
|
65
|
+
isEmpty: boolean;
|
|
66
|
+
/** The number of elements in the queue. */
|
|
67
|
+
size: SizeType.Arr;
|
|
68
|
+
/**
|
|
69
|
+
* Removes and returns the element at the front of the queue.
|
|
70
|
+
* @returns The element at the front of the queue, or `Optional.none` if the queue is empty.
|
|
71
|
+
*/
|
|
72
|
+
dequeue: () => Optional<T>;
|
|
73
|
+
/**
|
|
74
|
+
* Adds an element to the back of the queue.
|
|
75
|
+
* @param value The element to add.
|
|
76
|
+
*/
|
|
77
|
+
enqueue: (value: T) => void;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new Queue instance with FIFO (First-In, First-Out) behavior using a high-performance circular buffer.
|
|
81
|
+
*
|
|
82
|
+
* This factory function creates an optimized queue implementation that maintains excellent performance
|
|
83
|
+
* characteristics for both enqueue and dequeue operations. The underlying circular buffer automatically
|
|
84
|
+
* resizes to accommodate growing workloads while providing predictable O(1) operations.
|
|
85
|
+
*
|
|
86
|
+
* **Implementation Features:**
|
|
87
|
+
* - **O(1) enqueue operations** (amortized - occasionally O(n) when resizing)
|
|
88
|
+
* - **O(1) dequeue operations** (always)
|
|
89
|
+
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
90
|
+
* - **Memory efficient** - garbage collects removed elements immediately
|
|
91
|
+
* - **Circular buffer design** - eliminates need for array shifting operations
|
|
92
|
+
*
|
|
93
|
+
* **Performance Benefits:**
|
|
94
|
+
* - No array copying during normal operations
|
|
95
|
+
* - Minimal memory allocation overhead
|
|
96
|
+
* - Predictable performance under high load
|
|
97
|
+
* - Efficient memory usage with automatic cleanup
|
|
98
|
+
*
|
|
99
|
+
* @template T The type of elements stored in the queue.
|
|
100
|
+
* @param initialValues Optional array of initial elements to populate the queue.
|
|
101
|
+
* Elements will be dequeued in the same order they appear in the array.
|
|
102
|
+
* If provided, the initial buffer capacity will be at least twice the array length.
|
|
103
|
+
* @returns A new Queue instance optimized for high-performance FIFO operations.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* import { createQueue } from './queue';
|
|
108
|
+
*
|
|
109
|
+
* // Example 1: Basic FIFO workflow
|
|
110
|
+
* const requestQueue = createQueue<string>();
|
|
111
|
+
*
|
|
112
|
+
* // Add requests to the queue
|
|
113
|
+
* requestQueue.enqueue("GET /api/users"); // O(1)
|
|
114
|
+
* requestQueue.enqueue("POST /api/orders"); // O(1)
|
|
115
|
+
* requestQueue.enqueue("DELETE /api/cache"); // O(1)
|
|
116
|
+
*
|
|
117
|
+
* // Process requests in order
|
|
118
|
+
* while (!requestQueue.isEmpty) {
|
|
119
|
+
* const request = requestQueue.dequeue().unwrap(); // O(1)
|
|
120
|
+
* console.log(`Processing: ${request}`);
|
|
121
|
+
* }
|
|
122
|
+
* // Output:
|
|
123
|
+
* // Processing: GET /api/users
|
|
124
|
+
* // Processing: POST /api/orders
|
|
125
|
+
* // Processing: DELETE /api/cache
|
|
126
|
+
*
|
|
127
|
+
* // Example 2: High-throughput event processing
|
|
128
|
+
* type Event = { timestamp: number; type: string; data: any };
|
|
129
|
+
* const eventQueue = createQueue<Event>();
|
|
130
|
+
*
|
|
131
|
+
* // Simulate high-volume event ingestion
|
|
132
|
+
* for (let i = 0; i < 10000; i++) {
|
|
133
|
+
* eventQueue.enqueue({
|
|
134
|
+
* timestamp: Date.now(),
|
|
135
|
+
* type: `event-${i % 5}`,
|
|
136
|
+
* data: { value: i }
|
|
137
|
+
* }); // Each enqueue is O(1) amortized
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* // Process events efficiently
|
|
141
|
+
* let processedCount = 0;
|
|
142
|
+
* while (!eventQueue.isEmpty) {
|
|
143
|
+
* const event = eventQueue.dequeue().unwrap(); // O(1)
|
|
144
|
+
* // Process event...
|
|
145
|
+
* processedCount++;
|
|
146
|
+
* }
|
|
147
|
+
* console.log(`Processed ${processedCount} events`); // 10000
|
|
148
|
+
*
|
|
149
|
+
* // Example 3: Queue with pre-populated data
|
|
150
|
+
* const priorityTasks = createQueue<string>([
|
|
151
|
+
* "Initialize system",
|
|
152
|
+
* "Load configuration",
|
|
153
|
+
* "Start services",
|
|
154
|
+
* "Begin processing"
|
|
155
|
+
* ]);
|
|
156
|
+
*
|
|
157
|
+
* console.log(priorityTasks.size); // Output: 4
|
|
158
|
+
*
|
|
159
|
+
* // Execute tasks in initialization order
|
|
160
|
+
* while (!priorityTasks.isEmpty) {
|
|
161
|
+
* const task = priorityTasks.dequeue().unwrap();
|
|
162
|
+
* console.log(`Executing: ${task}`);
|
|
163
|
+
* }
|
|
164
|
+
*
|
|
165
|
+
* // Example 4: Producer-Consumer pattern
|
|
166
|
+
* const workQueue = createQueue<() => Promise<void>>();
|
|
167
|
+
*
|
|
168
|
+
* // Producer: Add work items
|
|
169
|
+
* const addWork = (workFn: () => Promise<void>) => {
|
|
170
|
+
* workQueue.enqueue(workFn);
|
|
171
|
+
* };
|
|
172
|
+
*
|
|
173
|
+
* // Consumer: Process work items
|
|
174
|
+
* const processWork = async () => {
|
|
175
|
+
* while (!workQueue.isEmpty) {
|
|
176
|
+
* const workItem = workQueue.dequeue().unwrap();
|
|
177
|
+
* await workItem();
|
|
178
|
+
* }
|
|
179
|
+
* };
|
|
180
|
+
*
|
|
181
|
+
* // Add some work
|
|
182
|
+
* addWork(async () => console.log("Work item 1"));
|
|
183
|
+
* addWork(async () => console.log("Work item 2"));
|
|
184
|
+
*
|
|
185
|
+
* // Process the work
|
|
186
|
+
* await processWork();
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export declare const createQueue: <T>(initialValues?: readonly T[]) => Queue<T>;
|
|
190
|
+
//# sourceMappingURL=queue.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.d.mts","sourceRoot":"","sources":["../../src/collections/queue.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAInD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,QAAQ,CAAC;IAC9B,oCAAoC;IACpC,OAAO,EAAE,OAAO,CAAC;IAEjB,2CAA2C;IAC3C,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC;IAEnB;;;OAGG;IACH,OAAO,EAAE,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE3B;;;OAGG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC7B,CAAC,CAAC;AAkMH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6GG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,EAAG,gBAAgB,SAAS,CAAC,EAAE,KAAG,KAAK,CAAC,CAAC,CACpC,CAAC"}
|