From 7b16d5c92d7ee61ec207e1c53e51c3c86f6fb2bc Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Thu, 4 Jun 2020 18:14:41 +0200 Subject: [PATCH] implement pivoting in struct search --- src/structs/Item.js | 6 ++++-- src/utils/StructStore.js | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/structs/Item.js b/src/structs/Item.js index c3859be1..1e158a10 100644 --- a/src/structs/Item.js +++ b/src/structs/Item.js @@ -712,10 +712,12 @@ export class ItemRef extends AbstractStructRef { */ this.parentSub = canCopyParentInfo && (info & binary.BIT6) === binary.BIT6 ? decoding.readVarString(decoder) : null const missing = this._missing - if (this.left !== null) { + // Only add items to missing if they don't preceed this item (indicating that it has already been added). + // @todo Creating missing items could be done outside this constructor + if (this.left !== null && this.left.client !== id.client) { missing.push(this.left) } - if (this.right !== null) { + if (this.right !== null && this.right.client !== id.client) { missing.push(this.right) } if (this.parent !== null) { diff --git a/src/utils/StructStore.js b/src/utils/StructStore.js index 59b88dd5..d74bd787 100644 --- a/src/utils/StructStore.js +++ b/src/utils/StructStore.js @@ -124,10 +124,15 @@ export const addStruct = (store, struct) => { export const findIndexSS = (structs, clock) => { let left = 0 let right = structs.length - 1 + let mid = structs[right] + let midclock = mid.id.clock + if (mid.id.clock === clock) { + return right + } + let midindex = math.floor((clock / (midclock + mid.length)) * right) // pivoting the search while (left <= right) { - const midindex = math.floor((left + right) / 2) - const mid = structs[midindex] - const midclock = mid.id.clock + mid = structs[midindex] + midclock = mid.id.clock if (midclock <= clock) { if (clock < midclock + mid.length) { return midindex @@ -136,6 +141,7 @@ export const findIndexSS = (structs, clock) => { } else { right = midindex - 1 } + midindex = math.floor((left + right) / 2) } // Always check state before looking for a struct in StructStore // Therefore the case of not finding a struct is unexpected