data-structure-typed 0.8.6

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 (166) hide show
  1. package/.idea/data-structure-typed.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/README.md +2 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +6 -0
  6. package/dist/data-structures/binary-tree/avl-tree.js +231 -0
  7. package/dist/data-structures/binary-tree/b-tree.js +6 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
  9. package/dist/data-structures/binary-tree/binary-tree.js +992 -0
  10. package/dist/data-structures/binary-tree/bst.js +431 -0
  11. package/dist/data-structures/binary-tree/index.js +20 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +6 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +151 -0
  14. package/dist/data-structures/binary-tree/splay-tree.js +6 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
  16. package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
  17. package/dist/data-structures/graph/abstract-graph.js +648 -0
  18. package/dist/data-structures/graph/directed-graph.js +268 -0
  19. package/dist/data-structures/graph/index.js +19 -0
  20. package/dist/data-structures/graph/undirected-graph.js +142 -0
  21. package/dist/data-structures/hash/coordinate-map.js +24 -0
  22. package/dist/data-structures/hash/coordinate-set.js +21 -0
  23. package/dist/data-structures/hash/hash-table.js +2 -0
  24. package/dist/data-structures/hash/index.js +17 -0
  25. package/dist/data-structures/hash/pair.js +2 -0
  26. package/dist/data-structures/hash/tree-map.js +2 -0
  27. package/dist/data-structures/hash/tree-set.js +2 -0
  28. package/dist/data-structures/heap/heap.js +114 -0
  29. package/dist/data-structures/heap/index.js +19 -0
  30. package/dist/data-structures/heap/max-heap.js +22 -0
  31. package/dist/data-structures/heap/min-heap.js +22 -0
  32. package/dist/data-structures/index.js +25 -0
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
  34. package/dist/data-structures/linked-list/index.js +18 -0
  35. package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
  37. package/dist/data-structures/matrix/index.js +19 -0
  38. package/dist/data-structures/matrix/matrix.js +14 -0
  39. package/dist/data-structures/matrix/matrix2d.js +119 -0
  40. package/dist/data-structures/matrix/navigator.js +78 -0
  41. package/dist/data-structures/matrix/vector2d.js +161 -0
  42. package/dist/data-structures/priority-queue/index.js +19 -0
  43. package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
  45. package/dist/data-structures/priority-queue/priority-queue.js +174 -0
  46. package/dist/data-structures/queue/deque.js +132 -0
  47. package/dist/data-structures/queue/index.js +17 -0
  48. package/dist/data-structures/queue/queue.js +113 -0
  49. package/dist/data-structures/stack/index.js +17 -0
  50. package/dist/data-structures/stack/stack.js +97 -0
  51. package/dist/data-structures/trampoline.js +52 -0
  52. package/dist/data-structures/trie/index.js +17 -0
  53. package/dist/data-structures/trie/trie.js +141 -0
  54. package/dist/index.js +17 -0
  55. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
  57. package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
  61. package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
  62. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
  64. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
  65. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
  66. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
  69. package/dist/types/data-structures/graph/index.d.ts +3 -0
  70. package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
  71. package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
  72. package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
  73. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  74. package/dist/types/data-structures/hash/index.d.ts +1 -0
  75. package/dist/types/data-structures/hash/pair.d.ts +1 -0
  76. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  77. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  78. package/dist/types/data-structures/heap/heap.d.ts +72 -0
  79. package/dist/types/data-structures/heap/index.d.ts +3 -0
  80. package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
  81. package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
  82. package/dist/types/data-structures/index.d.ts +9 -0
  83. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
  84. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  85. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
  86. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  87. package/dist/types/data-structures/matrix/index.d.ts +3 -0
  88. package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
  89. package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
  90. package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
  91. package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
  92. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  93. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
  94. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
  95. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
  96. package/dist/types/data-structures/queue/deque.d.ts +37 -0
  97. package/dist/types/data-structures/queue/index.d.ts +1 -0
  98. package/dist/types/data-structures/queue/queue.d.ts +76 -0
  99. package/dist/types/data-structures/stack/index.d.ts +1 -0
  100. package/dist/types/data-structures/stack/stack.d.ts +69 -0
  101. package/dist/types/data-structures/trampoline.d.ts +25 -0
  102. package/dist/types/data-structures/trie/index.d.ts +1 -0
  103. package/dist/types/data-structures/trie/trie.d.ts +28 -0
  104. package/dist/types/index.d.ts +1 -0
  105. package/dist/types/index.js +17 -0
  106. package/dist/types/types/index.d.ts +1 -0
  107. package/dist/types/types/utils.d.ts +46 -0
  108. package/dist/types/utils.d.ts +122 -0
  109. package/dist/types/utils.js +53 -0
  110. package/dist/utils.js +569 -0
  111. package/package.json +75 -0
  112. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  113. package/src/data-structures/binary-tree/avl-tree.ts +232 -0
  114. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  115. package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
  117. package/src/data-structures/binary-tree/bst.ts +404 -0
  118. package/src/data-structures/binary-tree/index.ts +4 -0
  119. package/src/data-structures/binary-tree/rb-tree.ts +3 -0
  120. package/src/data-structures/binary-tree/segment-tree.ts +164 -0
  121. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  122. package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
  123. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  124. package/src/data-structures/graph/abstract-graph.ts +789 -0
  125. package/src/data-structures/graph/directed-graph.ts +322 -0
  126. package/src/data-structures/graph/index.ts +3 -0
  127. package/src/data-structures/graph/undirected-graph.ts +154 -0
  128. package/src/data-structures/hash/coordinate-map.ts +24 -0
  129. package/src/data-structures/hash/coordinate-set.ts +20 -0
  130. package/src/data-structures/hash/hash-table.ts +1 -0
  131. package/src/data-structures/hash/index.ts +1 -0
  132. package/src/data-structures/hash/pair.ts +1 -0
  133. package/src/data-structures/hash/tree-map.ts +1 -0
  134. package/src/data-structures/hash/tree-set.ts +1 -0
  135. package/src/data-structures/heap/heap.ts +136 -0
  136. package/src/data-structures/heap/index.ts +3 -0
  137. package/src/data-structures/heap/max-heap.ts +22 -0
  138. package/src/data-structures/heap/min-heap.ts +24 -0
  139. package/src/data-structures/index.ts +10 -0
  140. package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
  141. package/src/data-structures/linked-list/index.ts +2 -0
  142. package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
  143. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  144. package/src/data-structures/matrix/index.ts +3 -0
  145. package/src/data-structures/matrix/matrix.ts +13 -0
  146. package/src/data-structures/matrix/matrix2d.ts +125 -0
  147. package/src/data-structures/matrix/navigator.ts +99 -0
  148. package/src/data-structures/matrix/vector2d.ts +189 -0
  149. package/src/data-structures/priority-queue/index.ts +3 -0
  150. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
  151. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
  152. package/src/data-structures/priority-queue/priority-queue.ts +208 -0
  153. package/src/data-structures/queue/deque.ts +139 -0
  154. package/src/data-structures/queue/index.ts +1 -0
  155. package/src/data-structures/queue/queue.ts +123 -0
  156. package/src/data-structures/stack/index.ts +1 -0
  157. package/src/data-structures/stack/stack.ts +104 -0
  158. package/src/data-structures/trampoline.ts +91 -0
  159. package/src/data-structures/trie/index.ts +1 -0
  160. package/src/data-structures/trie/trie.ts +153 -0
  161. package/src/index.ts +1 -0
  162. package/src/types/index.ts +1 -0
  163. package/src/types/patches/index.d.ts +0 -0
  164. package/src/types/utils.ts +158 -0
  165. package/src/utils.ts +605 -0
  166. package/tsconfig.json +52 -0
@@ -0,0 +1,750 @@
1
+ /** Type used for filter and find methods, returning a boolean */
2
+ type TTestFunction<NodeData> = (
3
+ data: NodeData,
4
+ index: number,
5
+ list: SinglyLinkedList<NodeData>,
6
+ ) => boolean;
7
+
8
+ /** Type used for map and forEach methods, returning anything */
9
+ type TMapFunction<NodeData> = (
10
+ data: any,
11
+ index: number,
12
+ list: SinglyLinkedList<NodeData>,
13
+ ) => any;
14
+
15
+ /**
16
+ * The class which represents one link or node in a linked list
17
+ * ```ts
18
+ * const node = new SinglyLinkedListNode(1, null, null, null);
19
+ * ```
20
+ */
21
+ export class SinglyLinkedListNode<NodeData = any> {
22
+ constructor(
23
+ /** Data stored on the node */
24
+ public val: NodeData,
25
+ /** The previous node in the list */
26
+ public prev: SinglyLinkedListNode<NodeData> | null,
27
+ /** The next link in the list */
28
+ public next: SinglyLinkedListNode<NodeData> | null,
29
+ /** The list this node belongs to */
30
+ public list: SinglyLinkedList<NodeData> | null,
31
+ ) {
32
+ }
33
+
34
+ /**
35
+ * Alias to .val
36
+ * ```ts
37
+ * new LinkedList(1, 2, 3).head.value; // 1
38
+ * ```
39
+ */
40
+ public get value() {
41
+ return this.val;
42
+ }
43
+
44
+ /**
45
+ * Get the index of this node
46
+ * ```ts
47
+ * new LinkedList(1, 2, 3).head.index; // 0
48
+ * ```
49
+ */
50
+ public get index() {
51
+ if (!this.list) {
52
+ return undefined;
53
+ }
54
+ return this.list.findIndex((value) => value === this.value);
55
+ }
56
+
57
+ /**
58
+ * Insert a new node before this one
59
+ * ```ts
60
+ * new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
61
+ * ```
62
+ * @param val Data to save in the node
63
+ */
64
+ public insertBefore(val: NodeData): SinglyLinkedList<NodeData> {
65
+ return this.list !== null
66
+ ? this.list.insertBefore(this, val)
67
+ : new SinglyLinkedList(val, this.val);
68
+ }
69
+
70
+ /**
71
+ * Insert new val after this node
72
+ * ```ts
73
+ * new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
74
+ * ```
75
+ * @param val Data to be saved in the node
76
+ */
77
+ public insertAfter(val: NodeData): SinglyLinkedList<NodeData> {
78
+ return this.list !== null
79
+ ? this.list.insertAfter(this, val)
80
+ : new SinglyLinkedList(this.val, val);
81
+ }
82
+
83
+ /**
84
+ * Remove this node
85
+ * ```ts
86
+ * new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
87
+ * ```
88
+ */
89
+ public remove(): SinglyLinkedListNode<NodeData> {
90
+ if (this.list === null) {
91
+ throw new ReferenceError('Node does not belong to any list');
92
+ }
93
+ return this.list.removeNode(this);
94
+ }
95
+ }
96
+
97
+
98
+ /**
99
+ * A doubly linked list
100
+ * ```ts
101
+ * const list = new LinkedList(1, 2, 3);
102
+ * const listFromArray = LinkedList.from([1, 2, 3]);
103
+ * ```
104
+ */
105
+ export class SinglyLinkedList<NodeData = any> {
106
+
107
+ /**
108
+ * The length of the list
109
+ */
110
+ public get length(): number {
111
+ return this.size;
112
+ }
113
+
114
+ /**
115
+ * Convert any iterable to a new linked list
116
+ * ```javascript
117
+ * const array = [1, 2, 3];
118
+ * const list = LinkedList.from(array);
119
+ * ```
120
+ * @param iterable Any iterable datatype like Array or Map
121
+ */
122
+ public static from<T>(iterable: Iterable<T>): SinglyLinkedList<T> {
123
+ return new SinglyLinkedList(...iterable);
124
+ }
125
+
126
+ /** The head of the list, the first node */
127
+ public head: SinglyLinkedListNode<NodeData> | null;
128
+
129
+ /** The tail of the list, the last node */
130
+ public tail: SinglyLinkedListNode<NodeData> | null;
131
+
132
+ /** Internal size reference */
133
+ private size: number;
134
+
135
+ constructor(...args: NodeData[]) {
136
+ this.head = null;
137
+ this.tail = null;
138
+ this.size = 0;
139
+
140
+ for (let i = 0; i < arguments.length; i++) {
141
+ this.append(args[i]);
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Get the node val at a specified index, zero based
147
+ * ```ts
148
+ * new LinkedList(1, 2, 3).get(0); // 1
149
+ * ```
150
+ * @param index to retrieve val at
151
+ */
152
+ public get(index: number): NodeData | undefined {
153
+ const node = this.getNode(index);
154
+ return node !== undefined ? node.val : undefined;
155
+ }
156
+
157
+ /**
158
+ * Get the node at index, zero based
159
+ * ```ts
160
+ * new LinkedList(1, 2, 3).getNode(0);
161
+ * // { prev: null, val: 1, next: SinglyLinkedListNode }
162
+ * ```
163
+ */
164
+ public getNode(index: number): SinglyLinkedListNode<NodeData> | undefined {
165
+ if (this.head === null || index < 0 || index >= this.length) {
166
+ return undefined;
167
+ }
168
+ const asc = index < this.length / 2;
169
+ const stopAt = asc ? index : this.length - index - 1;
170
+ const nextNode = asc ? 'next' : 'prev';
171
+ let currentNode = asc ? this.head : this.tail;
172
+ // TODO after no-non-null-assertion not ensure the logic
173
+ for (let currentIndex = 0; currentIndex < stopAt; currentIndex++) {
174
+ if (currentNode) {
175
+ currentNode = currentNode[nextNode];
176
+ }
177
+ }
178
+ return currentNode || undefined;
179
+ }
180
+
181
+ /**
182
+ * Return the first node and its index in the list that
183
+ * satisfies the testing function
184
+ * ```ts
185
+ * new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
186
+ * // { node: SinglyLinkedListNode, index: 0 }
187
+ * ```
188
+ * @param f A function to be applied to the val of each node
189
+ */
190
+ public findNodeIndex(f: TTestFunction<NodeData>): ({
191
+ node: SinglyLinkedListNode<NodeData>,
192
+ index: number,
193
+ }) | undefined {
194
+ let currentIndex = 0;
195
+ let currentNode = this.head;
196
+ while (currentNode) {
197
+ if (f(currentNode.val, currentIndex, this)) {
198
+ return {
199
+ index: currentIndex,
200
+ node: currentNode,
201
+ };
202
+ }
203
+ currentNode = currentNode.next;
204
+ currentIndex += 1;
205
+ }
206
+ return undefined;
207
+ }
208
+
209
+ /**
210
+ * Returns the first node in the list that
211
+ * satisfies the provided testing function. Otherwise undefined is returned.
212
+ * ```ts
213
+ * new LinkedList(1, 2, 3).findNode(val => val === 1);
214
+ * // { prev: null, val: 1, next: SinglyLinkedListNode }
215
+ * ```
216
+ * @param f Function to test val against
217
+ */
218
+ public findNode(f: TTestFunction<NodeData>): SinglyLinkedListNode<NodeData> | undefined {
219
+ const nodeIndex = this.findNodeIndex(f);
220
+ return nodeIndex !== undefined ? nodeIndex.node : undefined;
221
+ }
222
+
223
+ /**
224
+ * Returns the value of the first element in the list that
225
+ * satisfies the provided testing function. Otherwise undefined is returned.
226
+ * ```ts
227
+ * new LinkedList(1, 2, 3).find(val => val === 1); // 1
228
+ * ```
229
+ * @param f Function to test val against
230
+ */
231
+ public find(f: TTestFunction<NodeData>): NodeData | undefined {
232
+ const nodeIndex = this.findNodeIndex(f);
233
+ return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
234
+ }
235
+
236
+ /**
237
+ * Returns the index of the first node in the list that
238
+ * satisfies the provided testing function. Ohterwise -1 is returned.
239
+ * ```ts
240
+ * new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
241
+ * ```
242
+ * @param f Function to test val against
243
+ */
244
+ public findIndex(f: TTestFunction<NodeData>): number {
245
+ const nodeIndex = this.findNodeIndex(f);
246
+ return nodeIndex !== undefined ? nodeIndex.index : -1;
247
+ }
248
+
249
+ /**
250
+ * Append one or any number of nodes to the end of the list.
251
+ * This modifies the list in place and returns the list itself
252
+ * to make this method chainable.
253
+ * ```ts
254
+ * new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
255
+ * ```
256
+ * @param args Data to be stored in the node, takes any number of arguments
257
+ */
258
+ public append(...args: NodeData[]): SinglyLinkedList<NodeData> {
259
+ for (const val of args) {
260
+ const node = new SinglyLinkedListNode(val, this.tail, null, this);
261
+ if (this.head === null) {
262
+ this.head = node;
263
+ }
264
+ if (this.tail !== null) {
265
+ this.tail.next = node;
266
+ }
267
+ this.tail = node;
268
+ this.size += 1;
269
+ }
270
+ return this;
271
+ }
272
+
273
+ /**
274
+ * Synonym for append
275
+ * ```ts
276
+ * new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
277
+ * ```
278
+ * @param args Data to be stored, takes any number of arguments
279
+ */
280
+ public push(...args: NodeData[]): number {
281
+ this.append(...args);
282
+ return this.length;
283
+ }
284
+
285
+ /**
286
+ * Prepend any number of val arguments to the list. The
287
+ * argument list is prepended as a block to reduce confusion:
288
+ * ```javascript
289
+ * new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
290
+ * ```
291
+ * @param args Data to be stored in the node, accepts any number of arguments
292
+ */
293
+ public prepend(...args: NodeData[]): SinglyLinkedList<NodeData> {
294
+ const reverseArgs = Array.from(args).reverse();
295
+ for (const val of reverseArgs) {
296
+ const node = new SinglyLinkedListNode(val, null, this.head, this);
297
+ if (this.tail === null) {
298
+ this.tail = node;
299
+ }
300
+ if (this.head !== null) {
301
+ this.head.prev = node;
302
+ }
303
+ this.head = node;
304
+ this.size += 1;
305
+ }
306
+ return this;
307
+ }
308
+
309
+ /**
310
+ * Insert a new node at a given index position. If index is
311
+ * out of bounds, the node is appended, if index is negative
312
+ * or 0, it will be prepended.
313
+ * ```ts
314
+ * new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
315
+ * ```
316
+ * @param index The index to insert the new node at
317
+ * @param val Data to be stored on the new node
318
+ */
319
+ public insertAt(index: number, val: NodeData): SinglyLinkedList<NodeData> {
320
+ if (this.head === null) {
321
+ return this.append(val);
322
+ }
323
+ if (index <= 0) {
324
+ return this.prepend(val);
325
+ }
326
+
327
+ let currentNode = this.head;
328
+ let currentIndex = 0;
329
+ while (currentIndex < index - 1 && currentNode.next !== null) {
330
+ currentIndex += 1;
331
+ currentNode = currentNode.next;
332
+ }
333
+ currentNode.insertAfter(val);
334
+ return this;
335
+ }
336
+
337
+ /**
338
+ * Remove the specified node from the list and return the removed
339
+ * node afterwards.
340
+ * ```ts
341
+ * const list = new LinkedList(1, 2, 3);
342
+ * list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
343
+ * ```
344
+ * @param node The node to be removed
345
+ */
346
+ public removeNode(node: SinglyLinkedListNode<NodeData>): SinglyLinkedListNode<NodeData> {
347
+ if (node.list !== this) {
348
+ throw new ReferenceError('Node does not belong to this list');
349
+ }
350
+
351
+ if (node.prev !== null) {
352
+ node.prev.next = node.next;
353
+ }
354
+
355
+ if (node.next !== null) {
356
+ node.next.prev = node.prev;
357
+ }
358
+
359
+ if (this.head === node) {
360
+ this.head = node.next;
361
+ }
362
+
363
+ if (this.tail === node) {
364
+ this.tail = node.prev;
365
+ }
366
+
367
+ this.size -= 1;
368
+ node.next = null;
369
+ node.prev = null;
370
+ node.list = null;
371
+ return node;
372
+ }
373
+
374
+ /**
375
+ * Remove the node at the specified index
376
+ * ```ts
377
+ * new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
378
+ * ```
379
+ * @param index Index at which to remove
380
+ */
381
+ public removeAt(index: number): SinglyLinkedListNode<NodeData> | undefined {
382
+ const node = this.getNode(index);
383
+ return node !== undefined ? this.removeNode(node) : undefined;
384
+ }
385
+
386
+ /**
387
+ * Insert a new node before the reference node
388
+ * ```ts
389
+ * const list = new LinkedList(1, 3);
390
+ * list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
391
+ * ```
392
+ * @param referenceNode The node reference
393
+ * @param val Data to save in the node
394
+ */
395
+ public insertBefore(
396
+ referenceNode: SinglyLinkedListNode<NodeData>,
397
+ val: NodeData,
398
+ ): SinglyLinkedList<NodeData> {
399
+ const node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
400
+ if (referenceNode.prev === null) {
401
+ this.head = node;
402
+ }
403
+ if (referenceNode.prev !== null) {
404
+ referenceNode.prev.next = node;
405
+ }
406
+ referenceNode.prev = node;
407
+ this.size += 1;
408
+ return this;
409
+ }
410
+
411
+ /**
412
+ * Sorts the linked list using the provided compare function
413
+ * @param compare A function used to compare the val of two nodes. It should return
414
+ * a boolean. True will insert a before b, false will insert b before a.
415
+ * (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
416
+ * the sort order will be ascending.
417
+ */
418
+ public sort(compare: (a: NodeData, b: NodeData) => boolean): SinglyLinkedList<NodeData> {
419
+ if (this.head === null || this.tail === null) {
420
+ return this;
421
+ }
422
+ if (this.length < 2) {
423
+ return this;
424
+ }
425
+
426
+ const quicksort = (
427
+ start: SinglyLinkedListNode<NodeData>,
428
+ end: SinglyLinkedListNode<NodeData>,
429
+ ) => {
430
+ if (start === end) {
431
+ return;
432
+ }
433
+ const pivotData = end.val;
434
+ let current: SinglyLinkedListNode | null = start;
435
+ let split: SinglyLinkedListNode = start;
436
+ while (current && current !== end) {
437
+ const sort = compare(current.val, pivotData);
438
+ if (sort) {
439
+ if (current !== split) {
440
+ const temp = split.val;
441
+ split.val = current.val;
442
+ current.val = temp;
443
+ }
444
+ // TODO after no-non-null-assertion not ensure the logic
445
+ if (split.next) {
446
+ split = split.next;
447
+ }
448
+ }
449
+ current = current.next;
450
+ }
451
+ end.val = split.val;
452
+ split.val = pivotData;
453
+
454
+ if (start.next === end.prev) {
455
+ return;
456
+ }
457
+
458
+ if (split.prev && split !== start) {
459
+ quicksort(start, split.prev);
460
+ }
461
+ if (split.next && split !== end) {
462
+ quicksort(split.next, end);
463
+ }
464
+ };
465
+
466
+ quicksort(this.head, this.tail);
467
+ return this;
468
+ }
469
+
470
+ /**
471
+ * Insert a new node after this one
472
+ * ```ts
473
+ * const list = new LinkedList(2, 3);
474
+ * list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
475
+ * ```
476
+ * @param referenceNode The reference node
477
+ * @param val Data to be saved in the node
478
+ */
479
+ public insertAfter(
480
+ referenceNode: SinglyLinkedListNode<NodeData>,
481
+ val: NodeData,
482
+ ): SinglyLinkedList<NodeData> {
483
+ const node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
484
+ if (referenceNode.next === null) {
485
+ this.tail = node;
486
+ }
487
+ if (referenceNode.next !== null) {
488
+ referenceNode.next.prev = node;
489
+ }
490
+ referenceNode.next = node;
491
+ this.size += 1;
492
+ return this;
493
+ }
494
+
495
+ /**
496
+ * Remove the first node from the list and return the val of the removed node
497
+ * or undefined
498
+ * ```ts
499
+ * new LinkedList(1, 2, 3).shift(); // 1
500
+ * ```
501
+ */
502
+ public shift(): NodeData | undefined {
503
+ return this.removeFromAnyEnd(this.head);
504
+ }
505
+
506
+ /**
507
+ * Remove the last node from the list and return the val of the removed node
508
+ * or undefined if the list was empty
509
+ * ```ts
510
+ * new LinkedList(1, 2, 3).pop(); // 3
511
+ * ```
512
+ */
513
+ public pop(): NodeData | undefined {
514
+ return this.removeFromAnyEnd(this.tail);
515
+ }
516
+
517
+ /**
518
+ * Merge the current list with another. Both lists will be
519
+ * equal after merging.
520
+ * ```ts
521
+ * const list = new LinkedList(1, 2);
522
+ * const otherList = new LinkedList(3);
523
+ * list.merge(otherList);
524
+ * (list === otherList); // true
525
+ * ```
526
+ * @param list The list to be merged
527
+ */
528
+ public merge(list: SinglyLinkedList<NodeData>): void {
529
+ if (this.tail !== null) {
530
+ this.tail.next = list.head;
531
+ }
532
+ if (list.head !== null) {
533
+ list.head.prev = this.tail;
534
+ }
535
+ this.head = this.head || list.head;
536
+ this.tail = list.tail || this.tail;
537
+ this.size += list.size;
538
+ list.size = this.size;
539
+ list.head = this.head;
540
+ list.tail = this.tail;
541
+ }
542
+
543
+ /**
544
+ * Removes all nodes from a list
545
+ *
546
+ * ```ts
547
+ * list.clear();
548
+ * ```
549
+ */
550
+ public clear() {
551
+ this.head = null;
552
+ this.tail = null;
553
+ this.size = 0;
554
+ return this;
555
+ }
556
+
557
+ /**
558
+ * The slice() method returns a shallow copy of a
559
+ * portion of a list into a new list object selected
560
+ * from start to end (end not included).
561
+ * The original list will not be modified.
562
+ * ```ts
563
+ * const list = new LinkedList(1, 2, 3, 4, 5);
564
+ * const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
565
+ * ```
566
+ * @param start Start index
567
+ * @param end End index, optional
568
+ */
569
+ // eslint-disable-next-line @typescript-eslint/ban-types
570
+ public slice(start: number, end?: number): SinglyLinkedList<NodeData | {}> {
571
+ const list = new SinglyLinkedList();
572
+ let finish = end;
573
+
574
+ if (this.head === null || this.tail === null) {
575
+ return list;
576
+ }
577
+ if (finish === undefined || finish < start) {
578
+ finish = this.length;
579
+ }
580
+
581
+ let head: SinglyLinkedListNode<NodeData> | null | undefined = this.getNode(start);
582
+ for (let i = 0; i < finish - start && head !== null && head !== undefined; i++) {
583
+ list.append(head.val);
584
+ head = head.next;
585
+ }
586
+ return list;
587
+ }
588
+
589
+ /**
590
+ * The reverse() function reverses the list in place and returns the list
591
+ * itself.
592
+ * ```ts
593
+ * new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
594
+ * ```
595
+ */
596
+ public reverse(): SinglyLinkedList<NodeData> {
597
+ let currentNode = this.head;
598
+ while (currentNode) {
599
+ const next = currentNode.next;
600
+ currentNode.next = currentNode.prev;
601
+ currentNode.prev = next;
602
+ currentNode = currentNode.prev;
603
+ }
604
+ const tail = this.tail;
605
+ this.tail = this.head;
606
+ this.head = tail;
607
+ return this;
608
+ }
609
+
610
+ /**
611
+ * The forEach() method executes a provided function once for each list node.
612
+ * ```ts
613
+ * new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
614
+ * ```
615
+ * @param f Function to execute for each element, taking up to three arguments.
616
+ * @param reverse Indicates if the list should be walked in reverse order, default is false
617
+ */
618
+ public forEach(f: TMapFunction<NodeData>, reverse = false): void {
619
+ let currentIndex = reverse ? this.length - 1 : 0;
620
+ let currentNode = reverse ? this.tail : this.head;
621
+ const modifier = reverse ? -1 : 1;
622
+ const nextNode = reverse ? 'prev' : 'next';
623
+ while (currentNode) {
624
+ f(currentNode.val, currentIndex, this);
625
+ currentNode = currentNode[nextNode];
626
+ currentIndex += modifier;
627
+ }
628
+ }
629
+
630
+ /**
631
+ * The map() method creates a new list with the results of
632
+ * calling a provided function on every node in the calling list.
633
+ * ```ts
634
+ * new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
635
+ * ```
636
+ * @param f Function that produces an node of the new list, taking up to three arguments
637
+ * @param reverse Indicates if the list should be mapped in reverse order, default is false
638
+ */
639
+ // eslint-disable-next-line @typescript-eslint/ban-types
640
+ public map(f: TMapFunction<NodeData>, reverse = false): SinglyLinkedList<NodeData | {}> {
641
+ const list = new SinglyLinkedList();
642
+ this.forEach((val, index) => list.append(f(val, index, this)), reverse);
643
+ return list;
644
+ }
645
+
646
+ /**
647
+ * The filter() method creates a new list with all nodes
648
+ * that pass the test implemented by the provided function.
649
+ * ```ts
650
+ * new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
651
+ * ```
652
+ * @param f Function to test each node val in the list. Return true to keep the node
653
+ * @param reverse Indicates if the list should be filtered in reverse order, default is false
654
+ */
655
+ // eslint-disable-next-line @typescript-eslint/ban-types
656
+ public filter(f: TTestFunction<NodeData>, reverse = false): SinglyLinkedList<NodeData | {}> {
657
+ const list = new SinglyLinkedList();
658
+ this.forEach((val, index) => {
659
+ if (f(val, index, this)) {
660
+ list.append(val);
661
+ }
662
+ }, reverse);
663
+ return list;
664
+ }
665
+
666
+ /**
667
+ * Reduce over each node in the list
668
+ * ```ts
669
+ * new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
670
+ * ```
671
+ * @param f A reducer function
672
+ * @param start An initial value
673
+ * @returns The final state of the accumulator
674
+ */
675
+ public reduce(
676
+ f: (
677
+ accumulator: any,
678
+ currentNode: NodeData,
679
+ index: number,
680
+ list: SinglyLinkedList<NodeData>,
681
+ ) => any,
682
+ start?: any,
683
+ reverse = false,
684
+ ): any {
685
+ let currentIndex = reverse ? this.length - 1 : 0;
686
+ const modifier = reverse ? -1 : 1;
687
+ const nextNode = reverse ? 'prev' : 'next';
688
+ let currentElement = reverse ? this.tail : this.head;
689
+ let result;
690
+
691
+ if (start !== undefined) {
692
+ result = start;
693
+ } else if (currentElement) {
694
+ result = currentElement.val;
695
+ currentElement = currentElement[nextNode];
696
+ } else {
697
+ throw new TypeError('Reduce of empty LinkedList with no initial value');
698
+ }
699
+
700
+ while (currentElement) {
701
+ result = f(result, currentElement.val, currentIndex, this);
702
+ currentIndex += modifier;
703
+ currentElement = currentElement[nextNode];
704
+ }
705
+
706
+ return result;
707
+ }
708
+
709
+ /**
710
+ * Convert the linked list to an array
711
+ * ```ts
712
+ * new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
713
+ * ```
714
+ */
715
+ public toArray(): NodeData[] {
716
+ return [...this];
717
+ }
718
+
719
+ /**
720
+ * Convert a linked list to string
721
+ * ```ts
722
+ * new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
723
+ * ```
724
+ * @param separator Optional string to be placed in between val nodes, default is one space
725
+ */
726
+ public toString(separator = ' '): string {
727
+ return this.reduce((s, val) => `${s}${separator}${val}`);
728
+ }
729
+
730
+ /**
731
+ * The iterator implementation
732
+ * ```ts
733
+ * const list = new LinkedList(1, 2, 3);
734
+ * for (const val of list) { log(val); } // 1 2 3
735
+ * ```
736
+ */
737
+ public* [Symbol.iterator](): IterableIterator<NodeData> {
738
+ let element = this.head;
739
+
740
+ while (element !== null) {
741
+ yield element.val;
742
+ element = element.next;
743
+ }
744
+ }
745
+
746
+ /** Private helper function to reduce duplication of pop() and shift() methods */
747
+ private removeFromAnyEnd(node: SinglyLinkedListNode<NodeData> | null) {
748
+ return node !== null ? this.removeNode(node).val : undefined;
749
+ }
750
+ }