itclocks
Version:
An implementation of Interval Tree Clocks in TypeScript
93 lines (92 loc) • 4.25 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 NonLeafOccurrence_1 = require("./NonLeafOccurrence");
const LeafOccurrence_1 = require("./LeafOccurrence");
const ParseState_1 = require("./ParseState");
class Occurrences {
static zero() {
return Occurrences.with(0);
}
static with(value, left, right) {
if (left !== undefined && right !== undefined)
return new NonLeafOccurrence_1.NonLeafOccurrence(value, left, right);
return new LeafOccurrence_1.LeafOccurrence(value);
}
static fromString(occurrence) {
let node;
let nodeStack = [];
let stateStack = [ParseState_1.ParseState.VALUE];
for (let i = 0; stateStack.length > 0; i++) {
if (occurrence[i] === ' ' || occurrence[i] === ',')
continue;
if (occurrence[i] === ')') {
node = nodeStack.pop();
continue;
}
let state = stateStack.pop();
switch (state) {
case ParseState_1.ParseState.VALUE:
if (occurrence[i] === '(') {
nodeStack.push(new NonLeafOccurrence_1.NonLeafOccurrence());
stateStack.push(ParseState_1.ParseState.RIGHT);
stateStack.push(ParseState_1.ParseState.LEFT);
stateStack.push(ParseState_1.ParseState.VALUE);
}
else {
if (nodeStack.length === 0)
node = new LeafOccurrence_1.LeafOccurrence();
else
node = nodeStack[nodeStack.length - 1];
node.value = Number(occurrence[i]);
}
break;
case ParseState_1.ParseState.LEFT:
if (occurrence[i] === '(') {
node = new NonLeafOccurrence_1.NonLeafOccurrence();
nodeStack[nodeStack.length - 1].left = node;
nodeStack.push(node);
stateStack.push(ParseState_1.ParseState.RIGHT);
stateStack.push(ParseState_1.ParseState.LEFT);
stateStack.push(ParseState_1.ParseState.VALUE);
}
else {
nodeStack[nodeStack.length - 1].left = new LeafOccurrence_1.LeafOccurrence(Number(occurrence[i]));
}
break;
case ParseState_1.ParseState.RIGHT:
if (occurrence[i] === '(') {
node = new NonLeafOccurrence_1.NonLeafOccurrence();
nodeStack[nodeStack.length - 1].right = node;
nodeStack.push(node);
stateStack.push(ParseState_1.ParseState.RIGHT);
stateStack.push(ParseState_1.ParseState.LEFT);
stateStack.push(ParseState_1.ParseState.VALUE);
}
else {
nodeStack[nodeStack.length - 1].right = new LeafOccurrence_1.LeafOccurrence(Number(occurrence[i]));
}
break;
}
}
while (nodeStack.length > 0)
node = nodeStack.pop();
return node;
}
}
exports.Occurrences = Occurrences;
;