logootsropes-crdt
Version:
Provides several data structures to represent a text in a ropes-like structure and manipulating it
84 lines (77 loc) • 2.25 kB
JavaScript
var Utils = require('utils-crdt');
var Iterator = require('./iterator');
IteratorHelperIdentifier = function (id1, id2) {
this.id1 = id1;
this.id2 = id2;
this.nextOffset = null;
this.result = null;
};
/**
* Définition du type énuméré Result
*/
IteratorHelperIdentifier.Result = {
B1AfterB2: 'B1AfterB2',
B1BeforeB2: 'B1BeforeB2',
B1InsideB2: 'B1InsideB2',
B2InsideB1: 'B2InsideB1',
B1ConcatB2: 'B1ConcatB2',
B2ConcatB1: 'B2ConcatB1'
};
IteratorHelperIdentifier.prototype.compareBase = function () {
var b1 = Utils.iterator(this.id1.base);
var b2 = Utils.iterator(this.id2.base);
while (b1.hasNext() && b2.hasNext()) {
var i1 = b1.next();
var i2 = b2.next();
if (i1 > i2) {
return this.Result.B1AfterB2;
}
else if (i1 < i2) {
return this.Result.B1BeforeB2;
}
}
if (b1.hasNext()) { //b2 is shorter than b1
this.nextOffset = b1.next();
if (this.nextOffset < this.id2.begin) {
return this.Result.B1BeforeB2;
}
else if (this.nextOffset >= this.id2.end) {
return this.Result.B1AfterB2;
}
else {
return this.Result.B1InsideB2;
}
}
else if (b2.hasNext()) { //b1 is shorter than b2
this.nextOffset = b2.next();
if (this.nextOffset < this.id1.begin) {
return this.Result.B1AfterB2;
}
else if (this.nextOffset >= this.id1.end) {
return this.Result.B1BeforeB2;
}
else {
return this.Result.B2InsideB1;
}
}
else { // both bases have the same size
if (this.id1.end + 1 == this.id2.begin) {
return this.Result.B1ConcatB2;
}
else if (this.id1.begin == this.id2.end + 1) {
return this.Result.B2ConcatB1;
}
else if (this.id1.end < this.id2.begin) {
return this.Result.B1BeforeB2;
}
else {
return this.Result.B1AfterB2;
}
}
};
IteratorHelperIdentifier.prototype.computeResults = function() {
if(this.result == null)
this.result = this.compareBase();
return this.result;
};
module.exports = IteratorHelperIdentifier;