itclocks
Version:
An implementation of Interval Tree Clocks in TypeScript
87 lines (86 loc) • 3.52 kB
JavaScript
/**
* 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 ParseState_1 = require("./ParseState");
const NonLeafID_1 = require("./NonLeafID");
const LeafID_1 = require("./LeafID");
class IDs {
static zero() {
return new LeafID_1.LeafID(0);
}
static one() {
return new LeafID_1.LeafID(1);
}
static with(left, right) {
return new NonLeafID_1.NonLeafID(left, right);
}
static fromString(id) {
let node;
let nodeStack = [];
let stateStack = [ParseState_1.ParseState.VALUE];
for (let i = 0; stateStack.length > 0; i++) {
if (id[i] === ' ' || id[i] === ',')
continue;
if (id[i] === ')') {
node = nodeStack.pop();
continue;
}
let state = stateStack.pop();
switch (state) {
case ParseState_1.ParseState.VALUE:
if (id[i] === '(') {
nodeStack.push(new NonLeafID_1.NonLeafID());
stateStack.push(ParseState_1.ParseState.RIGHT);
stateStack.push(ParseState_1.ParseState.LEFT);
}
else {
node = new LeafID_1.LeafID(Number(id[i]));
}
break;
case ParseState_1.ParseState.LEFT:
if (id[i] === '(') {
node = new NonLeafID_1.NonLeafID();
nodeStack[nodeStack.length - 1].left = node;
nodeStack.push(node);
stateStack.push(ParseState_1.ParseState.RIGHT);
stateStack.push(ParseState_1.ParseState.LEFT);
}
else {
nodeStack[nodeStack.length - 1].left = new LeafID_1.LeafID(Number(id[i]));
}
break;
case ParseState_1.ParseState.RIGHT:
if (id[i] === '(') {
node = new NonLeafID_1.NonLeafID();
nodeStack[nodeStack.length - 1].right = node;
nodeStack.push(node);
stateStack.push(ParseState_1.ParseState.RIGHT);
stateStack.push(ParseState_1.ParseState.LEFT);
}
else {
nodeStack[nodeStack.length - 1].right = new LeafID_1.LeafID(Number(id[i]));
}
break;
}
}
while (nodeStack.length > 0)
node = nodeStack.pop();
return node;
}
}
exports.IDs = IDs;
;