Browse Source

Merge pull request #3376 from abpframework/feature/1650

Added Custom Methods to LinkedList for Adding and Dropping Multiple Nodes
pull/3399/head
Mehmet Erim 6 years ago
committed by GitHub
parent
commit
9fbee46837
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 628
      docs/en/UI/Angular/Linked-List.md
  2. 870
      npm/ng-packs/packages/core/src/lib/tests/linked-list.spec.ts
  3. 257
      npm/ng-packs/packages/core/src/lib/utils/linked-list.ts

628
docs/en/UI/Angular/Linked-List.md

@ -26,7 +26,7 @@ The constructor does not get any parameters.
### How to Add New Nodes
There are a few methods to create new nodes in a linked list and all of them are separately available as well as revealed from an `add` method.
There are several methods to create new nodes in a linked list and all of them are separately available as well as revealed by `add` and `addMany` methods.
@ -50,6 +50,22 @@ list.addHead('c');
#### addManyHead(values: T\[\]): ListNode\<T\>\[\]
Adds multiple nodes with given values as the first nodes in list:
```js
list.addManyHead(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
list.addManyHead(['x', 'y', 'z']);
// "x" <-> "y" <-> "z" <-> "a" <-> "b" <-> "c"
```
#### addTail(value: T): ListNode\<T\>
Adds a node with given value as the last node in list:
@ -70,6 +86,22 @@ list.addTail('c');
#### addManyTail(values: T\[\]): ListNode\<T\>\[\]
Adds multiple nodes with given values as the last nodes in list:
```js
list.addManyTail(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
list.addManyTail(['x', 'y', 'z']);
// "a" <-> "b" <-> "c" <-> "x" <-> "y" <-> "z"
```
#### addAfter(value: T, previousValue: T, compareFn = compare): ListNode\<T\>
Adds a node with given value after the first node that has the previous value:
@ -109,6 +141,40 @@ list.addAfter({ x: 0 }, { x: 2 }, (v1, v2) => v1.x === v2.x);
#### addManyAfter(values: T\[\], previousValue: T, compareFn = compare): ListNode\<T\>\[\]
Adds multiple nodes with given values after the first node that has the previous value:
```js
list.addManyTail(['a', 'b', 'b', 'c']);
// "a" <-> "b" <-> "b" <-> "c"
list.addManyAfter(['x', 'y'], 'b');
// "a" <-> "b" <-> "x" <-> "y" <-> "b" <-> "c"
```
You may pass a custom compare function to detect the searched value:
```js
list.addManyTail([{ x: 1 },{ x: 2 },{ x: 3 }]);
// {"x":1} <-> {"x":2} <-> {"x":3}
list.addManyAfter([{ x: 4 }, { x: 5 }], { x: 2 }, (v1, v2) => v1.x === v2.x);
// {"x":1} <-> {"x":2} <-> {"x":4} <-> {"x":5} <-> {"x":3}
```
> The default compare function checks deep equality, so you will rarely need to pass that parameter.
#### addBefore(value: T, nextValue: T, compareFn = compare): ListNode\<T\>
Adds a node with given value before the first node that has the next value:
@ -148,6 +214,40 @@ list.addBefore({ x: 0 }, { x: 2 }, (v1, v2) => v1.x === v2.x);
#### addManyBefore(values: T\[\], nextValue: T, compareFn = compare): ListNode\<T\>\[\]
Adds multiple nodes with given values before the first node that has the next value:
```js
list.addManyTail(['a', 'b', 'b', 'c']);
// "a" <-> "b" <-> "b" <-> "c"
list.addManyBefore(['x', 'y'], 'b');
// "a" <-> "x" <-> "y" <-> "b" <-> "b" <-> "c"
```
You may pass a custom compare function to detect the searched value:
```js
list.addManyTail([{ x: 1 },{ x: 2 },{ x: 3 }]);
// {"x":1} <-> {"x":2} <-> {"x":3}
list.addManyBefore([{ x: 4 }, { x: 5 }], { x: 2 }, (v1, v2) => v1.x === v2.x);
// {"x":1} <-> {"x":4} <-> {"x":5} <-> {"x":2} <-> {"x":3}
```
> The default compare function checks deep equality, so you will rarely need to pass that parameter.
#### addByIndex(value: T, position: number): ListNode\<T\>
Adds a node with given value at the specified position in the list:
@ -166,6 +266,52 @@ list.addByIndex('x', 2);
It works with negative index too:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
// "a" <-> "b" <-> "c"
list.addByIndex('x', -1);
// "a" <-> "b" <-> "x" <-> "c"
```
#### addManyByIndex(values: T\[\], position: number): ListNode\<T\>\[\]
Adds multiple nodes with given values at the specified position in the list:
```js
list.addManyTail(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
list.addManyByIndex(['x', 'y'], 2);
// "a" <-> "b" <-> "x" <-> "y" <-> "c"
```
It works with negative index too:
```js
list.addManyTail(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
list.addManyByIndex(['x', 'y'], -1);
// "a" <-> "b" <-> "x" <-> "y" <-> "c"
```
#### add(value: T).head(): ListNode\<T\>
Adds a node with given value as the first node in list:
@ -314,10 +460,172 @@ list.add('x').byIndex(2);
It works with negative index too:
```js
list.add('a').tail();
list.add('b').tail();
list.add('c').tail();
// "a" <-> "b" <-> "c"
list.add('x').byIndex(-1);
// "a" <-> "b" <-> "x" <-> "c"
```
> This is an alternative API for `addByIndex`.
#### addMany(values: T\[\]).head(): ListNode\<T\>\[\]
Adds multiple nodes with given values as the first nodes in list:
```js
list.addMany(['a', 'b', 'c']).head();
// "a" <-> "b" <-> "c"
list.addMany(['x', 'y', 'z']).head();
// "x" <-> "y" <-> "z" <-> "a" <-> "b" <-> "c"
```
> This is an alternative API for `addManyHead`.
#### addMany(values: T\[\]).tail(): ListNode\<T\>\[\]
Adds multiple nodes with given values as the last nodes in list:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.addMany(['x', 'y', 'z']).tail();
// "a" <-> "b" <-> "c" <-> "x" <-> "y" <-> "z"
```
> This is an alternative API for `addManyTail`.
#### addMany(values: T\[\]).after(previousValue: T, compareFn = compare): ListNode\<T\>\[\]
Adds multiple nodes with given values after the first node that has the previous value:
```js
list.addMany(['a', 'b', 'b', 'c']).tail();
// "a" <-> "b" <-> "b" <-> "c"
list.addMany(['x', 'y']).after('b');
// "a" <-> "b" <-> "x" <-> "y" <-> "b" <-> "c"
```
You may pass a custom compare function to detect the searched value:
```js
list.addMany([{ x: 1 }, { x: 2 }, { x: 3 }]).tail();
// {"x":1} <-> {"x":2} <-> {"x":3}
list.addMany([{ x: 4 }, { x: 5 }]).after({ x: 2 }, (v1, v2) => v1.x === v2.x);
// {"x":1} <-> {"x":2} <-> {"x":4} <-> {"x":5} <-> {"x":3}
```
> This is an alternative API for `addManyAfter`.
>
> The default compare function checks deep equality, so you will rarely need to pass that parameter.
#### addMany(values: T\[\]).before(nextValue: T, compareFn = compare): ListNode\<T\>\[\]
Adds multiple nodes with given values before the first node that has the next value:
```js
list.addMany(['a', 'b', 'b', 'c']).tail();
// "a" <-> "b" <-> "b" <-> "c"
list.addMany(['x', 'y']).before('b');
// "a" <-> "x" <-> "y" <-> "b" <-> "b" <-> "c"
```
You may pass a custom compare function to detect the searched value:
```js
list.addMany([{ x: 1 }, { x: 2 }, { x: 3 }]).tail();
// {"x":1} <-> {"x":2} <-> {"x":3}
list.addMany([{ x: 4 }, { x: 5 }]).before({ x: 2 }, (v1, v2) => v1.x === v2.x);
// {"x":1} <-> {"x":4} <-> {"x":5} <-> {"x":2} <-> {"x":3}
```
> This is an alternative API for `addManyBefore`.
>
> The default compare function checks deep equality, so you will rarely need to pass that parameter.
#### addMany(values: T\[\]).byIndex(position: number): ListNode\<T\>\[\]
Adds multiple nodes with given values at the specified position in the list:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.addMany(['x', 'y']).byIndex(2);
// "a" <-> "b" <-> "x" <-> "y" <-> "c"
```
It works with negative index too:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.addMany(['x', 'y']).byIndex(-1);
// "a" <-> "b" <-> "x" <-> "y" <-> "c"
```
> This is an alternative API for `addManyByIndex`.
### How to Remove Nodes
There are a few methods to remove nodes from a linked list and all of them are separately available as well as revealed from a `drop` method.
@ -329,9 +637,7 @@ There are a few methods to remove nodes from a linked list and all of them are s
Removes the first node from the list:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
@ -342,14 +648,28 @@ list.dropHead();
#### dropManyHead(count: number): ListNode\<T\>\[\]
Removes the first nodes from the list based on given count:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.dropManyHead(2);
// "c"
```
#### dropTail(): ListNode\<T\> | undefined
Removes the last node from the list:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
@ -360,14 +680,28 @@ list.dropTail();
#### dropManyTail(count: number): ListNode\<T\>\[\]
Removes the last nodes from the list based on given count:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.dropManyTail(2);
// "a"
```
#### dropByIndex(position: number): ListNode\<T\> | undefined
Removes the node with the specified position from the list:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
@ -378,16 +712,56 @@ list.dropByIndex(1);
It works with negative index too:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.dropByIndex(-2);
// "a" <-> "c"
```
#### dropManyByIndex(count: number, position: number): ListNode\<T\>\[\]
Removes the nodes starting from the specified position from the list based on given count:
```js
list.addMany(['a', 'b', 'c', 'd']).tail();
// "a" <-> "b" <-> "c" <-> "d
list.dropManyByIndex(2, 1);
// "a" <-> "d"
```
It works with negative index too:
```js
list.addMany(['a', 'b', 'c', 'd']).tail();
// "a" <-> "b" <-> "c" <-> "d
list.dropManyByIndex(2, -2);
// "a" <-> "d"
```
#### dropByValue(value: T, compareFn = compare): ListNode\<T\> | undefined
Removes the first node with given value from the list:
```js
list.addTail('a');
list.addTail('x');
list.addTail('b');
list.addTail('x');
list.addTail('c');
list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
// "a" <-> "x" <-> "b" <-> "x" <-> "c"
@ -401,11 +775,7 @@ list.dropByValue('x');
You may pass a custom compare function to detect the searched value:
```js
list.addTail({ x: 1 });
list.addTail({ x: 0 });
list.addTail({ x: 2 });
list.addTail({ x: 0 });
list.addTail({ x: 3 });
list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
// {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
@ -425,11 +795,7 @@ list.dropByValue({ x: 0 }, (v1, v2) => v1.x === v2.x);
Removes all nodes with given value from the list:
```js
list.addTail('a');
list.addTail('x');
list.addTail('b');
list.addTail('x');
list.addTail('c');
list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
// "a" <-> "x" <-> "b" <-> "x" <-> "c"
@ -443,11 +809,7 @@ list.dropByValueAll('x');
You may pass a custom compare function to detect the searched value:
```js
list.addTail({ x: 1 });
list.addTail({ x: 0 });
list.addTail({ x: 2 });
list.addTail({ x: 0 });
list.addTail({ x: 3 });
list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
// {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
@ -467,9 +829,7 @@ list.dropByValue({ x: 0 }, (v1, v2) => v1.x === v2.x);
Removes the first node in list:
```js
list.add('a').tail();
list.add('b').tail();
list.add('c').tail();
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
@ -489,9 +849,7 @@ list.drop().head();
Removes the last node in list:
```js
list.add('a').tail();
list.add('b').tail();
list.add('c').tail();
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
@ -511,9 +869,7 @@ list.drop().tail();
Removes the node with the specified position from the list:
```js
list.add('a').tail();
list.add('b').tail();
list.add('c').tail();
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
@ -524,6 +880,20 @@ list.drop().byIndex(1);
It works with negative index too:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.drop().byIndex(-2);
// "a" <-> "c"
```
> This is an alternative API for `dropByIndex`.
@ -533,11 +903,7 @@ list.drop().byIndex(1);
Removes the first node with given value from the list:
```js
list.add('a').tail();
list.add('x').tail();
list.add('b').tail();
list.add('x').tail();
list.add('c').tail();
list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
// "a" <-> "x" <-> "b" <-> "x" <-> "c"
@ -551,11 +917,7 @@ list.drop().byValue('x');
You may pass a custom compare function to detect the searched value:
```js
list.add({ x: 1 }).tail();
list.add({ x: 0 }).tail();
list.add({ x: 2 }).tail();
list.add({ x: 0 }).tail();
list.add({ x: 3 }).tail();
list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
// {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
@ -577,11 +939,7 @@ list.drop().byValue({ x: 0 }, (v1, v2) => v1.x === v2.x);
Removes all nodes with given value from the list:
```js
list.add('a').tail();
list.add('x').tail();
list.add('b').tail();
list.add('x').tail();
list.add('c').tail();
list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
// "a" <-> "x" <-> "b" <-> "x" <-> "c"
@ -595,11 +953,7 @@ list.drop().byValueAll('x');
You may pass a custom compare function to detect the searched value:
```js
list.add({ x: 1 }).tail();
list.add({ x: 0 }).tail();
list.add({ x: 2 }).tail();
list.add({ x: 0 }).tail();
list.add({ x: 3 }).tail();
list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
// {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
@ -616,6 +970,80 @@ list.drop().byValueAll({ x: 0 }, (v1, v2) => v1.x === v2.x);
#### dropMany(count: number).head(): ListNode\<T\>\[\]
Removes the first nodes from the list based on given count:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.dropMany(2).head();
// "c"
```
> This is an alternative API for `dropManyHead`.
#### dropMany(count: number).tail(): ListNode\<T\>\[\]
Removes the last nodes from the list based on given count:
```js
list.addMany(['a', 'b', 'c']).tail();
// "a" <-> "b" <-> "c"
list.dropMany(2).tail();
// "a"
```
> This is an alternative API for `dropManyTail`.
#### dropMany(count: number).byIndex(position: number): ListNode\<T\>\[\]
Removes the nodes starting from the specified position from the list based on given count:
```js
list.addMany(['a', 'b', 'c', 'd']).tail();
// "a" <-> "b" <-> "c" <-> "d
list.dropMany(2).byIndex(1);
// "a" <-> "d"
```
It works with negative index too:
```js
list.addMany(['a', 'b', 'c', 'd']).tail();
// "a" <-> "b" <-> "c" <-> "d
list.dropMany(2).byIndex(-2);
// "a" <-> "d"
```
> This is an alternative API for `dropManyByIndex`.
### How to Find Nodes
There are a few methods to find specific nodes in a linked list.
@ -627,10 +1055,7 @@ There are a few methods to find specific nodes in a linked list.
Finds the first node from the list that matches the given predicate:
```js
list.addTail('a');
list.addTail('b');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'b', 'c']);
// "a" <-> "b" <-> "b" <-> "c"
@ -650,10 +1075,7 @@ found.next.value === "b"
Finds the position of the first node from the list that matches the given predicate:
```js
list.addTail('a');
list.addTail('b');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'b', 'c']);
// "a" <-> "b" <-> "b" <-> "c"
@ -677,9 +1099,7 @@ i3 === -1
Finds and returns the node with specific position in the list:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
@ -699,10 +1119,7 @@ found.next.value === "c"
Finds the position of the first node from the list that has the given value:
```js
list.addTail('a');
list.addTail('b');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'b', 'c']);
// "a" <-> "b" <-> "b" <-> "c"
@ -724,11 +1141,7 @@ i3 === -1
You may pass a custom compare function to detect the searched value:
```js
list.addTail({ x: 1 });
list.addTail({ x: 0 });
list.addTail({ x: 2 });
list.addTail({ x: 0 });
list.addTail({ x: 3 });
list.addTailMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]);
// {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
@ -764,9 +1177,7 @@ There are a few ways to iterate over or display a linked list.
Runs a callback function on all nodes in a linked list from head to tail:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
@ -784,9 +1195,7 @@ list.forEach((node, index) => console.log(node.value + index));
A linked list is iterable. In other words, you may use methods like `for...of` on it.
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
@ -801,14 +1210,12 @@ for(const node of list) {
#### toArray(): T[]
#### toArray(): T\[\]
Converts a linked list to an array:
Converts a linked list to an array of values:
```js
list.addTail('a');
list.addTail('b');
list.addTail('c');
list.addTailMany(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
@ -821,15 +1228,32 @@ arr === ['a', 'b', 'c']
#### toNodeArray(): T\[\]
Converts a linked list to an array of nodes:
```js
list.addTailMany(['a', 'b', 'c']);
// "a" <-> "b" <-> "c"
const arr = list.toNodeArray();
/*
arr[0].value === 'a'
arr[1].value === 'a'
arr[2].value === 'a'
*/
```
#### toString(): string
Converts a linked list to a string representation of nodes and their relations:
```js
list.addTail('a');
list.addTail(2);
list.addTail('c');
list.addTail({ k: 4, v: 'd' });
list.addTailMany(['a', 2, 'c', { k: 4, v: 'd' }]);
// "a" <-> 2 <-> "c" <-> {"k":4,"v":"d"}
@ -842,3 +1266,19 @@ str === '"a" <-> 2 <-> "c" <-> {"k":4,"v":"d"}'
You may pass a custom mapper function to map values before stringifying them:
```js
list.addMany([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }]).tail();
// {"x":1} <-> {"x":2} <-> {"x":3} <-> {"x":4} <-> {"x":5}
const str = list.toString(value => value.x);
/*
str === '1 <-> 2 <-> 3 <-> 4 <-> 5'
*/
```

870
npm/ng-packs/packages/core/src/lib/tests/linked-list.spec.ts

File diff suppressed because it is too large

257
npm/ng-packs/packages/core/src/lib/utils/linked-list.ts

@ -27,65 +27,107 @@ export class LinkedList<T = any> {
return this.size;
}
private linkWith(
private attach(
value: T,
previousNode: ListNode<T> | undefined,
nextNode: ListNode<T> | undefined,
): ListNode<T> {
const node = new ListNode(value);
if (!previousNode) return this.addHead(value);
if (!nextNode) return this.addTail(value);
const node = new ListNode(value);
node.previous = previousNode;
previousNode.next = node;
node.next = nextNode;
nextNode.previous = node;
this.size += 1;
this.size++;
return node;
}
private attachMany(
values: T[],
previousNode: ListNode<T> | undefined,
nextNode: ListNode<T> | undefined,
): ListNode<T>[] {
if (!values.length) return [];
if (!previousNode) return this.addManyHead(values);
if (!nextNode) return this.addManyTail(values);
const list = new LinkedList<T>();
list.addManyTail(values);
list.first!.previous = previousNode;
previousNode.next = list.first;
list.last!.next = nextNode;
nextNode.previous = list.last;
this.size += values.length;
return list.toNodeArray();
}
private detach(node: ListNode<T>) {
if (!node.previous) return this.dropHead();
if (!node.next) return this.dropTail();
node.previous.next = node.next;
node.next.previous = node.previous;
this.size--;
return node;
}
add(value: T) {
return {
after: (previousValue: T, compareFn = compare) => {
return this.addAfter(value, previousValue, compareFn);
},
before: (nextValue: T, compareFn = compare) => {
return this.addBefore(value, nextValue, compareFn);
},
byIndex: (position: number): ListNode<T> => {
return this.addByIndex(value, position);
},
head: (): ListNode<T> => {
return this.addHead(value);
},
tail: (): ListNode<T> => {
return this.addTail(value);
},
after: (previousValue: T, compareFn: ListComparisonFn<T> = compare) =>
this.addAfter(value, previousValue, compareFn),
before: (nextValue: T, compareFn: ListComparisonFn<T> = compare) =>
this.addBefore(value, nextValue, compareFn),
byIndex: (position: number) => this.addByIndex(value, position),
head: () => this.addHead(value),
tail: () => this.addTail(value),
};
}
addMany(values: T[]) {
return {
after: (previousValue: T, compareFn: ListComparisonFn<T> = compare) =>
this.addManyAfter(values, previousValue, compareFn),
before: (nextValue: T, compareFn: ListComparisonFn<T> = compare) =>
this.addManyBefore(values, nextValue, compareFn),
byIndex: (position: number) => this.addManyByIndex(values, position),
head: () => this.addManyHead(values),
tail: () => this.addManyTail(values),
};
}
addAfter(value: T, previousValue: T, compareFn = compare): ListNode<T> {
addAfter(value: T, previousValue: T, compareFn: ListComparisonFn<T> = compare): ListNode<T> {
const previous = this.find(node => compareFn(node.value, previousValue));
return previous ? this.linkWith(value, previous, previous.next) : this.addTail(value);
return previous ? this.attach(value, previous, previous.next) : this.addTail(value);
}
addBefore(value: T, nextValue: T, compareFn = compare): ListNode<T> {
addBefore(value: T, nextValue: T, compareFn: ListComparisonFn<T> = compare): ListNode<T> {
const next = this.find(node => compareFn(node.value, nextValue));
return next ? this.linkWith(value, next.previous, next) : this.addHead(value);
return next ? this.attach(value, next.previous, next) : this.addHead(value);
}
addByIndex(value: T, position: number): ListNode<T> {
if (position < 0) position += this.size;
else if (position >= this.size) return this.addTail(value);
if (position <= 0) return this.addHead(value);
if (position >= this.size) return this.addTail(value);
const next = this.get(position)!;
return this.linkWith(value, next.previous, next);
return this.attach(value, next.previous, next);
}
addHead(value: T): ListNode<T> {
@ -97,7 +139,7 @@ export class LinkedList<T = any> {
else this.last = node;
this.first = node;
this.size += 1;
this.size++;
return node;
}
@ -114,51 +156,92 @@ export class LinkedList<T = any> {
this.last = node;
}
this.size += 1;
this.size++;
return node;
}
addManyAfter(
values: T[],
previousValue: T,
compareFn: ListComparisonFn<T> = compare,
): ListNode<T>[] {
const previous = this.find(node => compareFn(node.value, previousValue));
return previous ? this.attachMany(values, previous, previous.next) : this.addManyTail(values);
}
addManyBefore(
values: T[],
nextValue: T,
compareFn: ListComparisonFn<T> = compare,
): ListNode<T>[] {
const next = this.find(node => compareFn(node.value, nextValue));
return next ? this.attachMany(values, next.previous, next) : this.addManyHead(values);
}
addManyByIndex(values: T[], position: number): ListNode<T>[] {
if (position < 0) position += this.size;
if (position <= 0) return this.addManyHead(values);
if (position >= this.size) return this.addManyTail(values);
const next = this.get(position)!;
return this.attachMany(values, next.previous, next);
}
addManyHead(values: T[]): ListNode<T>[] {
return values.reduceRight<ListNode<T>[]>((nodes, value) => {
nodes.unshift(this.addHead(value));
return nodes;
}, []);
}
addManyTail(values: T[]): ListNode<T>[] {
return values.map(value => this.addTail(value));
}
drop() {
return {
byIndex: (position: number) => this.dropByIndex(position),
byValue: (value: T, compareFn = compare) => this.dropByValue(value, compareFn),
byValueAll: (value: T, compareFn = compare) => this.dropByValueAll(value, compareFn),
byValue: (value: T, compareFn: ListComparisonFn<T> = compare) =>
this.dropByValue(value, compareFn),
byValueAll: (value: T, compareFn: ListComparisonFn<T> = compare) =>
this.dropByValueAll(value, compareFn),
head: () => this.dropHead(),
tail: () => this.dropTail(),
};
}
dropMany(count: number) {
return {
byIndex: (position: number) => this.dropManyByIndex(count, position),
head: () => this.dropManyHead(count),
tail: () => this.dropManyTail(count),
};
}
dropByIndex(position: number): ListNode<T> | undefined {
if (position === 0) return this.dropHead();
else if (position === this.size - 1) return this.dropTail();
if (position < 0) position += this.size;
const current = this.get(position);
if (current) {
current.previous!.next = current.next;
current.next!.previous = current.previous;
this.size -= 1;
return current;
}
return undefined;
return current ? this.detach(current) : undefined;
}
dropByValue(value: T, compareFn = compare): ListNode<T> | undefined {
dropByValue(value: T, compareFn: ListComparisonFn<T> = compare): ListNode<T> | undefined {
const position = this.findIndex(node => compareFn(node.value, value));
if (position < 0) return undefined;
return this.dropByIndex(position);
return position < 0 ? undefined : this.dropByIndex(position);
}
dropByValueAll(value: T, compareFn = compare): ListNode<T>[] {
dropByValueAll(value: T, compareFn: ListComparisonFn<T> = compare): ListNode<T>[] {
const dropped: ListNode<T>[] = [];
for (let current = this.first, position = 0; current; position += 1, current = current.next) {
for (let current = this.first, position = 0; current; position++, current = current.next) {
if (compareFn(current.value, value)) {
dropped.push(this.dropByIndex(position - dropped.length)!);
}
@ -176,7 +259,7 @@ export class LinkedList<T = any> {
if (this.first) this.first.previous = undefined;
else this.last = undefined;
this.size -= 1;
this.size--;
return head;
}
@ -193,7 +276,7 @@ export class LinkedList<T = any> {
if (this.last) this.last.next = undefined;
else this.first = undefined;
this.size -= 1;
this.size--;
return tail;
}
@ -201,24 +284,66 @@ export class LinkedList<T = any> {
return undefined;
}
find(predicate: ListIteratorFunction<T>): ListNode<T> | undefined {
for (let current = this.first, position = 0; current; position += 1, current = current.next) {
dropManyByIndex(count: number, position: number): ListNode<T>[] {
if (count <= 0) return [];
if (position < 0) position = Math.max(position + this.size, 0);
else if (position >= this.size) return [];
count = Math.min(count, this.size - position);
const dropped: ListNode<T>[] = [];
while (count--) {
const current = this.get(position);
dropped.push(this.detach(current!)!);
}
return dropped;
}
dropManyHead(count: Exclude<number, 0>): ListNode<T>[] {
if (count <= 0) return [];
count = Math.min(count, this.size);
const dropped: ListNode<T>[] = [];
while (count--) dropped.unshift(this.dropHead()!);
return dropped;
}
dropManyTail(count: Exclude<number, 0>): ListNode<T>[] {
if (count <= 0) return [];
count = Math.min(count, this.size);
const dropped: ListNode<T>[] = [];
while (count--) dropped.push(this.dropTail()!);
return dropped;
}
find(predicate: ListIteratorFn<T>): ListNode<T> | undefined {
for (let current = this.first, position = 0; current; position++, current = current.next) {
if (predicate(current, position, this)) return current;
}
return undefined;
}
findIndex(predicate: ListIteratorFunction<T>): number {
for (let current = this.first, position = 0; current; position += 1, current = current.next) {
findIndex(predicate: ListIteratorFn<T>): number {
for (let current = this.first, position = 0; current; position++, current = current.next) {
if (predicate(current, position, this)) return position;
}
return -1;
}
forEach<R = boolean>(callback: ListIteratorFunction<T, R>) {
for (let node = this.first, position = 0; node; position += 1, node = node.next) {
forEach<R = boolean>(callback: ListIteratorFn<T, R>) {
for (let node = this.first, position = 0; node; position++, node = node.next) {
callback(node, position, this);
}
}
@ -227,7 +352,7 @@ export class LinkedList<T = any> {
return this.find((_, index) => position === index);
}
indexOf(value: T, compareFn = compare): number {
indexOf(value: T, compareFn: ListComparisonFn<T> = compare): number {
return this.findIndex(node => compareFn(node.value, value));
}
@ -239,20 +364,32 @@ export class LinkedList<T = any> {
return array;
}
toString(): string {
toNodeArray(): ListNode<T>[] {
const array = new Array(this.size);
this.forEach((node, index) => (array[index!] = node));
return array;
}
toString(mapperFn: ListMapperFn<T> = JSON.stringify): string {
return this.toArray()
.map(value => JSON.stringify(value))
.map(value => mapperFn(value))
.join(' <-> ');
}
*[Symbol.iterator]() {
for (let node = this.first, position = 0; node; position += 1, node = node.next) {
for (let node = this.first, position = 0; node; position++, node = node.next) {
yield node.value;
}
}
}
export type ListIteratorFunction<T = any, R = boolean> = (
export type ListMapperFn<T = any> = (value: T) => any;
export type ListComparisonFn<T = any> = (value1: T, value2: T) => boolean;
export type ListIteratorFn<T = any, R = boolean> = (
node: ListNode<T>,
index?: number,
list?: LinkedList,

Loading…
Cancel
Save