data-structure-typed 1.12.10 → 1.12.21

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 (158) hide show
  1. package/README.md +7 -0
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +14 -5
  3. package/dist/data-structures/binary-tree/avl-tree.js +15 -6
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +11 -2
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +11 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +26 -17
  7. package/dist/data-structures/binary-tree/binary-tree.js +72 -62
  8. package/dist/data-structures/binary-tree/bst.d.ts +92 -5
  9. package/dist/data-structures/binary-tree/bst.js +89 -5
  10. package/dist/data-structures/binary-tree/segment-tree.d.ts +41 -2
  11. package/dist/data-structures/binary-tree/segment-tree.js +41 -2
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +34 -3
  13. package/dist/data-structures/binary-tree/tree-multiset.js +35 -4
  14. package/dist/data-structures/graph/abstract-graph.d.ts +5 -0
  15. package/dist/data-structures/graph/abstract-graph.js +12 -4
  16. package/dist/data-structures/graph/directed-graph.d.ts +18 -4
  17. package/dist/data-structures/graph/directed-graph.js +24 -37
  18. package/dist/data-structures/graph/undirected-graph.d.ts +13 -0
  19. package/dist/data-structures/graph/undirected-graph.js +18 -2
  20. package/dist/data-structures/hash/coordinate-map.d.ts +5 -2
  21. package/dist/data-structures/hash/coordinate-map.js +5 -2
  22. package/dist/data-structures/hash/coordinate-set.d.ts +5 -2
  23. package/dist/data-structures/hash/coordinate-set.js +5 -2
  24. package/dist/data-structures/heap/heap.d.ts +9 -6
  25. package/dist/data-structures/heap/heap.js +8 -8
  26. package/dist/data-structures/heap/max-heap.d.ts +5 -2
  27. package/dist/data-structures/heap/max-heap.js +5 -2
  28. package/dist/data-structures/heap/min-heap.d.ts +5 -2
  29. package/dist/data-structures/heap/min-heap.js +5 -2
  30. package/dist/data-structures/index.d.ts +1 -0
  31. package/dist/data-structures/index.js +1 -0
  32. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +4 -4
  34. package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -2
  35. package/dist/data-structures/linked-list/singly-linked-list.js +5 -2
  36. package/dist/data-structures/matrix/matrix.d.ts +5 -2
  37. package/dist/data-structures/matrix/matrix.js +5 -2
  38. package/dist/data-structures/matrix/matrix2d.d.ts +5 -2
  39. package/dist/data-structures/matrix/matrix2d.js +5 -2
  40. package/dist/data-structures/matrix/navigator.d.ts +5 -2
  41. package/dist/data-structures/matrix/vector2d.d.ts +5 -2
  42. package/dist/data-structures/matrix/vector2d.js +5 -2
  43. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +5 -2
  44. package/dist/data-structures/priority-queue/max-priority-queue.js +5 -2
  45. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +5 -2
  46. package/dist/data-structures/priority-queue/min-priority-queue.js +5 -2
  47. package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -4
  48. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  49. package/dist/data-structures/queue/deque.d.ts +12 -9
  50. package/dist/data-structures/queue/deque.js +12 -9
  51. package/dist/data-structures/queue/queue.d.ts +4 -4
  52. package/dist/data-structures/queue/queue.js +4 -4
  53. package/dist/data-structures/stack/stack.d.ts +1 -1
  54. package/dist/data-structures/stack/stack.js +1 -1
  55. package/dist/data-structures/trie/trie.d.ts +6 -3
  56. package/dist/data-structures/trie/trie.js +7 -4
  57. package/dist/utils/index.d.ts +1 -0
  58. package/dist/utils/index.js +1 -0
  59. package/dist/utils/types/utils.d.ts +8 -10
  60. package/dist/utils/types/utils.js +0 -1
  61. package/dist/utils/utils.d.ts +18 -8
  62. package/dist/utils/utils.js +93 -47
  63. package/package.json +3 -3
  64. package/src/assets/logo.png +0 -0
  65. package/src/data-structures/binary-tree/avl-tree.ts +15 -6
  66. package/src/data-structures/binary-tree/binary-indexed-tree.ts +11 -2
  67. package/src/data-structures/binary-tree/binary-tree.ts +70 -58
  68. package/src/data-structures/binary-tree/bst.ts +94 -7
  69. package/src/data-structures/binary-tree/segment-tree.ts +41 -2
  70. package/src/data-structures/binary-tree/tree-multiset.ts +35 -4
  71. package/src/data-structures/graph/abstract-graph.ts +12 -4
  72. package/src/data-structures/graph/directed-graph.ts +26 -39
  73. package/src/data-structures/graph/undirected-graph.ts +18 -2
  74. package/src/data-structures/hash/coordinate-map.ts +5 -2
  75. package/src/data-structures/hash/coordinate-set.ts +5 -2
  76. package/src/data-structures/heap/heap.ts +13 -10
  77. package/src/data-structures/heap/max-heap.ts +5 -2
  78. package/src/data-structures/heap/min-heap.ts +5 -2
  79. package/src/data-structures/index.ts +2 -0
  80. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -6
  81. package/src/data-structures/linked-list/singly-linked-list.ts +5 -2
  82. package/src/data-structures/matrix/matrix.ts +5 -2
  83. package/src/data-structures/matrix/matrix2d.ts +5 -2
  84. package/src/data-structures/matrix/navigator.ts +5 -2
  85. package/src/data-structures/matrix/vector2d.ts +5 -2
  86. package/src/data-structures/priority-queue/max-priority-queue.ts +5 -2
  87. package/src/data-structures/priority-queue/min-priority-queue.ts +5 -2
  88. package/src/data-structures/priority-queue/priority-queue.ts +7 -4
  89. package/src/data-structures/queue/deque.ts +12 -9
  90. package/src/data-structures/queue/queue.ts +4 -4
  91. package/src/data-structures/stack/stack.ts +1 -1
  92. package/src/data-structures/trie/trie.ts +7 -4
  93. package/src/utils/index.ts +2 -1
  94. package/src/utils/types/utils.ts +10 -12
  95. package/src/utils/utils.ts +57 -11
  96. package/tests/unit/data-structures/binary-tree/bst.test.ts +1 -1
  97. package/tests/unit/data-structures/graph/directed-graph.test.ts +1 -0
  98. package/dist/utils/trampoline.d.ts +0 -14
  99. package/dist/utils/trampoline.js +0 -130
  100. package/docs/.nojekyll +0 -1
  101. package/docs/assets/highlight.css +0 -85
  102. package/docs/assets/main.js +0 -58
  103. package/docs/assets/search.js +0 -1
  104. package/docs/assets/style.css +0 -1367
  105. package/docs/classes/AVLTree.html +0 -2046
  106. package/docs/classes/AVLTreeNode.html +0 -423
  107. package/docs/classes/AaTree.html +0 -117
  108. package/docs/classes/AbstractEdge.html +0 -198
  109. package/docs/classes/AbstractGraph.html +0 -891
  110. package/docs/classes/AbstractVertex.html +0 -164
  111. package/docs/classes/ArrayDeque.html +0 -384
  112. package/docs/classes/BST.html +0 -1893
  113. package/docs/classes/BSTNode.html +0 -425
  114. package/docs/classes/BTree.html +0 -117
  115. package/docs/classes/BinaryIndexedTree.html +0 -244
  116. package/docs/classes/BinaryTree.html +0 -1754
  117. package/docs/classes/BinaryTreeNode.html +0 -396
  118. package/docs/classes/Character.html +0 -165
  119. package/docs/classes/CoordinateMap.html +0 -394
  120. package/docs/classes/CoordinateSet.html +0 -355
  121. package/docs/classes/Deque.html +0 -617
  122. package/docs/classes/DirectedEdge.html +0 -247
  123. package/docs/classes/DirectedGraph.html +0 -1207
  124. package/docs/classes/DirectedVertex.html +0 -154
  125. package/docs/classes/DoublyLinkedList.html +0 -619
  126. package/docs/classes/DoublyLinkedListNode.html +0 -160
  127. package/docs/classes/Heap.html +0 -315
  128. package/docs/classes/Matrix2D.html +0 -447
  129. package/docs/classes/MatrixNTI2D.html +0 -181
  130. package/docs/classes/MaxHeap.html +0 -325
  131. package/docs/classes/MaxPriorityQueue.html +0 -668
  132. package/docs/classes/MinHeap.html +0 -326
  133. package/docs/classes/MinPriorityQueue.html +0 -668
  134. package/docs/classes/Navigator.html +0 -285
  135. package/docs/classes/ObjectDeque.html +0 -289
  136. package/docs/classes/PriorityQueue.html +0 -643
  137. package/docs/classes/Queue.html +0 -337
  138. package/docs/classes/RBTree.html +0 -117
  139. package/docs/classes/SegmentTree.html +0 -234
  140. package/docs/classes/SegmentTreeNode.html +0 -302
  141. package/docs/classes/SinglyLinkedList.html +0 -1035
  142. package/docs/classes/SinglyLinkedListNode.html +0 -304
  143. package/docs/classes/SplayTree.html +0 -117
  144. package/docs/classes/Stack.html +0 -313
  145. package/docs/classes/TreeMultiSet.html +0 -1897
  146. package/docs/classes/Trie.html +0 -317
  147. package/docs/classes/TrieNode.html +0 -221
  148. package/docs/classes/TwoThreeTree.html +0 -117
  149. package/docs/classes/UndirectedEdge.html +0 -220
  150. package/docs/classes/UndirectedGraph.html +0 -1006
  151. package/docs/classes/UndirectedVertex.html +0 -154
  152. package/docs/classes/Vector2D.html +0 -746
  153. package/docs/enums/CP.html +0 -126
  154. package/docs/enums/FamilyPosition.html +0 -126
  155. package/docs/enums/LoopType.html +0 -119
  156. package/docs/index.html +0 -288
  157. package/docs/modules.html +0 -146
  158. package/src/utils/trampoline.ts +0 -51
@@ -1,18 +1,35 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {arrayRemove} from '../../utils';
6
9
  import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
7
10
  import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types';
8
11
 
9
12
  export class DirectedVertex extends AbstractVertex {
13
+ /**
14
+ * The constructor function initializes an object with a given id.
15
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
16
+ * vertex within a graph or network.
17
+ */
10
18
  constructor(id: VertexId) {
11
19
  super(id);
12
20
  }
13
21
  }
14
22
 
15
23
  export class DirectedEdge extends AbstractEdge {
24
+ /**
25
+ * The constructor function initializes the source and destination vertices of an edge, with an optional weight.
26
+ * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
27
+ * a graph.
28
+ * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
29
+ * to which an edge is directed.
30
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
31
+ * between two vertices.
32
+ */
16
33
  constructor(src: VertexId, dest: VertexId, weight?: number) {
17
34
  super(weight);
18
35
  this._src = src;
@@ -170,7 +187,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
170
187
 
171
188
  const destInEdges = this._inEdgeMap.get(dest);
172
189
  if (destInEdges && destInEdges.length > 0) {
173
- removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.dest === dest.id)[0];
190
+ removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.id)[0];
174
191
  }
175
192
 
176
193
  }
@@ -299,41 +316,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
299
316
  /**
300
317
  * when stored with adjacency list time: O(V+E)
301
318
  * when stored with adjacency matrix time: O(V^2)
302
- * The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
303
- * reverse order, or null if the graph contains a cycle.
304
- * @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
305
- * the graph. If there is a cycle in the graph, it returns `null`.
319
+ * The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
320
+ * order, or null if the graph contains a cycle.
321
+ * @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
322
+ * cycle in the graph. If there is a cycle, it returns `null`.
306
323
  */
307
324
  topologicalSort(): V[] | null {
308
- // vector<vector<int>> g;
309
- // vector<int> color;
310
- // int last;
311
- // bool hasCycle;
312
- //
313
- // bool topo_sort() {
314
- // int n = g.size();
315
- // vector<int> degree(n, 0);
316
- // queue<int> q;
317
- // for (int i = 0; i < n; i++) {
318
- // degree[i] = g[i].size();
319
- // if (degree[i] <= 1) {
320
- // q.push(i);
321
- // }
322
- // }
323
- // int cnt = 0;
324
- // while (!q.empty()) {
325
- // cnt++;
326
- // int root = q.front();
327
- // q.pop();
328
- // for (auto child : g[root]) {
329
- // degree[child]--;
330
- // if (degree[child] == 1) {
331
- // q.push(child);
332
- // }
333
- // }
334
- // }
335
- // return (cnt != n);
336
- // }
337
325
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
338
326
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
339
327
  const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
@@ -364,9 +352,8 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
364
352
  }
365
353
  }
366
354
 
367
- if (hasCycle) {
368
- return null;
369
- }
355
+ if (hasCycle) return null;
356
+
370
357
  return sorted.reverse();
371
358
  }
372
359
 
@@ -1,18 +1,34 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {arrayRemove} from '../../utils';
6
9
  import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
7
10
  import type {VertexId} from '../types';
8
11
 
9
12
  export class UndirectedVertex extends AbstractVertex {
13
+ /**
14
+ * The constructor function initializes an object with a given id.
15
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
16
+ * vertex within a graph or network.
17
+ */
10
18
  constructor(id: VertexId) {
11
19
  super(id);
12
20
  }
13
21
  }
14
22
 
15
23
  export class UndirectedEdge extends AbstractEdge {
24
+ /**
25
+ * The constructor function initializes an instance of a class with two vertex IDs and an optional weight.
26
+ * @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
27
+ * @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
28
+ * graph.
29
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
30
+ * between two vertices.
31
+ */
16
32
  constructor(v1: VertexId, v2: VertexId, weight?: number) {
17
33
  super(weight);
18
34
  this._vertices = [v1, v2];
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  export class CoordinateMap<V> extends Map<any, V> {
6
9
  private readonly _joint: string = '_';
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  export class CoordinateSet extends Set {
6
9
  private readonly _joint: string = '_';
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {PriorityQueue} from '../priority-queue';
6
9
  import type {HeapItem, HeapOptions} from '../types';
@@ -59,37 +62,37 @@ export abstract class Heap<T> {
59
62
  }
60
63
 
61
64
  /**
62
- * The `offer` function adds an element to a priority queue with an optional priority value.
65
+ * The `add` function adds an element to a priority queue with an optional priority value.
63
66
  * @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
64
67
  * type.
65
68
  * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
66
- * element being offered to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
69
+ * element being added to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
67
70
  * the value of `element`. If the `element` parameter is not a number, then the
68
- * @returns The `offer` method returns the instance of the `Heap` class.
71
+ * @returns The `add` method returns the instance of the `Heap` class.
69
72
  * @throws {Error} if priority is not a valid number
70
73
  */
71
- offer(element: T, priority?: number): Heap<T> {
74
+ add(element: T, priority?: number): Heap<T> {
72
75
  if (typeof element === 'number') {
73
76
  priority = element;
74
77
  } else {
75
78
  if (priority === undefined) {
76
- throw new Error('.offer expects a numeric priority');
79
+ throw new Error('.add expects a numeric priority');
77
80
  }
78
81
  }
79
82
 
80
83
  if (priority && Number.isNaN(+priority)) {
81
- throw new Error('.offer expects a numeric priority');
84
+ throw new Error('.add expects a numeric priority');
82
85
  }
83
86
 
84
87
  if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
85
88
  throw new Error(
86
- '.offer expects a numeric priority '
89
+ '.add expects a numeric priority '
87
90
  + 'or a constructor callback that returns a number'
88
91
  );
89
92
  }
90
93
 
91
94
  const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
92
- this._pq.offer({priority: _priority, element});
95
+ this._pq.add({priority: _priority, element});
93
96
  return this;
94
97
  }
95
98
 
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
 
6
9
  import {Heap} from './heap';
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
 
6
9
  import {Heap} from './heap';
@@ -8,4 +8,6 @@ export * from './heap';
8
8
  export * from './priority-queue';
9
9
  export * from './matrix';
10
10
  export * from './trie';
11
+ export * from './types';
12
+
11
13
 
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import type {DoublyLinkedListGetBy} from '../types';
6
9
 
@@ -34,7 +37,7 @@ export class DoublyLinkedList<T> {
34
37
  * the doubly linked list.
35
38
  * @returns A boolean value is being returned.
36
39
  */
37
- offerFirst(val: T): boolean {
40
+ addFirst(val: T): boolean {
38
41
  const newNode = new DoublyLinkedListNode(val);
39
42
  if (this._size === 0) {
40
43
  this._first = newNode;
@@ -54,7 +57,7 @@ export class DoublyLinkedList<T> {
54
57
  * doubly linked list.
55
58
  * @returns a boolean value, which is always true.
56
59
  */
57
- offerLast(val: T): boolean {
60
+ addLast(val: T): boolean {
58
61
  const newNode = new DoublyLinkedListNode(val);
59
62
  if (this._size === 0) {
60
63
  this._first = newNode;
@@ -247,8 +250,8 @@ export class DoublyLinkedList<T> {
247
250
  */
248
251
  insert(index: number, val: T): boolean {
249
252
  if (index < 0 || index > this._size) return false;
250
- if (index === 0) return !!this.offerFirst(val);
251
- if (index === this._size) return !!this.offerLast(val);
253
+ if (index === 0) return !!this.addFirst(val);
254
+ if (index === this._size) return !!this.addLast(val);
252
255
 
253
256
  const newNode = new DoublyLinkedListNode(val);
254
257
  const prevNode = this.get(index - 1, 'node');
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
 
6
9
  /**
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  // todo need to be improved
6
9
  export class MatrixNTI2D<T = number> {
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import Vector2D from './vector2d'
6
9
 
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import type {Direction, NavigatorParams, Turning} from '../types';
6
9
 
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  export class Vector2D {
6
9
  constructor(
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {PriorityQueue} from './priority-queue';
6
9
  import type {PriorityQueueOptions} from '../types';
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {PriorityQueue} from './priority-queue';
6
9
  import type {PriorityQueueOptions} from '../types';
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
6
9
 
@@ -52,11 +55,11 @@ export class PriorityQueue<T = number> {
52
55
  }
53
56
 
54
57
  /**
55
- * The "offer" function adds a node to the heap and ensures that the heap property is maintained.
58
+ * The "add" function adds a node to the heap and ensures that the heap property is maintained.
56
59
  * @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
57
60
  * that needs to be added to the heap.
58
61
  */
59
- offer(node: T) {
62
+ add(node: T) {
60
63
  this.nodes.push(node);
61
64
  this._heapifyUp(this.size - 1);
62
65
  }
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {DoublyLinkedList} from '../linked-list';
6
9
 
@@ -28,7 +31,7 @@ export class ObjectDeque<T> {
28
31
  return this._size;
29
32
  }
30
33
 
31
- offerFirst(value: T) {
34
+ addFirst(value: T) {
32
35
  if (this._size === 0) {
33
36
  const mid = Math.floor(this._capacity / 2);
34
37
  this._first = mid;
@@ -40,7 +43,7 @@ export class ObjectDeque<T> {
40
43
  this._size++;
41
44
  }
42
45
 
43
- offerLast(value: T) {
46
+ addLast(value: T) {
44
47
  if (this._size === 0) {
45
48
  const mid = Math.floor(this._capacity / 2);
46
49
  this._first = mid;
@@ -98,11 +101,11 @@ export class ArrayDeque<T> {
98
101
  }
99
102
 
100
103
  /**
101
- * The function "offerLast" adds a value to the end of an array.
104
+ * The function "addLast" adds a value to the end of an array.
102
105
  * @param {T} value - The value parameter represents the value that you want to add to the end of the array.
103
106
  * @returns The return value is the new length of the array after the value has been added.
104
107
  */
105
- offerLast(value: T) {
108
+ addLast(value: T) {
106
109
  return this._nodes.push(value);
107
110
  }
108
111
 
@@ -124,12 +127,12 @@ export class ArrayDeque<T> {
124
127
  }
125
128
 
126
129
  /**
127
- * The function "offerFirst" adds a value to the beginning of an array.
130
+ * The function "addFirst" adds a value to the beginning of an array.
128
131
  * @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
129
- * @returns The return value of the `offerFirst` function is the new length of the array `_nodes` after adding the
132
+ * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
130
133
  * `value` at the beginning.
131
134
  */
132
- offerFirst(value: T) {
135
+ addFirst(value: T) {
133
136
  return this._nodes.unshift(value);
134
137
  }
135
138
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
6
  export class Queue<T> {
@@ -31,11 +31,11 @@ export class Queue<T> {
31
31
  }
32
32
 
33
33
  /**
34
- * The offer function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
34
+ * The add function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
35
35
  * @param {T} element - The `element` parameter represents the element that you want to add to the queue.
36
- * @returns The `offer` method is returning a `Queue<T>` object.
36
+ * @returns The `add` method is returning a `Queue<T>` object.
37
37
  */
38
- offer(element: T): Queue<T> {
38
+ add(element: T): Queue<T> {
39
39
  this._nodes.push(element);
40
40
  return this;
41
41
  }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
6
  export class Stack<T> {
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  export class TrieNode {
6
9
  protected _value;
@@ -45,7 +48,7 @@ export class Trie {
45
48
  this._root = new TrieNode('');
46
49
  if (words) {
47
50
  for (const i of words) {
48
- this.put(i);
51
+ this.add(i);
49
52
  }
50
53
  }
51
54
  }
@@ -60,7 +63,7 @@ export class Trie {
60
63
  this._root = v;
61
64
  }
62
65
 
63
- put(word: string): boolean {
66
+ add(word: string): boolean {
64
67
  let cur = this._root;
65
68
  for (const c of word) {
66
69
  let nodeC = cur.children.get(c);
@@ -1 +1,2 @@
1
- export * from './utils';
1
+ export * from './utils';
2
+ export * from './types';
@@ -1,3 +1,12 @@
1
+ export type JSONSerializable = {
2
+ [key: string]: any
3
+ }
4
+ export type JSONValue = string | number | boolean | undefined | JSONObject;
5
+
6
+ export interface JSONObject {
7
+ [key: string]: JSONValue;
8
+ }
9
+
1
10
  export type AnyFunction<A extends any[] = any[], R = any> = (...args: A) => R;
2
11
  export type Primitive =
3
12
  | number
@@ -32,16 +41,6 @@ export type DeepLeavesWrap<T, TComplex> =
32
41
 
33
42
  type Json = null | string | number | boolean | Json [] | { [name: string]: Json }
34
43
 
35
- export type JSONSerializable = {
36
- [key: string]: any
37
- }
38
-
39
- export type JSONValue = string | number | boolean | undefined | JSONObject;
40
-
41
- export interface JSONObject {
42
- [key: string]: JSONValue;
43
- }
44
-
45
44
  export type TypeName<T> = T extends string
46
45
  ? 'string'
47
46
  : T extends number
@@ -173,8 +172,7 @@ export type CurryFunc<T> = T extends (...args: infer Args) => infer R
173
172
 
174
173
 
175
174
  export type ToThunkFn = () => ReturnType<TrlFn>;
176
- const THUNK_SYMBOL = Symbol('thunk')
177
- export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: typeof THUNK_SYMBOL };
175
+ export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: Symbol };
178
176
  export type TrlFn = (...args: any[]) => any;
179
177
  export type TrlAsyncFn = (...args: any[]) => any;
180
178
 
@@ -1,14 +1,5 @@
1
- import * as _ from 'lodash';
2
- import {AnyFunction} from './types';
3
-
4
- export type JSONSerializable = {
5
- [key: string]: any
6
- }
7
- export type JSONValue = string | number | boolean | undefined | JSONObject;
8
-
9
- export interface JSONObject {
10
- [key: string]: JSONValue;
11
- }
1
+ import _ from 'lodash';
2
+ import type {AnyFunction, JSONObject, JSONSerializable} from './types';
12
3
 
13
4
  export function randomText(length: number) {
14
5
  let result = '';
@@ -512,4 +503,59 @@ export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?
512
503
  }
513
504
  }
514
505
  return isToObj ? zippedObjCoords : zipped;
506
+ }
507
+
508
+ /**
509
+ * data-structure-typed
510
+ *
511
+ * @author Tyler Zeng
512
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
513
+ * @license MIT License
514
+ */
515
+ import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
516
+
517
+ export const THUNK_SYMBOL = Symbol('thunk')
518
+
519
+ export const isThunk = (fnOrValue: any) => {
520
+ return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
521
+ }
522
+
523
+ export const toThunk = (fn: ToThunkFn): Thunk => {
524
+ const thunk = () => fn()
525
+ thunk.__THUNK__ = THUNK_SYMBOL
526
+ return thunk
527
+ }
528
+
529
+ export const trampoline = (fn: TrlFn) => {
530
+ const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
531
+
532
+ return Object.assign(
533
+ (...args: [...Parameters<TrlFn>]) => {
534
+ let result = fn(...args)
535
+
536
+ while (isThunk(result) && typeof result === 'function') {
537
+ result = result()
538
+ }
539
+
540
+ return result
541
+ },
542
+ {cont}
543
+ )
544
+ }
545
+
546
+ export const trampolineAsync = (fn: TrlAsyncFn) => {
547
+ const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
548
+
549
+ return Object.assign(
550
+ async (...args: [...Parameters<TrlAsyncFn>]) => {
551
+ let result = await fn(...args)
552
+
553
+ while (isThunk(result) && typeof result === 'function') {
554
+ result = await result()
555
+ }
556
+
557
+ return result
558
+ },
559
+ {cont}
560
+ )
515
561
  }