ts-collection
Version:
This is re-write of the java collection classes in typescript. There is some tweak as typescript templates are not as equivalent as Java.
490 lines (489 loc) • 15.6 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var AbstractSequentialList_1 = require("./AbstractSequentialList");
var nosuchelementexception_1 = require("./nosuchelementexception");
var objects_1 = require("./objects");
var indexoutofboundsexception_1 = require("./indexoutofboundsexception");
var concurrentmodificationexception_1 = require("./concurrentmodificationexception");
var illegalstateexception_1 = require("../lang/illegalstateexception");
var unsupportedoperationexception_1 = require("../lang/unsupportedoperationexception");
var Node = /** @class */ (function () {
function Node(prev, element, next) {
this.prev = prev;
this.element = element;
this.next = next;
}
return Node;
}());
var LinkedList = /** @class */ (function (_super) {
__extends(LinkedList, _super);
function LinkedList(c) {
var _this = _super.call(this) || this;
if (c !== undefined || c !== null) {
_this.addAll(c);
}
return _this;
}
LinkedList.prototype.linkFirst = function (e) {
var f = this.m_First;
var newNode = new Node(null, e, f);
this.m_First = newNode;
if (f == null)
this.m_Last = newNode;
else
f.prev = newNode;
this.m_Size++;
this.modCount++;
};
LinkedList.prototype.linkLast = function (e) {
var l = this.m_Last;
var newNode = new Node(l, e, null);
this.m_Last = newNode;
if (l == null)
this.m_First = newNode;
else
l.next = newNode;
this.m_Size++;
this.modCount++;
};
/**
* Inserts element e before non-null Node succ.
*/
LinkedList.prototype.linkBefore = function (e, succ) {
var pred = succ.prev;
var newNode = new Node(pred, e, succ);
succ.prev = newNode;
if (pred == null)
this.m_First = newNode;
else
pred.next = newNode;
this.m_Size++;
this.modCount++;
};
/**
* Unlinks non-null first node f.
*/
LinkedList.prototype.unlinkFirst = function (f) {
// assert f == first && f != null;
var element = f.element;
var next = f.next;
f.element = null;
f.next = null; // help GC
this.m_First = next;
if (next == null)
this.m_Last = null;
else
next.prev = null;
this.m_Size--;
this.modCount++;
return element;
};
/**
* Unlinks non-null last node l.
*/
LinkedList.prototype.unlinkLast = function (l) {
// assert l == last && l != null;
var element = l.element;
var prev = l.prev;
l.element = null;
l.prev = null; // help GC
this.m_Last = prev;
if (prev == null)
this.m_First = null;
else
prev.next = null;
this.m_Size--;
this.modCount++;
return element;
};
/**
* Unlinks non-null node x.
*/
LinkedList.prototype.unlink = function (x) {
// assert x != null;
var element = x.element;
var next = x.next;
var prev = x.prev;
if (prev == null) {
this.m_First = next;
}
else {
prev.next = next;
x.prev = null;
}
if (next == null) {
this.m_Last = prev;
}
else {
next.prev = prev;
x.next = null;
}
x.element = null;
this.m_Size--;
this.modCount++;
return element;
};
LinkedList.prototype.enqueue = function (e) {
throw new unsupportedoperationexception_1.UnsupportedOperationException();
};
LinkedList.prototype.dequeue = function () {
throw new unsupportedoperationexception_1.UnsupportedOperationException();
};
LinkedList.prototype.getFirst = function () {
var f = this.m_First;
if (f === null) {
throw new nosuchelementexception_1.NoSuchElementException();
}
return f.element;
};
LinkedList.prototype.getLast = function () {
var l = this.m_Last;
if (l === null) {
throw new nosuchelementexception_1.NoSuchElementException();
}
return l.element;
};
LinkedList.prototype.removeFirst = function () {
var f = this.m_First;
if (f === null) {
throw new nosuchelementexception_1.NoSuchElementException();
}
return this.unlinkFirst(f);
};
LinkedList.prototype.removeLast = function () {
var l = this.m_Last;
if (l === null) {
throw new nosuchelementexception_1.NoSuchElementException();
}
return this.unlinkLast(l);
};
LinkedList.prototype.addFirst = function (e) {
this.linkFirst(e);
};
LinkedList.prototype.addLast = function (e) {
this.linkLast(e);
};
LinkedList.prototype.contains = function (e) {
return this.indexOf(e) !== -1;
};
LinkedList.prototype.size = function () {
return this.m_Size;
};
LinkedList.prototype.add = function (e) {
this.linkFirst(e);
return true;
};
LinkedList.prototype.remove = function (e) {
for (var x = this.m_First; x !== null; x = x.next) {
if (objects_1.Objects.equals(e, x.element)) {
this.unlink(x);
return true;
}
}
return false;
};
LinkedList.prototype.addAll = function (c) {
return this.addAllFrom(0, c);
};
LinkedList.prototype.addAllFrom = function (index, c) {
this.checkPositionIndex(index);
var cSize = c.size();
if (cSize === 0) {
return false;
}
var pred = null;
var succ = null;
if (index === this.m_Size) {
succ = null;
pred = this.m_Last;
}
else {
succ = this.nodeAt(index);
pred = succ.prev;
}
var iter = c.iterator();
while (iter.hasNext()) {
var e = iter.next();
var newNode = new Node(pred, e, null);
if (pred === null) {
this.m_First = newNode;
}
else {
pred.next = newNode;
}
}
if (succ === null) {
this.m_Last = pred;
}
else {
pred.next = succ;
succ.prev = pred;
}
this.m_Size += cSize;
this.modCount++;
return true;
};
LinkedList.prototype.clear = function () {
for (var x = this.m_First; x !== null;) {
var next = x.next;
x.element = null;
x.next = null;
x.prev = null;
x = next;
}
this.m_First = this.m_Last = null;
this.m_Size = 0;
this.modCount = 0;
};
LinkedList.prototype.get = function (index) {
this.checkElementIndex(index);
return this.nodeAt(index).element;
};
LinkedList.prototype.setAt = function (index, element) {
this.checkElementIndex(index);
var x = this.nodeAt(index);
var oldVal = x.element;
x.element = element;
return oldVal;
};
LinkedList.prototype.addAt = function (index, element) {
this.checkPositionIndex(index);
if (index === this.m_Size) {
this.linkLast(element);
}
else {
this.linkBefore(element, this.nodeAt(index));
}
};
LinkedList.prototype.removeAt = function (index) {
this.checkElementIndex(index);
return this.unlink(this.nodeAt(index));
};
LinkedList.prototype.checkElementIndex = function (index) {
if (this.isElementIndex(index)) {
}
};
LinkedList.prototype.isElementIndex = function (index) {
return index >= 0 && index < this.m_Size;
};
LinkedList.prototype.isPositionIndex = function (index) {
return index > 0 && index <= this.m_Size;
};
LinkedList.prototype.outOfBoundsMsg = function (index) {
return 'Index: ' + index + ", Size: " + this.m_Size;
};
LinkedList.prototype.checkPositionIndex = function (index) {
if (!this.isPositionIndex(index)) {
throw new indexoutofboundsexception_1.IndexOutOfBoundsException(this.outOfBoundsMsg(index));
}
};
LinkedList.prototype.nodeAt = function (index) {
if (index < (this.m_Size >> 1)) {
var x = this.m_First;
for (var i = 0; i < index; i++) {
x = x.next;
}
return x;
}
else {
var x = this.m_Last;
for (var i = this.m_Size - 1; i > index; i--) {
x = x.prev;
}
return x;
}
};
LinkedList.prototype.indexOf = function (e) {
var index = 0;
for (var x = this.m_First; x !== null; x = x.next) {
if (objects_1.Objects.equals(e, x.element)) {
return index;
}
index++;
}
return -1;
};
LinkedList.prototype.lastIndexOf = function (e) {
var index = 0;
for (var x = this.m_Last; x !== null; x = x.prev) {
if (objects_1.Objects.equals(e, x.element)) {
return index;
}
index++;
}
return -1;
};
LinkedList.prototype.peek = function () {
var f = this.m_First;
return (f === null) ? null : f.element;
};
LinkedList.prototype.element = function () {
return this.getFirst();
};
LinkedList.prototype.poll = function () {
var f = this.m_First;
return f === null ? null : this.unlinkFirst(f);
};
LinkedList.prototype.offer = function (e) {
return this.add(e);
};
LinkedList.prototype.offerFirst = function (e) {
this.addFirst(e);
return true;
};
LinkedList.prototype.offerLast = function (e) {
this.addLast(e);
return true;
};
LinkedList.prototype.peekFirst = function () {
var f = this.m_First;
return f === null ? null : f.element;
};
LinkedList.prototype.peekLast = function () {
var f = this.m_Last;
return f === null ? null : f.element;
};
LinkedList.prototype.pollFirst = function () {
var f = this.m_First;
return f === null ? null : this.unlinkFirst(f);
};
LinkedList.prototype.pollLast = function () {
var l = this.m_Last;
return l === null ? null : this.unlinkLast(l);
};
LinkedList.prototype.push = function (e) {
this.addFirst(e);
};
LinkedList.prototype.pop = function () {
return this.removeFirst();
};
LinkedList.prototype.removeFirstOccurance = function (e) {
return this.remove(e);
};
LinkedList.prototype.removeLastOccurance = function (e) {
for (var x = this.m_Last; x !== null; x = x.prev) {
if (objects_1.Objects.equals(e, x.element)) {
this.unlink(x);
return true;
}
}
return false;
};
LinkedList.prototype.listIteratorFrom = function (index) {
this.checkPositionIndex(index);
return new ListItr(this, index);
};
LinkedList.prototype.descendingIterator = function () {
return new DescendingIterator(this);
};
return LinkedList;
}(AbstractSequentialList_1.AbstractSequentialList));
exports.LinkedList = LinkedList;
var ListItr = /** @class */ (function () {
function ListItr(linkedList, index) {
this.linkedList = linkedList;
this.m_Next = (index === linkedList.size()) ? null : linkedList.nodeAt(index);
this.m_NextIndex = index;
this.m_ExpectedModeCount = linkedList.modCount;
}
ListItr.prototype.hasNext = function () {
return this.m_NextIndex < this.linkedList.size();
};
ListItr.prototype.next = function () {
this.checkForComodification();
if (!this.hasNext()) {
throw new nosuchelementexception_1.NoSuchElementException();
}
this.m_LastRetruned = this.m_Next;
this.m_Next = this.m_Next.next;
this.m_NextIndex++;
return this.m_LastRetruned.element;
};
ListItr.prototype.hasPrevious = function () {
return this.m_NextIndex > 0;
};
ListItr.prototype.previous = function () {
this.checkForComodification();
if (!this.hasPrevious()) {
throw new nosuchelementexception_1.NoSuchElementException();
}
this.m_LastRetruned = this.m_Next = (this.m_Next === null) ? this.linkedList.m_Last : this.m_Next.prev;
this.m_NextIndex--;
return this.m_LastRetruned.element;
};
ListItr.prototype.nextIndex = function () {
return this.m_NextIndex;
};
ListItr.prototype.previousIndex = function () {
return this.m_NextIndex - 1;
};
ListItr.prototype.remove = function () {
this.checkForComodification();
if (this.m_LastRetruned === null) {
throw new illegalstateexception_1.IllegalStateException();
}
var lastNext = this.m_LastRetruned.next;
this.linkedList.unlink(this.m_LastRetruned);
if (this.m_Next === this.m_LastRetruned) {
this.m_Next = lastNext;
}
else {
this.m_NextIndex--;
}
this.m_LastRetruned = null;
this.m_ExpectedModeCount++;
};
ListItr.prototype.set = function (e) {
if (this.m_LastRetruned === null) {
throw new illegalstateexception_1.IllegalStateException();
}
this.checkForComodification();
this.m_LastRetruned.element = e;
};
ListItr.prototype.add = function (e) {
this.checkForComodification();
this.m_LastRetruned = null;
if (this.m_Next === null) {
this.linkedList.linkLast(e);
}
else {
this.linkedList.linkBefore(e, this.m_Next);
}
this.m_NextIndex++;
this.m_ExpectedModeCount++;
};
ListItr.prototype.checkForComodification = function () {
if (this.linkedList.modCount !== this.m_ExpectedModeCount) {
throw new concurrentmodificationexception_1.ConcurrentModificationException();
}
};
return ListItr;
}());
var DescendingIterator = /** @class */ (function () {
function DescendingIterator(linkedList) {
this.listItr = new ListItr(linkedList, linkedList.size());
}
DescendingIterator.prototype.hasNext = function () {
return this.listItr.hasPrevious();
};
DescendingIterator.prototype.next = function () {
return this.listItr.previous();
};
DescendingIterator.prototype.remove = function () {
this.listItr.remove();
};
return DescendingIterator;
}());