data-structure-typed 1.35.1 → 1.40.0-rc

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 (163) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
  4. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
  6. package/dist/data-structures/binary-tree/avl-tree.js +90 -3
  7. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
  13. package/dist/data-structures/binary-tree/binary-tree.js +21 -0
  14. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/bst.d.ts +133 -0
  16. package/dist/data-structures/binary-tree/bst.js +114 -0
  17. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  18. package/dist/data-structures/binary-tree/index.d.ts +12 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  20. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  21. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  22. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  23. package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
  25. package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
  26. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  27. package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  28. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  29. package/dist/data-structures/graph/abstract-graph.js +269 -4
  30. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  31. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  32. package/dist/data-structures/graph/directed-graph.js +167 -0
  33. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  34. package/dist/data-structures/graph/index.d.ts +4 -0
  35. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  36. package/dist/data-structures/graph/map-graph.js +54 -0
  37. package/dist/data-structures/graph/map-graph.js.map +1 -1
  38. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  39. package/dist/data-structures/graph/undirected-graph.js +105 -0
  40. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  41. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  42. package/dist/data-structures/hash/coordinate-map.js +35 -0
  43. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  44. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  45. package/dist/data-structures/hash/coordinate-set.js +28 -0
  46. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  47. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  48. package/dist/data-structures/hash/hash-map.js +29 -1
  49. package/dist/data-structures/hash/hash-map.js.map +1 -1
  50. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  51. package/dist/data-structures/hash/hash-table.js +88 -6
  52. package/dist/data-structures/hash/hash-table.js.map +1 -1
  53. package/dist/data-structures/hash/index.d.ts +7 -0
  54. package/dist/data-structures/hash/pair.d.ts +2 -0
  55. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  56. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  57. package/dist/data-structures/heap/heap.d.ts +83 -0
  58. package/dist/data-structures/heap/heap.js +62 -0
  59. package/dist/data-structures/heap/heap.js.map +1 -1
  60. package/dist/data-structures/heap/index.d.ts +3 -0
  61. package/dist/data-structures/heap/max-heap.d.ts +23 -0
  62. package/dist/data-structures/heap/max-heap.js +16 -0
  63. package/dist/data-structures/heap/max-heap.js.map +1 -1
  64. package/dist/data-structures/heap/min-heap.d.ts +24 -0
  65. package/dist/data-structures/heap/min-heap.js +17 -0
  66. package/dist/data-structures/heap/min-heap.js.map +1 -1
  67. package/dist/data-structures/index.d.ts +11 -0
  68. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  69. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
  70. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  71. package/dist/data-structures/linked-list/index.d.ts +3 -0
  72. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  73. package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
  74. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  75. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  76. package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
  77. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  78. package/dist/data-structures/matrix/index.d.ts +4 -0
  79. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  80. package/dist/data-structures/matrix/matrix.js +15 -0
  81. package/dist/data-structures/matrix/matrix.js.map +1 -1
  82. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  83. package/dist/data-structures/matrix/matrix2d.js +91 -2
  84. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  85. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  86. package/dist/data-structures/matrix/navigator.js +28 -0
  87. package/dist/data-structures/matrix/navigator.js.map +1 -1
  88. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  89. package/dist/data-structures/matrix/vector2d.js +188 -1
  90. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  91. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  92. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  93. package/dist/data-structures/priority-queue/max-priority-queue.js +18 -0
  94. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  95. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  96. package/dist/data-structures/priority-queue/min-priority-queue.js +19 -0
  97. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  98. package/dist/data-structures/priority-queue/priority-queue.d.ts +180 -0
  99. package/dist/data-structures/priority-queue/priority-queue.js +141 -0
  100. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  101. package/dist/data-structures/queue/deque.d.ts +165 -0
  102. package/dist/data-structures/queue/deque.js +124 -0
  103. package/dist/data-structures/queue/deque.js.map +1 -1
  104. package/dist/data-structures/queue/index.d.ts +2 -0
  105. package/dist/data-structures/queue/queue.d.ts +107 -0
  106. package/dist/data-structures/queue/queue.js +80 -0
  107. package/dist/data-structures/queue/queue.js.map +1 -1
  108. package/dist/data-structures/stack/index.d.ts +1 -0
  109. package/dist/data-structures/stack/stack.d.ts +63 -0
  110. package/dist/data-structures/stack/stack.js +50 -0
  111. package/dist/data-structures/stack/stack.js.map +1 -1
  112. package/dist/data-structures/tree/index.d.ts +1 -0
  113. package/dist/data-structures/tree/tree.d.ts +14 -0
  114. package/dist/data-structures/tree/tree.js +1 -0
  115. package/dist/data-structures/tree/tree.js.map +1 -1
  116. package/dist/data-structures/trie/index.d.ts +1 -0
  117. package/dist/data-structures/trie/trie.d.ts +61 -0
  118. package/dist/data-structures/trie/trie.js +36 -0
  119. package/dist/data-structures/trie/trie.js.map +1 -1
  120. package/dist/index.d.ts +4 -0
  121. package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
  122. package/dist/interfaces/abstract-graph.d.ts +5 -0
  123. package/dist/interfaces/avl-tree.d.ts +7 -0
  124. package/dist/interfaces/binary-tree.d.ts +6 -0
  125. package/dist/interfaces/bst.d.ts +6 -0
  126. package/dist/interfaces/directed-graph.d.ts +3 -0
  127. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  128. package/dist/interfaces/heap.d.ts +1 -0
  129. package/dist/interfaces/index.d.ts +15 -0
  130. package/dist/interfaces/navigator.d.ts +1 -0
  131. package/dist/interfaces/priority-queue.d.ts +1 -0
  132. package/dist/interfaces/rb-tree.d.ts +6 -0
  133. package/dist/interfaces/segment-tree.d.ts +1 -0
  134. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  135. package/dist/interfaces/tree-multiset.d.ts +6 -0
  136. package/dist/interfaces/undirected-graph.d.ts +3 -0
  137. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  138. package/dist/types/data-structures/abstract-binary-tree.js +6 -0
  139. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  140. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  141. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  142. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  143. package/dist/types/data-structures/bst.d.ts +13 -0
  144. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  145. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  146. package/dist/types/data-structures/hash.d.ts +1 -0
  147. package/dist/types/data-structures/heap.d.ts +3 -0
  148. package/dist/types/data-structures/index.d.ts +16 -0
  149. package/dist/types/data-structures/map-graph.d.ts +1 -0
  150. package/dist/types/data-structures/navigator.d.ts +14 -0
  151. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  152. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  153. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  154. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  155. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  156. package/dist/types/helpers.d.ts +1 -0
  157. package/dist/types/index.d.ts +3 -0
  158. package/dist/types/utils/index.d.ts +2 -0
  159. package/dist/types/utils/utils.d.ts +7 -0
  160. package/dist/types/utils/validate-type.d.ts +19 -0
  161. package/dist/utils/index.d.ts +1 -0
  162. package/dist/utils/utils.d.ts +19 -0
  163. package/package.json +10 -6
@@ -1,10 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
4
11
  const linked_list_1 = require("../linked-list");
12
+ // O(n) time complexity of obtaining the value
13
+ // O(1) time complexity of adding at the beginning and the end
5
14
  class Deque extends linked_list_1.DoublyLinkedList {
6
15
  }
7
16
  exports.Deque = Deque;
17
+ // O(1) time complexity of obtaining the value
18
+ // O(n) time complexity of adding at the beginning and the end
19
+ // todo tested slowest one
8
20
  class ObjectDeque {
9
21
  constructor(capacity) {
10
22
  this._nodes = {};
@@ -39,6 +51,11 @@ class ObjectDeque {
39
51
  get size() {
40
52
  return this._size;
41
53
  }
54
+ /**
55
+ * The "addFirst" function adds a value to the beginning of an array-like data structure.
56
+ * @param {E} value - The `value` parameter represents the value that you want to add to the beginning of the data
57
+ * structure.
58
+ */
42
59
  addFirst(value) {
43
60
  if (this._size === 0) {
44
61
  const mid = Math.floor(this._capacity / 2);
@@ -51,6 +68,10 @@ class ObjectDeque {
51
68
  this._nodes[this._first] = value;
52
69
  this._size++;
53
70
  }
71
+ /**
72
+ * The addLast function adds a value to the end of an array-like data structure.
73
+ * @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
74
+ */
54
75
  addLast(value) {
55
76
  if (this._size === 0) {
56
77
  const mid = Math.floor(this._capacity / 2);
@@ -63,6 +84,10 @@ class ObjectDeque {
63
84
  this._nodes[this._last] = value;
64
85
  this._size++;
65
86
  }
87
+ /**
88
+ * The function `pollFirst()` removes and returns the first element in a data structure.
89
+ * @returns The value of the first element in the data structure.
90
+ */
66
91
  pollFirst() {
67
92
  if (!this._size)
68
93
  return;
@@ -72,10 +97,18 @@ class ObjectDeque {
72
97
  this._size--;
73
98
  return value;
74
99
  }
100
+ /**
101
+ * The `peekFirst` function returns the first element in an array-like data structure if it exists.
102
+ * @returns The element at the first position of the `_nodes` array.
103
+ */
75
104
  peekFirst() {
76
105
  if (this._size)
77
106
  return this._nodes[this._first];
78
107
  }
108
+ /**
109
+ * The `pollLast()` function removes and returns the last element in a data structure.
110
+ * @returns The value that was removed from the data structure.
111
+ */
79
112
  pollLast() {
80
113
  if (!this._size)
81
114
  return;
@@ -85,13 +118,28 @@ class ObjectDeque {
85
118
  this._size--;
86
119
  return value;
87
120
  }
121
+ /**
122
+ * The `peekLast()` function returns the last element in an array-like data structure.
123
+ * @returns The last element in the array "_nodes" is being returned.
124
+ */
88
125
  peekLast() {
89
126
  if (this._size)
90
127
  return this._nodes[this._last];
91
128
  }
129
+ /**
130
+ * The get function returns the element at the specified index in an array-like data structure.
131
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
132
+ * retrieve from the array.
133
+ * @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
134
+ * index, `null` is returned.
135
+ */
92
136
  get(index) {
93
137
  return this._nodes[this._first + index] || null;
94
138
  }
139
+ /**
140
+ * The function checks if the size of a data structure is less than or equal to zero.
141
+ * @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
142
+ */
95
143
  isEmpty() {
96
144
  return this._size <= 0;
97
145
  }
@@ -103,6 +151,8 @@ class ObjectDeque {
103
151
  }
104
152
  }
105
153
  exports.ObjectDeque = ObjectDeque;
154
+ // O(1) time complexity of obtaining the value
155
+ // O(n) time complexity of adding at the beginning and the end
106
156
  class ArrayDeque {
107
157
  constructor() {
108
158
  this._nodes = [];
@@ -110,41 +160,115 @@ class ArrayDeque {
110
160
  get size() {
111
161
  return this._nodes.length;
112
162
  }
163
+ /**
164
+ * O(n) time complexity of adding at the beginning and the end
165
+ */
166
+ /**
167
+ * The function "addLast" adds a value to the end of an array.
168
+ * @param {E} value - The value parameter represents the value that you want to add to the end of the array.
169
+ * @returns The return value is the new length of the array after the value has been added.
170
+ */
113
171
  addLast(value) {
114
172
  return this._nodes.push(value);
115
173
  }
174
+ /**
175
+ * The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
176
+ * @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
177
+ */
116
178
  pollLast() {
117
179
  var _a;
118
180
  return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
119
181
  }
182
+ /**
183
+ * The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
184
+ * @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
185
+ * empty.
186
+ */
120
187
  pollFirst() {
121
188
  var _a;
122
189
  return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
123
190
  }
191
+ /**
192
+ * O(n) time complexity of adding at the beginning and the end
193
+ */
194
+ /**
195
+ * The function "addFirst" adds a value to the beginning of an array.
196
+ * @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
197
+ * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
198
+ * `value` at the beginning.
199
+ */
124
200
  addFirst(value) {
125
201
  return this._nodes.unshift(value);
126
202
  }
203
+ /**
204
+ * The `peekFirst` function returns the first element of an array or null if the array is empty.
205
+ * @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
206
+ * empty, it will return `null`.
207
+ */
127
208
  peekFirst() {
128
209
  var _a;
129
210
  return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
130
211
  }
212
+ /**
213
+ * The `peekLast` function returns the last element of an array or null if the array is empty.
214
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
215
+ */
131
216
  peekLast() {
132
217
  var _a;
133
218
  return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
134
219
  }
220
+ /**
221
+ * O(1) time complexity of obtaining the value
222
+ */
223
+ /**
224
+ * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
225
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
226
+ * retrieve from the array.
227
+ * @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
228
+ * will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
229
+ */
135
230
  get(index) {
136
231
  var _a;
137
232
  return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
138
233
  }
234
+ /**
235
+ * The set function assigns a value to a specific index in an array.
236
+ * @param {number} index - The index parameter is a number that represents the position of the element in the array
237
+ * that you want to set a new value for.
238
+ * @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
239
+ * _nodes array.
240
+ * @returns The value that is being set at the specified index in the `_nodes` array.
241
+ */
139
242
  set(index, value) {
140
243
  return (this._nodes[index] = value);
141
244
  }
245
+ /**
246
+ * The insert function adds a value at a specified index in an array.
247
+ * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
248
+ * array. It is a number that represents the index of the array where the value should be inserted. The index starts
249
+ * from 0, so the first element of the array has an index of 0, the second element has
250
+ * @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
251
+ * index.
252
+ * @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
253
+ * are being removed, an empty array will be returned.
254
+ */
142
255
  insert(index, value) {
143
256
  return this._nodes.splice(index, 0, value);
144
257
  }
258
+ /**
259
+ * The remove function removes an element from an array at a specified index.
260
+ * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
261
+ * is a number that represents the index of the element to be removed.
262
+ * @returns The method is returning an array containing the removed element.
263
+ */
145
264
  remove(index) {
146
265
  return this._nodes.splice(index, 1);
147
266
  }
267
+ /**
268
+ * The function checks if an array called "_nodes" is empty.
269
+ * @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
270
+ * is 0, indicating that the array is empty. Otherwise, it returns `false`.
271
+ */
148
272
  isEmpty() {
149
273
  return this._nodes.length === 0;
150
274
  }
@@ -1 +1 @@
1
- {"version":3,"file":"deque.js","sourceRoot":"","sources":["../../../src/data-structures/queue/deque.ts"],"names":[],"mappings":";;;AAOA,g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CAAI,IAAI,CAAC;IACpC,CAAC;IAED;;;;;;;OAOG;IACH,GAAG,CAAC,KAAa,EAAE,KAAQ;QACzB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,KAAa,EAAE,KAAQ;QAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAa;QAClB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC;IAClC,CAAC;CACF;AA/HD,gCA+HC"}
@@ -0,0 +1,2 @@
1
+ export * from './queue';
2
+ export * from './deque';
@@ -0,0 +1,107 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
+ * @class
5
+ */
6
+ import { SinglyLinkedList } from '../linked-list';
7
+ export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
8
+ /**
9
+ * The enqueue function adds a value to the end of an array.
10
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
11
+ */
12
+ enqueue(value: E): void;
13
+ /**
14
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
15
+ * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
16
+ */
17
+ dequeue(): E | undefined;
18
+ /**
19
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
20
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
21
+ */
22
+ peek(): E | undefined;
23
+ }
24
+ export declare class Queue<E = any> {
25
+ /**
26
+ * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
27
+ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
28
+ * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
29
+ * initialized as an empty array.
30
+ */
31
+ constructor(elements?: E[]);
32
+ private _nodes;
33
+ get nodes(): E[];
34
+ set nodes(value: E[]);
35
+ private _offset;
36
+ get offset(): number;
37
+ set offset(value: number);
38
+ /**
39
+ * The size function returns the number of elements in an array.
40
+ * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
41
+ */
42
+ get size(): number;
43
+ /**
44
+ * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
45
+ * @public
46
+ * @static
47
+ * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
48
+ * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
49
+ * array.
50
+ */
51
+ static fromArray<E>(elements: E[]): Queue<E>;
52
+ /**
53
+ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
54
+ * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
55
+ * @returns The `add` method is returning a `Queue<E>` object.
56
+ */
57
+ push(element: E): Queue<E>;
58
+ /**
59
+ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
60
+ * necessary to optimize performance.
61
+ * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
62
+ */
63
+ shift(): E | undefined;
64
+ /**
65
+ * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
66
+ * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
67
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
68
+ */
69
+ peek(): E | undefined;
70
+ /**
71
+ * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
72
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
73
+ * array is empty, it returns `null`.
74
+ */
75
+ peekLast(): E | undefined;
76
+ /**
77
+ * The enqueue function adds a value to the end of a queue.
78
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
79
+ */
80
+ enqueue(value: E): void;
81
+ /**
82
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
83
+ * @returns The method is returning a value of type E or null.
84
+ */
85
+ dequeue(): E | undefined;
86
+ getAt(index: number): E | undefined;
87
+ /**
88
+ * The function checks if a data structure is empty by comparing its size to zero.
89
+ * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
90
+ */
91
+ isEmpty(): boolean;
92
+ /**
93
+ * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
94
+ * @returns An array of type E is being returned.
95
+ */
96
+ toArray(): E[];
97
+ /**
98
+ * The clear function resets the nodes array and offset to their initial values.
99
+ */
100
+ clear(): void;
101
+ /**
102
+ * The `clone()` function returns a new Queue object with the same elements as the original Queue.
103
+ * @returns The `clone()` method is returning a new instance of the `Queue` class.
104
+ */
105
+ clone(): Queue<E>;
106
+ [Symbol.iterator](): Generator<E, void, unknown>;
107
+ }
@@ -1,14 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Queue = exports.LinkedListQueue = void 0;
4
+ /**
5
+ * @license MIT
6
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
7
+ * @class
8
+ */
4
9
  const linked_list_1 = require("../linked-list");
5
10
  class LinkedListQueue extends linked_list_1.SinglyLinkedList {
11
+ /**
12
+ * The enqueue function adds a value to the end of an array.
13
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
14
+ */
6
15
  enqueue(value) {
7
16
  this.push(value);
8
17
  }
18
+ /**
19
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
20
+ * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
21
+ */
9
22
  dequeue() {
10
23
  return this.shift();
11
24
  }
25
+ /**
26
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
27
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
28
+ */
12
29
  peek() {
13
30
  var _a;
14
31
  return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
@@ -16,6 +33,12 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
16
33
  }
17
34
  exports.LinkedListQueue = LinkedListQueue;
18
35
  class Queue {
36
+ /**
37
+ * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
38
+ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
39
+ * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
40
+ * initialized as an empty array.
41
+ */
19
42
  constructor(elements) {
20
43
  this._nodes = elements || [];
21
44
  this._offset = 0;
@@ -32,16 +55,38 @@ class Queue {
32
55
  set offset(value) {
33
56
  this._offset = value;
34
57
  }
58
+ /**
59
+ * The size function returns the number of elements in an array.
60
+ * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
61
+ */
35
62
  get size() {
36
63
  return this.nodes.length - this.offset;
37
64
  }
65
+ /**
66
+ * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
67
+ * @public
68
+ * @static
69
+ * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
70
+ * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
71
+ * array.
72
+ */
38
73
  static fromArray(elements) {
39
74
  return new Queue(elements);
40
75
  }
76
+ /**
77
+ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
78
+ * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
79
+ * @returns The `add` method is returning a `Queue<E>` object.
80
+ */
41
81
  push(element) {
42
82
  this.nodes.push(element);
43
83
  return this;
44
84
  }
85
+ /**
86
+ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
87
+ * necessary to optimize performance.
88
+ * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
89
+ */
45
90
  shift() {
46
91
  if (this.size === 0)
47
92
  return undefined;
@@ -49,35 +94,70 @@ class Queue {
49
94
  this.offset += 1;
50
95
  if (this.offset * 2 < this.nodes.length)
51
96
  return first;
97
+ // only remove dequeued elements when reaching half size
98
+ // to decrease latency of shifting elements.
52
99
  this.nodes = this.nodes.slice(this.offset);
53
100
  this.offset = 0;
54
101
  return first;
55
102
  }
103
+ /**
104
+ * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
105
+ * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
106
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
107
+ */
56
108
  peek() {
57
109
  return this.size > 0 ? this.nodes[this.offset] : undefined;
58
110
  }
111
+ /**
112
+ * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
113
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
114
+ * array is empty, it returns `null`.
115
+ */
59
116
  peekLast() {
60
117
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
61
118
  }
119
+ /**
120
+ * The enqueue function adds a value to the end of a queue.
121
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
122
+ */
62
123
  enqueue(value) {
63
124
  this.push(value);
64
125
  }
126
+ /**
127
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
128
+ * @returns The method is returning a value of type E or null.
129
+ */
65
130
  dequeue() {
66
131
  return this.shift();
67
132
  }
68
133
  getAt(index) {
69
134
  return this.nodes[index];
70
135
  }
136
+ /**
137
+ * The function checks if a data structure is empty by comparing its size to zero.
138
+ * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
139
+ */
71
140
  isEmpty() {
72
141
  return this.size === 0;
73
142
  }
143
+ /**
144
+ * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
145
+ * @returns An array of type E is being returned.
146
+ */
74
147
  toArray() {
75
148
  return this.nodes.slice(this.offset);
76
149
  }
150
+ /**
151
+ * The clear function resets the nodes array and offset to their initial values.
152
+ */
77
153
  clear() {
78
154
  this.nodes = [];
79
155
  this.offset = 0;
80
156
  }
157
+ /**
158
+ * The `clone()` function returns a new Queue object with the same elements as the original Queue.
159
+ * @returns The `clone()` method is returning a new instance of the `Queue` class.
160
+ */
81
161
  clone() {
82
162
  return new Queue(this.nodes.slice(this.offset));
83
163
  }
@@ -1 +1 @@
1
- {"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAKA,gDAAgD;AAEhD,MAAa,eAAyB,SAAQ,8BAAmB;IAK/D,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAMD,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAMD,IAAI;;QACF,OAAO,MAAA,IAAI,CAAC,IAAI,0CAAE,GAAG,CAAC;IACxB,CAAC;CACF;AAxBD,0CAwBC;AAED,MAAa,KAAK;IAOhB,YAAY,QAAc;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACnB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,KAAU;QAClB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAID,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,MAAM,CAAC,KAAa;QACtB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACvB,CAAC;IAMD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IACzC,CAAC;IAUD,MAAM,CAAC,SAAS,CAAI,QAAa;QAC/B,OAAO,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC7B,CAAC;IAOD,IAAI,CAAC,OAAU;QACb,IAAI,CAAC,KAAK,CAAC,IA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C,IAAI,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAKD,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;IAMD,KAAK;QACH,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE;YAC7B,MAAM,IAAI,CAAC;SACZ;IACH,CAAC;CACF;AA7JD,sBA6JC"}
1
+ {"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,gDAAgD;AAEhD,MAAa,eAAyB,SAAQ,8BAAmB;IAC/D;;;OAGG;IACH,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;;OAGG;IACH,IAAI;;QACF,OAAO,MAAA,IAAI,CAAC,IAAI,0CAAE,GAAG,CAAC;IACxB,CAAC;CACF;AAxBD,0CAwBC;AAED,MAAa,KAAK;IAChB;;;;;OAKG;IACH,YAAY,QAAc;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACnB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,KAAU;QAClB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAID,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACt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AC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACvE,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,KAAa;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;IAED;;;OAGG;IACH,KAAK;QACH,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE;YAC7B,MAAM,IAAI,CAAC;SACZ;IACH,CAAC;CACF;AA7JD,sBA6JC"}
@@ -0,0 +1 @@
1
+ export * from './stack';
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
+ * @class
5
+ */
6
+ export declare class Stack<E = any> {
7
+ protected _elements: E[];
8
+ /**
9
+ * The constructor initializes an array of elements, which can be provided as an optional parameter.
10
+ * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
11
+ * of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
12
+ * is provided and is an array, it is assigned to the `_elements
13
+ */
14
+ constructor(elements?: E[]);
15
+ /**
16
+ * The function "fromArray" creates a new Stack object from an array of elements.
17
+ * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
18
+ * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
19
+ * array.
20
+ */
21
+ static fromArray<E>(elements: E[]): Stack<E>;
22
+ /**
23
+ * The function checks if an array is empty and returns a boolean value.
24
+ * @returns A boolean value indicating whether the `_elements` array is empty or not.
25
+ */
26
+ isEmpty(): boolean;
27
+ /**
28
+ * The size() function returns the number of elements in an array.
29
+ * @returns The size of the elements array.
30
+ */
31
+ size(): number;
32
+ /**
33
+ * The `peek` function returns the last element of an array, or null if the array is empty.
34
+ * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
35
+ */
36
+ peek(): E | null;
37
+ /**
38
+ * The push function adds an element to the stack and returns the updated stack.
39
+ * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
40
+ * @returns The `push` method is returning the updated `Stack<E>` object.
41
+ */
42
+ push(element: E): Stack<E>;
43
+ /**
44
+ * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
45
+ * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
46
+ * array is empty, it returns `null`.
47
+ */
48
+ pop(): E | null;
49
+ /**
50
+ * The toArray function returns a copy of the elements in an array.
51
+ * @returns An array of type E.
52
+ */
53
+ toArray(): E[];
54
+ /**
55
+ * The clear function clears the elements array.
56
+ */
57
+ clear(): void;
58
+ /**
59
+ * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
60
+ * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
61
+ */
62
+ clone(): Stack<E>;
63
+ }