data-structure-typed 1.19.3 → 1.19.4

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 (155) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -90
  4. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  6. package/dist/data-structures/binary-tree/binary-tree.js +9 -31
  7. package/dist/data-structures/binary-tree/bst.js +96 -139
  8. package/dist/data-structures/binary-tree/rb-tree.js +32 -56
  9. package/dist/data-structures/binary-tree/segment-tree.js +78 -120
  10. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
  12. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  13. package/dist/data-structures/graph/abstract-graph.js +340 -574
  14. package/dist/data-structures/graph/directed-graph.js +146 -276
  15. package/dist/data-structures/graph/undirected-graph.js +87 -176
  16. package/dist/data-structures/hash/coordinate-map.js +23 -45
  17. package/dist/data-structures/hash/coordinate-set.js +20 -42
  18. package/dist/data-structures/hash/hash-table.js +2 -5
  19. package/dist/data-structures/hash/pair.js +2 -5
  20. package/dist/data-structures/hash/tree-map.js +2 -5
  21. package/dist/data-structures/hash/tree-set.js +2 -5
  22. package/dist/data-structures/heap/heap.js +53 -77
  23. package/dist/data-structures/heap/max-heap.js +8 -26
  24. package/dist/data-structures/heap/min-heap.js +8 -26
  25. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
  26. package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
  27. package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
  28. package/dist/data-structures/matrix/matrix.js +7 -8
  29. package/dist/data-structures/matrix/matrix2d.js +76 -93
  30. package/dist/data-structures/matrix/navigator.js +18 -37
  31. package/dist/data-structures/matrix/vector2d.js +80 -101
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
  33. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
  34. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  35. package/dist/data-structures/queue/deque.js +82 -128
  36. package/dist/data-structures/queue/queue.js +24 -25
  37. package/dist/data-structures/stack/stack.js +21 -22
  38. package/dist/data-structures/tree/tree.js +32 -45
  39. package/dist/data-structures/trie/trie.js +93 -200
  40. package/dist/utils/utils.js +22 -107
  41. package/dist/utils/validate-type.js +2 -2
  42. package/package.json +1 -1
  43. package/src/assets/complexities-diff.jpg +0 -0
  44. package/src/assets/data-structure-complexities.jpg +0 -0
  45. package/src/assets/logo.png +0 -0
  46. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  47. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  48. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
  49. package/src/data-structures/binary-tree/avl-tree.ts +297 -0
  50. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  51. package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  52. package/src/data-structures/binary-tree/binary-tree.ts +40 -0
  53. package/src/data-structures/binary-tree/bst.ts +435 -0
  54. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  55. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  56. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  57. package/src/data-structures/binary-tree/index.ts +12 -0
  58. package/src/data-structures/binary-tree/rb-tree.ts +102 -0
  59. package/src/data-structures/binary-tree/segment-tree.ts +243 -0
  60. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  61. package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
  62. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  63. package/src/data-structures/diagrams/README.md +5 -0
  64. package/src/data-structures/graph/abstract-graph.ts +1032 -0
  65. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  66. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  67. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  68. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  69. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  70. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  71. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  72. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  73. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  74. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  75. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  76. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  77. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  78. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  79. package/src/data-structures/graph/directed-graph.ts +472 -0
  80. package/src/data-structures/graph/index.ts +3 -0
  81. package/src/data-structures/graph/undirected-graph.ts +270 -0
  82. package/src/data-structures/hash/coordinate-map.ts +67 -0
  83. package/src/data-structures/hash/coordinate-set.ts +56 -0
  84. package/src/data-structures/hash/hash-table.ts +3 -0
  85. package/src/data-structures/hash/index.ts +6 -0
  86. package/src/data-structures/hash/pair.ts +3 -0
  87. package/src/data-structures/hash/tree-map.ts +3 -0
  88. package/src/data-structures/hash/tree-set.ts +3 -0
  89. package/src/data-structures/heap/heap.ts +183 -0
  90. package/src/data-structures/heap/index.ts +3 -0
  91. package/src/data-structures/heap/max-heap.ts +31 -0
  92. package/src/data-structures/heap/min-heap.ts +34 -0
  93. package/src/data-structures/index.ts +15 -0
  94. package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
  95. package/src/data-structures/interfaces/abstract-graph.ts +40 -0
  96. package/src/data-structures/interfaces/avl-tree.ts +28 -0
  97. package/src/data-structures/interfaces/binary-tree.ts +8 -0
  98. package/src/data-structures/interfaces/bst.ts +32 -0
  99. package/src/data-structures/interfaces/directed-graph.ts +20 -0
  100. package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  101. package/src/data-structures/interfaces/heap.ts +1 -0
  102. package/src/data-structures/interfaces/index.ts +15 -0
  103. package/src/data-structures/interfaces/navigator.ts +1 -0
  104. package/src/data-structures/interfaces/priority-queue.ts +1 -0
  105. package/src/data-structures/interfaces/rb-tree.ts +11 -0
  106. package/src/data-structures/interfaces/segment-tree.ts +1 -0
  107. package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  108. package/src/data-structures/interfaces/tree-multiset.ts +12 -0
  109. package/src/data-structures/interfaces/undirected-graph.ts +6 -0
  110. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  111. package/src/data-structures/linked-list/index.ts +3 -0
  112. package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  113. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  114. package/src/data-structures/matrix/index.ts +4 -0
  115. package/src/data-structures/matrix/matrix.ts +27 -0
  116. package/src/data-structures/matrix/matrix2d.ts +208 -0
  117. package/src/data-structures/matrix/navigator.ts +122 -0
  118. package/src/data-structures/matrix/vector2d.ts +316 -0
  119. package/src/data-structures/priority-queue/index.ts +3 -0
  120. package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  121. package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  122. package/src/data-structures/priority-queue/priority-queue.ts +354 -0
  123. package/src/data-structures/queue/deque.ts +251 -0
  124. package/src/data-structures/queue/index.ts +2 -0
  125. package/src/data-structures/queue/queue.ts +120 -0
  126. package/src/data-structures/stack/index.ts +1 -0
  127. package/src/data-structures/stack/stack.ts +98 -0
  128. package/src/data-structures/tree/index.ts +1 -0
  129. package/src/data-structures/tree/tree.ts +69 -0
  130. package/src/data-structures/trie/index.ts +1 -0
  131. package/src/data-structures/trie/trie.ts +227 -0
  132. package/src/data-structures/types/abstract-binary-tree.ts +42 -0
  133. package/src/data-structures/types/abstract-graph.ts +5 -0
  134. package/src/data-structures/types/avl-tree.ts +5 -0
  135. package/src/data-structures/types/binary-tree.ts +9 -0
  136. package/src/data-structures/types/bst.ts +12 -0
  137. package/src/data-structures/types/directed-graph.ts +8 -0
  138. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  139. package/src/data-structures/types/heap.ts +5 -0
  140. package/src/data-structures/types/helpers.ts +1 -0
  141. package/src/data-structures/types/index.ts +15 -0
  142. package/src/data-structures/types/navigator.ts +13 -0
  143. package/src/data-structures/types/priority-queue.ts +9 -0
  144. package/src/data-structures/types/rb-tree.ts +8 -0
  145. package/src/data-structures/types/segment-tree.ts +1 -0
  146. package/src/data-structures/types/singly-linked-list.ts +1 -0
  147. package/src/data-structures/types/tree-multiset.ts +8 -0
  148. package/src/index.ts +2 -0
  149. package/src/utils/index.ts +3 -0
  150. package/src/utils/types/index.ts +2 -0
  151. package/src/utils/types/utils.ts +6 -0
  152. package/src/utils/types/validate-type.ts +25 -0
  153. package/src/utils/utils.ts +78 -0
  154. package/src/utils/validate-type.ts +69 -0
  155. package/tsconfig.json +1 -1
@@ -1,19 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  Object.defineProperty(exports, "__esModule", { value: true });
18
3
  exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
19
4
  /**
@@ -23,22 +8,17 @@ exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
23
8
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
24
9
  * @license MIT License
25
10
  */
26
- var linked_list_1 = require("../linked-list");
11
+ const linked_list_1 = require("../linked-list");
27
12
  // O(n) time complexity of obtaining the value
28
13
  // O(1) time complexity of adding at the beginning and the end
29
- var Deque = /** @class */ (function (_super) {
30
- __extends(Deque, _super);
31
- function Deque() {
32
- return _super !== null && _super.apply(this, arguments) || this;
33
- }
34
- return Deque;
35
- }(linked_list_1.DoublyLinkedList));
14
+ class Deque extends linked_list_1.DoublyLinkedList {
15
+ }
36
16
  exports.Deque = Deque;
37
17
  // O(1) time complexity of obtaining the value
38
18
  // O(n) time complexity of adding at the beginning and the end
39
19
  // todo tested slowest one
40
- var ObjectDeque = /** @class */ (function () {
41
- function ObjectDeque(capacity) {
20
+ class ObjectDeque {
21
+ constructor(capacity) {
42
22
  this._nodes = {};
43
23
  this._capacity = Number.MAX_SAFE_INTEGER;
44
24
  this._first = -1;
@@ -47,53 +27,33 @@ var ObjectDeque = /** @class */ (function () {
47
27
  if (capacity !== undefined)
48
28
  this._capacity = capacity;
49
29
  }
50
- Object.defineProperty(ObjectDeque.prototype, "nodes", {
51
- get: function () {
52
- return this._nodes;
53
- },
54
- enumerable: false,
55
- configurable: true
56
- });
57
- Object.defineProperty(ObjectDeque.prototype, "capacity", {
58
- get: function () {
59
- return this._capacity;
60
- },
61
- set: function (value) {
62
- this._capacity = value;
63
- },
64
- enumerable: false,
65
- configurable: true
66
- });
67
- Object.defineProperty(ObjectDeque.prototype, "first", {
68
- get: function () {
69
- return this._first;
70
- },
71
- set: function (value) {
72
- this._first = value;
73
- },
74
- enumerable: false,
75
- configurable: true
76
- });
77
- Object.defineProperty(ObjectDeque.prototype, "last", {
78
- get: function () {
79
- return this._last;
80
- },
81
- set: function (value) {
82
- this._last = value;
83
- },
84
- enumerable: false,
85
- configurable: true
86
- });
87
- Object.defineProperty(ObjectDeque.prototype, "size", {
88
- get: function () {
89
- return this._size;
90
- },
91
- enumerable: false,
92
- configurable: true
93
- });
94
- ObjectDeque.prototype.addFirst = function (value) {
30
+ get nodes() {
31
+ return this._nodes;
32
+ }
33
+ get capacity() {
34
+ return this._capacity;
35
+ }
36
+ set capacity(value) {
37
+ this._capacity = value;
38
+ }
39
+ get first() {
40
+ return this._first;
41
+ }
42
+ set first(value) {
43
+ this._first = value;
44
+ }
45
+ get last() {
46
+ return this._last;
47
+ }
48
+ set last(value) {
49
+ this._last = value;
50
+ }
51
+ get size() {
52
+ return this._size;
53
+ }
54
+ addFirst(value) {
95
55
  if (this._size === 0) {
96
- var mid = Math.floor(this._capacity / 2);
56
+ const mid = Math.floor(this._capacity / 2);
97
57
  this._first = mid;
98
58
  this._last = mid;
99
59
  }
@@ -102,10 +62,10 @@ var ObjectDeque = /** @class */ (function () {
102
62
  }
103
63
  this._nodes[this._first] = value;
104
64
  this._size++;
105
- };
106
- ObjectDeque.prototype.addLast = function (value) {
65
+ }
66
+ addLast(value) {
107
67
  if (this._size === 0) {
108
- var mid = Math.floor(this._capacity / 2);
68
+ const mid = Math.floor(this._capacity / 2);
109
69
  this._first = mid;
110
70
  this._last = mid;
111
71
  }
@@ -114,112 +74,107 @@ var ObjectDeque = /** @class */ (function () {
114
74
  }
115
75
  this._nodes[this._last] = value;
116
76
  this._size++;
117
- };
118
- ObjectDeque.prototype.pollFirst = function () {
77
+ }
78
+ pollFirst() {
119
79
  if (!this._size)
120
80
  return;
121
- var value = this.peekFirst();
81
+ const value = this.peekFirst();
122
82
  delete this._nodes[this._first];
123
83
  this._first++;
124
84
  this._size--;
125
85
  return value;
126
- };
127
- ObjectDeque.prototype.peekFirst = function () {
86
+ }
87
+ peekFirst() {
128
88
  if (this._size)
129
89
  return this._nodes[this._first];
130
- };
131
- ObjectDeque.prototype.pollLast = function () {
90
+ }
91
+ pollLast() {
132
92
  if (!this._size)
133
93
  return;
134
- var value = this.peekLast();
94
+ const value = this.peekLast();
135
95
  delete this._nodes[this._last];
136
96
  this._last--;
137
97
  this._size--;
138
98
  return value;
139
- };
140
- ObjectDeque.prototype.peekLast = function () {
99
+ }
100
+ peekLast() {
141
101
  if (this._size)
142
102
  return this._nodes[this._last];
143
- };
144
- ObjectDeque.prototype.get = function (index) {
103
+ }
104
+ get(index) {
145
105
  return this._nodes[this._first + index] || null;
146
- };
147
- ObjectDeque.prototype.isEmpty = function () {
106
+ }
107
+ isEmpty() {
148
108
  return this._size <= 0;
149
- };
150
- ObjectDeque.prototype._seNodes = function (value) {
109
+ }
110
+ _seNodes(value) {
151
111
  this._nodes = value;
152
- };
153
- ObjectDeque.prototype._setSize = function (value) {
112
+ }
113
+ _setSize(value) {
154
114
  this._size = value;
155
- };
156
- return ObjectDeque;
157
- }());
115
+ }
116
+ }
158
117
  exports.ObjectDeque = ObjectDeque;
159
118
  // O(1) time complexity of obtaining the value
160
119
  // O(n) time complexity of adding at the beginning and the end
161
- var ArrayDeque = /** @class */ (function () {
162
- function ArrayDeque() {
120
+ class ArrayDeque {
121
+ constructor() {
163
122
  this._nodes = [];
164
123
  }
165
- Object.defineProperty(ArrayDeque.prototype, "size", {
166
- get: function () {
167
- return this._nodes.length;
168
- },
169
- enumerable: false,
170
- configurable: true
171
- });
124
+ get size() {
125
+ return this._nodes.length;
126
+ }
172
127
  /**
173
128
  * The function "addLast" adds a value to the end of an array.
174
129
  * @param {T} value - The value parameter represents the value that you want to add to the end of the array.
175
130
  * @returns The return value is the new length of the array after the value has been added.
176
131
  */
177
- ArrayDeque.prototype.addLast = function (value) {
132
+ addLast(value) {
178
133
  return this._nodes.push(value);
179
- };
134
+ }
180
135
  /**
181
136
  * The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
182
137
  * @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
183
138
  */
184
- ArrayDeque.prototype.pollLast = function () {
139
+ pollLast() {
185
140
  var _a;
186
141
  return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
187
- };
142
+ }
188
143
  /**
189
144
  * The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
190
145
  * @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
191
146
  * empty.
192
147
  */
193
- ArrayDeque.prototype.pollFirst = function () {
148
+ pollFirst() {
194
149
  var _a;
195
150
  return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
196
- };
151
+ }
197
152
  /**
198
153
  * The function "addFirst" adds a value to the beginning of an array.
199
154
  * @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
200
155
  * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
201
156
  * `value` at the beginning.
202
157
  */
203
- ArrayDeque.prototype.addFirst = function (value) {
158
+ addFirst(value) {
204
159
  return this._nodes.unshift(value);
205
- };
160
+ }
206
161
  /**
207
162
  * The `peekFirst` function returns the first element of an array or null if the array is empty.
208
163
  * @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
209
164
  * empty, it will return `null`.
210
165
  */
211
- ArrayDeque.prototype.peekFirst = function () {
166
+ peekFirst() {
212
167
  var _a;
213
168
  return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
214
- };
169
+ }
215
170
  /**
216
171
  * The `peekLast` function returns the last element of an array or null if the array is empty.
217
172
  * @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
218
173
  */
219
- ArrayDeque.prototype.peekLast = function () {
174
+ peekLast() {
220
175
  var _a;
221
176
  return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
222
- };
177
+ }
223
178
  /**
224
179
  * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
225
180
  * @param {number} index - The index parameter is a number that represents the position of the element you want to
@@ -227,10 +182,10 @@ var ArrayDeque = /** @class */ (function () {
227
182
  * @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
228
183
  * will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
229
184
  */
230
- ArrayDeque.prototype.get = function (index) {
185
+ get(index) {
231
186
  var _a;
232
187
  return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
233
- };
188
+ }
234
189
  /**
235
190
  * The set function assigns a value to a specific index in an array.
236
191
  * @param {number} index - The index parameter is a number that represents the position of the element in the array
@@ -239,9 +194,9 @@ var ArrayDeque = /** @class */ (function () {
239
194
  * _nodes array.
240
195
  * @returns The value that is being set at the specified index in the `_nodes` array.
241
196
  */
242
- ArrayDeque.prototype.set = function (index, value) {
197
+ set(index, value) {
243
198
  return this._nodes[index] = value;
244
- };
199
+ }
245
200
  /**
246
201
  * The insert function adds a value at a specified index in an array.
247
202
  * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
@@ -252,26 +207,25 @@ var ArrayDeque = /** @class */ (function () {
252
207
  * @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
253
208
  * are being removed, an empty array will be returned.
254
209
  */
255
- ArrayDeque.prototype.insert = function (index, value) {
210
+ insert(index, value) {
256
211
  return this._nodes.splice(index, 0, value);
257
- };
212
+ }
258
213
  /**
259
214
  * The remove function removes an element from an array at a specified index.
260
215
  * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
261
216
  * is a number that represents the index of the element to be removed.
262
217
  * @returns The method is returning an array containing the removed element.
263
218
  */
264
- ArrayDeque.prototype.remove = function (index) {
219
+ remove(index) {
265
220
  return this._nodes.splice(index, 1);
266
- };
221
+ }
267
222
  /**
268
223
  * The function checks if an array called "_nodes" is empty.
269
224
  * @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
270
225
  * is 0, indicating that the array is empty. Otherwise, it returns `false`.
271
226
  */
272
- ArrayDeque.prototype.isEmpty = function () {
227
+ isEmpty() {
273
228
  return this._nodes.length === 0;
274
- };
275
- return ArrayDeque;
276
- }());
229
+ }
230
+ }
277
231
  exports.ArrayDeque = ArrayDeque;
@@ -6,14 +6,14 @@ exports.Queue = void 0;
6
6
  * @copyright Tyler Zeng <zrwusa@gmail.com>
7
7
  * @class
8
8
  */
9
- var Queue = /** @class */ (function () {
9
+ class Queue {
10
10
  /**
11
11
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
12
12
  * @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
13
13
  * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
14
14
  * initialized as an empty array.
15
15
  */
16
- function Queue(elements) {
16
+ constructor(elements) {
17
17
  this._nodes = elements || [];
18
18
  this._offset = 0;
19
19
  }
@@ -25,27 +25,27 @@ var Queue = /** @class */ (function () {
25
25
  * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
26
26
  * array.
27
27
  */
28
- Queue.fromArray = function (elements) {
28
+ static fromArray(elements) {
29
29
  return new Queue(elements);
30
- };
30
+ }
31
31
  /**
32
32
  * 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.
33
33
  * @param {T} element - The `element` parameter represents the element that you want to add to the queue.
34
34
  * @returns The `add` method is returning a `Queue<T>` object.
35
35
  */
36
- Queue.prototype.add = function (element) {
36
+ add(element) {
37
37
  this._nodes.push(element);
38
38
  return this;
39
- };
39
+ }
40
40
  /**
41
41
  * The `poll` function removes and returns the first element in the queue, and adjusts the internal data structure if
42
42
  * necessary to optimize performance.
43
43
  * @returns The function `poll()` returns either the first element in the queue or `null` if the queue is empty.
44
44
  */
45
- Queue.prototype.poll = function () {
45
+ poll() {
46
46
  if (this.size() === 0)
47
47
  return null;
48
- var first = this.peek();
48
+ const first = this.peek();
49
49
  this._offset += 1;
50
50
  if (this._offset * 2 < this._nodes.length)
51
51
  return first;
@@ -54,58 +54,57 @@ var Queue = /** @class */ (function () {
54
54
  this._nodes = this._nodes.slice(this._offset);
55
55
  this._offset = 0;
56
56
  return first;
57
- };
57
+ }
58
58
  /**
59
59
  * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
60
60
  * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
61
61
  * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
62
62
  */
63
- Queue.prototype.peek = function () {
63
+ peek() {
64
64
  return this.size() > 0 ? this._nodes[this._offset] : null;
65
- };
65
+ }
66
66
  /**
67
67
  * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
68
68
  * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
69
69
  * array is empty, it returns `null`.
70
70
  */
71
- Queue.prototype.peekLast = function () {
71
+ peekLast() {
72
72
  return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
73
- };
73
+ }
74
74
  /**
75
75
  * The size function returns the number of elements in an array.
76
76
  * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
77
77
  */
78
- Queue.prototype.size = function () {
78
+ size() {
79
79
  return this._nodes.length - this._offset;
80
- };
80
+ }
81
81
  /**
82
82
  * The function checks if a data structure is empty by comparing its size to zero.
83
83
  * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
84
84
  */
85
- Queue.prototype.isEmpty = function () {
85
+ isEmpty() {
86
86
  return this.size() === 0;
87
- };
87
+ }
88
88
  /**
89
89
  * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
90
90
  * @returns An array of type T is being returned.
91
91
  */
92
- Queue.prototype.toArray = function () {
92
+ toArray() {
93
93
  return this._nodes.slice(this._offset);
94
- };
94
+ }
95
95
  /**
96
96
  * The clear function resets the nodes array and offset to their initial values.
97
97
  */
98
- Queue.prototype.clear = function () {
98
+ clear() {
99
99
  this._nodes = [];
100
100
  this._offset = 0;
101
- };
101
+ }
102
102
  /**
103
103
  * The `clone()` function returns a new Queue object with the same elements as the original Queue.
104
104
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
105
105
  */
106
- Queue.prototype.clone = function () {
106
+ clone() {
107
107
  return new Queue(this._nodes.slice(this._offset));
108
- };
109
- return Queue;
110
- }());
108
+ }
109
+ }
111
110
  exports.Queue = Queue;
@@ -6,14 +6,14 @@ exports.Stack = void 0;
6
6
  * @copyright Tyler Zeng <zrwusa@gmail.com>
7
7
  * @class
8
8
  */
9
- var Stack = /** @class */ (function () {
9
+ class Stack {
10
10
  /**
11
11
  * The constructor initializes an array of elements, which can be provided as an optional parameter.
12
12
  * @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
13
13
  * of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
14
14
  * is provided and is an array, it is assigned to the `_elements
15
15
  */
16
- function Stack(elements) {
16
+ constructor(elements) {
17
17
  this._elements = Array.isArray(elements) ? elements : [];
18
18
  }
19
19
  /**
@@ -22,71 +22,70 @@ var Stack = /** @class */ (function () {
22
22
  * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
23
23
  * array.
24
24
  */
25
- Stack.fromArray = function (elements) {
25
+ static fromArray(elements) {
26
26
  return new Stack(elements);
27
- };
27
+ }
28
28
  /**
29
29
  * The function checks if an array is empty and returns a boolean value.
30
30
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
31
31
  */
32
- Stack.prototype.isEmpty = function () {
32
+ isEmpty() {
33
33
  return this._elements.length === 0;
34
- };
34
+ }
35
35
  /**
36
36
  * The size() function returns the number of elements in an array.
37
37
  * @returns The size of the elements array.
38
38
  */
39
- Stack.prototype.size = function () {
39
+ size() {
40
40
  return this._elements.length;
41
- };
41
+ }
42
42
  /**
43
43
  * The `peek` function returns the last element of an array, or null if the array is empty.
44
44
  * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
45
45
  */
46
- Stack.prototype.peek = function () {
46
+ peek() {
47
47
  if (this.isEmpty())
48
48
  return null;
49
49
  return this._elements[this._elements.length - 1];
50
- };
50
+ }
51
51
  /**
52
52
  * The push function adds an element to the stack and returns the updated stack.
53
53
  * @param {T} element - The parameter "element" is of type T, which means it can be any data type.
54
54
  * @returns The `push` method is returning the updated `Stack<T>` object.
55
55
  */
56
- Stack.prototype.push = function (element) {
56
+ push(element) {
57
57
  this._elements.push(element);
58
58
  return this;
59
- };
59
+ }
60
60
  /**
61
61
  * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
62
62
  * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
63
63
  * array is empty, it returns `null`.
64
64
  */
65
- Stack.prototype.pop = function () {
65
+ pop() {
66
66
  if (this.isEmpty())
67
67
  return null;
68
68
  return this._elements.pop() || null;
69
- };
69
+ }
70
70
  /**
71
71
  * The toArray function returns a copy of the elements in an array.
72
72
  * @returns An array of type T.
73
73
  */
74
- Stack.prototype.toArray = function () {
74
+ toArray() {
75
75
  return this._elements.slice();
76
- };
76
+ }
77
77
  /**
78
78
  * The clear function clears the elements array.
79
79
  */
80
- Stack.prototype.clear = function () {
80
+ clear() {
81
81
  this._elements = [];
82
- };
82
+ }
83
83
  /**
84
84
  * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
85
85
  * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
86
86
  */
87
- Stack.prototype.clone = function () {
87
+ clone() {
88
88
  return new Stack(this._elements.slice());
89
- };
90
- return Stack;
91
- }());
89
+ }
90
+ }
92
91
  exports.Stack = Stack;