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.
260 lines (259 loc) • 9.18 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 abstractqueue_1 = require("./abstractqueue");
var comparator_1 = require("./comparator");
var collection_1 = require("./collection");
var nullpointerexception_1 = require("../lang/nullpointerexception");
var concurrentmodificationexception_1 = require("./concurrentmodificationexception");
var nosuchelementexception_1 = require("./nosuchelementexception");
var sortedset_1 = require("./sortedset");
var objects_1 = require("./objects");
var PriorityQueue = /** @class */ (function (_super) {
__extends(PriorityQueue, _super);
function PriorityQueue(cmpOrCollection) {
var _this = _super.call(this) || this;
_this.m_Elements = new Array();
_this.m_Comparator = null;
_this.modCount = 0;
_this.m_Elements = new Array();
_this.m_Comparator = null;
if (comparator_1.isComparator(cmpOrCollection)) {
_this.m_Comparator = cmpOrCollection;
}
else if (collection_1.isCollection(cmpOrCollection)) {
var collection = cmpOrCollection;
if (sortedset_1.isSortedSet(collection)) {
var sortedSet = collection;
_this.m_Comparator = sortedSet.comparator();
_this.initElementsFromSortedSet(sortedSet);
}
else if (collection instanceof PriorityQueue) {
var pq = collection;
_this.m_Comparator = pq.comparator();
_this.initFromPriorityQueue(pq);
}
else {
_this.m_Comparator = null;
_this.initFromCollection(collection);
}
}
_this.modCount = 0;
return _this;
}
PriorityQueue.prototype.initFromPriorityQueue = function (pq) {
this.m_Comparator = pq.comparator();
for (var i = 0; i < pq.m_Elements.length; i++) {
this.m_Elements.push(pq.m_Elements[i]);
}
};
PriorityQueue.prototype.initElementsFromSortedSet = function (ss) {
var comparator = this.comparator();
if (ss.size() === 1 || comparator !== null) {
var itr_1 = ss.iterator();
while (itr_1.hasNext()) {
var ssElem = itr_1.next();
if (ssElem === null) {
throw new nullpointerexception_1.NullPointerException();
}
}
}
var itr = ss.iterator();
while (itr.hasNext()) {
this.m_Elements.push(itr.next());
}
};
PriorityQueue.prototype.initFromCollection = function (c) {
var itr = c.iterator();
while (itr.hasNext()) {
this.m_Elements.push(itr.next());
}
this.buildHeap();
};
PriorityQueue.prototype.comparator = function () {
return this.m_Comparator;
};
PriorityQueue.prototype.add = function (e) {
return this.offer(e);
};
PriorityQueue.prototype.offer = function (e) {
if (e === null) {
throw new nullpointerexception_1.NullPointerException();
}
this.modCount++;
this.m_Elements.push(e);
var length = this.m_Elements.length;
if (length > 1) {
this.heapifyPath(length - 1);
}
return true;
};
PriorityQueue.prototype.peek = function () {
if (this.m_Elements.length === 0) {
return null;
}
return this.m_Elements[0];
};
PriorityQueue.prototype.indexOf = function (e) {
for (var i = 0; i < this.m_Elements.length; i++) {
if (objects_1.Objects.equals(e, this.m_Elements[i])) {
return i;
}
}
return -1;
};
PriorityQueue.prototype.remove = function (o) {
var i = this.indexOf(o);
if (i === -1) {
return false;
}
this.removeAt(i);
return true;
};
PriorityQueue.prototype.contains = function (o) {
return this.indexOf(o) !== -1;
};
PriorityQueue.prototype.clear = function () {
this.m_Elements = new Array();
this.modCount++;
};
PriorityQueue.prototype.size = function () {
return this.m_Elements.length;
};
PriorityQueue.prototype.poll = function () {
if (this.m_Elements.length === 0) {
return null;
}
return this.removeAt(0);
};
PriorityQueue.prototype.iterator = function () {
return new Itr(this);
};
PriorityQueue.prototype.removeAt = function (index) {
if (index < 0 || index >= this.m_Elements.length) {
throw new nosuchelementexception_1.NoSuchElementException();
}
this.modCount++;
if (this.m_Elements.length - 1 === index) {
var e = this.m_Elements[this.m_Elements.length - 1];
this.m_Elements.splice(this.m_Elements.length - 1, 1);
return e;
}
else {
var e = this.m_Elements[index];
this.m_Elements[index] = this.m_Elements[this.m_Elements.length - 1];
this.m_Elements.splice(this.m_Elements.length - 1, 1);
this.heapify(index);
return e;
}
};
PriorityQueue.prototype.parent = function (index) {
return Math.floor(index / 2);
};
PriorityQueue.prototype.left = function (index) {
return 2 * index + 1;
};
PriorityQueue.prototype.right = function (index) {
return 2 * index + 2;
};
PriorityQueue.prototype.compare = function (e1, e2) {
if (this.m_Comparator !== null) {
return this.m_Comparator.compare(e1, e2);
}
var ec1 = e1;
return ec1.compareTo(e2);
};
PriorityQueue.prototype.heapifyPath = function (index) {
if (index < 1 || index > this.m_Elements.length - 1) {
return;
}
var p = this.parent(index);
while (index > 0) {
var cmp = this.compare(this.m_Elements[p], this.m_Elements[index]);
if (cmp > 0) {
this.swap(index, p);
}
index = p;
p = this.parent(p);
}
};
PriorityQueue.prototype.swap = function (i1, i2) {
var tmp = this.m_Elements[i1];
this.m_Elements[i1] = this.m_Elements[i2];
this.m_Elements[i2] = tmp;
};
PriorityQueue.prototype.heapify = function (index) {
if (index < 0 || index >= this.m_Elements.length) {
return;
}
var l = this.left(index);
var r = this.right(index);
var largest = index;
if (l < this.m_Elements.length && this.compare(this.m_Elements[largest], this.m_Elements[l]) > 0) {
largest = l;
}
if (r < this.m_Elements.length && this.compare(this.m_Elements[largest], this.m_Elements[r]) > 0) {
largest = r;
}
if (largest !== index) {
this.swap(index, largest);
this.heapify(largest);
}
};
PriorityQueue.prototype.buildHeap = function () {
var index = Math.floor(this.m_Elements.length / 2);
for (var i = index; i > 0; i--) {
this.heapify(i);
}
};
return PriorityQueue;
}(abstractqueue_1.AbstractQueue));
exports.PriorityQueue = PriorityQueue;
var Itr = /** @class */ (function () {
function Itr(iteratorOf) {
this.iteratorOf = iteratorOf;
this.cursor = 0;
this.lastRet = -1;
this.expectedModCount = iteratorOf.modCount;
}
Itr.prototype.hasNext = function () {
if (this.cursor < this.iteratorOf.size()) {
return true;
}
return false;
};
Itr.prototype.next = function () {
if (this.expectedModCount !== this.iteratorOf.modCount) {
throw new concurrentmodificationexception_1.ConcurrentModificationException();
}
if (this.cursor < this.iteratorOf.size()) {
this.lastRet = this.cursor;
var e = this.iteratorOf.m_Elements[this.lastRet];
this.cursor++;
return e;
}
throw new nosuchelementexception_1.NoSuchElementException();
};
Itr.prototype.remove = function () {
if (this.expectedModCount !== this.iteratorOf.modCount) {
throw new concurrentmodificationexception_1.ConcurrentModificationException();
}
if (this.lastRet !== -1) {
var e = this.iteratorOf.removeAt(this.lastRet);
}
};
return Itr;
}());
exports.Itr = Itr;