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,317 @@
|
|
|
1
|
+
import { Arr } from '../array/array-utils.mjs';
|
|
2
|
+
import '../array/tuple-utils.mjs';
|
|
3
|
+
import { Optional } from '../functional/optional.mjs';
|
|
4
|
+
import '../functional/result.mjs';
|
|
5
|
+
import '../number/branded-types/finite-number.mjs';
|
|
6
|
+
import '../number/branded-types/int.mjs';
|
|
7
|
+
import '../number/branded-types/int16.mjs';
|
|
8
|
+
import '../number/branded-types/int32.mjs';
|
|
9
|
+
import '../number/branded-types/non-negative-finite-number.mjs';
|
|
10
|
+
import '../number/branded-types/non-negative-int16.mjs';
|
|
11
|
+
import '../number/branded-types/non-negative-int32.mjs';
|
|
12
|
+
import '../number/branded-types/non-zero-finite-number.mjs';
|
|
13
|
+
import '../number/branded-types/non-zero-int.mjs';
|
|
14
|
+
import '../number/branded-types/non-zero-int16.mjs';
|
|
15
|
+
import '../number/branded-types/non-zero-int32.mjs';
|
|
16
|
+
import '../number/branded-types/non-zero-safe-int.mjs';
|
|
17
|
+
import '../number/branded-types/non-zero-uint16.mjs';
|
|
18
|
+
import '../number/branded-types/non-zero-uint32.mjs';
|
|
19
|
+
import '../number/branded-types/positive-finite-number.mjs';
|
|
20
|
+
import '../number/branded-types/positive-int.mjs';
|
|
21
|
+
import '../number/branded-types/positive-int16.mjs';
|
|
22
|
+
import '../number/branded-types/positive-int32.mjs';
|
|
23
|
+
import '../number/branded-types/positive-safe-int.mjs';
|
|
24
|
+
import '../number/branded-types/positive-uint16.mjs';
|
|
25
|
+
import '../number/branded-types/positive-uint32.mjs';
|
|
26
|
+
import '../number/branded-types/safe-int.mjs';
|
|
27
|
+
import '../number/branded-types/safe-uint.mjs';
|
|
28
|
+
import '../number/branded-types/uint.mjs';
|
|
29
|
+
import '../number/branded-types/uint16.mjs';
|
|
30
|
+
import { asUint32 } from '../number/branded-types/uint32.mjs';
|
|
31
|
+
import '../number/enum/int8.mjs';
|
|
32
|
+
import '../number/enum/uint8.mjs';
|
|
33
|
+
import '../number/num.mjs';
|
|
34
|
+
import '../number/refined-number-utils.mjs';
|
|
35
|
+
import { castMutable } from '../others/cast-mutable.mjs';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Class implementation for a queue with FIFO (First-In, First-Out) behavior using a circular buffer.
|
|
39
|
+
* This implementation provides O(1) enqueue and dequeue operations by using a fixed-size buffer
|
|
40
|
+
* with head and tail pointers that wrap around when they reach the buffer boundary.
|
|
41
|
+
*
|
|
42
|
+
* The circular buffer automatically resizes when it becomes full, ensuring that the queue
|
|
43
|
+
* can grow to accommodate any number of elements while maintaining efficient operations.
|
|
44
|
+
*
|
|
45
|
+
* @template T The type of elements in the queue.
|
|
46
|
+
* @implements Queue
|
|
47
|
+
*/
|
|
48
|
+
class QueueClass {
|
|
49
|
+
/** @internal Circular buffer to store queue elements. */
|
|
50
|
+
#buffer;
|
|
51
|
+
/** @internal Index of the first element (front of queue). */
|
|
52
|
+
#head;
|
|
53
|
+
/** @internal Index where the next element will be added (back of queue). */
|
|
54
|
+
#tail;
|
|
55
|
+
/** @internal Current number of elements in the queue. */
|
|
56
|
+
#mut_size;
|
|
57
|
+
/** @internal Current capacity of the buffer. */
|
|
58
|
+
#capacity;
|
|
59
|
+
/** @internal Initial capacity for new queues. */
|
|
60
|
+
static #INITIAL_CAPACITY = 8;
|
|
61
|
+
/**
|
|
62
|
+
* Constructs a new QueueClass instance.
|
|
63
|
+
* @param initialValues Optional initial values to populate the queue.
|
|
64
|
+
*/
|
|
65
|
+
constructor(initialValues = []) {
|
|
66
|
+
const initialCapacity = asUint32(Math.max(QueueClass.#INITIAL_CAPACITY, initialValues.length * 2));
|
|
67
|
+
this.#buffer = castMutable(Arr.create(initialCapacity, undefined));
|
|
68
|
+
this.#head = 0;
|
|
69
|
+
this.#tail = 0;
|
|
70
|
+
this.#mut_size = 0;
|
|
71
|
+
this.#capacity = initialCapacity;
|
|
72
|
+
// Add initial values
|
|
73
|
+
for (const value of initialValues) {
|
|
74
|
+
this.enqueue(value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** @inheritdoc */
|
|
78
|
+
get isEmpty() {
|
|
79
|
+
return this.#mut_size === 0;
|
|
80
|
+
}
|
|
81
|
+
/** @inheritdoc */
|
|
82
|
+
get size() {
|
|
83
|
+
return asUint32(this.#mut_size);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Removes and returns the element at the front of the queue (FIFO).
|
|
87
|
+
*
|
|
88
|
+
* This operation removes the element that was added earliest (first-in) and returns it.
|
|
89
|
+
* If the queue is empty, returns `Optional.none`. The operation is guaranteed to be O(1)
|
|
90
|
+
* and does not require any array shifting or copying.
|
|
91
|
+
*
|
|
92
|
+
* **Time Complexity:** O(1) - constant time operation
|
|
93
|
+
* **Space Complexity:** O(1) - no additional memory allocation
|
|
94
|
+
*
|
|
95
|
+
* @returns An Optional containing the removed element, or `Optional.none` if the queue is empty.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const queue = createQueue<string>();
|
|
100
|
+
*
|
|
101
|
+
* // Add some elements
|
|
102
|
+
* queue.enqueue("first");
|
|
103
|
+
* queue.enqueue("second");
|
|
104
|
+
* queue.enqueue("third");
|
|
105
|
+
*
|
|
106
|
+
* // Remove elements in FIFO order
|
|
107
|
+
* const first = queue.dequeue();
|
|
108
|
+
* if (first.isSome) {
|
|
109
|
+
* console.log(first.value); // "first"
|
|
110
|
+
* }
|
|
111
|
+
*
|
|
112
|
+
* const second = queue.dequeue().unwrap(); // "second"
|
|
113
|
+
* console.log(queue.size); // 1
|
|
114
|
+
*
|
|
115
|
+
* // Safe handling of empty queue
|
|
116
|
+
* const emptyQueue = createQueue<number>();
|
|
117
|
+
* const result = emptyQueue.dequeue();
|
|
118
|
+
* if (result.isNone) {
|
|
119
|
+
* console.log("Queue is empty");
|
|
120
|
+
* }
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
dequeue() {
|
|
124
|
+
if (this.isEmpty) {
|
|
125
|
+
return Optional.none;
|
|
126
|
+
}
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
128
|
+
const element = this.#buffer[this.#head];
|
|
129
|
+
this.#buffer[this.#head] = undefined; // Clear reference for garbage collection
|
|
130
|
+
this.#head = (this.#head + 1) % this.#capacity;
|
|
131
|
+
this.#mut_size -= 1;
|
|
132
|
+
return Optional.some(element);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Adds an element to the back of the queue (FIFO).
|
|
136
|
+
*
|
|
137
|
+
* This operation adds the element to the end of the queue, where it will be the last
|
|
138
|
+
* to be dequeued (first-in, first-out ordering). The operation is amortized O(1),
|
|
139
|
+
* meaning it's O(1) for most operations with occasional O(n) when the buffer needs resizing.
|
|
140
|
+
*
|
|
141
|
+
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is needed
|
|
142
|
+
* **Space Complexity:** O(1) - constant additional memory per element
|
|
143
|
+
*
|
|
144
|
+
* **Buffer Resizing:** When the internal buffer becomes full, it automatically doubles
|
|
145
|
+
* in size and reorganizes elements to maintain the circular buffer structure.
|
|
146
|
+
*
|
|
147
|
+
* @param value The element to add to the back of the queue.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const taskQueue = createQueue<string>();
|
|
152
|
+
*
|
|
153
|
+
* // Add tasks in order of arrival
|
|
154
|
+
* taskQueue.enqueue("Process order #1001"); // O(1)
|
|
155
|
+
* taskQueue.enqueue("Send notification"); // O(1)
|
|
156
|
+
* taskQueue.enqueue("Update inventory"); // O(1)
|
|
157
|
+
*
|
|
158
|
+
* console.log(taskQueue.size); // 3
|
|
159
|
+
*
|
|
160
|
+
* // Tasks will be processed in the order they were added
|
|
161
|
+
* while (!taskQueue.isEmpty) {
|
|
162
|
+
* const task = taskQueue.dequeue().unwrap();
|
|
163
|
+
* console.log(`Executing: ${task}`);
|
|
164
|
+
* }
|
|
165
|
+
*
|
|
166
|
+
* // High-volume enqueueing (demonstrates amortized O(1) performance)
|
|
167
|
+
* const dataQueue = createQueue<number>();
|
|
168
|
+
*
|
|
169
|
+
* for (let i = 0; i < 1000000; i++) {
|
|
170
|
+
* dataQueue.enqueue(i); // Each operation is O(1) amortized
|
|
171
|
+
* }
|
|
172
|
+
*
|
|
173
|
+
* console.log(dataQueue.size); // 1000000
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
enqueue(value) {
|
|
177
|
+
// Resize if buffer is full
|
|
178
|
+
if (this.#mut_size === this.#capacity) {
|
|
179
|
+
this.#resize();
|
|
180
|
+
}
|
|
181
|
+
this.#buffer[this.#tail] = value;
|
|
182
|
+
this.#tail = (this.#tail + 1) % this.#capacity;
|
|
183
|
+
this.#mut_size += 1;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @internal
|
|
187
|
+
* Resizes the circular buffer when it becomes full.
|
|
188
|
+
* Doubles the capacity and reorganizes elements to maintain queue order.
|
|
189
|
+
*/
|
|
190
|
+
#resize() {
|
|
191
|
+
const newCapacity = asUint32(this.#capacity * 2);
|
|
192
|
+
const newBuffer = castMutable(Arr.create(newCapacity, undefined));
|
|
193
|
+
// Copy elements in order from head to tail
|
|
194
|
+
for (let i = 0; i < this.#mut_size; i++) {
|
|
195
|
+
const sourceIndex = (this.#head + i) % this.#capacity;
|
|
196
|
+
newBuffer[i] = this.#buffer[sourceIndex];
|
|
197
|
+
}
|
|
198
|
+
this.#buffer = newBuffer;
|
|
199
|
+
this.#head = 0;
|
|
200
|
+
this.#tail = this.#mut_size;
|
|
201
|
+
this.#capacity = newCapacity;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Creates a new Queue instance with FIFO (First-In, First-Out) behavior using a high-performance circular buffer.
|
|
206
|
+
*
|
|
207
|
+
* This factory function creates an optimized queue implementation that maintains excellent performance
|
|
208
|
+
* characteristics for both enqueue and dequeue operations. The underlying circular buffer automatically
|
|
209
|
+
* resizes to accommodate growing workloads while providing predictable O(1) operations.
|
|
210
|
+
*
|
|
211
|
+
* **Implementation Features:**
|
|
212
|
+
* - **O(1) enqueue operations** (amortized - occasionally O(n) when resizing)
|
|
213
|
+
* - **O(1) dequeue operations** (always)
|
|
214
|
+
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
215
|
+
* - **Memory efficient** - garbage collects removed elements immediately
|
|
216
|
+
* - **Circular buffer design** - eliminates need for array shifting operations
|
|
217
|
+
*
|
|
218
|
+
* **Performance Benefits:**
|
|
219
|
+
* - No array copying during normal operations
|
|
220
|
+
* - Minimal memory allocation overhead
|
|
221
|
+
* - Predictable performance under high load
|
|
222
|
+
* - Efficient memory usage with automatic cleanup
|
|
223
|
+
*
|
|
224
|
+
* @template T The type of elements stored in the queue.
|
|
225
|
+
* @param initialValues Optional array of initial elements to populate the queue.
|
|
226
|
+
* Elements will be dequeued in the same order they appear in the array.
|
|
227
|
+
* If provided, the initial buffer capacity will be at least twice the array length.
|
|
228
|
+
* @returns A new Queue instance optimized for high-performance FIFO operations.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* import { createQueue } from './queue';
|
|
233
|
+
*
|
|
234
|
+
* // Example 1: Basic FIFO workflow
|
|
235
|
+
* const requestQueue = createQueue<string>();
|
|
236
|
+
*
|
|
237
|
+
* // Add requests to the queue
|
|
238
|
+
* requestQueue.enqueue("GET /api/users"); // O(1)
|
|
239
|
+
* requestQueue.enqueue("POST /api/orders"); // O(1)
|
|
240
|
+
* requestQueue.enqueue("DELETE /api/cache"); // O(1)
|
|
241
|
+
*
|
|
242
|
+
* // Process requests in order
|
|
243
|
+
* while (!requestQueue.isEmpty) {
|
|
244
|
+
* const request = requestQueue.dequeue().unwrap(); // O(1)
|
|
245
|
+
* console.log(`Processing: ${request}`);
|
|
246
|
+
* }
|
|
247
|
+
* // Output:
|
|
248
|
+
* // Processing: GET /api/users
|
|
249
|
+
* // Processing: POST /api/orders
|
|
250
|
+
* // Processing: DELETE /api/cache
|
|
251
|
+
*
|
|
252
|
+
* // Example 2: High-throughput event processing
|
|
253
|
+
* type Event = { timestamp: number; type: string; data: any };
|
|
254
|
+
* const eventQueue = createQueue<Event>();
|
|
255
|
+
*
|
|
256
|
+
* // Simulate high-volume event ingestion
|
|
257
|
+
* for (let i = 0; i < 10000; i++) {
|
|
258
|
+
* eventQueue.enqueue({
|
|
259
|
+
* timestamp: Date.now(),
|
|
260
|
+
* type: `event-${i % 5}`,
|
|
261
|
+
* data: { value: i }
|
|
262
|
+
* }); // Each enqueue is O(1) amortized
|
|
263
|
+
* }
|
|
264
|
+
*
|
|
265
|
+
* // Process events efficiently
|
|
266
|
+
* let processedCount = 0;
|
|
267
|
+
* while (!eventQueue.isEmpty) {
|
|
268
|
+
* const event = eventQueue.dequeue().unwrap(); // O(1)
|
|
269
|
+
* // Process event...
|
|
270
|
+
* processedCount++;
|
|
271
|
+
* }
|
|
272
|
+
* console.log(`Processed ${processedCount} events`); // 10000
|
|
273
|
+
*
|
|
274
|
+
* // Example 3: Queue with pre-populated data
|
|
275
|
+
* const priorityTasks = createQueue<string>([
|
|
276
|
+
* "Initialize system",
|
|
277
|
+
* "Load configuration",
|
|
278
|
+
* "Start services",
|
|
279
|
+
* "Begin processing"
|
|
280
|
+
* ]);
|
|
281
|
+
*
|
|
282
|
+
* console.log(priorityTasks.size); // Output: 4
|
|
283
|
+
*
|
|
284
|
+
* // Execute tasks in initialization order
|
|
285
|
+
* while (!priorityTasks.isEmpty) {
|
|
286
|
+
* const task = priorityTasks.dequeue().unwrap();
|
|
287
|
+
* console.log(`Executing: ${task}`);
|
|
288
|
+
* }
|
|
289
|
+
*
|
|
290
|
+
* // Example 4: Producer-Consumer pattern
|
|
291
|
+
* const workQueue = createQueue<() => Promise<void>>();
|
|
292
|
+
*
|
|
293
|
+
* // Producer: Add work items
|
|
294
|
+
* const addWork = (workFn: () => Promise<void>) => {
|
|
295
|
+
* workQueue.enqueue(workFn);
|
|
296
|
+
* };
|
|
297
|
+
*
|
|
298
|
+
* // Consumer: Process work items
|
|
299
|
+
* const processWork = async () => {
|
|
300
|
+
* while (!workQueue.isEmpty) {
|
|
301
|
+
* const workItem = workQueue.dequeue().unwrap();
|
|
302
|
+
* await workItem();
|
|
303
|
+
* }
|
|
304
|
+
* };
|
|
305
|
+
*
|
|
306
|
+
* // Add some work
|
|
307
|
+
* addWork(async () => console.log("Work item 1"));
|
|
308
|
+
* addWork(async () => console.log("Work item 2"));
|
|
309
|
+
*
|
|
310
|
+
* // Process the work
|
|
311
|
+
* await processWork();
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
const createQueue = (initialValues) => new QueueClass(initialValues);
|
|
315
|
+
|
|
316
|
+
export { createQueue };
|
|
317
|
+
//# sourceMappingURL=queue.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.mjs","sources":["../../src/collections/queue.mts"],"sourcesContent":[null],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { Optional } from '../functional/index.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for a high-performance stack with LIFO (Last-In, First-Out) behavior.
|
|
4
|
+
*
|
|
5
|
+
* This interface defines a mutable stack data structure where elements are added to and removed
|
|
6
|
+
* from the top, following the Last-In, First-Out principle. The implementation uses a dynamic
|
|
7
|
+
* array for optimal performance, providing O(1) operations for both push and pop operations.
|
|
8
|
+
*
|
|
9
|
+
* **LIFO Behavior:**
|
|
10
|
+
* - **push**: Adds elements to the top of the stack
|
|
11
|
+
* - **pop**: Removes and returns elements from the top of the stack
|
|
12
|
+
* - The last element added is the first element to be removed
|
|
13
|
+
*
|
|
14
|
+
* **Performance Characteristics:**
|
|
15
|
+
* - push: O(1) amortized (O(n) when buffer needs resizing)
|
|
16
|
+
* - pop: O(1) always
|
|
17
|
+
* - size/isEmpty: O(1) always
|
|
18
|
+
* - Memory efficient with automatic garbage collection of removed elements
|
|
19
|
+
*
|
|
20
|
+
* **Use Cases:**
|
|
21
|
+
* - Function call management and recursion
|
|
22
|
+
* - Undo/redo functionality
|
|
23
|
+
* - Expression evaluation and parsing
|
|
24
|
+
* - Depth-first search algorithms
|
|
25
|
+
* - Backtracking algorithms
|
|
26
|
+
* - Browser history management
|
|
27
|
+
*
|
|
28
|
+
* @template T The type of elements stored in the stack.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { createStack, Stack } from './stack';
|
|
33
|
+
*
|
|
34
|
+
* // Example 1: Basic LIFO operations
|
|
35
|
+
* const operationStack: Stack<string> = createStack<string>();
|
|
36
|
+
*
|
|
37
|
+
* operationStack.push("operation1"); // Add to top
|
|
38
|
+
* operationStack.push("operation2"); // Add to top
|
|
39
|
+
* operationStack.push("operation3"); // Add to top
|
|
40
|
+
*
|
|
41
|
+
* console.log(operationStack.size); // Output: 3
|
|
42
|
+
*
|
|
43
|
+
* // Process operations in LIFO order
|
|
44
|
+
* console.log(operationStack.pop().unwrap()); // "operation3" (last in, first out)
|
|
45
|
+
* console.log(operationStack.pop().unwrap()); // "operation2"
|
|
46
|
+
* console.log(operationStack.size); // Output: 1
|
|
47
|
+
*
|
|
48
|
+
* // Example 2: Undo functionality
|
|
49
|
+
* type Action = { type: string; data: any; timestamp: number };
|
|
50
|
+
* const undoStack: Stack<Action> = createStack<Action>();
|
|
51
|
+
*
|
|
52
|
+
* undoStack.push({ type: "delete", data: { id: 123 }, timestamp: Date.now() });
|
|
53
|
+
* undoStack.push({ type: "edit", data: { field: "name", oldValue: "old" }, timestamp: Date.now() });
|
|
54
|
+
*
|
|
55
|
+
* // Undo last action
|
|
56
|
+
* if (!undoStack.isEmpty) {
|
|
57
|
+
* const lastAction = undoStack.pop().unwrap();
|
|
58
|
+
* console.log(`Undoing: ${lastAction.type}`);
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export type Stack<T> = Readonly<{
|
|
63
|
+
/** Checks if the stack is empty. */
|
|
64
|
+
isEmpty: boolean;
|
|
65
|
+
/** The number of elements in the stack. */
|
|
66
|
+
size: SizeType.Arr;
|
|
67
|
+
/**
|
|
68
|
+
* Removes and returns the element at the top of the stack.
|
|
69
|
+
* @returns The element at the top of the stack, or `Optional.none` if the stack is empty.
|
|
70
|
+
*/
|
|
71
|
+
pop: () => Optional<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Adds an element to the top of the stack.
|
|
74
|
+
* @param value The element to add.
|
|
75
|
+
*/
|
|
76
|
+
push: (value: T) => void;
|
|
77
|
+
}>;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a high-performance dynamic array.
|
|
80
|
+
*
|
|
81
|
+
* This factory function creates an optimized stack implementation that maintains excellent performance
|
|
82
|
+
* characteristics for both push and pop operations. The underlying dynamic array automatically
|
|
83
|
+
* resizes to accommodate growing workloads while providing predictable O(1) operations.
|
|
84
|
+
*
|
|
85
|
+
* **Implementation Features:**
|
|
86
|
+
* - **O(1) push operations** (amortized - occasionally O(n) when resizing)
|
|
87
|
+
* - **O(1) pop operations** (always)
|
|
88
|
+
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
89
|
+
* - **Memory efficient** - garbage collects removed elements immediately
|
|
90
|
+
* - **Dynamic array design** - eliminates need for complex memory management
|
|
91
|
+
*
|
|
92
|
+
* **Performance Benefits:**
|
|
93
|
+
* - No array shifting required for stack operations
|
|
94
|
+
* - Minimal memory allocation overhead
|
|
95
|
+
* - Predictable performance under high load
|
|
96
|
+
* - Efficient memory usage with automatic cleanup
|
|
97
|
+
*
|
|
98
|
+
* @template T The type of elements stored in the stack.
|
|
99
|
+
* @param initialValues Optional array of initial elements to populate the stack.
|
|
100
|
+
* Elements will be popped in reverse order of how they appear in the array
|
|
101
|
+
* (last array element will be popped first).
|
|
102
|
+
* If provided, the initial buffer capacity will be at least twice the array length.
|
|
103
|
+
* @returns A new Stack instance optimized for high-performance LIFO operations.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* import { createStack } from './stack';
|
|
108
|
+
*
|
|
109
|
+
* // Example 1: Function call simulation
|
|
110
|
+
* type FunctionCall = { name: string; args: any[]; context: any };
|
|
111
|
+
* const callStack = createStack<FunctionCall>();
|
|
112
|
+
*
|
|
113
|
+
* // Simulate function calls (push onto stack)
|
|
114
|
+
* callStack.push({ name: "main", args: [], context: {} }); // O(1)
|
|
115
|
+
* callStack.push({ name: "processData", args: [data], context: {} }); // O(1)
|
|
116
|
+
* callStack.push({ name: "validateInput", args: [input], context: {} }); // O(1)
|
|
117
|
+
*
|
|
118
|
+
* // Simulate function returns (pop from stack)
|
|
119
|
+
* while (!callStack.isEmpty) {
|
|
120
|
+
* const call = callStack.pop().unwrap(); // O(1)
|
|
121
|
+
* console.log(`Returning from: ${call.name}`);
|
|
122
|
+
* }
|
|
123
|
+
* // Output:
|
|
124
|
+
* // Returning from: validateInput
|
|
125
|
+
* // Returning from: processData
|
|
126
|
+
* // Returning from: main
|
|
127
|
+
*
|
|
128
|
+
* // Example 2: Expression evaluation with operator precedence
|
|
129
|
+
* const operatorStack = createStack<string>();
|
|
130
|
+
* const operandStack = createStack<number>();
|
|
131
|
+
*
|
|
132
|
+
* // Simulate parsing "3 + 4 * 2"
|
|
133
|
+
* operandStack.push(3);
|
|
134
|
+
* operatorStack.push("+");
|
|
135
|
+
* operandStack.push(4);
|
|
136
|
+
* operatorStack.push("*"); // Higher precedence
|
|
137
|
+
* operandStack.push(2);
|
|
138
|
+
*
|
|
139
|
+
* // Process higher precedence first (LIFO)
|
|
140
|
+
* const op = operatorStack.pop().unwrap(); // "*"
|
|
141
|
+
* const b = operandStack.pop().unwrap(); // 2
|
|
142
|
+
* const a = operandStack.pop().unwrap(); // 4
|
|
143
|
+
* operandStack.push(a * b); // Push result: 8
|
|
144
|
+
*
|
|
145
|
+
* // Example 3: Undo/Redo functionality
|
|
146
|
+
* type EditAction = {
|
|
147
|
+
* type: 'insert' | 'delete' | 'modify';
|
|
148
|
+
* position: number;
|
|
149
|
+
* oldValue: string;
|
|
150
|
+
* newValue: string;
|
|
151
|
+
* };
|
|
152
|
+
*
|
|
153
|
+
* const undoStack = createStack<EditAction>();
|
|
154
|
+
* const redoStack = createStack<EditAction>();
|
|
155
|
+
*
|
|
156
|
+
* // Perform edits (push to undo stack)
|
|
157
|
+
* const edit1: EditAction = { type: 'insert', position: 0, oldValue: '', newValue: 'Hello' };
|
|
158
|
+
* const edit2: EditAction = { type: 'insert', position: 5, oldValue: '', newValue: ' World' };
|
|
159
|
+
*
|
|
160
|
+
* undoStack.push(edit1);
|
|
161
|
+
* undoStack.push(edit2);
|
|
162
|
+
*
|
|
163
|
+
* // Undo last edit
|
|
164
|
+
* if (!undoStack.isEmpty) {
|
|
165
|
+
* const lastEdit = undoStack.pop().unwrap();
|
|
166
|
+
* redoStack.push(lastEdit);
|
|
167
|
+
* console.log(`Undid: ${lastEdit.type} at position ${lastEdit.position}`);
|
|
168
|
+
* }
|
|
169
|
+
*
|
|
170
|
+
* // Example 4: High-throughput data processing
|
|
171
|
+
* const processingStack = createStack<number>();
|
|
172
|
+
*
|
|
173
|
+
* // Add large amount of data (demonstrates amortized O(1) performance)
|
|
174
|
+
* for (let i = 0; i < 100000; i++) {
|
|
175
|
+
* processingStack.push(i); // Each push is O(1) amortized
|
|
176
|
+
* }
|
|
177
|
+
*
|
|
178
|
+
* // Process data in LIFO order
|
|
179
|
+
* let processedCount = 0;
|
|
180
|
+
* while (!processingStack.isEmpty) {
|
|
181
|
+
* const value = processingStack.pop().unwrap(); // O(1)
|
|
182
|
+
* // Process value...
|
|
183
|
+
* processedCount++;
|
|
184
|
+
* }
|
|
185
|
+
* console.log(`Processed ${processedCount} items`); // 100000
|
|
186
|
+
*
|
|
187
|
+
* // Example 5: Stack with pre-populated data
|
|
188
|
+
* const historyStack = createStack<string>([
|
|
189
|
+
* "page1.html",
|
|
190
|
+
* "page2.html",
|
|
191
|
+
* "page3.html",
|
|
192
|
+
* "page4.html"
|
|
193
|
+
* ]);
|
|
194
|
+
*
|
|
195
|
+
* console.log(historyStack.size); // Output: 4
|
|
196
|
+
*
|
|
197
|
+
* // Navigate back through history (LIFO order)
|
|
198
|
+
* while (!historyStack.isEmpty) {
|
|
199
|
+
* const page = historyStack.pop().unwrap();
|
|
200
|
+
* console.log(`Going back to: ${page}`);
|
|
201
|
+
* }
|
|
202
|
+
* // Output:
|
|
203
|
+
* // Going back to: page4.html (last added, first removed)
|
|
204
|
+
* // Going back to: page3.html
|
|
205
|
+
* // Going back to: page2.html
|
|
206
|
+
* // Going back to: page1.html
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export declare const createStack: <T>(initialValues?: readonly T[]) => Stack<T>;
|
|
210
|
+
//# sourceMappingURL=stack.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.d.mts","sourceRoot":"","sources":["../../src/collections/stack.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAInD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;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,GAAG,EAAE,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEvB;;;OAGG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC1B,CAAC,CAAC;AA+MH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkIG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,EAAG,gBAAgB,SAAS,CAAC,EAAE,KAAG,KAAK,CAAC,CAAC,CACpC,CAAC"}
|