UNPKG

itclocks

Version:

An implementation of Interval Tree Clocks in TypeScript

98 lines (97 loc) 3.19 kB
"use strict"; /** * Copyright (C) 2017 Gabriel Batista Galli * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); const ID_1 = require("./ID"); const IDs_1 = require("./IDs"); class NonLeafID extends ID_1.ID { constructor(left, right) { super(); this.left = left; this.right = right; } get value() { throw new ReferenceError("Cannot get value on an internal node"); } isOne() { return false; } isZero() { return false; } isLeaf() { return false; } normalize() { return this._normalize(this.left.normalize(), this.right.normalize()); } _normalize(left, right) { if (left.isZero() && right.isZero()) return IDs_1.IDs.zero(); if (left.isOne() && right.isOne()) return IDs_1.IDs.one(); return IDs_1.IDs.with(left, right); } split() { if (this.left.isZero()) return this.splitWithLeftZero(); if (this.right.isZero()) return this.splitWithRightZero(); return [ IDs_1.IDs.with(this.left, IDs_1.IDs.zero()), IDs_1.IDs.with(IDs_1.IDs.zero(), this.right) ]; } splitWithLeftZero() { let rightSplit = this.right.split(); return [ IDs_1.IDs.with(IDs_1.IDs.zero(), rightSplit[0]), IDs_1.IDs.with(IDs_1.IDs.zero(), rightSplit[1]) ]; } splitWithRightZero() { let leftSplit = this.left.split(); return [ IDs_1.IDs.with(leftSplit[0], IDs_1.IDs.zero()), IDs_1.IDs.with(leftSplit[1], IDs_1.IDs.zero()) ]; } sum(other) { if (other.isZero()) return this; if (!other.isLeaf()) return this.sumNonLeaf(other); throw new TypeError("Can't sum " + this + " with 1."); } sumNonLeaf(other) { let leftSum = this.left.sum(other.left); let rightSum = this.right.sum(other.right); let sum = IDs_1.IDs.with(leftSum, rightSum); return sum.normalize(); } equals(object) { if (!(object instanceof NonLeafID)) return false; let other = object; return this.left.equals(other.left) && this.right.equals(other.right); } toString() { return "(" + this.left + ", " + this.right + ")"; } } exports.NonLeafID = NonLeafID;