From d1fda080d967ddc4e1661508e384cd97058d0a29 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 22 Jul 2015 19:30:00 +0200 Subject: [PATCH] added some fixes and started DeleteStore implementation --- src/OperationStores/Memory.js | 21 +++++++++++++ src/OperationStores/RedBlackTree.js | 46 ++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/OperationStores/Memory.js b/src/OperationStores/Memory.js index fed52b39..ec142b4c 100644 --- a/src/OperationStores/Memory.js +++ b/src/OperationStores/Memory.js @@ -8,6 +8,27 @@ function copyObject (o) { return c } +class DeletionStore { // eslint-disable-line + constructor () { + this.ds = {} + } + deleteId (id) { + var dv = this.db[id[0]] + if (dv === void 0) { + dv = [] + this.db[id[0]] = dv + } + for (var i in dv) { + if (dv[i].pos <= id[1] && id[1] < dv[i].pos + length) { + // within the bound, already deleted + return + } else { + + } + } + } +} + Y.Memory = (function () { // eslint-disable-line no-unused-vars class Transaction extends AbstractTransaction { // eslint-disable-line diff --git a/src/OperationStores/RedBlackTree.js b/src/OperationStores/RedBlackTree.js index 9a627ef8..371ed4b1 100644 --- a/src/OperationStores/RedBlackTree.js +++ b/src/OperationStores/RedBlackTree.js @@ -80,6 +80,22 @@ class N { return p.parent } } + prev () { + if (this.left !== null) { + // search the most right node in the left tree + var o = this.left + while (o.right !== null) { + o = o.right + } + return o + } else { + var p = this + while (p.parent !== null && p !== p.parent.right) { + p = p.parent + } + return p.parent + } + } rotateRight (tree) { var parent = this.parent var newParent = this.left @@ -118,7 +134,7 @@ class RBTree { // eslint-disable-line no-unused-vars } var o = this.root if (o === null) { - return false + return null } else { while (true) { if ((from === null || smaller(from, o.val.id)) && o.left !== null) { @@ -140,6 +156,34 @@ class RBTree { // eslint-disable-line no-unused-vars } } } + findNodeWithUpperBound (to) { + if (to === void 0) { + throw new Error('You must define from!') + } + var o = this.root + if (o === null) { + return null + } else { + while (true) { + if ((to === null || 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)) { + // o is not within the bound, maybe one of the left elements is.. + if (o.left !== null) { + o = o.left + } else { + // there is no left element. Search for the prev smaller element, + // this should be within the bounds + return o.prev() + } + } else { + return o + } + } + } + } iterate (from, to, f) { var o = this.findNodeWithLowerBound(from) while (o !== null && (to === null || smaller(o.val.id, to) || compareIds(o.val.id, to))) {