From 77114efe2147e79f3dca767746fbdc01fab6e5f0 Mon Sep 17 00:00:00 2001 From: David Sehnal <david.sehnal@gmail.com> Date: Thu, 26 Oct 2017 22:42:51 +0200 Subject: [PATCH] ordered set cleanup --- src/mol-base/collections/ordered-set/base.ts | 40 +++++++++---------- .../ordered-set/segment-iterator.ts | 4 +- src/perf-tests/sets.ts | 12 +++--- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/mol-base/collections/ordered-set/base.ts b/src/mol-base/collections/ordered-set/base.ts index 654e816d2..1eddd1ad5 100644 --- a/src/mol-base/collections/ordered-set/base.ts +++ b/src/mol-base/collections/ordered-set/base.ts @@ -177,13 +177,13 @@ function rangeSearchIndex(r: Range, value: number) { return value - _rsiR.fst; } -const _maxIntRangeRet = { i: 0, j: 0, endA: 0, endB: 0 }; +const _maxIntRangeRet = { startI: 0, startJ: 0, endI: 0, endJ: 0 }; function getMaxIntersectionRange(xs: SortedArray, ys: SortedArray) { - const la = xs.length - 1, lb = ys.length - 1; - _maxIntRangeRet.i = binarySearchPredIndex(xs, ys[0]); - _maxIntRangeRet.j = binarySearchPredIndex(ys, xs[0]); - _maxIntRangeRet.endA = Math.min(binarySearchPredIndex(xs, ys[lb]), la); - _maxIntRangeRet.endB = Math.min(binarySearchPredIndex(ys, xs[la]), lb); + _maxIntRangeRet.startI = binarySearchPredIndex(xs, ys[0]); + _maxIntRangeRet.startJ = binarySearchPredIndex(ys, xs[0]); + _maxIntRangeRet.endI = binarySearchPredIndex(xs, ys[ys.length - 1] + 1); + _maxIntRangeRet.endJ = binarySearchPredIndex(ys, xs[xs.length - 1] + 1); + return _maxIntRangeRet; } @@ -207,8 +207,8 @@ function equalAA(a: SortedArray, b: SortedArray) { function areIntersectingAA(xs: SortedArray, ys: SortedArray) { if (xs === ys) return true; - let { i, j, endA, endB } = getMaxIntersectionRange(xs, ys); - while (i <= endA && j <= endB) { + let { startI: i, startJ: j, endI, endJ } = getMaxIntersectionRange(xs, ys); + while (i < endI && j < endJ) { const x = xs[i], y = ys[j]; if (x < y) { i++; } else if (x > y) { j++; } @@ -221,12 +221,12 @@ function isSubsetAA(a: SortedArray, b: SortedArray) { if (a === b) return true; const lenB = b.length; - let { i, j, endA, endB } = getMaxIntersectionRange(a, b); + let { startI: i, startJ: j, endI, endJ } = getMaxIntersectionRange(a, b); // must be able to advance by lenB elements - if (endB - j + 1 < lenB || endA - i + 1 < lenB) return false; + if (endJ - j < lenB || endI - i < lenB) return false; let equal = 0; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { i++; } else if (x > y) { j++; } @@ -285,10 +285,10 @@ function unionAR(a: SortedArray, b: Range) { function unionAA(a: SortedArray, b: SortedArray) { if (a === b) return a; - let { i: sI, j: sJ, endA, endB } = getMaxIntersectionRange(a, b); + const { startI: sI, startJ: sJ, endI, endJ } = getMaxIntersectionRange(a, b); let i = sI, j = sJ; let commonCount = 0; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { i++; } else if (x > y) { j++; } @@ -318,7 +318,7 @@ function unionAA(a: SortedArray, b: SortedArray) { // insert the common part i = sI; j = sJ; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { indices[offset++] = x; i++; } else if (x > y) { indices[offset++] = y; j++; } @@ -362,10 +362,10 @@ function intersectAR(a: SortedArray, r: Range) { function intersectAA(a: SortedArray, b: SortedArray) { if (a === b) return a; - let { i: sI, j: sJ, endA, endB } = getMaxIntersectionRange(a, b); + const { startI: sI, startJ: sJ, endI, endJ } = getMaxIntersectionRange(a, b); let i = sI, j = sJ; let commonCount = 0; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { i++; } else if (x > y) { j++; } @@ -384,7 +384,7 @@ function intersectAA(a: SortedArray, b: SortedArray) { let offset = 0; i = sI; j = sJ; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { i++; } else if (x > y) { j++; } @@ -479,10 +479,10 @@ function subtractAA(a: SortedArray, b: SortedArray) { const lenA = a.length; - let { i: sI, j: sJ, endA, endB } = getMaxIntersectionRange(a, b); + const { startI: sI, startJ: sJ, endI, endJ } = getMaxIntersectionRange(a, b); let i = sI, j = sJ; let commonCount = 0; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { i++; } else if (x > y) { j++; } @@ -502,7 +502,7 @@ function subtractAA(a: SortedArray, b: SortedArray) { i = sI; j = sJ; - while (i <= endA && j <= endB) { + while (i < endI && j < endJ) { const x = a[i], y = b[j]; if (x < y) { indices[offset++] = x; i++; } else if (x > y) { j++; } diff --git a/src/mol-base/collections/ordered-set/segment-iterator.ts b/src/mol-base/collections/ordered-set/segment-iterator.ts index 078e4b697..25229f85c 100644 --- a/src/mol-base/collections/ordered-set/segment-iterator.ts +++ b/src/mol-base/collections/ordered-set/segment-iterator.ts @@ -22,7 +22,7 @@ class SegmentIterator implements Iterator<{ segmentIndex: number } & OrderedSet. } move() { - this.done = this.segmentRange.end <= this.segmentRange.start || this.setRange.end <= this.setRange.start; + this.done = this.segmentRange.end <= this.segmentRange.start; while (!this.done) { if (!this.updateValue()) { this.updateSegmentRange(); @@ -52,7 +52,7 @@ class SegmentIterator implements Iterator<{ segmentIndex: number } & OrderedSet. const min = OrderedSet.getAt(this.set, this.setRange.start), max = OrderedSet.getAt(this.set, this.setRange.end - 1); this.segmentRange.start = this.getSegmentIndex(min); this.segmentRange.end = this.getSegmentIndex(max) + 1; - this.done = this.segmentRange.end <= this.segmentRange.start || this.setRange.end <= this.setRange.start; + this.done = this.segmentRange.end <= this.segmentRange.start; } constructor(private segments: OrderedSet, private set: OrderedSet) { diff --git a/src/perf-tests/sets.ts b/src/perf-tests/sets.ts index a35f15dd4..255d606e9 100644 --- a/src/perf-tests/sets.ts +++ b/src/perf-tests/sets.ts @@ -155,12 +155,8 @@ export namespace Union { export function run() { const suite = new B.Suite(); - // const values: number[] = []; - // for (let i = 0; i < 1000000; i++) values[i] = (Math.random() * 1000) | 0; - - const randomArray = createArray(100); - const { ordSet: osA, set: sA, obj: oA } = createData(randomArray); - const { ordSet: osB, set: sB, obj: oB } = createData(randomArray); + const { ordSet: osA, set: sA, obj: oA } = createData(createArray(1000)); + const { ordSet: osB, set: sB, obj: oB } = createData(createArray(1000)); console.log(OrdSet.size(unionOS(osA, osB)), Object.keys(unionO(oA, oB)).length, unionS(sA, sB).size); console.log(OrdSet.size(intOS(osA, osB)), Object.keys(intO(oA, oB)).length, intS(sA, sB).size); @@ -244,4 +240,6 @@ export function testSegments() { console.log(`${s.segmentIndex}: ${OrdSet.getAt(data, j)}`); } } -} \ No newline at end of file +} + +Union.run(); \ No newline at end of file -- GitLab