@@ -22,9 +22,11 @@ A lightweight TypeScript library for managing various queue and stack structures
22
22
23
23
* [ Installation] ( #installation )
24
24
* [ Api] ( #api )
25
- * ` ProcessingQueue `
26
- * ` Queue `
27
- * ` Stack `
25
+ * [ ` Elements ` ] ( #elements )
26
+ * [ ` ProcessingQueue ` ] ( #processingqueue )
27
+ * [ ` Processing ` ] ( #processing )
28
+ * [ ` Queue ` ] ( #queue )
29
+ * [ ` Stack ` ] ( #stack )
28
30
* [ Git] ( #git )
29
31
* [ Commit] ( #commit )
30
32
* [ Versioning] ( #versioning )
@@ -40,12 +42,215 @@ npm install @typescript-package/queue
40
42
41
43
``` typescript
42
44
import {
45
+ // Class.
46
+ Elements ,
47
+ // Abstract.
43
48
ProcessingQueue ,
44
49
Queue ,
45
50
Stack
46
51
} from ' @typescript-package/queue' ;
47
52
```
48
53
54
+ ### ` Elements `
55
+
56
+ ``` typescript
57
+ import { Elements } from ' @typescript-package/queue' ;
58
+
59
+ let elements = new Elements (
60
+ [1 , 2 , 3 , 4 ], // Array type elements.
61
+ 15 // Maximum size of the elements.
62
+ );
63
+
64
+ // Appends the value at the end of an array state.
65
+ elements .append (5 );
66
+ console .log (elements .state ); // Output: [1, 2, 3, 4, 5]
67
+
68
+ // Inserts the value at the specified index into the array state.
69
+ elements .insert (2 , 10 );
70
+ console .log (elements .state ); // Output: [1, 2, 10, 3, 4, 5]
71
+
72
+ // Adds the values at the beginning of array state.
73
+ elements .prepend (0 );
74
+ console .log (elements .state ); // Output: [0, 1, 2, 10, 3, 4, 5]
75
+
76
+ // Updates the value at the index in the array state.
77
+ elements .update (0 , 127 );
78
+ console .log (elements .state ); // Output: [127, 1, 2, 10, 3, 4, 5]
79
+ ```
80
+
81
+ ### ` ProcessingQueue `
82
+
83
+ ``` typescript
84
+ import { ProcessingQueue } from ' @typescript-package/queue' ;
85
+
86
+ // Initialize the `ProcessingQueue`.
87
+ let processingQueue = new ProcessingQueue (
88
+ 2 , // concurrency
89
+ 10 , // size
90
+ 1 , 2 , 3 // items
91
+ );
92
+
93
+ // The maximum number of elements that can be processed concurrently.
94
+ console .log (` concurrency, ` , processingQueue .concurrency ); // Output: 2
95
+
96
+ // A set containing all elements that have been successfully processed.
97
+ console .log (` processed, ` , processingQueue .processed ); // Output: Set(0)
98
+
99
+ // Checks whether the queue is empty.
100
+ console .log (` isEmpty(), ` , processingQueue .isEmpty ()); // Output: false
101
+
102
+ // Checks whether the queue is full.
103
+ console .log (` isFull(), ` , processingQueue .isFull ()); // Output: false
104
+
105
+ // The maximum queue size.
106
+ console .log (` size, ` , processingQueue .size ); // Output: 10
107
+
108
+ // The actual queue Elements state - raw array state of the queue.
109
+ console .log (` state, ` , processingQueue .state ); // Output: [1, 2, 3]
110
+
111
+ // The actual queue length.
112
+ console .log (` length, ` , processingQueue .length ); // Output: 3
113
+
114
+ // Adds a new element to the queue.
115
+ console .log (` enqueue(4), ` , processingQueue .enqueue (4 ));
116
+
117
+ // The actual queue length.
118
+ console .log (` length, ` , processingQueue .length ); // Output: 4
119
+
120
+ // Returns the first element in the queue.
121
+ console .log (` peek(), ` , processingQueue .peek ()); // Output: 1
122
+
123
+ // Returns the first element in the queue.
124
+ console .log (` dequeue(), ` , processingQueue .dequeue ()); // Output: 1
125
+
126
+ // The actual queue length.
127
+ console .log (` length, ` , processingQueue .length ); // Output: 3
128
+
129
+ // Adds to the full.
130
+ processingQueue .enqueue (5 ).enqueue (6 ).enqueue (7 ).enqueue (8 ).enqueue (9 ).enqueue (10 ).enqueue (11 );
131
+
132
+ // The actual queue Elements state - raw array state of the queue.
133
+ console .log (` state, ` , processingQueue .state ); // Output: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
134
+
135
+ // Waits for all elements in the queue to be processed and returns the set of processed elements.
136
+ processingQueue .isCompleted ().then (
137
+ processed => console .log (` Completed ` , processed ), // Output: Completed Set(10)
138
+ reason => console .log (reason )
139
+ );
140
+
141
+ // Starts processing elements in the queue using the provided callback function.
142
+ processingQueue .run (element => console .log (` Processed ` , element )); // Output: Processed {element}
143
+
144
+ // A set containing all elements that have been successfully processed.
145
+ console .log (` processed, ` , processingQueue .processed ); // Output: Set(10)
146
+ ```
147
+
148
+ ### ` Processing `
149
+
150
+ ### ` Queue `
151
+
152
+ ``` typescript
153
+ import { Queue } from ' @typescript-package/queue' ;
154
+
155
+ // Initialize the new `Queue`.
156
+ let queue = new Queue (
157
+ 10 , // size
158
+ 1 , 2 , 3 // item
159
+ );
160
+
161
+ // Adds a new element to the queue.
162
+ queue .enqueue (4 );
163
+
164
+ // The actual queue Elements state - raw array state of the queue.
165
+ console .log (` state, ` , queue .state ); // Output: [1, 2, 3, 4]
166
+
167
+ // Returns the first element in the queue.
168
+ console .log (` peek(), ` , queue .peek ()); // Output: 1
169
+
170
+ // Checks if the queue is empty.
171
+ console .log (` isEmpty(), ` , queue .isEmpty ()); // Output: false
172
+
173
+ // The maximum queue size.
174
+ console .log (` size, ` , queue .size ); // Output: 10
175
+
176
+ // The actual queue Elements state - raw array state of the queue.
177
+ console .log (` state, ` , queue .state ); // Output: [1, 2, 3, 4]
178
+
179
+ // Adds to the full.
180
+ queue .enqueue (5 ).enqueue (6 ).enqueue (7 ).enqueue (8 ).enqueue (9 ).enqueue (10 );
181
+
182
+ // Checks whether the queue is full.
183
+ console .log (` isFull(), ` , queue .isFull ()); // Output: true
184
+
185
+ try {
186
+ queue .enqueue (11 );
187
+ } catch (error ) {
188
+ console .log (error ); // Error: Queue is full.
189
+ }
190
+
191
+ // Clears the queue.
192
+ console .log (` clear(), ` , queue .clear ());
193
+
194
+ // Checks if the queue is empty.
195
+ console .log (` isEmpty(), ` , queue .isEmpty ()); // Output: true
196
+
197
+ // The actual queue Elements state - raw array state of the queue.
198
+ console .log (` state, ` , queue .state ); // Output: []
199
+
200
+ ```
201
+
202
+ ### ` Stack `
203
+
204
+ ``` typescript
205
+ import { Stack } from ' @typescript-package/queue' ;
206
+
207
+ // Initialize the `Stack`.
208
+ let stack = new Stack (
209
+ 10 , // size
210
+ 1 , 2 , 3 // items
211
+ );
212
+
213
+ // The actual stack length.
214
+ console .log (` length, ` , stack .length ); // Output: 3
215
+
216
+ // Adds a new element on the stack.
217
+ stack .push (4 );
218
+
219
+ // The maximum stack size.
220
+ console .log (` size, ` , stack .size ); // Output: 10
221
+
222
+ // The actual stack length.
223
+ console .log (` length, ` , stack .length ); // Output: 4
224
+
225
+ // Returns the top element on the stack.
226
+ console .log (` peek(), ` , stack .peek ()); // Output: 4
227
+
228
+ // The actual stack `Elements` state.
229
+ console .log (` state, ` , stack .state ); // Output: [1, 2, 3, 4]
230
+
231
+ // Removes and returns the top element from the stack.
232
+ console .log (` pop(), ` , stack .pop ()); // Output: 4
233
+
234
+ // The actual stack `Elements` state.
235
+ console .log (` state, ` , stack .state ); // Output: [1, 2, 3]
236
+
237
+ // Adds to the full.
238
+ stack .push (4 ).push (5 ).push (6 ).push (7 ).push (8 ).push (9 ).push (10 );
239
+
240
+ // The actual stack `Elements` state.
241
+ console .log (` state, ` , stack .state ); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
242
+
243
+ // Checks if the stack is full.
244
+ console .log (` isFull(), ` , stack .isFull ()); // Output: true
245
+
246
+ // Clears the queue.
247
+ stack .clear ()
248
+
249
+ // The actual stack `Elements` state.
250
+ console .log (` state, ` , stack .state ); // Output: []
251
+ console .log (` isEmpty(), ` , stack .isEmpty ()); // Output: true
252
+ ```
253
+
49
254
## GIT
50
255
51
256
### Commit
0 commit comments