data-structure-typed 1.53.3 → 1.53.5

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.
@@ -2177,28 +2177,33 @@ var dataStructureTyped = (() => {
2177
2177
  return (_a = this.tail) == null ? void 0 : _a.value;
2178
2178
  }
2179
2179
  /**
2180
- * Time Complexity: O(n)
2181
- * Space Complexity: O(n)
2180
+ * Time Complexity: O(1)
2181
+ * Space Complexity: O(1)
2182
2182
  *
2183
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
2184
- * given array.
2185
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
2186
- * @returns The `fromArray` function returns a DoublyLinkedList object.
2183
+ * The function `isNode` in TypeScript checks if a given input is an instance of
2184
+ * `DoublyLinkedListNode`.
2185
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2186
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
2187
+ * be one of the following types:
2188
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
2189
+ * instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
2190
+ * parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
2191
+ * the function returns `false`.
2187
2192
  */
2188
- static fromArray(data) {
2189
- return new _DoublyLinkedList(data);
2193
+ isNode(elementNodeOrPredicate) {
2194
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
2190
2195
  }
2191
2196
  /**
2192
2197
  * Time Complexity: O(1)
2193
2198
  * Space Complexity: O(1)
2194
2199
  *
2195
- * The push function adds a new element to the end of a doubly linked list.
2196
- * @param {E} element - The "element" parameter represents the value that you want to add to the
2197
- * doubly linked list.
2198
- * @returns The `push` method is returning a boolean value, `true`.
2200
+ * The `push` function adds a new element or node to the end of a doubly linked list.
2201
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
2202
+ * method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
2203
+ * @returns The `push` method is returning a boolean value, specifically `true`.
2199
2204
  */
2200
- push(element) {
2201
- const newNode = new DoublyLinkedListNode(element);
2205
+ push(elementOrNode) {
2206
+ const newNode = this._ensureNode(elementOrNode);
2202
2207
  if (!this.head) {
2203
2208
  this._head = newNode;
2204
2209
  this._tail = newNode;
@@ -2254,13 +2259,14 @@ var dataStructureTyped = (() => {
2254
2259
  * Time Complexity: O(1)
2255
2260
  * Space Complexity: O(1)
2256
2261
  *
2257
- * The unshift function adds a new element to the beginning of a doubly linked list.
2258
- * @param {E} element - The "element" parameter represents the value of the element that you want to
2259
- * add to the beginning of the doubly linked list.
2260
- * @returns The `unshift` method is returning a boolean value, `true`.
2262
+ * The unshift function adds a new element or node to the beginning of a doubly linked list.
2263
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
2264
+ * `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
2265
+ * element of type `E`.
2266
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
2261
2267
  */
2262
- unshift(element) {
2263
- const newNode = new DoublyLinkedListNode(element);
2268
+ unshift(elementOrNode) {
2269
+ const newNode = this._ensureNode(elementOrNode);
2264
2270
  if (!this.head) {
2265
2271
  this._head = newNode;
2266
2272
  this._tail = newNode;
@@ -2313,16 +2319,26 @@ var dataStructureTyped = (() => {
2313
2319
  * Time Complexity: O(n)
2314
2320
  * Space Complexity: O(1)
2315
2321
  *
2316
- * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
2317
- * node if found, otherwise it returns undefined.
2318
- * @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
2319
- * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
2320
- * is found in the linked list. If no such node is found, it returns `undefined`.
2322
+ * This TypeScript function searches for a node in a doubly linked list based on a given element node
2323
+ * or predicate.
2324
+ * @param {| E
2325
+ * | DoublyLinkedListNode<E>
2326
+ * | ((node: DoublyLinkedListNode<E>) => boolean)
2327
+ * | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
2328
+ * node in a doubly linked list based on a given element, node, or predicate function. The
2329
+ * `elementNodeOrPredicate` parameter can be one of the following:
2330
+ * @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
2331
+ * input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
2332
+ * Otherwise, it iterates through the linked list starting from the head node and applies the
2333
+ * provided predicate function to each node. If a node satisfies the predicate, that node is
2334
+ * returned. If
2321
2335
  */
2322
- getNode(value) {
2336
+ getNode(elementNodeOrPredicate) {
2337
+ if (elementNodeOrPredicate === void 0) return;
2338
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2323
2339
  let current = this.head;
2324
2340
  while (current) {
2325
- if (current.value === value) {
2341
+ if (predicate(current)) {
2326
2342
  return current;
2327
2343
  }
2328
2344
  current = current.next;
@@ -2333,25 +2349,27 @@ var dataStructureTyped = (() => {
2333
2349
  * Time Complexity: O(n)
2334
2350
  * Space Complexity: O(1)
2335
2351
  *
2336
- * The `insert` function inserts a value at a specified index in a doubly linked list.
2337
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
2338
- * DoublyLinkedList. It is of type number.
2339
- * @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
2340
- * specified index.
2341
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
2342
- * if the index is out of bounds.
2352
+ * The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
2353
+ * @param {number} index - The `index` parameter in the `addAt` method represents the position at
2354
+ * which you want to add a new element or node in the doubly linked list. It indicates the location
2355
+ * where the new element or node should be inserted.
2356
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
2357
+ * `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
2358
+ * @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
2359
+ * successfully added at the specified index, and `false` if the index is out of bounds (less than 0
2360
+ * or greater than the size of the list).
2343
2361
  */
2344
- addAt(index, value) {
2362
+ addAt(index, newElementOrNode) {
2345
2363
  if (index < 0 || index > this._size) return false;
2346
2364
  if (index === 0) {
2347
- this.unshift(value);
2365
+ this.unshift(newElementOrNode);
2348
2366
  return true;
2349
2367
  }
2350
2368
  if (index === this._size) {
2351
- this.push(value);
2369
+ this.push(newElementOrNode);
2352
2370
  return true;
2353
2371
  }
2354
- const newNode = new DoublyLinkedListNode(value);
2372
+ const newNode = this._ensureNode(newElementOrNode);
2355
2373
  const prevNode = this.getNodeAt(index - 1);
2356
2374
  const nextNode = prevNode.next;
2357
2375
  newNode.prev = prevNode;
@@ -2365,24 +2383,26 @@ var dataStructureTyped = (() => {
2365
2383
  * Time Complexity: O(1) or O(n)
2366
2384
  * Space Complexity: O(1)
2367
2385
  *
2368
- * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
2369
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
2370
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
2371
- * itself.
2372
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
2373
- * list.
2374
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
2375
- * insertion fails.
2386
+ * The `addBefore` function in TypeScript adds a new element or node before an existing element or
2387
+ * node in a doubly linked list.
2388
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
2389
+ * in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
2390
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
2391
+ * represents the element or node that you want to add before the `existingElementOrNode` in a doubly
2392
+ * linked list.
2393
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
2394
+ * successfully added before the existing element or node, and `false` if the existing element or
2395
+ * node was not found.
2376
2396
  */
2377
- addBefore(existingValueOrNode, newValue) {
2397
+ addBefore(existingElementOrNode, newElementOrNode) {
2378
2398
  let existingNode;
2379
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
2380
- existingNode = existingValueOrNode;
2399
+ if (existingElementOrNode instanceof DoublyLinkedListNode) {
2400
+ existingNode = existingElementOrNode;
2381
2401
  } else {
2382
- existingNode = this.getNode(existingValueOrNode);
2402
+ existingNode = this.getNode(existingElementOrNode);
2383
2403
  }
2384
2404
  if (existingNode) {
2385
- const newNode = new DoublyLinkedListNode(newValue);
2405
+ const newNode = this._ensureNode(newElementOrNode);
2386
2406
  newNode.prev = existingNode.prev;
2387
2407
  if (existingNode.prev) {
2388
2408
  existingNode.prev.next = newNode;
@@ -2401,23 +2421,27 @@ var dataStructureTyped = (() => {
2401
2421
  * Time Complexity: O(1) or O(n)
2402
2422
  * Space Complexity: O(1)
2403
2423
  *
2404
- * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
2405
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
2406
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
2407
- * itself.
2408
- * @param {E} newValue - The value that you want to insert into the doubly linked list.
2409
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
2410
- * existing value or node is not found in the doubly linked list.
2424
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
2425
+ * in a doubly linked list.
2426
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
2427
+ * element or node in the doubly linked list after which you want to add a new element or node.
2428
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
2429
+ * `addAfter` method represents the element or node that you want to add after the existing element
2430
+ * or node in a doubly linked list. This parameter can be either an element value or a
2431
+ * `DoublyLinkedListNode` object that you want to insert
2432
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
2433
+ * successfully added after the existing element or node, and `false` if the existing element or node
2434
+ * was not found in the linked list.
2411
2435
  */
2412
- addAfter(existingValueOrNode, newValue) {
2436
+ addAfter(existingElementOrNode, newElementOrNode) {
2413
2437
  let existingNode;
2414
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
2415
- existingNode = existingValueOrNode;
2438
+ if (existingElementOrNode instanceof DoublyLinkedListNode) {
2439
+ existingNode = existingElementOrNode;
2416
2440
  } else {
2417
- existingNode = this.getNode(existingValueOrNode);
2441
+ existingNode = this.getNode(existingElementOrNode);
2418
2442
  }
2419
2443
  if (existingNode) {
2420
- const newNode = new DoublyLinkedListNode(newValue);
2444
+ const newNode = this._ensureNode(newElementOrNode);
2421
2445
  newNode.next = existingNode.next;
2422
2446
  if (existingNode.next) {
2423
2447
  existingNode.next.prev = newNode;
@@ -2464,19 +2488,17 @@ var dataStructureTyped = (() => {
2464
2488
  * Time Complexity: O(1) or O(n)
2465
2489
  * Space Complexity: O(1)
2466
2490
  *
2467
- * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
2468
- * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
2469
- * a `DoublyLinkedListNode<E>` object.
2470
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
2471
- * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
2491
+ * The `delete` function removes a specified element or node from a doubly linked list if it exists.
2492
+ * @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
2493
+ * the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
2494
+ * can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
2495
+ * doubly linked list
2496
+ * @returns The `delete` method returns a boolean value - `true` if the element or node was
2497
+ * successfully deleted from the doubly linked list, and `false` if the element or node was not found
2498
+ * in the list.
2472
2499
  */
2473
- delete(valOrNode) {
2474
- let node;
2475
- if (valOrNode instanceof DoublyLinkedListNode) {
2476
- node = valOrNode;
2477
- } else {
2478
- node = this.getNode(valOrNode);
2479
- }
2500
+ delete(elementOrNode) {
2501
+ const node = this.getNode(elementOrNode);
2480
2502
  if (node) {
2481
2503
  if (node === this.head) {
2482
2504
  this.shift();
@@ -2518,17 +2540,21 @@ var dataStructureTyped = (() => {
2518
2540
  * Time Complexity: O(n)
2519
2541
  * Space Complexity: O(1)
2520
2542
  *
2521
- * The function returns the index of the first occurrence of a given value in a linked list.
2522
- * @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
2523
- * that we are searching for in the linked list.
2524
- * @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
2525
- * list. If the value is not found, it returns -1.
2543
+ * The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
2544
+ * Linked List.
2545
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
2546
+ * `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
2547
+ * element of type `E`.
2548
+ * @returns The `indexOf` method is returning the index of the element or node in the doubly linked
2549
+ * list. If the element or node is found in the list, the method returns the index of that element or
2550
+ * node. If the element or node is not found in the list, the method returns -1.
2526
2551
  */
2527
- indexOf(value) {
2552
+ indexOf(elementOrNode) {
2553
+ const predicate = this._ensurePredicate(elementOrNode);
2528
2554
  let index = 0;
2529
2555
  let current = this.head;
2530
2556
  while (current) {
2531
- if (current.value === value) {
2557
+ if (predicate(current)) {
2532
2558
  return index;
2533
2559
  }
2534
2560
  index++;
@@ -2540,19 +2566,43 @@ var dataStructureTyped = (() => {
2540
2566
  * Time Complexity: O(n)
2541
2567
  * Space Complexity: O(1)
2542
2568
  *
2543
- * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
2544
- * value that satisfies the given callback function, or undefined if no value satisfies the callback.
2545
- * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
2546
- * function is used to determine whether a given value satisfies a certain condition.
2547
- * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
2548
- * the callback function. If no value satisfies the condition, it returns `undefined`.
2549
2569
  */
2550
- findBackward(callback) {
2570
+ /**
2571
+ * This function retrieves an element from a doubly linked list based on a given element
2572
+ * node or predicate.
2573
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2574
+ * elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
2575
+ * which can be one of the following types:
2576
+ * @returns The `get` method returns the value of the first node in the doubly linked list that
2577
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
2578
+ */
2579
+ get(elementNodeOrPredicate) {
2580
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2581
+ let current = this.head;
2582
+ while (current) {
2583
+ if (predicate(current)) return current.value;
2584
+ current = current.next;
2585
+ }
2586
+ return void 0;
2587
+ }
2588
+ /**
2589
+ * Time Complexity: O(n)
2590
+ * Space Complexity: O(1)
2591
+ *
2592
+ * The `getBackward` function searches for a specific element in a doubly linked list starting from
2593
+ * the tail and moving backwards.
2594
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2595
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
2596
+ * function can be one of the following types:
2597
+ * @returns The `getBackward` method returns the value of the element node that matches the provided
2598
+ * predicate when traversing the doubly linked list backwards. If no matching element is found, it
2599
+ * returns `undefined`.
2600
+ */
2601
+ getBackward(elementNodeOrPredicate) {
2602
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2551
2603
  let current = this.tail;
2552
2604
  while (current) {
2553
- if (callback(current.value)) {
2554
- return current.value;
2555
- }
2605
+ if (predicate(current)) return current.value;
2556
2606
  current = current.prev;
2557
2607
  }
2558
2608
  return void 0;
@@ -2674,6 +2724,18 @@ var dataStructureTyped = (() => {
2674
2724
  }
2675
2725
  return mappedList;
2676
2726
  }
2727
+ /**
2728
+ * Time Complexity: O(n)
2729
+ * Space Complexity: O(n)
2730
+ *
2731
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
2732
+ * given array.
2733
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
2734
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
2735
+ */
2736
+ static fromArray(data) {
2737
+ return new _DoublyLinkedList(data);
2738
+ }
2677
2739
  /**
2678
2740
  * The function returns an iterator that iterates over the values of a linked list.
2679
2741
  */
@@ -2684,6 +2746,45 @@ var dataStructureTyped = (() => {
2684
2746
  current = current.next;
2685
2747
  }
2686
2748
  }
2749
+ /**
2750
+ * The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
2751
+ * as an argument and returns a boolean.
2752
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2753
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
2754
+ * types:
2755
+ * @returns The _isPredicate method is returning a boolean value indicating whether the
2756
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
2757
+ * function, the method will return true, indicating that it is a predicate function.
2758
+ */
2759
+ _isPredicate(elementNodeOrPredicate) {
2760
+ return typeof elementNodeOrPredicate === "function";
2761
+ }
2762
+ /**
2763
+ * The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
2764
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
2765
+ * an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
2766
+ * @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
2767
+ * as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
2768
+ * value and returned.
2769
+ */
2770
+ _ensureNode(elementOrNode) {
2771
+ if (this.isNode(elementOrNode)) return elementOrNode;
2772
+ return new DoublyLinkedListNode(elementOrNode);
2773
+ }
2774
+ /**
2775
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
2776
+ * function, or a value to compare with the node's value.
2777
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2778
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
2779
+ * types:
2780
+ * @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
2781
+ * returns a boolean value based on the conditions specified in the code.
2782
+ */
2783
+ _ensurePredicate(elementNodeOrPredicate) {
2784
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
2785
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
2786
+ return (node) => node.value === elementNodeOrPredicate;
2787
+ }
2687
2788
  };
2688
2789
 
2689
2790
  // src/data-structures/linked-list/skip-linked-list.ts