added comments to most of the classes.
This commit is contained in:
@@ -1,15 +1,6 @@
|
||||
/* global Y */
|
||||
'use strict'
|
||||
|
||||
function copyObject (o) {
|
||||
var c = {}
|
||||
for (var key in o) {
|
||||
c[key] = o[key]
|
||||
}
|
||||
return c
|
||||
}
|
||||
Y.utils.copyObject = copyObject
|
||||
|
||||
class DeleteStore extends Y.utils.RBTree {
|
||||
constructor () {
|
||||
super()
|
||||
@@ -120,8 +111,8 @@ class DeleteStore extends Y.utils.RBTree {
|
||||
|
||||
Y.utils.DeleteStore = DeleteStore
|
||||
|
||||
Y.Memory = (function () { // eslint-disable-line no-unused-vars
|
||||
class Transaction extends Y.AbstractTransaction { // eslint-disable-line
|
||||
Y.Memory = (function () {
|
||||
class Transaction extends Y.AbstractTransaction {
|
||||
|
||||
constructor (store) {
|
||||
super(store)
|
||||
@@ -144,28 +135,25 @@ Y.Memory = (function () { // eslint-disable-line no-unused-vars
|
||||
* getOpsFromDeleteSet (ds) {
|
||||
return this.ds.getDeletions(ds)
|
||||
}
|
||||
* setOperation (op) { // eslint-disable-line
|
||||
* setOperation (op) {
|
||||
// TODO: you can remove this step! probs..
|
||||
var n = this.os.findNode(op.id)
|
||||
n.val = op
|
||||
return op
|
||||
}
|
||||
* addOperation (op) { // eslint-disable-line
|
||||
* addOperation (op) {
|
||||
this.os.add(op)
|
||||
}
|
||||
* getOperation (id) { // eslint-disable-line
|
||||
if (id == null) {
|
||||
throw new Error('You must define id!')
|
||||
}
|
||||
* getOperation (id) {
|
||||
return this.os.find(id)
|
||||
}
|
||||
* removeOperation (id) { // eslint-disable-line
|
||||
* removeOperation (id) {
|
||||
this.os.delete(id)
|
||||
}
|
||||
* setState (state) { // eslint-disable-line
|
||||
* setState (state) {
|
||||
this.ss[state.user] = state.clock
|
||||
}
|
||||
* getState (user) { // eslint-disable-line
|
||||
* getState (user) {
|
||||
var clock = this.ss[user]
|
||||
if (clock == null) {
|
||||
clock = 0
|
||||
@@ -175,7 +163,7 @@ Y.Memory = (function () { // eslint-disable-line no-unused-vars
|
||||
clock: clock
|
||||
}
|
||||
}
|
||||
* getStateVector () { // eslint-disable-line
|
||||
* getStateVector () {
|
||||
var stateVector = []
|
||||
for (var user in this.ss) {
|
||||
var clock = this.ss[user]
|
||||
@@ -186,7 +174,7 @@ Y.Memory = (function () { // eslint-disable-line no-unused-vars
|
||||
}
|
||||
return stateVector
|
||||
}
|
||||
* getStateSet () { // eslint-disable-line
|
||||
* getStateSet () {
|
||||
return this.ss
|
||||
}
|
||||
* getOperations (startSS) {
|
||||
@@ -225,7 +213,7 @@ Y.Memory = (function () { // eslint-disable-line no-unused-vars
|
||||
}
|
||||
* makeOperationReady (ss, op) {
|
||||
// instead of ss, you could use currSS (a ss that increments when you add an operation)
|
||||
op = copyObject(op)
|
||||
op = Y.utils.copyObject(op)
|
||||
var o = op
|
||||
var clock
|
||||
while (o.right != null) {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/* global Y */
|
||||
'use strict'
|
||||
|
||||
function smaller (a, b) {
|
||||
return a[0] < b[0] || (a[0] === b[0] && a[1] < b[1])
|
||||
}
|
||||
Y.utils.smaller = smaller
|
||||
/*
|
||||
This file contains a not so fancy implemantion of a Red Black Tree.
|
||||
*/
|
||||
|
||||
class N {
|
||||
// A created node is always red!
|
||||
@@ -126,7 +125,7 @@ class N {
|
||||
}
|
||||
}
|
||||
|
||||
class RBTree { // eslint-disable-line no-unused-vars
|
||||
class RBTree {
|
||||
constructor () {
|
||||
this.root = null
|
||||
this.length = 0
|
||||
@@ -140,11 +139,11 @@ class RBTree { // eslint-disable-line no-unused-vars
|
||||
return null
|
||||
} else {
|
||||
while (true) {
|
||||
if ((from === null || smaller(from, o.val.id)) && o.left !== null) {
|
||||
if ((from === null || Y.utils.smaller(from, o.val.id)) && o.left !== null) {
|
||||
// o is included in the bound
|
||||
// try to find an element that is closer to the bound
|
||||
o = o.left
|
||||
} else if (from !== null && smaller(o.val.id, from)) {
|
||||
} else if (from !== null && Y.utils.smaller(o.val.id, from)) {
|
||||
// o is not within the bound, maybe one of the right elements is..
|
||||
if (o.right !== null) {
|
||||
o = o.right
|
||||
@@ -168,11 +167,11 @@ class RBTree { // eslint-disable-line no-unused-vars
|
||||
return null
|
||||
} else {
|
||||
while (true) {
|
||||
if ((to === null || smaller(o.val.id, to)) && o.right !== null) {
|
||||
if ((to === null || Y.utils.smaller(o.val.id, to)) && o.right !== null) {
|
||||
// o is included in the bound
|
||||
// try to find an element that is closer to the bound
|
||||
o = o.right
|
||||
} else if (to !== null && smaller(to, o.val.id)) {
|
||||
} else if (to !== null && Y.utils.smaller(to, o.val.id)) {
|
||||
// o is not within the bound, maybe one of the left elements is..
|
||||
if (o.left !== null) {
|
||||
o = o.left
|
||||
@@ -189,7 +188,7 @@ class RBTree { // eslint-disable-line no-unused-vars
|
||||
}
|
||||
iterate (from, to, f) {
|
||||
var o = this.findNodeWithLowerBound(from)
|
||||
while (o !== null && (to === null || smaller(o.val.id, to) || Y.utils.compareIds(o.val.id, to))) {
|
||||
while (o !== null && (to === null || Y.utils.smaller(o.val.id, to) || Y.utils.compareIds(o.val.id, to))) {
|
||||
f(o.val)
|
||||
o = o.next()
|
||||
}
|
||||
@@ -226,9 +225,9 @@ class RBTree { // eslint-disable-line no-unused-vars
|
||||
if (o === null) {
|
||||
return false
|
||||
}
|
||||
if (smaller(id, o.val.id)) {
|
||||
if (Y.utils.smaller(id, o.val.id)) {
|
||||
o = o.left
|
||||
} else if (smaller(o.val.id, id)) {
|
||||
} else if (Y.utils.smaller(o.val.id, id)) {
|
||||
o = o.right
|
||||
} else {
|
||||
return o
|
||||
@@ -386,14 +385,14 @@ class RBTree { // eslint-disable-line no-unused-vars
|
||||
if (this.root !== null) {
|
||||
var p = this.root // p abbrev. parent
|
||||
while (true) {
|
||||
if (smaller(node.val.id, p.val.id)) {
|
||||
if (Y.utils.smaller(node.val.id, p.val.id)) {
|
||||
if (p.left === null) {
|
||||
p.left = node
|
||||
break
|
||||
} else {
|
||||
p = p.left
|
||||
}
|
||||
} else if (smaller(p.val.id, node.val.id)) {
|
||||
} else if (Y.utils.smaller(p.val.id, node.val.id)) {
|
||||
if (p.right === null) {
|
||||
p.right = node
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user