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.
294 lines (293 loc) • 9.93 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 abstractmap_1 = require("../abstractmap");
var treemap_1 = require("../treemap");
var illegalargumentexception_1 = require("../../lang/illegalargumentexception");
var TreeMapKeySet_1 = require("./TreeMapKeySet");
var NavigableSubMap = /** @class */ (function (_super) {
__extends(NavigableSubMap, _super);
function NavigableSubMap(map, m_FromStart, lo, loInclusive, m_ToEnd, hi, hiInclusive) {
var _this = _super.call(this) || this;
_this.map = map;
_this.m_FromStart = m_FromStart;
_this.lo = lo;
_this.loInclusive = loInclusive;
_this.m_ToEnd = m_ToEnd;
_this.hi = hi;
_this.hiInclusive = hiInclusive;
_this.navigableKeySetView = null;
if (!m_FromStart && !m_ToEnd) {
if (_this.map.compareKeys(lo, hi) > 0) {
throw new illegalargumentexception_1.IllegalArgumentException('fromKey > toKey');
}
}
else {
if (!m_FromStart) {
_this.map.compareKeys(lo, lo);
}
if (!m_ToEnd) {
_this.map.compareKeys(hi, hi);
}
}
return _this;
}
Object.defineProperty(NavigableSubMap.prototype, "modCount", {
get: function () {
return this.map.modCount;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NavigableSubMap.prototype, "fromStart", {
get: function () {
return this.m_FromStart;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NavigableSubMap.prototype, "toEnd", {
get: function () {
return this.m_ToEnd;
},
enumerable: true,
configurable: true
});
NavigableSubMap.prototype.tooLow = function (key) {
if (!this.m_FromStart) {
var c = this.map.compareKeys(key, this.lo);
if (c < 0 || (c === 0 && !this.loInclusive)) {
return true;
}
}
return false;
};
NavigableSubMap.prototype.tooHigh = function (key) {
if (!this.m_ToEnd) {
var c = this.map.compareKeys(key, this.hi);
if (c > 0 || (c === 0 && !this.hiInclusive)) {
return true;
}
}
return false;
};
NavigableSubMap.prototype.inRange = function (key, inclusive) {
var inc = true;
if (inclusive !== undefined) {
inc = inclusive;
}
if (inc) {
return !this.tooLow(key) && !this.tooHigh(key);
}
else {
return this.inClosedRange(key);
}
};
NavigableSubMap.prototype.inClosedRange = function (key) {
return (this.m_FromStart || this.map.compareKeys(key, this.lo) >= 0) && (this.m_ToEnd || this.map.compareKeys(this.hi, key) >= 0);
};
NavigableSubMap.prototype.absLowest = function () {
var e = null;
if (this.m_FromStart) {
e = this.map.firstEntry();
}
else {
if (this.loInclusive) {
e = this.map.ceilingEntry(this.lo);
}
else {
e = this.map.higherEntry(this.lo);
}
}
if (e === null || this.tooHigh(e.key)) {
return null;
}
return e;
};
NavigableSubMap.prototype.absHighest = function () {
var e = null;
if (this.m_ToEnd) {
e = this.map.lastEntry();
}
else {
if (this.hiInclusive) {
e = this.map.floorEntry(this.hi);
}
else {
e = this.map.lowerEntry(this.hi);
}
}
if (e === null || this.tooLow(e.key)) {
return null;
}
return e;
};
NavigableSubMap.prototype.absCeiling = function (key) {
if (this.tooLow(key)) {
return this.absLowest();
}
var e = this.map.ceilingEntry(key);
if (e === null || this.tooHigh(e.key)) {
return null;
}
return e;
};
NavigableSubMap.prototype.absHigher = function (key) {
if (this.tooLow(key)) {
return this.absLowest();
}
var e = this.map.ceilingEntry(key);
if (e === null || this.tooHigh(e.getKey())) {
return null;
}
return e;
};
NavigableSubMap.prototype.absFloor = function (key) {
if (this.tooHigh(key)) {
return this.absHighest();
}
var e = this.map.lowerEntry(key);
if (e === null || this.tooLow(e.key)) {
return null;
}
return e;
};
NavigableSubMap.prototype.absLower = function (key) {
if (this.tooHigh(key)) {
return this.absHigher(key);
}
var e = this.map.lowerEntry(key);
if (e === null || this.tooLow(e.key)) {
return null;
}
return e;
};
NavigableSubMap.prototype.absHighFence = function () {
if (this.m_ToEnd) {
return null;
}
else {
if (this.hiInclusive) {
return this.map.higherEntry(this.hi);
}
else {
return this.map.ceilingEntry(this.hi);
}
}
};
NavigableSubMap.prototype.absLowFence = function () {
if (this.m_FromStart) {
return null;
}
else {
if (this.loInclusive) {
return this.map.lowerEntry(this.lo);
}
else {
return this.map.ceilingEntry(this.hi);
}
}
};
NavigableSubMap.prototype.isEmpty = function () {
return (this.m_FromStart && this.m_ToEnd) ? this.map.isEmpty() : this.entrySet().isEmpty();
};
NavigableSubMap.prototype.size = function () {
return (this.m_FromStart && this.m_ToEnd) ? this.map.size() : this.entrySet().size();
};
NavigableSubMap.prototype.containsKey = function (key) {
return this.inRange(key) && this.map.containsKey(key);
};
NavigableSubMap.prototype.put = function (key, value) {
if (!this.inRange(key)) {
throw new illegalargumentexception_1.IllegalArgumentException("Key out of range");
}
return this.map.put(key, value);
};
NavigableSubMap.prototype.get = function (key) {
if (!this.inRange(key)) {
return null;
}
return this.map.get(key);
};
NavigableSubMap.prototype.remove = function (key) {
return !this.inRange(key) ? null : this.map.remove(key);
};
NavigableSubMap.prototype.ceilingEntry = function (key) {
return this.map.exportEntry(this.subCeiling(key));
};
NavigableSubMap.prototype.ceilingKey = function (key) {
return treemap_1.TreeMap.keyOrNull(this.subCeiling(key));
};
NavigableSubMap.prototype.higherEntry = function (key) {
return this.map.exportEntry(this.subHigher(key));
};
NavigableSubMap.prototype.higherKey = function (key) {
return treemap_1.TreeMap.keyOrNull(this.subHigher(key));
};
NavigableSubMap.prototype.floorEntry = function (key) {
return this.map.exportEntry(this.subFloor(key));
};
NavigableSubMap.prototype.floorKey = function (key) {
return treemap_1.TreeMap.keyOrNull(this.subFloor(key));
};
NavigableSubMap.prototype.lowerEntry = function (key) {
return this.map.exportEntry(this.subLower(key));
};
NavigableSubMap.prototype.lowerKey = function (key) {
return treemap_1.TreeMap.keyOrNull(this.subLower(key));
};
NavigableSubMap.prototype.firstKey = function () {
return treemap_1.TreeMap.keyOrNull(this.subLowest());
};
NavigableSubMap.prototype.lastKey = function () {
return treemap_1.TreeMap.keyOrNull(this.subHighest());
};
NavigableSubMap.prototype.firstEntry = function () {
return this.map.exportEntry(this.subLowest());
};
NavigableSubMap.prototype.lastEntry = function () {
return this.map.exportEntry(this.subHighest());
};
NavigableSubMap.prototype.pollFirstEntry = function () {
var e = this.subLowest();
var result = this.map.exportEntry(e);
if (e !== null) {
this.map.remove(e.getKey());
}
return result;
};
NavigableSubMap.prototype.pollLastEntry = function () {
var e = this.subHighest();
var result = this.map.exportEntry(e);
if (e !== null) {
this.map.remove(e.getKey());
}
return result;
};
NavigableSubMap.prototype.navigableKeySet = function () {
if (this.navigableKeySetView === null) {
this.navigableKeySetView = new TreeMapKeySet_1.TreeMapKeySet(this.map);
}
return this.navigableKeySetView;
};
NavigableSubMap.prototype.keySet = function () {
return this.navigableKeySet();
};
NavigableSubMap.prototype.descendingKeySet = function () {
return this.descendingMap().navigableKeySet();
};
return NavigableSubMap;
}(abstractmap_1.AbstractMap));
exports.NavigableSubMap = NavigableSubMap;