-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
406 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,207 @@ | ||
import { LinkedListItemInternal } from '../models/LinkedListItemInternal'; | ||
import { IteratorEnumerable } from '../enumerables/IteratorEnumerable'; | ||
import { Errors } from '../Errors'; | ||
import { LinkedListNode } from '../models/LinkedListNode'; | ||
import { LinkedListNodeInternal } from '../models/LinkedListNodeInternal'; | ||
|
||
import { ReadOnlyLinkedList } from './ReadOnlyLinkedList'; | ||
import { Enumerable } from './Enumerable'; | ||
|
||
/** | ||
* Represents a linked list of objects. | ||
*/ | ||
export class LinkedList<T> extends ReadOnlyLinkedList<T> { | ||
public asReadOnly(): ReadOnlyLinkedList<T> { | ||
return this; | ||
export class LinkedList<T> extends Enumerable<T> { | ||
protected sizeInternal = 0; | ||
protected headInternal: LinkedListNodeInternal<T> | undefined; | ||
protected tailInternal: LinkedListNodeInternal<T> | undefined; | ||
|
||
public constructor(source?: Iterable<T>) { | ||
super(); | ||
|
||
if (source != null) { | ||
for (const item of source) { | ||
this.addTail(item); | ||
} | ||
} | ||
} | ||
|
||
public addFirst(value: T): void { | ||
const item = new LinkedListItemInternal<T>(value); | ||
if (this.size === 0) { | ||
this.firstInternal = this.lastInternal = item; | ||
} else { | ||
this.firstInternal!.previous = item; | ||
item.next = this.firstInternal; | ||
this.firstInternal = item; | ||
public *[Symbol.iterator](): Iterator<T> { | ||
for (let node = this.headOrDefault; node != null; node = node.next) { | ||
yield node.value; | ||
} | ||
} | ||
|
||
++this.sizeInternal; | ||
public get fromTail(): Enumerable<T> { | ||
return this.nodesFromTail.select(x => x.value); | ||
} | ||
|
||
public get nodes(): Enumerable<LinkedListNode<T>> { | ||
return new IteratorEnumerable(this.nodesInternal()); | ||
} | ||
|
||
public get nodesFromTail(): Enumerable<LinkedListNode<T>> { | ||
return new IteratorEnumerable(this.nodesReverseInternal()); | ||
} | ||
|
||
public addLast(value: T): void { | ||
this.add(value); | ||
public get size(): number { | ||
return this.sizeInternal; | ||
} | ||
|
||
public removeFirst(): void { | ||
if (this.size === 0) { | ||
return; | ||
public get head(): LinkedListNode<T> { | ||
if (this.headInternal == null) { | ||
throw Errors.linkedListEmpty(); | ||
} | ||
|
||
const oldFirst = this.firstInternal; | ||
this.firstInternal = this.firstInternal!.next; | ||
oldFirst!.next = undefined; | ||
return this.headInternal; | ||
} | ||
|
||
public get headOrDefault(): LinkedListNode<T> | undefined { | ||
return this.headInternal; | ||
} | ||
|
||
if (this.firstInternal == null) { | ||
this.lastInternal = undefined; | ||
} else { | ||
this.firstInternal.previous = undefined; | ||
public get tail(): LinkedListNode<T> { | ||
if (this.tailInternal == null) { | ||
throw Errors.linkedListEmpty(); | ||
} | ||
|
||
--this.sizeInternal; | ||
return this.tailInternal; | ||
} | ||
|
||
public removeLast(): void { | ||
if (this.size === 0) { | ||
return; | ||
public get tailOrDefault(): LinkedListNode<T> | undefined { | ||
return this.tailInternal; | ||
} | ||
|
||
public find(value: T): LinkedListNode<T> { | ||
const node = this.findOrDefault(value); | ||
if (node == null) { | ||
throw Errors.valueNotFoundLinkedList(); | ||
} | ||
|
||
const oldLast = this.lastInternal; | ||
this.lastInternal = this.lastInternal!.previous; | ||
oldLast!.previous = undefined; | ||
return node; | ||
} | ||
|
||
public findOrDefault(value: T): LinkedListNode<T> | undefined { | ||
const node = this.nodes.firstOrDefault(x => x.value === value); | ||
return node; | ||
} | ||
|
||
if (this.lastInternal == null) { | ||
this.firstInternal = undefined; | ||
} else { | ||
this.lastInternal.next = undefined; | ||
public findLast(value: T): LinkedListNode<T> { | ||
const node = this.findLastOrDefault(value); | ||
if (node == null) { | ||
throw Errors.valueNotFoundLinkedList(); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
public findLastOrDefault(value: T): LinkedListNode<T> | undefined { | ||
const node = this.nodes.reverse().firstOrDefault(x => x.value === value); | ||
return node; | ||
} | ||
|
||
public addHead(value: T): void { | ||
this.addBefore(this.headInternal, value); | ||
} | ||
|
||
public addTail(value: T): void { | ||
this.addAfter(this.tailInternal, value); | ||
} | ||
|
||
public addAfter(node: LinkedListNode<T> | undefined, value: T): void { | ||
const nodeInternal = this.extractNode(node); | ||
const newNode = new LinkedListNodeInternal(value, this, nodeInternal, nodeInternal?.nextInternal); | ||
this.addNode(newNode); | ||
} | ||
|
||
public addBefore(node: LinkedListNode<T> | undefined, value: T): void { | ||
const nodeInternal = this.extractNode(node); | ||
const newNode = new LinkedListNodeInternal(value, this, nodeInternal?.previousInternal, nodeInternal); | ||
this.addNode(newNode); | ||
} | ||
|
||
public removeHead(): void { | ||
this.remove(this.head); | ||
} | ||
|
||
public removeTail(): void { | ||
this.remove(this.tail); | ||
} | ||
|
||
public remove(node: LinkedListNode<T>): void { | ||
const n = this.extractNode(node); | ||
const prev = n.previousInternal; | ||
const next = n.nextInternal; | ||
|
||
if (prev != null) { | ||
prev.nextInternal = next; | ||
} | ||
|
||
if (next != null) { | ||
next.previousInternal = prev; | ||
} | ||
|
||
if (node === this.headInternal) { | ||
this.headInternal = next; | ||
} | ||
|
||
if (node === this.tailInternal) { | ||
this.tailInternal = prev; | ||
} | ||
|
||
--this.sizeInternal; | ||
|
||
n.previousInternal = undefined; | ||
n.nextInternal = undefined; | ||
} | ||
|
||
public clear(): void { | ||
this.sizeInternal = 0; | ||
this.firstInternal = this.lastInternal = undefined; | ||
this.headInternal = this.tailInternal = undefined; | ||
} | ||
|
||
private addNode(node: LinkedListNodeInternal<T>): void { | ||
if (node.previousInternal != null) { | ||
node.previousInternal.nextInternal = node; | ||
} | ||
|
||
if (node.nextInternal != null) { | ||
node.nextInternal.previousInternal = node; | ||
} | ||
|
||
if (node.previousInternal == null) { | ||
this.headInternal = node; | ||
} | ||
|
||
if (node.nextInternal == null) { | ||
this.tailInternal = node; | ||
} | ||
|
||
++this.sizeInternal; | ||
} | ||
|
||
private extractNode(node: LinkedListNode<T>): LinkedListNodeInternal<T>; | ||
private extractNode(node: LinkedListNode<T> | undefined): LinkedListNodeInternal<T> | undefined; | ||
private extractNode(node: LinkedListNode<T> | undefined): LinkedListNodeInternal<T> | undefined { | ||
if (node == null) { | ||
return undefined; | ||
} | ||
|
||
if (node instanceof LinkedListNodeInternal) { | ||
if (node.linkedList === this) { | ||
return node; | ||
} | ||
} | ||
|
||
throw new Error('Node does not belong to LinkedList'); | ||
} | ||
|
||
private *nodesInternal(): Iterator<LinkedListNodeInternal<T>> { | ||
for (let node = this.headInternal; node != null; node = node.nextInternal) { | ||
yield node; | ||
} | ||
} | ||
|
||
private *nodesReverseInternal(): Iterator<LinkedListNodeInternal<T>> { | ||
for (let node = this.tailInternal; node != null; node = node.previousInternal) { | ||
yield node; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.