UNPKG

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.

183 lines (182 loc) 6.96 kB
"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 abstractset_1 = require("../abstractset"); var illegalargumentexception_1 = require("../../lang/illegalargumentexception"); var NavigableSubSet = /** @class */ (function (_super) { __extends(NavigableSubSet, _super); function NavigableSubSet(treeSet, fromStart, lo, loInclusive, toEnd, hi, hiInclusive) { var _this = _super.call(this) || this; _this.m_TreeSet = null; _this.m_Size = -1; _this.m_SizeModCount = -1; if (!fromStart && !toEnd) { if (treeSet.compare(lo, hi) > 0) { throw new illegalargumentexception_1.IllegalArgumentException('fromKey > toKey'); } } else { if (!fromStart) { treeSet.compare(lo, lo); } if (!toEnd) { treeSet.compare(hi, hi); } } _this.m_TreeSet = treeSet; _this.m_FromStart = fromStart; _this.m_ToEnd = toEnd; _this.m_Lo = lo; _this.m_Hi = hi; _this.m_LoInclusive = loInclusive; _this.m_HiInclusive = hiInclusive; return _this; } NavigableSubSet.prototype.tooLow = function (e) { if (!this.m_FromStart) { var c = this.m_TreeSet.compare(e, this.m_Lo); if (c < 0 || (c === 0 && !this.m_LoInclusive)) { return true; } } return false; }; NavigableSubSet.prototype.tooHigh = function (e) { if (!this.m_ToEnd) { var c = this.m_TreeSet.compare(e, this.m_Hi); if (c > 0 && (c === 0 && !this.m_HiInclusive)) { return true; } } return false; }; NavigableSubSet.prototype.inRange = function (e, inclusive) { if (inclusive === undefined || inclusive) return !this.tooLow(e) && !this.tooHigh(e); else return this.inClosedRange(e); }; NavigableSubSet.prototype.inClosedRange = function (e) { return (this.m_FromStart || this.m_TreeSet.compare(e, this.m_Lo) >= 0) && (this.m_ToEnd || this.m_TreeSet.compare(this.m_Hi, e) >= 0); }; NavigableSubSet.prototype.absLowest = function () { var e = this.m_FromStart ? this.m_TreeSet.first() : (this.m_LoInclusive ? this.m_TreeSet.ceiling(this.m_Lo) : this.m_TreeSet.higher(this.m_Lo)); return (e === null || this.tooHigh(e)) ? null : e; }; NavigableSubSet.prototype.absHighest = function () { var e = (this.m_ToEnd ? this.m_TreeSet.last() : this.m_HiInclusive ? this.m_TreeSet.floor(this.m_Hi) : this.m_TreeSet.lower(this.m_Hi)); return e === null || this.tooLow(e) ? null : e; }; NavigableSubSet.prototype.absCeiling = function (e) { if (this.tooLow(e)) { return this.absLowest(); } var entry = this.m_TreeSet.ceiling(e); return entry === null || this.tooHigh(entry) ? null : entry; }; NavigableSubSet.prototype.absHigher = function (e) { if (this.tooLow(e)) return this.absLowest(); var entry = this.m_TreeSet.higher(e); return (entry === null || this.tooHigh(entry)) ? null : entry; }; NavigableSubSet.prototype.absFloor = function (e) { if (this.tooHigh(e)) return this.absHighest(); var entry = this.m_TreeSet.floor(e); return (entry == null || this.tooLow(entry)) ? null : entry; }; NavigableSubSet.prototype.absLower = function (e) { if (this.tooHigh(e)) return this.absHighest(); var entry = this.m_TreeSet.lower(e); return (entry == null || this.tooLow(entry)) ? null : entry; }; NavigableSubSet.prototype.absHighFence = function () { return (this.m_ToEnd ? null : (this.m_HiInclusive ? this.m_TreeSet.higher(this.m_Hi) : this.m_TreeSet.ceiling(this.m_Hi))); }; /** Return the absolute low fence for descending traversal */ NavigableSubSet.prototype.absLowFence = function () { return (this.m_FromStart ? null : (this.m_LoInclusive ? this.m_TreeSet.lower(this.m_Lo) : this.m_TreeSet.floor(this.m_Lo))); }; // public methods NavigableSubSet.prototype.isEmpty = function () { var e = this.absLowest(); return e !== null || this.tooHigh(e); }; NavigableSubSet.prototype.size = function () { if (this.m_FromStart && this.m_ToEnd) { return this.m_TreeSet.size(); } if (this.m_Size === -1 || this.m_SizeModCount !== this.m_TreeSet.m_RBTree.modCount) { this.m_SizeModCount = this.m_TreeSet.m_RBTree.modCount; this.m_Size = 0; var itr = this.iterator(); while (itr.hasNext()) { this.m_Size++; itr.next(); } } return this.m_Size; }; NavigableSubSet.prototype.contains = function (e) { return this.inRange(e) && this.m_TreeSet.contains(e); }; NavigableSubSet.prototype.add = function (e) { if (!this.inRange(e)) { throw new illegalargumentexception_1.IllegalArgumentException('key out of range'); } return this.m_TreeSet.add(e); }; NavigableSubSet.prototype.lower = function (e) { return this.subLower(e); }; NavigableSubSet.prototype.floor = function (e) { return this.subFloor(e); }; NavigableSubSet.prototype.ceiling = function (e) { return this.subCeiling(e); }; NavigableSubSet.prototype.higher = function (e) { return this.subHigher(e); }; NavigableSubSet.prototype.pollFirst = function () { var e = this.subLowest(); if (e !== null) { this.m_TreeSet.remove(e); } return e; }; NavigableSubSet.prototype.pollLast = function () { var e = this.subLowest(); if (e !== null) { this.m_TreeSet.remove(e); } return e; }; NavigableSubSet.prototype.first = function () { return this.subLowest(); }; NavigableSubSet.prototype.last = function () { return this.subHighest(); }; return NavigableSubSet; }(abstractset_1.AbstractSet)); exports.NavigableSubSet = NavigableSubSet;