ts-data-forge 14.6.2 → 14.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/dist/array/impl/array-utils-creation.mjs +103 -103
  2. package/dist/array/impl/array-utils-element-access.mjs +46 -46
  3. package/dist/array/impl/array-utils-iterators.mjs +54 -54
  4. package/dist/array/impl/array-utils-length-bounded-array-cast.mjs +49 -49
  5. package/dist/array/impl/array-utils-length-bounded-array-guard.mjs +40 -40
  6. package/dist/array/impl/array-utils-length-bounded-tuple-cast.mjs +42 -42
  7. package/dist/array/impl/array-utils-length-bounded-tuple-guard.mjs +46 -46
  8. package/dist/array/impl/array-utils-modification.mjs +4 -4
  9. package/dist/array/impl/array-utils-reducing-value.mjs +11 -11
  10. package/dist/array/impl/array-utils-search.mjs +6 -6
  11. package/dist/array/impl/array-utils-set-op.mjs +116 -116
  12. package/dist/array/impl/array-utils-size.mjs +6 -6
  13. package/dist/array/impl/array-utils-slice-clamped.mjs +1 -1
  14. package/dist/array/impl/array-utils-slicing.mjs +56 -56
  15. package/dist/array/impl/array-utils-transformation.mjs +156 -156
  16. package/dist/array/impl/array-utils-validation.mjs +36 -36
  17. package/dist/collections/imap-mapped.mjs +195 -195
  18. package/dist/collections/imap.mjs +143 -143
  19. package/dist/collections/iset-mapped.mjs +320 -320
  20. package/dist/collections/iset.mjs +207 -207
  21. package/dist/collections/queue.mjs +130 -130
  22. package/dist/collections/stack.mjs +125 -125
  23. package/dist/expect-type.mjs +68 -68
  24. package/dist/functional/async-result/impl/async-result-flat-map.mjs +3 -3
  25. package/dist/functional/async-result/impl/async-result-map-err.mjs +2 -2
  26. package/dist/functional/async-result/impl/async-result-map.mjs +2 -2
  27. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +31 -31
  28. package/dist/functional/optional/impl/optional-expect-to-be.mjs +1 -1
  29. package/dist/functional/optional/impl/optional-filter.mjs +1 -1
  30. package/dist/functional/optional/impl/optional-flat-map.mjs +1 -1
  31. package/dist/functional/optional/impl/optional-from-nullable.mjs +24 -24
  32. package/dist/functional/optional/impl/optional-is-none.mjs +19 -19
  33. package/dist/functional/optional/impl/optional-is-optional.mjs +18 -18
  34. package/dist/functional/optional/impl/optional-is-some.mjs +20 -20
  35. package/dist/functional/optional/impl/optional-map.mjs +2 -2
  36. package/dist/functional/optional/impl/optional-match.mjs +2 -2
  37. package/dist/functional/optional/impl/optional-none.mjs +16 -16
  38. package/dist/functional/optional/impl/optional-or-else.mjs +1 -1
  39. package/dist/functional/optional/impl/optional-some.mjs +19 -19
  40. package/dist/functional/optional/impl/optional-to-nullable.mjs +26 -26
  41. package/dist/functional/optional/impl/optional-unwrap-or.mjs +1 -1
  42. package/dist/functional/optional/impl/optional-unwrap-throw.mjs +28 -28
  43. package/dist/functional/optional/impl/optional-unwrap.mjs +1 -1
  44. package/dist/functional/optional/impl/optional-zip.mjs +32 -32
  45. package/dist/functional/optional/impl/tag.mjs +1 -1
  46. package/dist/functional/pipe.mjs +19 -19
  47. package/dist/functional/result/impl/result-err.mjs +25 -25
  48. package/dist/functional/result/impl/result-expect-to-be.mjs +2 -2
  49. package/dist/functional/result/impl/result-flat-map.mjs +4 -4
  50. package/dist/functional/result/impl/result-fold.mjs +4 -4
  51. package/dist/functional/result/impl/result-from-optional.mjs +2 -2
  52. package/dist/functional/result/impl/result-from-promise.mjs +34 -34
  53. package/dist/functional/result/impl/result-from-throwable.mjs +29 -29
  54. package/dist/functional/result/impl/result-is-err.mjs +19 -19
  55. package/dist/functional/result/impl/result-is-ok.mjs +19 -19
  56. package/dist/functional/result/impl/result-is-result.mjs +19 -19
  57. package/dist/functional/result/impl/result-map-err.mjs +4 -4
  58. package/dist/functional/result/impl/result-map.mjs +4 -4
  59. package/dist/functional/result/impl/result-match.mjs +3 -3
  60. package/dist/functional/result/impl/result-ok.mjs +25 -25
  61. package/dist/functional/result/impl/result-or-else.mjs +2 -2
  62. package/dist/functional/result/impl/result-safe-try.mjs +15 -15
  63. package/dist/functional/result/impl/result-safe-unwrap.mjs +33 -33
  64. package/dist/functional/result/impl/result-swap.mjs +20 -20
  65. package/dist/functional/result/impl/result-to-optional.mjs +27 -27
  66. package/dist/functional/result/impl/result-unwrap-err-or.mjs +2 -2
  67. package/dist/functional/result/impl/result-unwrap-err-throw.mjs +31 -31
  68. package/dist/functional/result/impl/result-unwrap-err.mjs +30 -30
  69. package/dist/functional/result/impl/result-unwrap-ok-or.mjs +2 -2
  70. package/dist/functional/result/impl/result-unwrap-ok.mjs +1 -1
  71. package/dist/functional/result/impl/result-unwrap-throw.mjs +31 -31
  72. package/dist/functional/result/impl/result-zip.mjs +29 -29
  73. package/dist/functional/result/impl/tag.mjs +1 -1
  74. package/dist/functional/ternary-result/impl/tag.mjs +2 -2
  75. package/dist/functional/ternary-result/impl/ternary-result-err.mjs +13 -13
  76. package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs +4 -4
  77. package/dist/functional/ternary-result/impl/ternary-result-fold.mjs +4 -4
  78. package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +18 -18
  79. package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +17 -17
  80. package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs +17 -17
  81. package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +19 -19
  82. package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +19 -19
  83. package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +18 -18
  84. package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs +2 -2
  85. package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs +3 -3
  86. package/dist/functional/ternary-result/impl/ternary-result-map.mjs +4 -4
  87. package/dist/functional/ternary-result/impl/ternary-result-ok.mjs +14 -14
  88. package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +19 -19
  89. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +19 -19
  90. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +17 -17
  91. package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs +1 -1
  92. package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +20 -20
  93. package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +18 -18
  94. package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +17 -17
  95. package/dist/functional/ternary-result/impl/ternary-result-warn.mjs +16 -16
  96. package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +31 -31
  97. package/dist/functional/ternary-result/impl/variant-name.mjs +1 -1
  98. package/dist/guard/has-key.mjs +41 -41
  99. package/dist/guard/is-non-empty-string.mjs +37 -37
  100. package/dist/guard/is-non-null-object.mjs +34 -34
  101. package/dist/guard/is-primitive.mjs +31 -31
  102. package/dist/guard/is-record.mjs +86 -86
  103. package/dist/guard/is-type.mjs +387 -387
  104. package/dist/guard/key-is-in.mjs +40 -40
  105. package/dist/iterator/range.mjs +1 -1
  106. package/dist/json/json.mjs +289 -289
  107. package/dist/number/branded-types/finite-number.mjs +116 -116
  108. package/dist/number/branded-types/int.mjs +223 -223
  109. package/dist/number/branded-types/int16.mjs +116 -116
  110. package/dist/number/branded-types/int32.mjs +116 -116
  111. package/dist/number/branded-types/negative-finite-number.mjs +126 -126
  112. package/dist/number/branded-types/negative-int.mjs +104 -104
  113. package/dist/number/branded-types/negative-safe-int.mjs +104 -104
  114. package/dist/number/branded-types/non-negative-finite-number.mjs +132 -132
  115. package/dist/number/branded-types/non-negative-int16.mjs +113 -113
  116. package/dist/number/branded-types/non-negative-int32.mjs +113 -113
  117. package/dist/number/branded-types/non-positive-finite-number.mjs +116 -116
  118. package/dist/number/branded-types/non-positive-int.mjs +97 -97
  119. package/dist/number/branded-types/non-positive-safe-int.mjs +98 -98
  120. package/dist/number/branded-types/non-zero-finite-number.mjs +112 -112
  121. package/dist/number/branded-types/non-zero-int.mjs +82 -82
  122. package/dist/number/branded-types/non-zero-int16.mjs +95 -95
  123. package/dist/number/branded-types/non-zero-int32.mjs +95 -95
  124. package/dist/number/branded-types/non-zero-safe-int.mjs +99 -99
  125. package/dist/number/branded-types/non-zero-uint16.mjs +95 -95
  126. package/dist/number/branded-types/non-zero-uint32.mjs +95 -95
  127. package/dist/number/branded-types/positive-finite-number.mjs +134 -134
  128. package/dist/number/branded-types/positive-int.mjs +237 -237
  129. package/dist/number/branded-types/positive-int16.mjs +110 -110
  130. package/dist/number/branded-types/positive-int32.mjs +110 -110
  131. package/dist/number/branded-types/positive-safe-int.mjs +268 -268
  132. package/dist/number/branded-types/positive-uint16.mjs +110 -110
  133. package/dist/number/branded-types/positive-uint32.mjs +110 -110
  134. package/dist/number/branded-types/safe-int.mjs +259 -259
  135. package/dist/number/branded-types/safe-uint.mjs +111 -111
  136. package/dist/number/branded-types/uint.mjs +225 -225
  137. package/dist/number/branded-types/uint16.mjs +108 -108
  138. package/dist/number/branded-types/uint32.mjs +108 -108
  139. package/dist/number/enum/int8.mjs +118 -118
  140. package/dist/number/enum/uint8.mjs +109 -109
  141. package/dist/number/num-arithmetic-types.mjs +1 -1
  142. package/dist/number/num.mjs +478 -478
  143. package/dist/number/refined-number-utils.mjs +170 -170
  144. package/dist/object/object.mjs +234 -234
  145. package/dist/others/cast-mutable.mjs +33 -33
  146. package/dist/others/cast-readonly.mjs +32 -32
  147. package/dist/others/debounce-function.mjs +13 -13
  148. package/dist/others/fast-deep-equal.mjs +14 -14
  149. package/dist/others/if-then.mjs +29 -29
  150. package/dist/others/tuple.mjs +50 -50
  151. package/dist/others/unknown-to-string.mjs +38 -38
  152. package/dist/promise/promise.mjs +47 -47
  153. package/dist/string/str.mjs +177 -177
  154. package/dist/types.mjs +1 -1
  155. package/package.json +6 -6
  156. package/src/guard/is-type.test.mts +2 -2
  157. package/src/number/branded-types/finite-number.test.mts +1 -1
  158. package/src/number/branded-types/int.test.mts +1 -1
  159. package/src/number/branded-types/int16.test.mts +1 -1
  160. package/src/number/branded-types/int32.test.mts +1 -1
  161. package/src/number/branded-types/non-negative-finite-number.test.mts +1 -1
  162. package/src/number/branded-types/non-negative-int16.test.mts +1 -1
  163. package/src/number/branded-types/non-negative-int32.test.mts +1 -1
  164. package/src/number/branded-types/non-zero-finite-number.test.mts +1 -1
  165. package/src/number/branded-types/non-zero-int.test.mts +1 -1
  166. package/src/number/branded-types/non-zero-int16.test.mts +1 -1
  167. package/src/number/branded-types/non-zero-int32.test.mts +1 -1
  168. package/src/number/branded-types/non-zero-safe-int.test.mts +1 -1
  169. package/src/number/branded-types/non-zero-uint16.test.mts +1 -1
  170. package/src/number/branded-types/non-zero-uint32.test.mts +1 -1
  171. package/src/number/branded-types/positive-finite-number.test.mts +1 -1
  172. package/src/number/branded-types/positive-int.test.mts +1 -1
  173. package/src/number/branded-types/positive-int16.test.mts +1 -1
  174. package/src/number/branded-types/positive-int32.test.mts +1 -1
  175. package/src/number/branded-types/positive-safe-int.test.mts +1 -1
  176. package/src/number/branded-types/positive-uint16.test.mts +1 -1
  177. package/src/number/branded-types/positive-uint32.test.mts +1 -1
  178. package/src/number/branded-types/safe-int.test.mts +1 -1
  179. package/src/number/branded-types/safe-uint.test.mts +1 -1
  180. package/src/number/branded-types/uint.test.mts +1 -1
  181. package/src/number/branded-types/uint16.test.mts +1 -1
  182. package/src/number/branded-types/uint32.test.mts +1 -1
@@ -1,37 +1,37 @@
1
1
  import { Optional } from '../functional/index.mjs';
2
2
  import { range } from '../iterator/index.mjs';
3
3
  import { asSafeUint, asUint32 } from '../number/index.mjs';
4
- /**
5
- * Class implementation for a queue with FIFO (First-In, First-Out) behavior
6
- * using a circular buffer. This implementation provides O(1) enqueue and
7
- * dequeue operations by using a fixed-size buffer with head and tail pointers
8
- * that wrap around when they reach the buffer boundary.
9
- *
10
- * The circular buffer automatically resizes when it becomes full, ensuring that
11
- * the queue can grow to accommodate any number of elements while maintaining
12
- * efficient operations.
13
- *
14
- * @template T The type of elements in the queue.
15
- * @implements Queue
16
- */
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
17
  class QueueClass {
18
- /** @internal Initial capacity for new queues. */
18
+
19
19
  static #INITIAL_CAPACITY = 8;
20
- /** @internal Circular buffer to store queue elements. */
20
+
21
21
  #mut_buffer;
22
- /** @internal Index of the first element (front of queue). */
22
+
23
23
  #mut_head;
24
- /** @internal Index where the next element will be added (back of queue). */
24
+
25
25
  #mut_tail;
26
- /** @internal Current number of elements in the queue. */
26
+
27
27
  #mut_size;
28
- /** @internal Current capacity of the buffer. */
28
+
29
29
  #mut_capacity;
30
- /**
31
- * Constructs a new QueueClass instance.
32
- *
33
- * @param initialValues Optional initial values to populate the queue.
34
- */
30
+
31
+
32
+
33
+
34
+
35
35
  constructor(initialValues = []) {
36
36
  const initialCapacity = asUint32(Math.max(QueueClass.#INITIAL_CAPACITY, initialValues.length * 2));
37
37
  this.#mut_buffer = Array.from({ length: initialCapacity }, () => undefined);
@@ -39,20 +39,20 @@ class QueueClass {
39
39
  this.#mut_tail = 0;
40
40
  this.#mut_size = 0;
41
41
  this.#mut_capacity = initialCapacity;
42
- // Add initial values
42
+
43
43
  for (const value of initialValues) {
44
44
  this.enqueue(value);
45
45
  }
46
46
  }
47
- /**
48
- * @internal
49
- * Resizes the circular buffer when it becomes full.
50
- * Doubles the capacity and reorganizes elements to maintain queue order.
51
- */
47
+
48
+
49
+
50
+
51
+
52
52
  #resize() {
53
53
  const newCapacity = asUint32(this.#mut_capacity * 2);
54
54
  const newBuffer = Array.from({ length: newCapacity }, () => undefined);
55
- // Copy elements in order from head to tail
55
+
56
56
  for (const i of range(asSafeUint(this.#mut_size))) {
57
57
  const sourceIndex = (this.#mut_head + i) % this.#mut_capacity;
58
58
  newBuffer[i] = this.#mut_buffer[sourceIndex];
@@ -62,58 +62,58 @@ class QueueClass {
62
62
  this.#mut_tail = this.#mut_size;
63
63
  this.#mut_capacity = newCapacity;
64
64
  }
65
- /** @inheritdoc */
65
+
66
66
  get isEmpty() {
67
67
  return this.#mut_size === 0;
68
68
  }
69
- /** @inheritdoc */
69
+
70
70
  get size() {
71
71
  return asUint32(this.#mut_size);
72
72
  }
73
- /**
74
- * Removes and returns the element at the front of the queue (FIFO).
75
- *
76
- * This operation removes the element that was added earliest (first-in) and
77
- * returns it. If the queue is empty, returns `Optional.none`. The operation
78
- * is guaranteed to be O(1) and does not require any array shifting or
79
- * copying.
80
- *
81
- * **Time Complexity:** O(1) - constant time operation **Space Complexity:**
82
- * O(1) - no additional memory allocation
83
- *
84
- * @returns An Optional containing the removed element, or `Optional.none` if
85
- * the queue is empty.
86
- */
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
87
  dequeue() {
88
88
  if (this.isEmpty) {
89
89
  return Optional.none;
90
90
  }
91
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
91
+
92
92
  const element = this.#mut_buffer[this.#mut_head];
93
- this.#mut_buffer[this.#mut_head] = undefined; // Clear reference for garbage collection
93
+ this.#mut_buffer[this.#mut_head] = undefined;
94
94
  this.#mut_head = (this.#mut_head + 1) % this.#mut_capacity;
95
95
  this.#mut_size -= 1;
96
96
  return Optional.some(element);
97
97
  }
98
- /**
99
- * Adds an element to the back of the queue (FIFO).
100
- *
101
- * This operation adds the element to the end of the queue, where it will be
102
- * the last to be dequeued (first-in, first-out ordering). The operation is
103
- * amortized O(1), meaning it's O(1) for most operations with occasional O(n)
104
- * when the buffer needs resizing.
105
- *
106
- * **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
107
- * needed **Space Complexity:** O(1) - constant additional memory per element
108
- *
109
- * **Buffer Resizing:** When the internal buffer becomes full, it
110
- * automatically doubles in size and reorganizes elements to maintain the
111
- * circular buffer structure.
112
- *
113
- * @param value The element to add to the back of the queue.
114
- */
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
115
  enqueue(value) {
116
- // Resize if buffer is full
116
+
117
117
  if (this.#mut_size === this.#mut_capacity) {
118
118
  this.#resize();
119
119
  }
@@ -122,68 +122,68 @@ class QueueClass {
122
122
  this.#mut_size += 1;
123
123
  }
124
124
  }
125
- /**
126
- * Creates a new Queue instance with FIFO (First-In, First-Out) behavior using a
127
- * high-performance circular buffer.
128
- *
129
- * This factory function creates an optimized queue implementation that
130
- * maintains excellent performance characteristics for both enqueue and dequeue
131
- * operations. The underlying circular buffer automatically resizes to
132
- * accommodate growing workloads while providing predictable O(1) operations.
133
- *
134
- * **Implementation Features:**
135
- *
136
- * - **O(1) enqueue operations** (amortized - occasionally O(n) when resizing)
137
- * - **O(1) dequeue operations** (always)
138
- * - **Automatic buffer resizing** - starts at 8 elements, doubles when full
139
- * - **Memory efficient** - garbage collects removed elements immediately
140
- * - **Circular buffer design** - eliminates need for array shifting operations
141
- *
142
- * **Performance Benefits:**
143
- *
144
- * - No array copying during normal operations
145
- * - Minimal memory allocation overhead
146
- * - Predictable performance under high load
147
- * - Efficient memory usage with automatic cleanup
148
- *
149
- * @example
150
- *
151
- * ```ts
152
- * const queue = createQueue<number>();
153
- *
154
- * assert.isTrue(queue.isEmpty);
155
- *
156
- * assert.isTrue(queue.size === 0);
157
- *
158
- * queue.enqueue(1);
159
- *
160
- * queue.enqueue(2);
161
- *
162
- * assert.isFalse(queue.isEmpty);
163
- *
164
- * assert.isTrue(queue.size === 2);
165
- *
166
- * assert.deepStrictEqual(queue.dequeue(), Optional.some(1));
167
- *
168
- * assert.deepStrictEqual(queue.dequeue(), Optional.some(2));
169
- *
170
- * assert.deepStrictEqual(queue.dequeue(), Optional.none);
171
- *
172
- * const seededQueue = createQueue(['first', 'second']);
173
- *
174
- * assert.isTrue(seededQueue.size === 2);
175
- *
176
- * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));
177
- *
178
- * assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));
179
- * ```
180
- *
181
- * @template T The type of elements stored in the queue.
182
- * @param initialValues Optional array of initial elements to populate the
183
- * queue. Elements will be dequeued in the same order they appear in the
184
- * array. If provided, the initial buffer capacity will be at least twice the
185
- * array length.
186
- * @returns A new Queue instance optimized for high-performance FIFO operations.
187
- */
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
188
  export const createQueue = (initialValues) => new QueueClass(initialValues);
189
189
  //# sourceMappingURL=queue.mjs.map
@@ -1,108 +1,108 @@
1
1
  import { Optional } from '../functional/index.mjs';
2
2
  import { range } from '../iterator/index.mjs';
3
3
  import { asUint32, Uint32 } from '../number/index.mjs';
4
- /**
5
- * Class implementation for a stack with LIFO (Last-In, First-Out) behavior
6
- * using a dynamic array. This implementation provides O(1) amortized push and
7
- * O(1) pop operations by using a resizable buffer that grows as needed.
8
- *
9
- * The underlying array automatically resizes when it becomes full, ensuring
10
- * that the stack can grow to accommodate any number of elements while
11
- * maintaining efficient operations.
12
- *
13
- * @template T The type of elements in the stack.
14
- * @implements Stack
15
- */
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
16
  class StackClass {
17
- /** @internal Initial capacity for new stacks. */
17
+
18
18
  static #INITIAL_CAPACITY = 8;
19
- /** @internal Dynamic array to store stack elements. */
19
+
20
20
  #mut_buffer;
21
- /** @internal Current number of elements in the stack. */
21
+
22
22
  #mut_size;
23
- /** @internal Current capacity of the buffer. */
23
+
24
24
  #mut_capacity;
25
- /**
26
- * Constructs a new StackClass instance.
27
- *
28
- * @param initialValues Optional initial values to populate the stack.
29
- */
25
+
26
+
27
+
28
+
29
+
30
30
  constructor(initialValues = []) {
31
31
  const initialCapacity = asUint32(Math.max(StackClass.#INITIAL_CAPACITY, initialValues.length * 2));
32
32
  this.#mut_buffer = Array.from({ length: initialCapacity }, () => undefined);
33
33
  this.#mut_size = asUint32(0);
34
34
  this.#mut_capacity = initialCapacity;
35
- // Add initial values
35
+
36
36
  for (const value of initialValues) {
37
37
  this.push(value);
38
38
  }
39
39
  }
40
- /**
41
- * @internal
42
- * Resizes the buffer when it becomes full.
43
- * Doubles the capacity while preserving all elements.
44
- */
40
+
41
+
42
+
43
+
44
+
45
45
  #resize() {
46
46
  const newCapacity = asUint32(this.#mut_capacity * 2);
47
47
  const newBuffer = Array.from({ length: newCapacity }, () => undefined);
48
- // Copy existing elements
48
+
49
49
  for (const i of range(this.#mut_size)) {
50
50
  newBuffer[i] = this.#mut_buffer[i];
51
51
  }
52
52
  this.#mut_buffer = newBuffer;
53
53
  this.#mut_capacity = newCapacity;
54
54
  }
55
- /** @inheritdoc */
55
+
56
56
  get isEmpty() {
57
57
  return this.#mut_size === 0;
58
58
  }
59
- /** @inheritdoc */
59
+
60
60
  get size() {
61
61
  return asUint32(this.#mut_size);
62
62
  }
63
- /**
64
- * Removes and returns the element at the top of the stack (LIFO).
65
- *
66
- * This operation removes the element that was added most recently (last-in)
67
- * and returns it. If the stack is empty, returns `Optional.none`. The
68
- * operation is guaranteed to be O(1) and does not require any array resizing
69
- * or memory reallocation.
70
- *
71
- * **Time Complexity:** O(1) - constant time operation **Space Complexity:**
72
- * O(1) - no additional memory allocation
73
- *
74
- * @returns An Optional containing the removed element, or `Optional.none` if
75
- * the stack is empty.
76
- */
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
77
  pop() {
78
78
  if (this.isEmpty) {
79
79
  return Optional.none;
80
80
  }
81
81
  this.#mut_size = Uint32.sub(this.#mut_size, 1);
82
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
82
+
83
83
  const element = this.#mut_buffer[this.#mut_size];
84
- this.#mut_buffer[this.#mut_size] = undefined; // Clear reference for garbage collection
84
+ this.#mut_buffer[this.#mut_size] = undefined;
85
85
  return Optional.some(element);
86
86
  }
87
- /**
88
- * Adds an element to the top of the stack (LIFO).
89
- *
90
- * This operation adds the element to the top of the stack, where it will be
91
- * the first to be popped (last-in, first-out ordering). The operation is
92
- * amortized O(1), meaning it's O(1) for most operations with occasional O(n)
93
- * when the buffer needs resizing.
94
- *
95
- * **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
96
- * needed **Space Complexity:** O(1) - constant additional memory per element
97
- *
98
- * **Buffer Resizing:** When the internal buffer becomes full, it
99
- * automatically doubles in size and copies existing elements to maintain the
100
- * stack structure.
101
- *
102
- * @param value The element to add to the top of the stack.
103
- */
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
104
  push(value) {
105
- // Resize if buffer is full
105
+
106
106
  if (this.#mut_size === this.#mut_capacity) {
107
107
  this.#resize();
108
108
  }
@@ -110,66 +110,66 @@ class StackClass {
110
110
  this.#mut_size = Uint32.add(this.#mut_size, 1);
111
111
  }
112
112
  }
113
- /**
114
- * Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a
115
- * high-performance dynamic array.
116
- *
117
- * This factory function creates an optimized stack implementation that
118
- * maintains excellent performance characteristics for both push and pop
119
- * operations. The underlying dynamic array automatically resizes to accommodate
120
- * growing workloads while providing predictable O(1) operations.
121
- *
122
- * **Implementation Features:**
123
- *
124
- * - **O(1) push operations** (amortized - occasionally O(n) when resizing)
125
- * - **O(1) pop operations** (always)
126
- * - **Automatic buffer resizing** - starts at 8 elements, doubles when full
127
- * - **Memory efficient** - garbage collects removed elements immediately
128
- * - **Dynamic array design** - eliminates need for complex memory management
129
- *
130
- * **Performance Benefits:**
131
- *
132
- * - No array shifting required for stack operations
133
- * - Minimal memory allocation overhead
134
- * - Predictable performance under high load
135
- * - Efficient memory usage with automatic cleanup
136
- *
137
- * @example
138
- *
139
- * ```ts
140
- * const stack = createStack<string>();
141
- *
142
- * assert.isTrue(stack.isEmpty);
143
- *
144
- * assert.isTrue(stack.size === 0);
145
- *
146
- * stack.push('first');
147
- *
148
- * stack.push('second');
149
- *
150
- * assert.isFalse(stack.isEmpty);
151
- *
152
- * assert.isTrue(stack.size === 2);
153
- *
154
- * assert.deepStrictEqual(stack.pop(), Optional.some('second'));
155
- *
156
- * assert.deepStrictEqual(stack.pop(), Optional.some('first'));
157
- *
158
- * assert.deepStrictEqual(stack.pop(), Optional.none);
159
- *
160
- * const seededStack = createStack([10, 20, 30]);
161
- *
162
- * assert.isTrue(seededStack.size === 3);
163
- *
164
- * assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
165
- * ```
166
- *
167
- * @template T The type of elements stored in the stack.
168
- * @param initialValues Optional array of initial elements to populate the
169
- * stack. Elements will be popped in reverse order of how they appear in the
170
- * array (last array element will be popped first). If provided, the initial
171
- * buffer capacity will be at least twice the array length.
172
- * @returns A new Stack instance optimized for high-performance LIFO operations.
173
- */
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
174
  export const createStack = (initialValues) => new StackClass(initialValues);
175
175
  //# sourceMappingURL=stack.mjs.map