From e8686c29f798d6ac3f76cb5275ca0498b288f1e4 Mon Sep 17 00:00:00 2001 From: Rafael Weinstein Date: Mon, 20 Jun 2016 10:22:46 -0700 Subject: [PATCH] Remove Set.intersect (#1852) Remove Set.intersect --- js/src/set-test.js | 59 ------------------------------ js/src/set.js | 91 ---------------------------------------------- 2 files changed, 150 deletions(-) diff --git a/js/src/set-test.js b/js/src/set-test.js index c628ee66bf..d41865f728 100644 --- a/js/src/set-test.js +++ b/js/src/set-test.js @@ -511,33 +511,6 @@ suite('CompoundSet', () => { }); }); - async function testIntersect(expect: Array, seqs: Array>) { - const first = build(db, seqs[0]); - const sets:Array = []; - for (let i = 1; i < seqs.length; i++) { - sets.push(build(db, seqs[i])); - } - - const result = await first.intersect(...sets); - const actual = []; - await result.forEach(v => { actual.push(v); }); - assert.deepEqual(expect, actual); - } - - test('LONG: intersect', async () => { - await testIntersect(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']]); - await testIntersect(['a', 'h'], [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], ['a', 'h', 'i', 'j', 'k', 'l', 'm', 'n']]); - await testIntersect(['d', 'e', 'f', 'g', 'h'], [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k']]); - await testIntersect(['h'], [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k'], ['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']]); - await testIntersect([], [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k'], ['i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']]); - }); - test('iterator at 0', async () => { const test = async (expected, items) => { const set = new Set(items); @@ -679,36 +652,4 @@ suite('CompoundSet', () => { await t(10, SetLeafSequence); await t(100, OrderedMetaSequence); }); - - test('Type after mutations - interesect', async () => { - async function t(n, c) { - const nums: any = intSequence(n); - const strings = nums.map(n => String.fromCodePoint(n)); - const combined = nums.concat(strings); - - const numSet = new Set(nums); - assert.equal(numSet.size, n); - assert.instanceOf(numSet.sequence, c); - assert.isTrue(equals(numSet.type, makeSetType(numberType))); - - const stringSet = new Set(strings); - assert.equal(stringSet.size, n); - assert.instanceOf(stringSet.sequence, c); - assert.isTrue(equals(stringSet.type, makeSetType(stringType))); - - const combinedSet = new Set(combined); - assert.equal(combinedSet.size, 2 * n); - assert.instanceOf(combinedSet.sequence, c); - assert.isTrue(equals(combinedSet.type, makeSetType(makeUnionType([numberType, stringType])))); - - const s = await combinedSet.intersect(numSet); - assert.isTrue(equals(s, numSet)); - - const s2 = await combinedSet.intersect(stringSet); - assert.isTrue(equals(s2, stringSet)); - } - - await t(10, SetLeafSequence); - await t(100, OrderedMetaSequence); - }); }); diff --git a/js/src/set.js b/js/src/set.js index 5ff7957de0..c614a338e8 100644 --- a/js/src/set.js +++ b/js/src/set.js @@ -155,37 +155,6 @@ export default class Set extends Collection { return this.sequence.numLeaves; } - async intersect(...sets: Array>): Promise> { - if (sets.length === 0) { - return this; - } - - let cursor = await this.sequence.newCursorAt(null); - if (!cursor.valid) { - return this; - } - - const values: Array = []; - - for (let i = 0; cursor.valid && i < sets.length; i++) { - const first = cursor.getCurrent(); - const next = await sets[i].sequence.newCursorAt(first); - if (!next.valid) { - break; - } - - cursor = new SetIntersectionCursor(cursor, next); - await cursor.align(); - } - - while (cursor.valid) { - values.push(cursor.getCurrent()); - await cursor.advance(); - } - - return new Set(values); - } - /** * Returns a 2-tuple [added, removed] sorted values. */ @@ -211,63 +180,3 @@ export class SetLeafSequence extends OrderedSequence { return getValueChunks(this.items); } } - -type OrderedCursor = { - valid: boolean; - getCurrent(): K; - advanceTo(key: K): Promise; - advance(): Promise; -} - -class SetIntersectionCursor { - s1: OrderedCursor; - s2: OrderedCursor; - valid: boolean; - - constructor(s1: OrderedCursor, s2: OrderedCursor) { - invariant(s1.valid && s2.valid); - this.s1 = s1; - this.s2 = s2; - this.valid = true; - } - - getCurrent(): K { - invariant(this.valid); - return this.s1.getCurrent(); - } - - async align(): Promise { - let v1 = this.s1.getCurrent(); - let v2 = this.s2.getCurrent(); - - let i; - while ((i = compare(v1, v2)) !== 0) { - if (i < 0) { - if (!await this.s1.advanceTo(v2)) { - return this.valid = false; - } - - v1 = this.s1.getCurrent(); - continue; - } - - if (!await this.s2.advanceTo(v1)) { - return this.valid = false; - } - - v2 = this.s2.getCurrent(); - } - - return this.valid = true; - } - - async advanceTo(key: K): Promise { - invariant(this.valid); - return this.valid = await this.s1.advanceTo(key) && await this.align(); - } - - async advance(): Promise { - invariant(this.valid); - return this.valid = await this.s1.advance() && await this.align(); - } -}