ts-data-forge 9.0.0 → 9.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/dist/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-creation.mjs +17 -112
- package/dist/array/impl/array-utils-creation.mjs.map +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts.map +1 -1
- package/dist/array/impl/array-utils-element-access.mjs +22 -61
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/array/impl/array-utils-iterators.d.mts.map +1 -1
- package/dist/array/impl/array-utils-iterators.mjs +9 -63
- package/dist/array/impl/array-utils-iterators.mjs.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs +78 -78
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +95 -89
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-search.mjs +100 -92
- package/dist/array/impl/array-utils-search.mjs.map +1 -1
- package/dist/array/impl/array-utils-set-op.d.mts.map +1 -1
- package/dist/array/impl/array-utils-set-op.mjs +31 -141
- package/dist/array/impl/array-utils-set-op.mjs.map +1 -1
- package/dist/array/impl/array-utils-size.d.mts.map +1 -1
- package/dist/array/impl/array-utils-size.mjs +4 -8
- package/dist/array/impl/array-utils-size.mjs.map +1 -1
- package/dist/array/impl/array-utils-slice-clamped.mjs +11 -12
- package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs +44 -96
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +150 -292
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-validation.mjs +18 -169
- package/dist/array/impl/array-utils-validation.mjs.map +1 -1
- package/dist/collections/imap-mapped.d.mts.map +1 -1
- package/dist/collections/imap-mapped.mjs +243 -330
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.d.mts.map +1 -1
- package/dist/collections/imap.mjs +190 -275
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/iset-mapped.d.mts +1 -1
- package/dist/collections/iset-mapped.d.mts.map +1 -1
- package/dist/collections/iset-mapped.mjs +222 -444
- package/dist/collections/iset-mapped.mjs.map +1 -1
- package/dist/collections/iset.d.mts +1 -1
- package/dist/collections/iset.d.mts.map +1 -1
- package/dist/collections/iset.mjs +158 -324
- package/dist/collections/iset.mjs.map +1 -1
- package/dist/collections/queue.d.mts.map +1 -1
- package/dist/collections/queue.mjs +106 -178
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.d.mts.map +1 -1
- package/dist/collections/stack.mjs +95 -165
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/expect-type.d.mts +1 -1
- package/dist/expect-type.d.mts.map +1 -1
- package/dist/expect-type.mjs +1 -69
- package/dist/expect-type.mjs.map +1 -1
- package/dist/functional/match.mjs +9 -9
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-expect-to-be.mjs +13 -14
- package/dist/functional/optional/impl/optional-expect-to-be.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-filter.mjs +14 -15
- package/dist/functional/optional/impl/optional-filter.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-flat-map.mjs +10 -11
- package/dist/functional/optional/impl/optional-flat-map.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-from-nullable.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-from-nullable.mjs +1 -25
- package/dist/functional/optional/impl/optional-from-nullable.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-none.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-none.mjs +0 -19
- package/dist/functional/optional/impl/optional-is-none.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-optional.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-optional.mjs +1 -23
- package/dist/functional/optional/impl/optional-is-optional.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-some.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-some.mjs +0 -20
- package/dist/functional/optional/impl/optional-is-some.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-map.mjs +13 -14
- package/dist/functional/optional/impl/optional-map.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-none.mjs +0 -16
- package/dist/functional/optional/impl/optional-none.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-or-else.mjs +10 -11
- package/dist/functional/optional/impl/optional-or-else.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-some.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-some.mjs +2 -21
- package/dist/functional/optional/impl/optional-some.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-to-nullable.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-to-nullable.mjs +1 -27
- package/dist/functional/optional/impl/optional-to-nullable.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-or.mjs +9 -10
- package/dist/functional/optional/impl/optional-unwrap-or.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs +4 -32
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap.mjs +4 -4
- package/dist/functional/optional/impl/optional-unwrap.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-zip.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-zip.mjs +1 -35
- package/dist/functional/optional/impl/optional-zip.mjs.map +1 -1
- package/dist/functional/optional/impl/tag.mjs +2 -3
- package/dist/functional/optional/impl/tag.mjs.map +1 -1
- package/dist/functional/pipe.d.mts.map +1 -1
- package/dist/functional/pipe.mjs +9 -34
- package/dist/functional/pipe.mjs.map +1 -1
- package/dist/functional/result/impl/result-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-err.mjs +2 -27
- package/dist/functional/result/impl/result-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-expect-to-be.mjs +13 -15
- package/dist/functional/result/impl/result-expect-to-be.mjs.map +1 -1
- package/dist/functional/result/impl/result-flat-map.mjs +16 -16
- package/dist/functional/result/impl/result-flat-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-fold.mjs +16 -16
- package/dist/functional/result/impl/result-fold.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-promise.mjs +4 -31
- package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-throwable.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-throwable.mjs +8 -38
- package/dist/functional/result/impl/result-from-throwable.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-err.mjs +0 -19
- package/dist/functional/result/impl/result-is-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-ok.mjs +0 -19
- package/dist/functional/result/impl/result-is-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-result.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-result.mjs +1 -23
- package/dist/functional/result/impl/result-is-result.mjs.map +1 -1
- package/dist/functional/result/impl/result-map-err.mjs +16 -16
- package/dist/functional/result/impl/result-map-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-map.mjs +16 -16
- package/dist/functional/result/impl/result-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-ok.mjs +2 -27
- package/dist/functional/result/impl/result-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-or-else.mjs +10 -12
- package/dist/functional/result/impl/result-or-else.mjs.map +1 -1
- package/dist/functional/result/impl/result-swap.d.mts.map +1 -1
- package/dist/functional/result/impl/result-swap.mjs +4 -22
- package/dist/functional/result/impl/result-swap.mjs.map +1 -1
- package/dist/functional/result/impl/result-to-optional.d.mts.map +1 -1
- package/dist/functional/result/impl/result-to-optional.mjs +0 -27
- package/dist/functional/result/impl/result-to-optional.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-or.mjs +9 -11
- package/dist/functional/result/impl/result-unwrap-err-or.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs +13 -40
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err.mjs +4 -32
- package/dist/functional/result/impl/result-unwrap-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs +9 -11
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok.mjs +4 -4
- package/dist/functional/result/impl/result-unwrap-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.mjs +4 -35
- package/dist/functional/result/impl/result-unwrap-throw.mjs.map +1 -1
- package/dist/functional/result/impl/result-zip.d.mts.map +1 -1
- package/dist/functional/result/impl/result-zip.mjs +1 -34
- package/dist/functional/result/impl/result-zip.mjs.map +1 -1
- package/dist/functional/result/impl/tag.mjs +2 -3
- package/dist/functional/result/impl/tag.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/tag.mjs +3 -5
- package/dist/functional/ternary-result/impl/tag.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs +2 -15
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs +13 -13
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs +27 -26
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs +21 -22
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +4 -20
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +7 -25
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs +0 -17
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +0 -19
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +1 -25
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +0 -18
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs +13 -15
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs +18 -18
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs +21 -22
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs +2 -16
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +4 -22
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +12 -27
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +4 -19
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs +4 -5
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +15 -27
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +10 -24
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +4 -19
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs +3 -19
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +14 -45
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/variant-name.d.mts +1 -1
- package/dist/functional/ternary-result/impl/variant-name.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/variant-name.mjs +8 -9
- package/dist/functional/ternary-result/impl/variant-name.mjs.map +1 -1
- package/dist/guard/has-key.d.mts.map +1 -1
- package/dist/guard/has-key.mjs +0 -41
- package/dist/guard/has-key.mjs.map +1 -1
- package/dist/guard/is-length-bounded-string.d.mts.map +1 -1
- package/dist/guard/is-length-bounded-string.mjs +0 -157
- package/dist/guard/is-length-bounded-string.mjs.map +1 -1
- package/dist/guard/is-non-empty-string.d.mts +1 -1
- package/dist/guard/is-non-empty-string.d.mts.map +1 -1
- package/dist/guard/is-non-empty-string.mjs +1 -38
- package/dist/guard/is-non-empty-string.mjs.map +1 -1
- package/dist/guard/is-non-null-object.d.mts.map +1 -1
- package/dist/guard/is-non-null-object.mjs +1 -35
- package/dist/guard/is-non-null-object.mjs.map +1 -1
- package/dist/guard/is-primitive.d.mts.map +1 -1
- package/dist/guard/is-primitive.mjs +12 -43
- package/dist/guard/is-primitive.mjs.map +1 -1
- package/dist/guard/is-record.d.mts.map +1 -1
- package/dist/guard/is-record.mjs +0 -86
- package/dist/guard/is-record.mjs.map +1 -1
- package/dist/guard/is-type.d.mts.map +1 -1
- package/dist/guard/is-type.mjs +12 -399
- package/dist/guard/is-type.mjs.map +1 -1
- package/dist/guard/key-is-in.d.mts.map +1 -1
- package/dist/guard/key-is-in.mjs +0 -40
- package/dist/guard/key-is-in.mjs.map +1 -1
- package/dist/iterator/range.mjs +16 -16
- package/dist/iterator/range.mjs.map +1 -1
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +52 -340
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/branded-types/finite-number.d.mts +3 -3
- package/dist/number/branded-types/finite-number.mjs +135 -154
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +4 -4
- package/dist/number/branded-types/int.mjs +205 -245
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.d.mts +15 -15
- package/dist/number/branded-types/int16.mjs +116 -123
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +11 -11
- package/dist/number/branded-types/int32.mjs +116 -123
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +4 -4
- package/dist/number/branded-types/negative-finite-number.mjs +129 -144
- package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +5 -5
- package/dist/number/branded-types/negative-int.mjs +94 -113
- package/dist/number/branded-types/negative-int.mjs.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +5 -5
- package/dist/number/branded-types/negative-safe-int.mjs +103 -124
- package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +5 -5
- package/dist/number/branded-types/non-negative-finite-number.mjs +136 -142
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +16 -16
- package/dist/number/branded-types/non-negative-int16.mjs +109 -118
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +12 -12
- package/dist/number/branded-types/non-negative-int32.mjs +110 -119
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +3 -3
- package/dist/number/branded-types/non-positive-finite-number.mjs +120 -124
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-int.d.mts +4 -4
- package/dist/number/branded-types/non-positive-int.mjs +167 -212
- package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.d.mts +4 -4
- package/dist/number/branded-types/non-positive-safe-int.mjs +95 -106
- package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +4 -4
- package/dist/number/branded-types/non-zero-finite-number.mjs +114 -123
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.d.mts +2 -2
- package/dist/number/branded-types/non-zero-int.mjs +72 -83
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.d.mts +7 -7
- package/dist/number/branded-types/non-zero-int16.mjs +96 -107
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +5 -5
- package/dist/number/branded-types/non-zero-int32.mjs +97 -108
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +2 -2
- package/dist/number/branded-types/non-zero-safe-int.mjs +100 -111
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +8 -8
- package/dist/number/branded-types/non-zero-uint16.mjs +96 -107
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +6 -6
- package/dist/number/branded-types/non-zero-uint32.mjs +96 -107
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +8 -8
- package/dist/number/branded-types/positive-finite-number.mjs +139 -141
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +11 -11
- package/dist/number/branded-types/positive-int.mjs +228 -271
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +19 -19
- package/dist/number/branded-types/positive-int16.mjs +109 -118
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +15 -15
- package/dist/number/branded-types/positive-int32.mjs +109 -118
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +11 -11
- package/dist/number/branded-types/positive-safe-int.mjs +244 -275
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +17 -17
- package/dist/number/branded-types/positive-uint16.mjs +109 -118
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +13 -13
- package/dist/number/branded-types/positive-uint32.mjs +110 -119
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +4 -4
- package/dist/number/branded-types/safe-int.mjs +251 -292
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +8 -8
- package/dist/number/branded-types/safe-uint.mjs +111 -119
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +8 -8
- package/dist/number/branded-types/uint.mjs +198 -225
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +14 -14
- package/dist/number/branded-types/uint16.mjs +109 -119
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +10 -10
- package/dist/number/branded-types/uint32.mjs +110 -117
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.mjs +119 -145
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.mjs +107 -121
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/number/num.d.mts +2 -2
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +76 -537
- package/dist/number/num.mjs.map +1 -1
- package/dist/number/refined-number-utils.d.mts +1 -1
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs +127 -263
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +107 -222
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/cast-mutable.d.mts.map +1 -1
- package/dist/others/cast-mutable.mjs +8 -37
- package/dist/others/cast-mutable.mjs.map +1 -1
- package/dist/others/cast-readonly.d.mts.map +1 -1
- package/dist/others/cast-readonly.mjs +8 -36
- package/dist/others/cast-readonly.mjs.map +1 -1
- package/dist/others/debounce-function.d.mts.map +1 -1
- package/dist/others/debounce-function.mjs +7 -16
- package/dist/others/debounce-function.mjs.map +1 -1
- package/dist/others/fast-deep-equal.d.mts.map +1 -1
- package/dist/others/fast-deep-equal.mjs +43 -59
- package/dist/others/fast-deep-equal.mjs.map +1 -1
- package/dist/others/if-then.d.mts.map +1 -1
- package/dist/others/if-then.mjs +0 -29
- package/dist/others/if-then.mjs.map +1 -1
- package/dist/others/map-nullable.mjs +9 -9
- package/dist/others/map-nullable.mjs.map +1 -1
- package/dist/others/memoize-function.mjs +12 -12
- package/dist/others/memoize-function.mjs.map +1 -1
- package/dist/others/tuple.d.mts.map +1 -1
- package/dist/others/tuple.mjs +0 -50
- package/dist/others/tuple.mjs.map +1 -1
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs +41 -88
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/dist/promise/promise.d.mts.map +1 -1
- package/dist/promise/promise.mjs +4 -49
- package/dist/promise/promise.mjs.map +1 -1
- package/package.json +8 -7
|
@@ -39,190 +39,118 @@ import '../number/enum/uint8.mjs';
|
|
|
39
39
|
import '../number/num.mjs';
|
|
40
40
|
import '../number/refined-number-utils.mjs';
|
|
41
41
|
|
|
42
|
-
/**
|
|
43
|
-
* Class implementation for a queue with FIFO (First-In, First-Out) behavior
|
|
44
|
-
* using a circular buffer. This implementation provides O(1) enqueue and
|
|
45
|
-
* dequeue operations by using a fixed-size buffer with head and tail pointers
|
|
46
|
-
* that wrap around when they reach the buffer boundary.
|
|
47
|
-
*
|
|
48
|
-
* The circular buffer automatically resizes when it becomes full, ensuring that
|
|
49
|
-
* the queue can grow to accommodate any number of elements while maintaining
|
|
50
|
-
* efficient operations.
|
|
51
|
-
*
|
|
52
|
-
* @template T The type of elements in the queue.
|
|
53
|
-
* @implements Queue
|
|
54
|
-
*/
|
|
55
42
|
class QueueClass {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
43
|
+
/** @internal Initial capacity for new queues. */
|
|
44
|
+
static #INITIAL_CAPACITY = 8;
|
|
45
|
+
/** @internal Circular buffer to store queue elements. */
|
|
46
|
+
#mut_buffer;
|
|
47
|
+
/** @internal Index of the first element (front of queue). */
|
|
48
|
+
#mut_head;
|
|
49
|
+
/** @internal Index where the next element will be added (back of queue). */
|
|
50
|
+
#mut_tail;
|
|
51
|
+
/** @internal Current number of elements in the queue. */
|
|
52
|
+
#mut_size;
|
|
53
|
+
/** @internal Current capacity of the buffer. */
|
|
54
|
+
#mut_capacity;
|
|
55
|
+
/**
|
|
56
|
+
* Constructs a new QueueClass instance.
|
|
57
|
+
*
|
|
58
|
+
* @param initialValues Optional initial values to populate the queue.
|
|
59
|
+
*/
|
|
60
|
+
constructor(initialValues = []) {
|
|
61
|
+
const initialCapacity = asUint32(
|
|
62
|
+
Math.max(QueueClass.#INITIAL_CAPACITY, initialValues.length * 2)
|
|
63
|
+
);
|
|
64
|
+
this.#mut_buffer = Array.from(
|
|
65
|
+
{ length: initialCapacity },
|
|
66
|
+
() => void 0
|
|
67
|
+
);
|
|
68
|
+
this.#mut_head = 0;
|
|
69
|
+
this.#mut_tail = 0;
|
|
70
|
+
this.#mut_size = 0;
|
|
71
|
+
this.#mut_capacity = initialCapacity;
|
|
72
|
+
for (const value of initialValues) {
|
|
73
|
+
this.enqueue(value);
|
|
84
74
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
this.#mut_tail = this.#mut_size;
|
|
101
|
-
this.#mut_capacity = newCapacity;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @internal
|
|
78
|
+
* Resizes the circular buffer when it becomes full.
|
|
79
|
+
* Doubles the capacity and reorganizes elements to maintain queue order.
|
|
80
|
+
*/
|
|
81
|
+
#resize() {
|
|
82
|
+
const newCapacity = asUint32(this.#mut_capacity * 2);
|
|
83
|
+
const newBuffer = Array.from(
|
|
84
|
+
{ length: newCapacity },
|
|
85
|
+
() => void 0
|
|
86
|
+
);
|
|
87
|
+
for (const i of range(asSafeUint(this.#mut_size))) {
|
|
88
|
+
const sourceIndex = (this.#mut_head + i) % this.#mut_capacity;
|
|
89
|
+
newBuffer[i] = this.#mut_buffer[sourceIndex];
|
|
102
90
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
91
|
+
this.#mut_buffer = newBuffer;
|
|
92
|
+
this.#mut_head = 0;
|
|
93
|
+
this.#mut_tail = this.#mut_size;
|
|
94
|
+
this.#mut_capacity = newCapacity;
|
|
95
|
+
}
|
|
96
|
+
/** @inheritdoc */
|
|
97
|
+
get isEmpty() {
|
|
98
|
+
return this.#mut_size === 0;
|
|
99
|
+
}
|
|
100
|
+
/** @inheritdoc */
|
|
101
|
+
get size() {
|
|
102
|
+
return asUint32(this.#mut_size);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Removes and returns the element at the front of the queue (FIFO).
|
|
106
|
+
*
|
|
107
|
+
* This operation removes the element that was added earliest (first-in) and
|
|
108
|
+
* returns it. If the queue is empty, returns `Optional.none`. The operation
|
|
109
|
+
* is guaranteed to be O(1) and does not require any array shifting or
|
|
110
|
+
* copying.
|
|
111
|
+
*
|
|
112
|
+
* **Time Complexity:** O(1) - constant time operation **Space Complexity:**
|
|
113
|
+
* O(1) - no additional memory allocation
|
|
114
|
+
*
|
|
115
|
+
* @returns An Optional containing the removed element, or `Optional.none` if
|
|
116
|
+
* the queue is empty.
|
|
117
|
+
*/
|
|
118
|
+
dequeue() {
|
|
119
|
+
if (this.isEmpty) {
|
|
120
|
+
return none;
|
|
106
121
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.#mut_size -= 1;
|
|
134
|
-
return some(element);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Adds an element to the back of the queue (FIFO).
|
|
138
|
-
*
|
|
139
|
-
* This operation adds the element to the end of the queue, where it will be
|
|
140
|
-
* the last to be dequeued (first-in, first-out ordering). The operation is
|
|
141
|
-
* amortized O(1), meaning it's O(1) for most operations with occasional O(n)
|
|
142
|
-
* when the buffer needs resizing.
|
|
143
|
-
*
|
|
144
|
-
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
|
|
145
|
-
* needed **Space Complexity:** O(1) - constant additional memory per element
|
|
146
|
-
*
|
|
147
|
-
* **Buffer Resizing:** When the internal buffer becomes full, it
|
|
148
|
-
* automatically doubles in size and reorganizes elements to maintain the
|
|
149
|
-
* circular buffer structure.
|
|
150
|
-
*
|
|
151
|
-
* @param value The element to add to the back of the queue.
|
|
152
|
-
*/
|
|
153
|
-
enqueue(value) {
|
|
154
|
-
// Resize if buffer is full
|
|
155
|
-
if (this.#mut_size === this.#mut_capacity) {
|
|
156
|
-
this.#resize();
|
|
157
|
-
}
|
|
158
|
-
this.#mut_buffer[this.#mut_tail] = value;
|
|
159
|
-
this.#mut_tail = (this.#mut_tail + 1) % this.#mut_capacity;
|
|
160
|
-
this.#mut_size += 1;
|
|
122
|
+
const element = this.#mut_buffer[this.#mut_head];
|
|
123
|
+
this.#mut_buffer[this.#mut_head] = void 0;
|
|
124
|
+
this.#mut_head = (this.#mut_head + 1) % this.#mut_capacity;
|
|
125
|
+
this.#mut_size -= 1;
|
|
126
|
+
return some(element);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Adds an element to the back of the queue (FIFO).
|
|
130
|
+
*
|
|
131
|
+
* This operation adds the element to the end of the queue, where it will be
|
|
132
|
+
* the last to be dequeued (first-in, first-out ordering). The operation is
|
|
133
|
+
* amortized O(1), meaning it's O(1) for most operations with occasional O(n)
|
|
134
|
+
* when the buffer needs resizing.
|
|
135
|
+
*
|
|
136
|
+
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
|
|
137
|
+
* needed **Space Complexity:** O(1) - constant additional memory per element
|
|
138
|
+
*
|
|
139
|
+
* **Buffer Resizing:** When the internal buffer becomes full, it
|
|
140
|
+
* automatically doubles in size and reorganizes elements to maintain the
|
|
141
|
+
* circular buffer structure.
|
|
142
|
+
*
|
|
143
|
+
* @param value The element to add to the back of the queue.
|
|
144
|
+
*/
|
|
145
|
+
enqueue(value) {
|
|
146
|
+
if (this.#mut_size === this.#mut_capacity) {
|
|
147
|
+
this.#resize();
|
|
161
148
|
}
|
|
149
|
+
this.#mut_buffer[this.#mut_tail] = value;
|
|
150
|
+
this.#mut_tail = (this.#mut_tail + 1) % this.#mut_capacity;
|
|
151
|
+
this.#mut_size += 1;
|
|
152
|
+
}
|
|
162
153
|
}
|
|
163
|
-
/**
|
|
164
|
-
* Creates a new Queue instance with FIFO (First-In, First-Out) behavior using a
|
|
165
|
-
* high-performance circular buffer.
|
|
166
|
-
*
|
|
167
|
-
* This factory function creates an optimized queue implementation that
|
|
168
|
-
* maintains excellent performance characteristics for both enqueue and dequeue
|
|
169
|
-
* operations. The underlying circular buffer automatically resizes to
|
|
170
|
-
* accommodate growing workloads while providing predictable O(1) operations.
|
|
171
|
-
*
|
|
172
|
-
* **Implementation Features:**
|
|
173
|
-
*
|
|
174
|
-
* - **O(1) enqueue operations** (amortized - occasionally O(n) when resizing)
|
|
175
|
-
* - **O(1) dequeue operations** (always)
|
|
176
|
-
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
177
|
-
* - **Memory efficient** - garbage collects removed elements immediately
|
|
178
|
-
* - **Circular buffer design** - eliminates need for array shifting operations
|
|
179
|
-
*
|
|
180
|
-
* **Performance Benefits:**
|
|
181
|
-
*
|
|
182
|
-
* - No array copying during normal operations
|
|
183
|
-
* - Minimal memory allocation overhead
|
|
184
|
-
* - Predictable performance under high load
|
|
185
|
-
* - Efficient memory usage with automatic cleanup
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
*
|
|
189
|
-
* ```ts
|
|
190
|
-
* const queue = createQueue<number>();
|
|
191
|
-
*
|
|
192
|
-
* assert.isTrue(queue.isEmpty);
|
|
193
|
-
*
|
|
194
|
-
* assert.isTrue(queue.size === 0);
|
|
195
|
-
*
|
|
196
|
-
* queue.enqueue(1);
|
|
197
|
-
*
|
|
198
|
-
* queue.enqueue(2);
|
|
199
|
-
*
|
|
200
|
-
* assert.isFalse(queue.isEmpty);
|
|
201
|
-
*
|
|
202
|
-
* assert.isTrue(queue.size === 2);
|
|
203
|
-
*
|
|
204
|
-
* assert.deepStrictEqual(queue.dequeue(), Optional.some(1));
|
|
205
|
-
*
|
|
206
|
-
* assert.deepStrictEqual(queue.dequeue(), Optional.some(2));
|
|
207
|
-
*
|
|
208
|
-
* assert.deepStrictEqual(queue.dequeue(), Optional.none);
|
|
209
|
-
*
|
|
210
|
-
* const seededQueue = createQueue(['first', 'second']);
|
|
211
|
-
*
|
|
212
|
-
* assert.isTrue(seededQueue.size === 2);
|
|
213
|
-
*
|
|
214
|
-
* assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));
|
|
215
|
-
*
|
|
216
|
-
* assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));
|
|
217
|
-
* ```
|
|
218
|
-
*
|
|
219
|
-
* @template T The type of elements stored in the queue.
|
|
220
|
-
* @param initialValues Optional array of initial elements to populate the
|
|
221
|
-
* queue. Elements will be dequeued in the same order they appear in the
|
|
222
|
-
* array. If provided, the initial buffer capacity will be at least twice the
|
|
223
|
-
* array length.
|
|
224
|
-
* @returns A new Queue instance optimized for high-performance FIFO operations.
|
|
225
|
-
*/
|
|
226
154
|
const createQueue = (initialValues) => new QueueClass(initialValues);
|
|
227
155
|
|
|
228
156
|
export { createQueue };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.mjs","sources":["../../src/collections/queue.mts"],"sourcesContent":[null],"names":["Optional.none","Optional.some"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"queue.mjs","sources":["../../src/collections/queue.mts"],"sourcesContent":["import { Optional } from '../functional/index.mjs';\nimport { range } from '../iterator/index.mjs';\nimport { asSafeUint, asUint32 } from '../number/index.mjs';\nimport { type SizeType } from '../types.mjs';\n\n/**\n * Interface for a high-performance queue with FIFO (First-In, First-Out)\n * behavior.\n *\n * This interface defines a mutable queue data structure where elements are\n * added to the back and removed from the front, maintaining the order in which\n * they were added. The implementation uses a circular buffer for optimal\n * performance, providing O(1) operations for both enqueue and dequeue\n * operations.\n *\n * **FIFO Behavior:**\n *\n * - **enqueue**: Adds elements to the back of the queue\n * - **dequeue**: Removes and returns elements from the front of the queue\n * - Elements are processed in the exact order they were added\n *\n * **Performance Characteristics:**\n *\n * - Enqueue: O(1) amortized (O(n) when buffer needs resizing)\n * - Dequeue: O(1) always\n * - Size/isEmpty: O(1) always\n * - Memory efficient with automatic garbage collection of removed elements\n *\n * **Use Cases:**\n *\n * - Task scheduling and job queues\n * - Breadth-first search algorithms\n * - Event processing systems\n * - Producer-consumer patterns\n * - Buffer management for streaming data\n *\n * @template T The type of elements stored in the queue.\n */\nexport type Queue<T> = Readonly<{\n /**\n * Checks if the queue is empty.\n *\n * @example\n *\n * ```ts\n * const queue = createQueue<number>();\n *\n * assert.isTrue(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 0);\n *\n * queue.enqueue(1);\n *\n * queue.enqueue(2);\n *\n * assert.isFalse(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 2);\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(1));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(2));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.none);\n *\n * const seededQueue = createQueue(['first', 'second']);\n *\n * assert.isTrue(seededQueue.size === 2);\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));\n * ```\n */\n isEmpty: boolean;\n\n /**\n * The number of elements in the queue.\n *\n * @example\n *\n * ```ts\n * const queue = createQueue<number>();\n *\n * assert.isTrue(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 0);\n *\n * queue.enqueue(1);\n *\n * queue.enqueue(2);\n *\n * assert.isFalse(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 2);\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(1));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(2));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.none);\n *\n * const seededQueue = createQueue(['first', 'second']);\n *\n * assert.isTrue(seededQueue.size === 2);\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));\n * ```\n */\n size: SizeType.Arr;\n\n /**\n * Removes and returns the element at the front of the queue.\n *\n * @example\n *\n * ```ts\n * const queue = createQueue<number>();\n *\n * assert.isTrue(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 0);\n *\n * queue.enqueue(1);\n *\n * queue.enqueue(2);\n *\n * assert.isFalse(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 2);\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(1));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(2));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.none);\n *\n * const seededQueue = createQueue(['first', 'second']);\n *\n * assert.isTrue(seededQueue.size === 2);\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));\n * ```\n *\n * @returns The element at the front of the queue, or `Optional.none` if the\n * queue is empty.\n */\n dequeue: () => Optional<T>;\n\n /**\n * Adds an element to the back of the queue.\n *\n * @example\n *\n * ```ts\n * const queue = createQueue<number>();\n *\n * assert.isTrue(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 0);\n *\n * queue.enqueue(1);\n *\n * queue.enqueue(2);\n *\n * assert.isFalse(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 2);\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(1));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(2));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.none);\n *\n * const seededQueue = createQueue(['first', 'second']);\n *\n * assert.isTrue(seededQueue.size === 2);\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));\n * ```\n *\n * @param value The element to add.\n */\n enqueue: (value: T) => void;\n}>;\n\n/**\n * Class implementation for a queue with FIFO (First-In, First-Out) behavior\n * using a circular buffer. This implementation provides O(1) enqueue and\n * dequeue operations by using a fixed-size buffer with head and tail pointers\n * that wrap around when they reach the buffer boundary.\n *\n * The circular buffer automatically resizes when it becomes full, ensuring that\n * the queue can grow to accommodate any number of elements while maintaining\n * efficient operations.\n *\n * @template T The type of elements in the queue.\n * @implements Queue\n */\nclass QueueClass<T> implements Queue<T> {\n /** @internal Initial capacity for new queues. */\n static readonly #INITIAL_CAPACITY = 8;\n\n /** @internal Circular buffer to store queue elements. */\n #mut_buffer: (T | undefined)[];\n\n /** @internal Index of the first element (front of queue). */\n #mut_head: number;\n\n /** @internal Index where the next element will be added (back of queue). */\n #mut_tail: number;\n\n /** @internal Current number of elements in the queue. */\n #mut_size: number;\n\n /** @internal Current capacity of the buffer. */\n #mut_capacity: number;\n\n /**\n * Constructs a new QueueClass instance.\n *\n * @param initialValues Optional initial values to populate the queue.\n */\n constructor(initialValues: readonly T[] = []) {\n const initialCapacity = asUint32(\n Math.max(QueueClass.#INITIAL_CAPACITY, initialValues.length * 2),\n );\n\n this.#mut_buffer = Array.from<unknown, T | undefined>(\n { length: initialCapacity },\n () => undefined,\n );\n\n this.#mut_head = 0;\n\n this.#mut_tail = 0;\n\n this.#mut_size = 0;\n\n this.#mut_capacity = initialCapacity;\n\n // Add initial values\n for (const value of initialValues) {\n this.enqueue(value);\n }\n }\n\n /**\n * @internal\n * Resizes the circular buffer when it becomes full.\n * Doubles the capacity and reorganizes elements to maintain queue order.\n */\n #resize(): void {\n const newCapacity = asUint32(this.#mut_capacity * 2);\n\n const newBuffer = Array.from<unknown, T | undefined>(\n { length: newCapacity },\n () => undefined,\n );\n\n // Copy elements in order from head to tail\n for (const i of range(asSafeUint(this.#mut_size))) {\n const sourceIndex = (this.#mut_head + i) % this.#mut_capacity;\n\n newBuffer[i] = this.#mut_buffer[sourceIndex];\n }\n\n this.#mut_buffer = newBuffer;\n\n this.#mut_head = 0;\n\n this.#mut_tail = this.#mut_size;\n\n this.#mut_capacity = newCapacity;\n }\n\n /** @inheritdoc */\n get isEmpty(): boolean {\n return this.#mut_size === 0;\n }\n\n /** @inheritdoc */\n get size(): SizeType.Arr {\n return asUint32(this.#mut_size);\n }\n\n /**\n * Removes and returns the element at the front of the queue (FIFO).\n *\n * This operation removes the element that was added earliest (first-in) and\n * returns it. If the queue is empty, returns `Optional.none`. The operation\n * is guaranteed to be O(1) and does not require any array shifting or\n * copying.\n *\n * **Time Complexity:** O(1) - constant time operation **Space Complexity:**\n * O(1) - no additional memory allocation\n *\n * @returns An Optional containing the removed element, or `Optional.none` if\n * the queue is empty.\n */\n dequeue(): Optional<T> {\n if (this.isEmpty) {\n return Optional.none;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const element = this.#mut_buffer[this.#mut_head]!;\n\n this.#mut_buffer[this.#mut_head] = undefined; // Clear reference for garbage collection\n\n this.#mut_head = (this.#mut_head + 1) % this.#mut_capacity;\n\n this.#mut_size -= 1;\n\n return Optional.some(element);\n }\n\n /**\n * Adds an element to the back of the queue (FIFO).\n *\n * This operation adds the element to the end of the queue, where it will be\n * the last to be dequeued (first-in, first-out ordering). The operation is\n * amortized O(1), meaning it's O(1) for most operations with occasional O(n)\n * when the buffer needs resizing.\n *\n * **Time Complexity:** O(1) amortized - O(n) only when buffer resize is\n * needed **Space Complexity:** O(1) - constant additional memory per element\n *\n * **Buffer Resizing:** When the internal buffer becomes full, it\n * automatically doubles in size and reorganizes elements to maintain the\n * circular buffer structure.\n *\n * @param value The element to add to the back of the queue.\n */\n enqueue(value: T): void {\n // Resize if buffer is full\n if (this.#mut_size === this.#mut_capacity) {\n this.#resize();\n }\n\n this.#mut_buffer[this.#mut_tail] = value;\n\n this.#mut_tail = (this.#mut_tail + 1) % this.#mut_capacity;\n\n this.#mut_size += 1;\n }\n}\n\n/**\n * Creates a new Queue instance with FIFO (First-In, First-Out) behavior using a\n * high-performance circular buffer.\n *\n * This factory function creates an optimized queue implementation that\n * maintains excellent performance characteristics for both enqueue and dequeue\n * operations. The underlying circular buffer automatically resizes to\n * accommodate growing workloads while providing predictable O(1) operations.\n *\n * **Implementation Features:**\n *\n * - **O(1) enqueue operations** (amortized - occasionally O(n) when resizing)\n * - **O(1) dequeue operations** (always)\n * - **Automatic buffer resizing** - starts at 8 elements, doubles when full\n * - **Memory efficient** - garbage collects removed elements immediately\n * - **Circular buffer design** - eliminates need for array shifting operations\n *\n * **Performance Benefits:**\n *\n * - No array copying during normal operations\n * - Minimal memory allocation overhead\n * - Predictable performance under high load\n * - Efficient memory usage with automatic cleanup\n *\n * @example\n *\n * ```ts\n * const queue = createQueue<number>();\n *\n * assert.isTrue(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 0);\n *\n * queue.enqueue(1);\n *\n * queue.enqueue(2);\n *\n * assert.isFalse(queue.isEmpty);\n *\n * assert.isTrue(queue.size === 2);\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(1));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.some(2));\n *\n * assert.deepStrictEqual(queue.dequeue(), Optional.none);\n *\n * const seededQueue = createQueue(['first', 'second']);\n *\n * assert.isTrue(seededQueue.size === 2);\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));\n *\n * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));\n * ```\n *\n * @template T The type of elements stored in the queue.\n * @param initialValues Optional array of initial elements to populate the\n * queue. Elements will be dequeued in the same order they appear in the\n * array. If provided, the initial buffer capacity will be at least twice the\n * array length.\n * @returns A new Queue instance optimized for high-performance FIFO operations.\n */\nexport const createQueue = <T,>(initialValues?: readonly T[]): Queue<T> =>\n new QueueClass<T>(initialValues);\n"],"names":["Optional.none","Optional.some"],"mappings":"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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stack.d.mts","sourceRoot":"","sources":["../../src/collections/stack.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAGnD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,QAAQ,CAAC;IAC9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC;IAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,GAAG,EAAE,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC1B,CAAC,CAAC;AA+IH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"stack.d.mts","sourceRoot":"","sources":["../../src/collections/stack.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAGnD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,QAAQ,CAAC;IAC9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC;IAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,GAAG,EAAE,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC1B,CAAC,CAAC;AA+IH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,kBAAmB,SAAS,CAAC,EAAE,KAAG,KAAK,CAAC,CAAC,CACpC,CAAC"}
|
|
@@ -39,177 +39,107 @@ import '../number/enum/uint8.mjs';
|
|
|
39
39
|
import '../number/num.mjs';
|
|
40
40
|
import '../number/refined-number-utils.mjs';
|
|
41
41
|
|
|
42
|
-
/**
|
|
43
|
-
* Class implementation for a stack with LIFO (Last-In, First-Out) behavior
|
|
44
|
-
* using a dynamic array. This implementation provides O(1) amortized push and
|
|
45
|
-
* O(1) pop operations by using a resizable buffer that grows as needed.
|
|
46
|
-
*
|
|
47
|
-
* The underlying array automatically resizes when it becomes full, ensuring
|
|
48
|
-
* that the stack can grow to accommodate any number of elements while
|
|
49
|
-
* maintaining efficient operations.
|
|
50
|
-
*
|
|
51
|
-
* @template T The type of elements in the stack.
|
|
52
|
-
* @implements Stack
|
|
53
|
-
*/
|
|
54
42
|
class StackClass {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
43
|
+
/** @internal Initial capacity for new stacks. */
|
|
44
|
+
static #INITIAL_CAPACITY = 8;
|
|
45
|
+
/** @internal Dynamic array to store stack elements. */
|
|
46
|
+
#mut_buffer;
|
|
47
|
+
/** @internal Current number of elements in the stack. */
|
|
48
|
+
#mut_size;
|
|
49
|
+
/** @internal Current capacity of the buffer. */
|
|
50
|
+
#mut_capacity;
|
|
51
|
+
/**
|
|
52
|
+
* Constructs a new StackClass instance.
|
|
53
|
+
*
|
|
54
|
+
* @param initialValues Optional initial values to populate the stack.
|
|
55
|
+
*/
|
|
56
|
+
constructor(initialValues = []) {
|
|
57
|
+
const initialCapacity = asUint32(
|
|
58
|
+
Math.max(StackClass.#INITIAL_CAPACITY, initialValues.length * 2)
|
|
59
|
+
);
|
|
60
|
+
this.#mut_buffer = Array.from(
|
|
61
|
+
{ length: initialCapacity },
|
|
62
|
+
() => void 0
|
|
63
|
+
);
|
|
64
|
+
this.#mut_size = asUint32(0);
|
|
65
|
+
this.#mut_capacity = initialCapacity;
|
|
66
|
+
for (const value of initialValues) {
|
|
67
|
+
this.push(value);
|
|
77
68
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* @internal
|
|
72
|
+
* Resizes the buffer when it becomes full.
|
|
73
|
+
* Doubles the capacity while preserving all elements.
|
|
74
|
+
*/
|
|
75
|
+
#resize() {
|
|
76
|
+
const newCapacity = asUint32(this.#mut_capacity * 2);
|
|
77
|
+
const newBuffer = Array.from(
|
|
78
|
+
{ length: newCapacity },
|
|
79
|
+
() => void 0
|
|
80
|
+
);
|
|
81
|
+
for (const i of range(this.#mut_size)) {
|
|
82
|
+
newBuffer[i] = this.#mut_buffer[i];
|
|
92
83
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
this.#mut_buffer = newBuffer;
|
|
85
|
+
this.#mut_capacity = newCapacity;
|
|
86
|
+
}
|
|
87
|
+
/** @inheritdoc */
|
|
88
|
+
get isEmpty() {
|
|
89
|
+
return this.#mut_size === 0;
|
|
90
|
+
}
|
|
91
|
+
/** @inheritdoc */
|
|
92
|
+
get size() {
|
|
93
|
+
return asUint32(this.#mut_size);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Removes and returns the element at the top of the stack (LIFO).
|
|
97
|
+
*
|
|
98
|
+
* This operation removes the element that was added most recently (last-in)
|
|
99
|
+
* and returns it. If the stack is empty, returns `Optional.none`. The
|
|
100
|
+
* operation is guaranteed to be O(1) and does not require any array resizing
|
|
101
|
+
* or memory reallocation.
|
|
102
|
+
*
|
|
103
|
+
* **Time Complexity:** O(1) - constant time operation **Space Complexity:**
|
|
104
|
+
* O(1) - no additional memory allocation
|
|
105
|
+
*
|
|
106
|
+
* @returns An Optional containing the removed element, or `Optional.none` if
|
|
107
|
+
* the stack is empty.
|
|
108
|
+
*/
|
|
109
|
+
pop() {
|
|
110
|
+
if (this.isEmpty) {
|
|
111
|
+
return none;
|
|
96
112
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this.#mut_buffer[this.#mut_size] = undefined; // Clear reference for garbage collection
|
|
123
|
-
return some(element);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Adds an element to the top of the stack (LIFO).
|
|
127
|
-
*
|
|
128
|
-
* This operation adds the element to the top of the stack, where it will be
|
|
129
|
-
* the first to be popped (last-in, first-out ordering). The operation is
|
|
130
|
-
* amortized O(1), meaning it's O(1) for most operations with occasional O(n)
|
|
131
|
-
* when the buffer needs resizing.
|
|
132
|
-
*
|
|
133
|
-
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
|
|
134
|
-
* needed **Space Complexity:** O(1) - constant additional memory per element
|
|
135
|
-
*
|
|
136
|
-
* **Buffer Resizing:** When the internal buffer becomes full, it
|
|
137
|
-
* automatically doubles in size and copies existing elements to maintain the
|
|
138
|
-
* stack structure.
|
|
139
|
-
*
|
|
140
|
-
* @param value The element to add to the top of the stack.
|
|
141
|
-
*/
|
|
142
|
-
push(value) {
|
|
143
|
-
// Resize if buffer is full
|
|
144
|
-
if (this.#mut_size === this.#mut_capacity) {
|
|
145
|
-
this.#resize();
|
|
146
|
-
}
|
|
147
|
-
this.#mut_buffer[this.#mut_size] = value;
|
|
148
|
-
this.#mut_size = Uint32.add(this.#mut_size, 1);
|
|
113
|
+
this.#mut_size = Uint32.sub(this.#mut_size, 1);
|
|
114
|
+
const element = this.#mut_buffer[this.#mut_size];
|
|
115
|
+
this.#mut_buffer[this.#mut_size] = void 0;
|
|
116
|
+
return some(element);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Adds an element to the top of the stack (LIFO).
|
|
120
|
+
*
|
|
121
|
+
* This operation adds the element to the top of the stack, where it will be
|
|
122
|
+
* the first to be popped (last-in, first-out ordering). The operation is
|
|
123
|
+
* amortized O(1), meaning it's O(1) for most operations with occasional O(n)
|
|
124
|
+
* when the buffer needs resizing.
|
|
125
|
+
*
|
|
126
|
+
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
|
|
127
|
+
* needed **Space Complexity:** O(1) - constant additional memory per element
|
|
128
|
+
*
|
|
129
|
+
* **Buffer Resizing:** When the internal buffer becomes full, it
|
|
130
|
+
* automatically doubles in size and copies existing elements to maintain the
|
|
131
|
+
* stack structure.
|
|
132
|
+
*
|
|
133
|
+
* @param value The element to add to the top of the stack.
|
|
134
|
+
*/
|
|
135
|
+
push(value) {
|
|
136
|
+
if (this.#mut_size === this.#mut_capacity) {
|
|
137
|
+
this.#resize();
|
|
149
138
|
}
|
|
139
|
+
this.#mut_buffer[this.#mut_size] = value;
|
|
140
|
+
this.#mut_size = Uint32.add(this.#mut_size, 1);
|
|
141
|
+
}
|
|
150
142
|
}
|
|
151
|
-
/**
|
|
152
|
-
* Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a
|
|
153
|
-
* high-performance dynamic array.
|
|
154
|
-
*
|
|
155
|
-
* This factory function creates an optimized stack implementation that
|
|
156
|
-
* maintains excellent performance characteristics for both push and pop
|
|
157
|
-
* operations. The underlying dynamic array automatically resizes to accommodate
|
|
158
|
-
* growing workloads while providing predictable O(1) operations.
|
|
159
|
-
*
|
|
160
|
-
* **Implementation Features:**
|
|
161
|
-
*
|
|
162
|
-
* - **O(1) push operations** (amortized - occasionally O(n) when resizing)
|
|
163
|
-
* - **O(1) pop operations** (always)
|
|
164
|
-
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
165
|
-
* - **Memory efficient** - garbage collects removed elements immediately
|
|
166
|
-
* - **Dynamic array design** - eliminates need for complex memory management
|
|
167
|
-
*
|
|
168
|
-
* **Performance Benefits:**
|
|
169
|
-
*
|
|
170
|
-
* - No array shifting required for stack operations
|
|
171
|
-
* - Minimal memory allocation overhead
|
|
172
|
-
* - Predictable performance under high load
|
|
173
|
-
* - Efficient memory usage with automatic cleanup
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
*
|
|
177
|
-
* ```ts
|
|
178
|
-
* const stack = createStack<string>();
|
|
179
|
-
*
|
|
180
|
-
* assert.isTrue(stack.isEmpty);
|
|
181
|
-
*
|
|
182
|
-
* assert.isTrue(stack.size === 0);
|
|
183
|
-
*
|
|
184
|
-
* stack.push('first');
|
|
185
|
-
*
|
|
186
|
-
* // eslint-disable-next-line unicorn/prefer-single-call
|
|
187
|
-
* stack.push('second');
|
|
188
|
-
*
|
|
189
|
-
* assert.isFalse(stack.isEmpty);
|
|
190
|
-
*
|
|
191
|
-
* assert.isTrue(stack.size === 2);
|
|
192
|
-
*
|
|
193
|
-
* assert.deepStrictEqual(stack.pop(), Optional.some('second'));
|
|
194
|
-
*
|
|
195
|
-
* assert.deepStrictEqual(stack.pop(), Optional.some('first'));
|
|
196
|
-
*
|
|
197
|
-
* assert.deepStrictEqual(stack.pop(), Optional.none);
|
|
198
|
-
*
|
|
199
|
-
* const seededStack = createStack([10, 20, 30]);
|
|
200
|
-
*
|
|
201
|
-
* assert.isTrue(seededStack.size === 3);
|
|
202
|
-
*
|
|
203
|
-
* assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
|
|
204
|
-
* ```
|
|
205
|
-
*
|
|
206
|
-
* @template T The type of elements stored in the stack.
|
|
207
|
-
* @param initialValues Optional array of initial elements to populate the
|
|
208
|
-
* stack. Elements will be popped in reverse order of how they appear in the
|
|
209
|
-
* array (last array element will be popped first). If provided, the initial
|
|
210
|
-
* buffer capacity will be at least twice the array length.
|
|
211
|
-
* @returns A new Stack instance optimized for high-performance LIFO operations.
|
|
212
|
-
*/
|
|
213
143
|
const createStack = (initialValues) => new StackClass(initialValues);
|
|
214
144
|
|
|
215
145
|
export { createStack };
|