Skip to content
Snippets Groups Projects
Commit 77114efe authored by David Sehnal's avatar David Sehnal
Browse files

ordered set cleanup

parent 9bf0bab0
No related branches found
No related tags found
No related merge requests found
...@@ -177,13 +177,13 @@ function rangeSearchIndex(r: Range, value: number) { ...@@ -177,13 +177,13 @@ function rangeSearchIndex(r: Range, value: number) {
return value - _rsiR.fst; 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) { function getMaxIntersectionRange(xs: SortedArray, ys: SortedArray) {
const la = xs.length - 1, lb = ys.length - 1; _maxIntRangeRet.startI = binarySearchPredIndex(xs, ys[0]);
_maxIntRangeRet.i = binarySearchPredIndex(xs, ys[0]); _maxIntRangeRet.startJ = binarySearchPredIndex(ys, xs[0]);
_maxIntRangeRet.j = binarySearchPredIndex(ys, xs[0]); _maxIntRangeRet.endI = binarySearchPredIndex(xs, ys[ys.length - 1] + 1);
_maxIntRangeRet.endA = Math.min(binarySearchPredIndex(xs, ys[lb]), la); _maxIntRangeRet.endJ = binarySearchPredIndex(ys, xs[xs.length - 1] + 1);
_maxIntRangeRet.endB = Math.min(binarySearchPredIndex(ys, xs[la]), lb);
return _maxIntRangeRet; return _maxIntRangeRet;
} }
...@@ -207,8 +207,8 @@ function equalAA(a: SortedArray, b: SortedArray) { ...@@ -207,8 +207,8 @@ function equalAA(a: SortedArray, b: SortedArray) {
function areIntersectingAA(xs: SortedArray, ys: SortedArray) { function areIntersectingAA(xs: SortedArray, ys: SortedArray) {
if (xs === ys) return true; if (xs === ys) return true;
let { i, j, endA, endB } = getMaxIntersectionRange(xs, ys); let { startI: i, startJ: j, endI, endJ } = getMaxIntersectionRange(xs, ys);
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = xs[i], y = ys[j]; const x = xs[i], y = ys[j];
if (x < y) { i++; } if (x < y) { i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
...@@ -221,12 +221,12 @@ function isSubsetAA(a: SortedArray, b: SortedArray) { ...@@ -221,12 +221,12 @@ function isSubsetAA(a: SortedArray, b: SortedArray) {
if (a === b) return true; if (a === b) return true;
const lenB = b.length; 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 // 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; let equal = 0;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { i++; } if (x < y) { i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
...@@ -285,10 +285,10 @@ function unionAR(a: SortedArray, b: Range) { ...@@ -285,10 +285,10 @@ function unionAR(a: SortedArray, b: Range) {
function unionAA(a: SortedArray, b: SortedArray) { function unionAA(a: SortedArray, b: SortedArray) {
if (a === b) return a; 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 i = sI, j = sJ;
let commonCount = 0; let commonCount = 0;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { i++; } if (x < y) { i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
...@@ -318,7 +318,7 @@ function unionAA(a: SortedArray, b: SortedArray) { ...@@ -318,7 +318,7 @@ function unionAA(a: SortedArray, b: SortedArray) {
// insert the common part // insert the common part
i = sI; i = sI;
j = sJ; j = sJ;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { indices[offset++] = x; i++; } if (x < y) { indices[offset++] = x; i++; }
else if (x > y) { indices[offset++] = y; j++; } else if (x > y) { indices[offset++] = y; j++; }
...@@ -362,10 +362,10 @@ function intersectAR(a: SortedArray, r: Range) { ...@@ -362,10 +362,10 @@ function intersectAR(a: SortedArray, r: Range) {
function intersectAA(a: SortedArray, b: SortedArray) { function intersectAA(a: SortedArray, b: SortedArray) {
if (a === b) return a; 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 i = sI, j = sJ;
let commonCount = 0; let commonCount = 0;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { i++; } if (x < y) { i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
...@@ -384,7 +384,7 @@ function intersectAA(a: SortedArray, b: SortedArray) { ...@@ -384,7 +384,7 @@ function intersectAA(a: SortedArray, b: SortedArray) {
let offset = 0; let offset = 0;
i = sI; i = sI;
j = sJ; j = sJ;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { i++; } if (x < y) { i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
...@@ -479,10 +479,10 @@ function subtractAA(a: SortedArray, b: SortedArray) { ...@@ -479,10 +479,10 @@ function subtractAA(a: SortedArray, b: SortedArray) {
const lenA = a.length; 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 i = sI, j = sJ;
let commonCount = 0; let commonCount = 0;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { i++; } if (x < y) { i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
...@@ -502,7 +502,7 @@ function subtractAA(a: SortedArray, b: SortedArray) { ...@@ -502,7 +502,7 @@ function subtractAA(a: SortedArray, b: SortedArray) {
i = sI; i = sI;
j = sJ; j = sJ;
while (i <= endA && j <= endB) { while (i < endI && j < endJ) {
const x = a[i], y = b[j]; const x = a[i], y = b[j];
if (x < y) { indices[offset++] = x; i++; } if (x < y) { indices[offset++] = x; i++; }
else if (x > y) { j++; } else if (x > y) { j++; }
......
...@@ -22,7 +22,7 @@ class SegmentIterator implements Iterator<{ segmentIndex: number } & OrderedSet. ...@@ -22,7 +22,7 @@ class SegmentIterator implements Iterator<{ segmentIndex: number } & OrderedSet.
} }
move() { 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) { while (!this.done) {
if (!this.updateValue()) { if (!this.updateValue()) {
this.updateSegmentRange(); this.updateSegmentRange();
...@@ -52,7 +52,7 @@ class SegmentIterator implements Iterator<{ segmentIndex: number } & OrderedSet. ...@@ -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); 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.start = this.getSegmentIndex(min);
this.segmentRange.end = this.getSegmentIndex(max) + 1; 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) { constructor(private segments: OrderedSet, private set: OrderedSet) {
......
...@@ -155,12 +155,8 @@ export namespace Union { ...@@ -155,12 +155,8 @@ export namespace Union {
export function run() { export function run() {
const suite = new B.Suite(); const suite = new B.Suite();
// const values: number[] = []; const { ordSet: osA, set: sA, obj: oA } = createData(createArray(1000));
// for (let i = 0; i < 1000000; i++) values[i] = (Math.random() * 1000) | 0; const { ordSet: osB, set: sB, obj: oB } = createData(createArray(1000));
const randomArray = createArray(100);
const { ordSet: osA, set: sA, obj: oA } = createData(randomArray);
const { ordSet: osB, set: sB, obj: oB } = createData(randomArray);
console.log(OrdSet.size(unionOS(osA, osB)), Object.keys(unionO(oA, oB)).length, unionS(sA, sB).size); 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); console.log(OrdSet.size(intOS(osA, osB)), Object.keys(intO(oA, oB)).length, intS(sA, sB).size);
...@@ -244,4 +240,6 @@ export function testSegments() { ...@@ -244,4 +240,6 @@ export function testSegments() {
console.log(`${s.segmentIndex}: ${OrdSet.getAt(data, j)}`); console.log(`${s.segmentIndex}: ${OrdSet.getAt(data, j)}`);
} }
} }
} }
\ No newline at end of file
Union.run();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment