typescript-ds-lib 0.2.7 → 0.2.8
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/package.json +2 -4
- package/lib/binary-search-tree.ts +0 -218
- package/lib/deque.ts +0 -86
- package/lib/hash-table.ts +0 -179
- package/lib/linked-list.ts +0 -314
- package/lib/map.ts +0 -55
- package/lib/matrix.ts +0 -427
- package/lib/priority-queue.ts +0 -71
- package/lib/queue.ts +0 -62
- package/lib/red-black-tree.ts +0 -350
- package/lib/set.ts +0 -83
- package/lib/stack.ts +0 -59
- package/types/index.ts +0 -1
package/lib/matrix.ts
DELETED
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
export interface Matrix<T> {
|
|
2
|
-
// Basic operations
|
|
3
|
-
get(row: number, col: number): T | undefined;
|
|
4
|
-
set(row: number, col: number, value: T): void;
|
|
5
|
-
rows(): number;
|
|
6
|
-
columns(): number;
|
|
7
|
-
|
|
8
|
-
// Bulk operations
|
|
9
|
-
fill(value: T): void;
|
|
10
|
-
clear(): void;
|
|
11
|
-
resize(rows: number, cols: number): void;
|
|
12
|
-
|
|
13
|
-
// State checks
|
|
14
|
-
isEmpty(): boolean;
|
|
15
|
-
size(): number;
|
|
16
|
-
isSquare(): boolean;
|
|
17
|
-
isSymmetric(): boolean;
|
|
18
|
-
|
|
19
|
-
// Row/Column operations
|
|
20
|
-
getRow(row: number): T[];
|
|
21
|
-
getColumn(col: number): T[];
|
|
22
|
-
setRow(row: number, values: T[]): void;
|
|
23
|
-
setColumn(col: number, values: T[]): void;
|
|
24
|
-
swapRows(row1: number, row2: number): void;
|
|
25
|
-
swapColumns(col1: number, col2: number): void;
|
|
26
|
-
|
|
27
|
-
// Matrix transformations
|
|
28
|
-
transpose(): Matrix<T>;
|
|
29
|
-
add(other: Matrix<T>): Matrix<T>;
|
|
30
|
-
subtract(other: Matrix<T>): Matrix<T>;
|
|
31
|
-
multiply(other: Matrix<T>): Matrix<T>;
|
|
32
|
-
scalarMultiply(scalar: number): Matrix<T>;
|
|
33
|
-
|
|
34
|
-
// Element-wise operations
|
|
35
|
-
map(fn: (value: T, row: number, col: number) => T): Matrix<T>;
|
|
36
|
-
forEach(fn: (value: T, row: number, col: number) => void): void;
|
|
37
|
-
|
|
38
|
-
// Utility methods
|
|
39
|
-
clone(): Matrix<T>;
|
|
40
|
-
toArray(): T[][];
|
|
41
|
-
equals(other: Matrix<T>): boolean;
|
|
42
|
-
|
|
43
|
-
// Submatrix operations
|
|
44
|
-
submatrix(startRow: number, startCol: number, endRow: number, endCol: number): Matrix<T>;
|
|
45
|
-
insertMatrix(other: Matrix<T>, startRow: number, startCol: number): void;
|
|
46
|
-
|
|
47
|
-
// Diagonal operations
|
|
48
|
-
getDiagonal(): T[];
|
|
49
|
-
setDiagonal(values: T[]): void;
|
|
50
|
-
trace(): T;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export class Matrix<T> implements Matrix<T> {
|
|
55
|
-
private data: T[][];
|
|
56
|
-
private numRows: number;
|
|
57
|
-
private numCols: number;
|
|
58
|
-
|
|
59
|
-
constructor(rows: number, cols: number) {
|
|
60
|
-
this.numRows = rows;
|
|
61
|
-
this.numCols = cols;
|
|
62
|
-
this.data = Array(rows).fill(null).map(() => Array(cols).fill(undefined));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Gets the value at the specified position. The value at [row,col] or undefined if out of bounds.
|
|
67
|
-
*/
|
|
68
|
-
get(row: number, col: number): T | undefined {
|
|
69
|
-
if (!this.isValidPosition(row, col)) return undefined;
|
|
70
|
-
return this.data[row][col];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Sets a value at the specified position.
|
|
75
|
-
*/
|
|
76
|
-
set(row: number, col: number, value: T): void {
|
|
77
|
-
if (!this.isValidPosition(row, col)) return;
|
|
78
|
-
this.data[row][col] = value;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Returns the number of rows in the matrix.
|
|
83
|
-
*/
|
|
84
|
-
rows(): number {
|
|
85
|
-
return this.numRows;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Returns the number of columns in the matrix.
|
|
90
|
-
*/
|
|
91
|
-
columns(): number {
|
|
92
|
-
return this.numCols;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Fills the entire matrix with a value.
|
|
97
|
-
*/
|
|
98
|
-
fill(value: T): void {
|
|
99
|
-
this.data = Array(this.numRows).fill(null).map(() => Array(this.numCols).fill(value));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Clears the matrix by setting all elements to undefined.
|
|
104
|
-
*/
|
|
105
|
-
clear(): void {
|
|
106
|
-
this.data = Array(this.numRows).fill(null).map(() => Array(this.numCols).fill(undefined));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Checks if the matrix is empty (has zero dimensions).
|
|
111
|
-
*/
|
|
112
|
-
isEmpty(): boolean {
|
|
113
|
-
return this.numRows === 0 || this.numCols === 0;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Returns the total number of elements in the matrix.
|
|
118
|
-
*/
|
|
119
|
-
size(): number {
|
|
120
|
-
return this.numRows * this.numCols;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Checks if the matrix is square (same number of rows and columns).
|
|
125
|
-
*/
|
|
126
|
-
isSquare(): boolean {
|
|
127
|
-
return this.numRows === this.numCols;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Checks if the matrix is symmetric (equal to its transpose).
|
|
132
|
-
*/
|
|
133
|
-
isSymmetric(): boolean {
|
|
134
|
-
if (!this.isSquare()) return false;
|
|
135
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
136
|
-
for (let j = 0; j < i; j++) {
|
|
137
|
-
if (this.data[i][j] !== this.data[j][i]) return false;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Creates a new matrix that is the transpose of this matrix.
|
|
145
|
-
*/
|
|
146
|
-
transpose(): Matrix<T> {
|
|
147
|
-
const result = new Matrix<T>(this.numCols, this.numRows);
|
|
148
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
149
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
150
|
-
result.set(j, i, this.data[i][j]);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return result;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Adds another matrix to this one.
|
|
158
|
-
*/
|
|
159
|
-
add(other: Matrix<T>): Matrix<T> {
|
|
160
|
-
if (this.numRows !== other.rows() || this.numCols !== other.columns()) {
|
|
161
|
-
throw new Error('Matrix dimensions must match for addition');
|
|
162
|
-
}
|
|
163
|
-
const result = new Matrix<T>(this.numRows, this.numCols);
|
|
164
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
165
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
166
|
-
const sum = (this.data[i][j] as any) + (other.get(i, j) as any);
|
|
167
|
-
result.set(i, j, sum as T);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return result;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Subtracts another matrix from this one.
|
|
175
|
-
*/
|
|
176
|
-
subtract(other: Matrix<T>): Matrix<T> {
|
|
177
|
-
if (this.numRows !== other.rows() || this.numCols !== other.columns()) {
|
|
178
|
-
throw new Error('Matrix dimensions must match for subtraction');
|
|
179
|
-
}
|
|
180
|
-
const result = new Matrix<T>(this.numRows, this.numCols);
|
|
181
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
182
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
183
|
-
const diff = (this.data[i][j] as any) - (other.get(i, j) as any);
|
|
184
|
-
result.set(i, j, diff as T);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return result;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Multiplies this matrix with another matrix.
|
|
192
|
-
*/
|
|
193
|
-
multiply(other: Matrix<T>): Matrix<T> {
|
|
194
|
-
if (this.numCols !== other.rows()) {
|
|
195
|
-
throw new Error('Matrix dimensions must be compatible for multiplication');
|
|
196
|
-
}
|
|
197
|
-
const result = new Matrix<T>(this.numRows, other.columns());
|
|
198
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
199
|
-
for (let j = 0; j < other.columns(); j++) {
|
|
200
|
-
let sum: any = 0;
|
|
201
|
-
for (let k = 0; k < this.numCols; k++) {
|
|
202
|
-
sum += (this.data[i][k] as any) * (other.get(k, j) as any);
|
|
203
|
-
}
|
|
204
|
-
result.set(i, j, sum as T);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return result;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Multiplies the matrix by a scalar value.
|
|
212
|
-
*/
|
|
213
|
-
scalarMultiply(scalar: number): Matrix<T> {
|
|
214
|
-
const result = new Matrix<T>(this.numRows, this.numCols);
|
|
215
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
216
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
217
|
-
const product = (this.data[i][j] as any) * scalar;
|
|
218
|
-
result.set(i, j, product as T);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return result;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Applies a function to each element and returns a new matrix.
|
|
226
|
-
*/
|
|
227
|
-
map(fn: (value: T, row: number, col: number) => T): Matrix<T> {
|
|
228
|
-
const result = new Matrix<T>(this.numRows, this.numCols);
|
|
229
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
230
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
231
|
-
result.set(i, j, fn(this.data[i][j], i, j));
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return result;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Executes a function for each element in the matrix.
|
|
239
|
-
*/
|
|
240
|
-
forEach(fn: (value: T, row: number, col: number) => void): void {
|
|
241
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
242
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
243
|
-
fn(this.data[i][j], i, j);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Creates a deep copy of this matrix.
|
|
250
|
-
*/
|
|
251
|
-
clone(): Matrix<T> {
|
|
252
|
-
const result = new Matrix<T>(this.numRows, this.numCols);
|
|
253
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
254
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
255
|
-
result.set(i, j, this.data[i][j]);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Converts the matrix to a 2D array.
|
|
263
|
-
*/
|
|
264
|
-
toArray(): T[][] {
|
|
265
|
-
return this.data.map(row => [...row]);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Checks if this matrix equals another matrix.
|
|
270
|
-
*/
|
|
271
|
-
equals(other: Matrix<T>): boolean {
|
|
272
|
-
if (this.numRows !== other.rows() || this.numCols !== other.columns()) {
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
275
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
276
|
-
for (let j = 0; j < this.numCols; j++) {
|
|
277
|
-
if (this.data[i][j] !== other.get(i, j)) return false;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Gets a copy of the specified row.
|
|
285
|
-
*/
|
|
286
|
-
getRow(row: number): T[] {
|
|
287
|
-
if (row < 0 || row >= this.numRows) {
|
|
288
|
-
return [];
|
|
289
|
-
}
|
|
290
|
-
return [...this.data[row]];
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Gets a copy of the specified column.
|
|
295
|
-
*/
|
|
296
|
-
getColumn(col: number): T[] {
|
|
297
|
-
if (col < 0 || col >= this.numCols) {
|
|
298
|
-
return [];
|
|
299
|
-
}
|
|
300
|
-
return this.data.map(row => row[col]);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Sets values for an entire row.
|
|
305
|
-
*/
|
|
306
|
-
setRow(row: number, values: T[]): void {
|
|
307
|
-
if (row < 0 || row >= this.numRows || values.length !== this.numCols) {
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
this.data[row] = [...values];
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Sets values for an entire column.
|
|
315
|
-
*/
|
|
316
|
-
setColumn(col: number, values: T[]): void {
|
|
317
|
-
if (col < 0 || col >= this.numCols || values.length !== this.numRows) {
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
321
|
-
this.data[i][col] = values[i];
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Swaps two rows in the matrix.
|
|
327
|
-
*/
|
|
328
|
-
swapRows(row1: number, row2: number): void {
|
|
329
|
-
if (!this.isValidPosition(row1, 0) || !this.isValidPosition(row2, 0)) return;
|
|
330
|
-
[this.data[row1], this.data[row2]] = [this.data[row2], this.data[row1]];
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Swaps two columns in the matrix.
|
|
335
|
-
*/
|
|
336
|
-
swapColumns(col1: number, col2: number): void {
|
|
337
|
-
if (!this.isValidPosition(0, col1) || !this.isValidPosition(0, col2)) return;
|
|
338
|
-
for (let i = 0; i < this.numRows; i++) {
|
|
339
|
-
[this.data[i][col1], this.data[i][col2]] = [this.data[i][col2], this.data[i][col1]];
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Extracts a submatrix from this matrix.
|
|
345
|
-
*/
|
|
346
|
-
submatrix(startRow: number, startCol: number, endRow: number, endCol: number): Matrix<T> {
|
|
347
|
-
if (!this.isValidPosition(startRow, startCol) || !this.isValidPosition(endRow, endCol)) {
|
|
348
|
-
throw new Error('Invalid submatrix bounds');
|
|
349
|
-
}
|
|
350
|
-
const rows = endRow - startRow + 1;
|
|
351
|
-
const cols = endCol - startCol + 1;
|
|
352
|
-
const result = new Matrix<T>(rows, cols);
|
|
353
|
-
for (let i = 0; i < rows; i++) {
|
|
354
|
-
for (let j = 0; j < cols; j++) {
|
|
355
|
-
result.set(i, j, this.data[startRow + i][startCol + j]);
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
return result;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Inserts another matrix into this matrix at the specified position.
|
|
363
|
-
*/
|
|
364
|
-
insertMatrix(other: Matrix<T>, startRow: number, startCol: number): void {
|
|
365
|
-
if (!this.isValidPosition(startRow, startCol)) return;
|
|
366
|
-
const maxRows = Math.min(other.rows(), this.numRows - startRow);
|
|
367
|
-
const maxCols = Math.min(other.columns(), this.numCols - startCol);
|
|
368
|
-
for (let i = 0; i < maxRows; i++) {
|
|
369
|
-
for (let j = 0; j < maxCols; j++) {
|
|
370
|
-
this.data[startRow + i][startCol + j] = other.get(i, j)!;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Gets the diagonal elements of the matrix.
|
|
377
|
-
*/
|
|
378
|
-
getDiagonal(): T[] {
|
|
379
|
-
const size = Math.min(this.numRows, this.numCols);
|
|
380
|
-
const result: T[] = [];
|
|
381
|
-
for (let i = 0; i < size; i++) {
|
|
382
|
-
result.push(this.data[i][i]);
|
|
383
|
-
}
|
|
384
|
-
return result;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Sets the diagonal elements of the matrix.
|
|
389
|
-
*/
|
|
390
|
-
setDiagonal(values: T[]): void {
|
|
391
|
-
const size = Math.min(this.numRows, this.numCols, values.length);
|
|
392
|
-
for (let i = 0; i < size; i++) {
|
|
393
|
-
this.data[i][i] = values[i];
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Calculates the trace (sum of diagonal elements) of the matrix.
|
|
399
|
-
*/
|
|
400
|
-
trace(): T {
|
|
401
|
-
if (!this.isSquare() || this.isEmpty()) {
|
|
402
|
-
throw new Error('Trace is only defined for non-empty square matrices');
|
|
403
|
-
}
|
|
404
|
-
return this.getDiagonal().reduce((sum: any, val: any) => sum + val);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Resizes the matrix to new dimensions, preserving existing values where possible.
|
|
409
|
-
*/
|
|
410
|
-
resize(rows: number, cols: number): void {
|
|
411
|
-
const newData: T[][] = Array(rows).fill(null).map(() => Array(cols).fill(undefined));
|
|
412
|
-
const minRows = Math.min(rows, this.numRows);
|
|
413
|
-
const minCols = Math.min(cols, this.numCols);
|
|
414
|
-
for (let i = 0; i < minRows; i++) {
|
|
415
|
-
for (let j = 0; j < minCols; j++) {
|
|
416
|
-
newData[i][j] = this.data[i][j];
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
this.data = newData;
|
|
420
|
-
this.numRows = rows;
|
|
421
|
-
this.numCols = cols;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
private isValidPosition(row: number, col: number): boolean {
|
|
425
|
-
return row >= 0 && row < this.numRows && col >= 0 && col < this.numCols;
|
|
426
|
-
}
|
|
427
|
-
}
|
package/lib/priority-queue.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { LinkedList } from './linked-list';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export interface PriorityQueue<T> {
|
|
5
|
-
push(element: T, priority: number): void;
|
|
6
|
-
pop(): T | undefined;
|
|
7
|
-
front(): T | undefined;
|
|
8
|
-
isEmpty(): boolean;
|
|
9
|
-
size(): number;
|
|
10
|
-
clear(): void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export class PriorityQueue<T> implements PriorityQueue<T> {
|
|
15
|
-
private list: LinkedList<{ element: T; priority: number }>;
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
this.list = new LinkedList<{ element: T; priority: number }>();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Adds an element with a priority to the queue.
|
|
23
|
-
* Lower priority numbers have higher precedence.
|
|
24
|
-
*/
|
|
25
|
-
push(element: T, priority: number): void {
|
|
26
|
-
const item = { element, priority };
|
|
27
|
-
if (this.list.isEmpty()) {
|
|
28
|
-
this.list.pushBack(item);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (!this.list.insertBefore(item, (current) => current.priority < priority)) {
|
|
32
|
-
this.list.pushBack(item);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Removes and returns the highest priority element from the queue, or undefined if queue is empty.
|
|
38
|
-
*/
|
|
39
|
-
pop(): T | undefined {
|
|
40
|
-
const item = this.list.popFront();
|
|
41
|
-
return item?.element;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Returns the highest priority element without removing it, or undefined if queue is empty.
|
|
46
|
-
*/
|
|
47
|
-
front(): T | undefined {
|
|
48
|
-
return this.list.front()?.element;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Checks if the queue is empty. Returns true if empty, false otherwise.
|
|
53
|
-
*/
|
|
54
|
-
isEmpty(): boolean {
|
|
55
|
-
return this.list.isEmpty();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Returns the number of elements in the queue.
|
|
60
|
-
*/
|
|
61
|
-
size(): number {
|
|
62
|
-
return this.list.size();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Removes all elements from the queue.
|
|
67
|
-
*/
|
|
68
|
-
clear(): void {
|
|
69
|
-
this.list.clear();
|
|
70
|
-
}
|
|
71
|
-
}
|
package/lib/queue.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { LinkedList } from './linked-list';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export interface Queue<T> {
|
|
5
|
-
push(element: T): void;
|
|
6
|
-
pop(): T | undefined;
|
|
7
|
-
front(): T | undefined;
|
|
8
|
-
isEmpty(): boolean;
|
|
9
|
-
size(): number;
|
|
10
|
-
clear(): void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export class Queue<T> implements Queue<T> {
|
|
15
|
-
private items: LinkedList<T>;
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
this.items = new LinkedList<T>();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Adds an element to the back of the queue.
|
|
23
|
-
*/
|
|
24
|
-
push(element: T): void {
|
|
25
|
-
this.items.pushBack(element);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Removes and returns the front element from the queue, or undefined if queue is empty.
|
|
30
|
-
*/
|
|
31
|
-
pop(): T | undefined {
|
|
32
|
-
return this.items.popFront();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Returns the front element of the queue without removing it, or undefined if queue is empty.
|
|
37
|
-
*/
|
|
38
|
-
front(): T | undefined {
|
|
39
|
-
return this.items.get(0);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Checks if the queue is empty. Returns true if the queue is empty, false otherwise.
|
|
44
|
-
*/
|
|
45
|
-
isEmpty(): boolean {
|
|
46
|
-
return this.items.isEmpty();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Returns the number of elements in the queue.
|
|
51
|
-
*/
|
|
52
|
-
size(): number {
|
|
53
|
-
return this.items.size();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Removes all elements from the queue.
|
|
58
|
-
*/
|
|
59
|
-
clear(): void {
|
|
60
|
-
this.items.clear();
|
|
61
|
-
}
|
|
62
|
-
}
|