data-structure-typed 2.0.0 → 2.0.2
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +8 -9
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +14 -14
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +66 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +66 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +47 -0
- package/dist/cjs/data-structures/queue/queue.js +47 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +121 -0
- package/dist/cjs/data-structures/stack/stack.js +121 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/types/utils/utils.d.ts +1 -7
- package/dist/cjs/utils/utils.d.ts +3 -49
- package/dist/cjs/utils/utils.js +13 -82
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.js +8 -9
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +14 -14
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/esm/data-structures/hash/hash-map.js +46 -0
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +66 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +66 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +47 -0
- package/dist/esm/data-structures/queue/queue.js +47 -0
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +121 -0
- package/dist/esm/data-structures/stack/stack.js +121 -0
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/types/utils/utils.d.ts +1 -7
- package/dist/esm/utils/utils.d.ts +3 -49
- package/dist/esm/utils/utils.js +10 -68
- package/dist/esm/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +32 -80
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +9 -10
- package/src/data-structures/graph/abstract-graph.ts +14 -14
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +66 -0
- package/src/data-structures/queue/queue.ts +47 -0
- package/src/data-structures/stack/stack.ts +121 -0
- package/src/types/utils/utils.ts +1 -6
- package/src/utils/utils.ts +11 -83
- package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +72 -1
- package/test/unit/data-structures/queue/queue.test.ts +214 -0
- package/test/unit/data-structures/stack/stack.test.ts +165 -0
- package/test/unit/utils/utils.test.ts +35 -2
|
@@ -14,6 +14,53 @@ import { LinearBase } from '../base/linear-base';
|
|
|
14
14
|
* 5. Data Buffering: Acting as a buffer for data packets in network communication.
|
|
15
15
|
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
16
16
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
17
|
+
* @example
|
|
18
|
+
* // Sliding Window using Queue
|
|
19
|
+
* const nums = [2, 3, 4, 1, 5];
|
|
20
|
+
* const k = 2;
|
|
21
|
+
* const queue = new Queue<number>();
|
|
22
|
+
*
|
|
23
|
+
* let maxSum = 0;
|
|
24
|
+
* let currentSum = 0;
|
|
25
|
+
*
|
|
26
|
+
* nums.forEach((num, i) => {
|
|
27
|
+
* queue.push(num);
|
|
28
|
+
* currentSum += num;
|
|
29
|
+
*
|
|
30
|
+
* if (queue.length > k) {
|
|
31
|
+
* currentSum -= queue.shift()!;
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* if (queue.length === k) {
|
|
35
|
+
* maxSum = Math.max(maxSum, currentSum);
|
|
36
|
+
* }
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* console.log(maxSum); // 7
|
|
40
|
+
* @example
|
|
41
|
+
* // Breadth-First Search (BFS) using Queue
|
|
42
|
+
* const graph: { [key in number]: number[] } = {
|
|
43
|
+
* 1: [2, 3],
|
|
44
|
+
* 2: [4, 5],
|
|
45
|
+
* 3: [],
|
|
46
|
+
* 4: [],
|
|
47
|
+
* 5: []
|
|
48
|
+
* };
|
|
49
|
+
*
|
|
50
|
+
* const queue = new Queue<number>();
|
|
51
|
+
* const visited: number[] = [];
|
|
52
|
+
*
|
|
53
|
+
* queue.push(1);
|
|
54
|
+
*
|
|
55
|
+
* while (!queue.isEmpty()) {
|
|
56
|
+
* const node = queue.shift()!;
|
|
57
|
+
* if (!visited.includes(node)) {
|
|
58
|
+
* visited.push(node);
|
|
59
|
+
* graph[node].forEach(neighbor => queue.push(neighbor));
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* console.log(visited); // [1, 2, 3, 4, 5]
|
|
17
64
|
*/
|
|
18
65
|
export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
19
66
|
constructor(elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>);
|
|
@@ -8,6 +8,53 @@ import { LinearBase } from '../base/linear-base';
|
|
|
8
8
|
* 5. Data Buffering: Acting as a buffer for data packets in network communication.
|
|
9
9
|
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
10
10
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
11
|
+
* @example
|
|
12
|
+
* // Sliding Window using Queue
|
|
13
|
+
* const nums = [2, 3, 4, 1, 5];
|
|
14
|
+
* const k = 2;
|
|
15
|
+
* const queue = new Queue<number>();
|
|
16
|
+
*
|
|
17
|
+
* let maxSum = 0;
|
|
18
|
+
* let currentSum = 0;
|
|
19
|
+
*
|
|
20
|
+
* nums.forEach((num, i) => {
|
|
21
|
+
* queue.push(num);
|
|
22
|
+
* currentSum += num;
|
|
23
|
+
*
|
|
24
|
+
* if (queue.length > k) {
|
|
25
|
+
* currentSum -= queue.shift()!;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* if (queue.length === k) {
|
|
29
|
+
* maxSum = Math.max(maxSum, currentSum);
|
|
30
|
+
* }
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* console.log(maxSum); // 7
|
|
34
|
+
* @example
|
|
35
|
+
* // Breadth-First Search (BFS) using Queue
|
|
36
|
+
* const graph: { [key in number]: number[] } = {
|
|
37
|
+
* 1: [2, 3],
|
|
38
|
+
* 2: [4, 5],
|
|
39
|
+
* 3: [],
|
|
40
|
+
* 4: [],
|
|
41
|
+
* 5: []
|
|
42
|
+
* };
|
|
43
|
+
*
|
|
44
|
+
* const queue = new Queue<number>();
|
|
45
|
+
* const visited: number[] = [];
|
|
46
|
+
*
|
|
47
|
+
* queue.push(1);
|
|
48
|
+
*
|
|
49
|
+
* while (!queue.isEmpty()) {
|
|
50
|
+
* const node = queue.shift()!;
|
|
51
|
+
* if (!visited.includes(node)) {
|
|
52
|
+
* visited.push(node);
|
|
53
|
+
* graph[node].forEach(neighbor => queue.push(neighbor));
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* console.log(visited); // [1, 2, 3, 4, 5]
|
|
11
58
|
*/
|
|
12
59
|
export class Queue extends LinearBase {
|
|
13
60
|
constructor(elements = [], options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,MAAM,OAAO,KAAwB,SAAQ,UAAgB;IAC3D,YAAY,WAAsC,EAAE,EAAE,OAA4B;QAChF,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;YAC3C,IAAI,CAAC,iBAAiB,GAAG,gBAAgB,CAAC;QAC5C,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC;IAES,SAAS,GAAQ,EAAE,CAAC;IAE9B,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAES,OAAO,GAAW,CAAC,CAAC;IAE9B,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC5C,CAAC;IAES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;;OAOG;IACM,KAAK;QACZ,OAAO,IAAI,eAAe,CAAO,IAAI,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,CAAS,CAAC;IAC1G,CAAC;CACF"}
|
|
@@ -14,6 +14,127 @@ import { IterableElementBase } from '../base';
|
|
|
14
14
|
* 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
15
15
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
16
16
|
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
17
|
+
* @example
|
|
18
|
+
* // Balanced Parentheses or Brackets
|
|
19
|
+
* type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
20
|
+
*
|
|
21
|
+
* const stack = new Stack<string>();
|
|
22
|
+
* const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
23
|
+
* const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
24
|
+
* for (const char of input) {
|
|
25
|
+
* if ('([{'.includes(char)) {
|
|
26
|
+
* stack.push(char);
|
|
27
|
+
* } else if (')]}'.includes(char)) {
|
|
28
|
+
* if (stack.pop() !== matches[char]) {
|
|
29
|
+
* fail('Parentheses are not balanced');
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* console.log(stack.isEmpty()); // true
|
|
34
|
+
* @example
|
|
35
|
+
* // Expression Evaluation and Conversion
|
|
36
|
+
* const stack = new Stack<number>();
|
|
37
|
+
* const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
38
|
+
* expression.forEach(token => {
|
|
39
|
+
* if (typeof token === 'number') {
|
|
40
|
+
* stack.push(token);
|
|
41
|
+
* } else {
|
|
42
|
+
* const b = stack.pop()!;
|
|
43
|
+
* const a = stack.pop()!;
|
|
44
|
+
* stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
45
|
+
* }
|
|
46
|
+
* });
|
|
47
|
+
* console.log(stack.pop()); // 8
|
|
48
|
+
* @example
|
|
49
|
+
* // Depth-First Search (DFS)
|
|
50
|
+
* const stack = new Stack<number>();
|
|
51
|
+
* const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
52
|
+
* const visited: number[] = [];
|
|
53
|
+
* stack.push(1);
|
|
54
|
+
* while (!stack.isEmpty()) {
|
|
55
|
+
* const node = stack.pop()!;
|
|
56
|
+
* if (!visited.includes(node)) {
|
|
57
|
+
* visited.push(node);
|
|
58
|
+
* graph[node].forEach(neighbor => stack.push(neighbor));
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* console.log(visited); // [1, 3, 5, 2, 4]
|
|
62
|
+
* @example
|
|
63
|
+
* // Backtracking Algorithms
|
|
64
|
+
* const stack = new Stack<[number, number]>();
|
|
65
|
+
* const maze = [
|
|
66
|
+
* ['S', ' ', 'X'],
|
|
67
|
+
* ['X', ' ', 'X'],
|
|
68
|
+
* [' ', ' ', 'E']
|
|
69
|
+
* ];
|
|
70
|
+
* const start: [number, number] = [0, 0];
|
|
71
|
+
* const end = [2, 2];
|
|
72
|
+
* const directions = [
|
|
73
|
+
* [0, 1], // To the right
|
|
74
|
+
* [1, 0], // down
|
|
75
|
+
* [0, -1], // left
|
|
76
|
+
* [-1, 0] // up
|
|
77
|
+
* ];
|
|
78
|
+
*
|
|
79
|
+
* const visited = new Set<string>(); // Used to record visited nodes
|
|
80
|
+
* stack.push(start);
|
|
81
|
+
* const path: number[][] = [];
|
|
82
|
+
*
|
|
83
|
+
* while (!stack.isEmpty()) {
|
|
84
|
+
* const [x, y] = stack.pop()!;
|
|
85
|
+
* if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
86
|
+
* visited.add(`${x},${y}`);
|
|
87
|
+
*
|
|
88
|
+
* path.push([x, y]);
|
|
89
|
+
*
|
|
90
|
+
* if (x === end[0] && y === end[1]) {
|
|
91
|
+
* break; // Find the end point and exit
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* for (const [dx, dy] of directions) {
|
|
95
|
+
* const nx = x + dx;
|
|
96
|
+
* const ny = y + dy;
|
|
97
|
+
* if (
|
|
98
|
+
* maze[nx]?.[ny] === ' ' || // feasible path
|
|
99
|
+
* maze[nx]?.[ny] === 'E' // destination
|
|
100
|
+
* ) {
|
|
101
|
+
* stack.push([nx, ny]);
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* expect(path).toContainEqual(end);
|
|
107
|
+
* @example
|
|
108
|
+
* // Function Call Stack
|
|
109
|
+
* const functionStack = new Stack<string>();
|
|
110
|
+
* functionStack.push('main');
|
|
111
|
+
* functionStack.push('foo');
|
|
112
|
+
* functionStack.push('bar');
|
|
113
|
+
* console.log(functionStack.pop()); // 'bar'
|
|
114
|
+
* console.log(functionStack.pop()); // 'foo'
|
|
115
|
+
* console.log(functionStack.pop()); // 'main'
|
|
116
|
+
* @example
|
|
117
|
+
* // Simplify File Paths
|
|
118
|
+
* const stack = new Stack<string>();
|
|
119
|
+
* const path = '/a/./b/../../c';
|
|
120
|
+
* path.split('/').forEach(segment => {
|
|
121
|
+
* if (segment === '..') stack.pop();
|
|
122
|
+
* else if (segment && segment !== '.') stack.push(segment);
|
|
123
|
+
* });
|
|
124
|
+
* console.log(stack.elements.join('/')); // 'c'
|
|
125
|
+
* @example
|
|
126
|
+
* // Stock Span Problem
|
|
127
|
+
* const stack = new Stack<number>();
|
|
128
|
+
* const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
129
|
+
* const spans: number[] = [];
|
|
130
|
+
* prices.forEach((price, i) => {
|
|
131
|
+
* while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
132
|
+
* stack.pop();
|
|
133
|
+
* }
|
|
134
|
+
* spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
135
|
+
* stack.push(i);
|
|
136
|
+
* });
|
|
137
|
+
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
17
138
|
*/
|
|
18
139
|
export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
19
140
|
constructor(elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>);
|
|
@@ -6,6 +6,127 @@ import { IterableElementBase } from '../base';
|
|
|
6
6
|
* 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
7
7
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
8
8
|
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
9
|
+
* @example
|
|
10
|
+
* // Balanced Parentheses or Brackets
|
|
11
|
+
* type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
12
|
+
*
|
|
13
|
+
* const stack = new Stack<string>();
|
|
14
|
+
* const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
15
|
+
* const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
16
|
+
* for (const char of input) {
|
|
17
|
+
* if ('([{'.includes(char)) {
|
|
18
|
+
* stack.push(char);
|
|
19
|
+
* } else if (')]}'.includes(char)) {
|
|
20
|
+
* if (stack.pop() !== matches[char]) {
|
|
21
|
+
* fail('Parentheses are not balanced');
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* console.log(stack.isEmpty()); // true
|
|
26
|
+
* @example
|
|
27
|
+
* // Expression Evaluation and Conversion
|
|
28
|
+
* const stack = new Stack<number>();
|
|
29
|
+
* const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
30
|
+
* expression.forEach(token => {
|
|
31
|
+
* if (typeof token === 'number') {
|
|
32
|
+
* stack.push(token);
|
|
33
|
+
* } else {
|
|
34
|
+
* const b = stack.pop()!;
|
|
35
|
+
* const a = stack.pop()!;
|
|
36
|
+
* stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
* console.log(stack.pop()); // 8
|
|
40
|
+
* @example
|
|
41
|
+
* // Depth-First Search (DFS)
|
|
42
|
+
* const stack = new Stack<number>();
|
|
43
|
+
* const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
44
|
+
* const visited: number[] = [];
|
|
45
|
+
* stack.push(1);
|
|
46
|
+
* while (!stack.isEmpty()) {
|
|
47
|
+
* const node = stack.pop()!;
|
|
48
|
+
* if (!visited.includes(node)) {
|
|
49
|
+
* visited.push(node);
|
|
50
|
+
* graph[node].forEach(neighbor => stack.push(neighbor));
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
* console.log(visited); // [1, 3, 5, 2, 4]
|
|
54
|
+
* @example
|
|
55
|
+
* // Backtracking Algorithms
|
|
56
|
+
* const stack = new Stack<[number, number]>();
|
|
57
|
+
* const maze = [
|
|
58
|
+
* ['S', ' ', 'X'],
|
|
59
|
+
* ['X', ' ', 'X'],
|
|
60
|
+
* [' ', ' ', 'E']
|
|
61
|
+
* ];
|
|
62
|
+
* const start: [number, number] = [0, 0];
|
|
63
|
+
* const end = [2, 2];
|
|
64
|
+
* const directions = [
|
|
65
|
+
* [0, 1], // To the right
|
|
66
|
+
* [1, 0], // down
|
|
67
|
+
* [0, -1], // left
|
|
68
|
+
* [-1, 0] // up
|
|
69
|
+
* ];
|
|
70
|
+
*
|
|
71
|
+
* const visited = new Set<string>(); // Used to record visited nodes
|
|
72
|
+
* stack.push(start);
|
|
73
|
+
* const path: number[][] = [];
|
|
74
|
+
*
|
|
75
|
+
* while (!stack.isEmpty()) {
|
|
76
|
+
* const [x, y] = stack.pop()!;
|
|
77
|
+
* if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
78
|
+
* visited.add(`${x},${y}`);
|
|
79
|
+
*
|
|
80
|
+
* path.push([x, y]);
|
|
81
|
+
*
|
|
82
|
+
* if (x === end[0] && y === end[1]) {
|
|
83
|
+
* break; // Find the end point and exit
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* for (const [dx, dy] of directions) {
|
|
87
|
+
* const nx = x + dx;
|
|
88
|
+
* const ny = y + dy;
|
|
89
|
+
* if (
|
|
90
|
+
* maze[nx]?.[ny] === ' ' || // feasible path
|
|
91
|
+
* maze[nx]?.[ny] === 'E' // destination
|
|
92
|
+
* ) {
|
|
93
|
+
* stack.push([nx, ny]);
|
|
94
|
+
* }
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* expect(path).toContainEqual(end);
|
|
99
|
+
* @example
|
|
100
|
+
* // Function Call Stack
|
|
101
|
+
* const functionStack = new Stack<string>();
|
|
102
|
+
* functionStack.push('main');
|
|
103
|
+
* functionStack.push('foo');
|
|
104
|
+
* functionStack.push('bar');
|
|
105
|
+
* console.log(functionStack.pop()); // 'bar'
|
|
106
|
+
* console.log(functionStack.pop()); // 'foo'
|
|
107
|
+
* console.log(functionStack.pop()); // 'main'
|
|
108
|
+
* @example
|
|
109
|
+
* // Simplify File Paths
|
|
110
|
+
* const stack = new Stack<string>();
|
|
111
|
+
* const path = '/a/./b/../../c';
|
|
112
|
+
* path.split('/').forEach(segment => {
|
|
113
|
+
* if (segment === '..') stack.pop();
|
|
114
|
+
* else if (segment && segment !== '.') stack.push(segment);
|
|
115
|
+
* });
|
|
116
|
+
* console.log(stack.elements.join('/')); // 'c'
|
|
117
|
+
* @example
|
|
118
|
+
* // Stock Span Problem
|
|
119
|
+
* const stack = new Stack<number>();
|
|
120
|
+
* const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
121
|
+
* const spans: number[] = [];
|
|
122
|
+
* prices.forEach((price, i) => {
|
|
123
|
+
* while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
124
|
+
* stack.pop();
|
|
125
|
+
* }
|
|
126
|
+
* spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
127
|
+
* stack.push(i);
|
|
128
|
+
* });
|
|
129
|
+
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
9
130
|
*/
|
|
10
131
|
export class Stack extends IterableElementBase {
|
|
11
132
|
constructor(elements = [], options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../../../../src/data-structures/stack/stack.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C
|
|
1
|
+
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../../../../src/data-structures/stack/stack.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgIG;AACH,MAAM,OAAO,KAAwB,SAAQ,mBAAyB;IACpE,YAAY,WAAsC,EAAE,EAAE,OAA4B;QAChF,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC;IAES,SAAS,GAAQ,EAAE,CAAC;IAE9B;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAA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K,MAAM,EAAE,IAAI,IAAI,EAAE,CAAC;YACtB,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;YACvD,KAAK,EAAE,CAAC;QACV,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACO,CAAC,YAAY;QACrB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC9C,MAAM,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export type Thunk<R = any> = ToThunkFn<R> & {
|
|
3
|
-
__THUNK__?: symbol;
|
|
4
|
-
};
|
|
5
|
-
export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
|
|
6
|
-
export type TrlAsyncFn = (...args: any[]) => any;
|
|
7
|
-
export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
1
|
+
export type Thunk<T> = () => T | Thunk<T>;
|
|
8
2
|
export type Any = string | number | bigint | boolean | symbol | undefined | object;
|
|
9
3
|
export type Arithmetic = number | bigint;
|
|
10
4
|
export type ComparablePrimitive = number | bigint | string | boolean;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { Comparable, Thunk
|
|
8
|
+
import type { Comparable, Thunk } from '../types';
|
|
9
9
|
/**
|
|
10
10
|
* The function generates a random UUID (Universally Unique Identifier) in TypeScript.
|
|
11
11
|
* @returns A randomly generated UUID (Universally Unique Identifier) in the format
|
|
@@ -23,54 +23,8 @@ export declare const uuidV4: () => string;
|
|
|
23
23
|
* `predicate` function.
|
|
24
24
|
*/
|
|
25
25
|
export declare const arrayRemove: <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean) => T[];
|
|
26
|
-
export declare
|
|
27
|
-
|
|
28
|
-
* The function `isThunk` checks if a given value is a function with a specific symbol property.
|
|
29
|
-
* @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
|
|
30
|
-
* function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
|
|
31
|
-
* around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
|
|
32
|
-
* @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
|
|
33
|
-
* property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
|
|
34
|
-
* met, otherwise it will be `false`.
|
|
35
|
-
*/
|
|
36
|
-
export declare const isThunk: (fnOrValue: any) => boolean;
|
|
37
|
-
/**
|
|
38
|
-
* The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
|
|
39
|
-
* @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
|
|
40
|
-
* @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
|
|
41
|
-
* of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
|
|
42
|
-
* function takes a function `fn` as an argument and returns a thunk function that, when called, will
|
|
43
|
-
* execute the `fn` function provided as an argument.
|
|
44
|
-
*/
|
|
45
|
-
export declare const toThunk: (fn: ToThunkFn) => Thunk;
|
|
46
|
-
/**
|
|
47
|
-
* The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
|
|
48
|
-
* stack overflow.
|
|
49
|
-
* @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
|
|
50
|
-
* number of arguments and returns a value.
|
|
51
|
-
* @returns The `trampoline` function returns an object with two properties:
|
|
52
|
-
* 1. A function that executes the provided function `fn` and continues to execute any thunks returned
|
|
53
|
-
* by `fn` until a non-thunk value is returned.
|
|
54
|
-
* 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
|
|
55
|
-
*/
|
|
56
|
-
export declare const trampoline: (fn: TrlFn) => ((...args: [...Parameters<TrlFn>]) => any) & {
|
|
57
|
-
cont: (...args: [...Parameters<TrlFn>]) => ReturnType<TrlFn>;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
|
|
61
|
-
* function.
|
|
62
|
-
* @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
|
|
63
|
-
* function that returns a Promise. This function will be called recursively until a non-thunk value is
|
|
64
|
-
* returned.
|
|
65
|
-
* @returns The `trampolineAsync` function returns an object with two properties:
|
|
66
|
-
* 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
|
|
67
|
-
* thunks returned by the function until a non-thunk value is returned.
|
|
68
|
-
* 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
|
|
69
|
-
* and returns it.
|
|
70
|
-
*/
|
|
71
|
-
export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Parameters<TrlAsyncFn>]) => Promise<any>) & {
|
|
72
|
-
cont: (...args: [...Parameters<TrlAsyncFn>]) => ReturnType<TrlAsyncFn>;
|
|
73
|
-
};
|
|
26
|
+
export declare function isThunk<T>(result: T | Thunk<T>): result is Thunk<T>;
|
|
27
|
+
export declare function trampoline<T>(thunk: Thunk<T>): T;
|
|
74
28
|
/**
|
|
75
29
|
* The function `getMSB` returns the most significant bit of a given number.
|
|
76
30
|
* @param {number} value - The `value` parameter is a number for which we want to find the position of
|
package/dist/esm/utils/utils.js
CHANGED
|
@@ -32,74 +32,16 @@ export const arrayRemove = function (array, predicate) {
|
|
|
32
32
|
}
|
|
33
33
|
return result;
|
|
34
34
|
};
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
export const isThunk = (fnOrValue) => {
|
|
46
|
-
return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
|
|
50
|
-
* @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
|
|
51
|
-
* @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
|
|
52
|
-
* of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
|
|
53
|
-
* function takes a function `fn` as an argument and returns a thunk function that, when called, will
|
|
54
|
-
* execute the `fn` function provided as an argument.
|
|
55
|
-
*/
|
|
56
|
-
export const toThunk = (fn) => {
|
|
57
|
-
const thunk = () => fn();
|
|
58
|
-
thunk.__THUNK__ = THUNK_SYMBOL;
|
|
59
|
-
return thunk;
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
|
|
63
|
-
* stack overflow.
|
|
64
|
-
* @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
|
|
65
|
-
* number of arguments and returns a value.
|
|
66
|
-
* @returns The `trampoline` function returns an object with two properties:
|
|
67
|
-
* 1. A function that executes the provided function `fn` and continues to execute any thunks returned
|
|
68
|
-
* by `fn` until a non-thunk value is returned.
|
|
69
|
-
* 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
|
|
70
|
-
*/
|
|
71
|
-
export const trampoline = (fn) => {
|
|
72
|
-
const cont = (...args) => toThunk(() => fn(...args));
|
|
73
|
-
return Object.assign((...args) => {
|
|
74
|
-
let result = fn(...args);
|
|
75
|
-
while (isThunk(result) && typeof result === 'function') {
|
|
76
|
-
result = result();
|
|
77
|
-
}
|
|
78
|
-
return result;
|
|
79
|
-
}, { cont });
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
|
|
83
|
-
* function.
|
|
84
|
-
* @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
|
|
85
|
-
* function that returns a Promise. This function will be called recursively until a non-thunk value is
|
|
86
|
-
* returned.
|
|
87
|
-
* @returns The `trampolineAsync` function returns an object with two properties:
|
|
88
|
-
* 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
|
|
89
|
-
* thunks returned by the function until a non-thunk value is returned.
|
|
90
|
-
* 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
|
|
91
|
-
* and returns it.
|
|
92
|
-
*/
|
|
93
|
-
export const trampolineAsync = (fn) => {
|
|
94
|
-
const cont = (...args) => toThunk(() => fn(...args));
|
|
95
|
-
return Object.assign(async (...args) => {
|
|
96
|
-
let result = await fn(...args);
|
|
97
|
-
while (isThunk(result) && typeof result === 'function') {
|
|
98
|
-
result = await result();
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}, { cont });
|
|
102
|
-
};
|
|
35
|
+
export function isThunk(result) {
|
|
36
|
+
return typeof result === 'function';
|
|
37
|
+
}
|
|
38
|
+
export function trampoline(thunk) {
|
|
39
|
+
let result = thunk;
|
|
40
|
+
while (isThunk(result)) {
|
|
41
|
+
result = result();
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
103
45
|
/**
|
|
104
46
|
* The function `getMSB` returns the most significant bit of a given number.
|
|
105
47
|
* @param {number} value - The `value` parameter is a number for which we want to find the position of
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/utils/utils.ts"],"names":[],"mappings":"AASA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,OAAO,sCAAsC,CAAC,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC;QACvE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,C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