diff --git a/src/mol-data/int/_spec/ordered-set.spec.ts b/src/mol-data/int/_spec/ordered-set.spec.ts index 8cbad68ceb07cdb09f51398ebf61274f53b14cfc..a8f2a5d10c39c9f771c0d6da0eca81c299a39537 100644 --- a/src/mol-data/int/_spec/ordered-set.spec.ts +++ b/src/mol-data/int/_spec/ordered-set.spec.ts @@ -25,6 +25,7 @@ describe('ordered set', () => { const singleton10 = OrderedSet.ofSingleton(10); const range1_4 = OrderedSet.ofRange(1, 4); const arr136 = OrderedSet.ofSortedArray([1, 3, 6]); + const arr12369 = OrderedSet.ofSortedArray([1, 2, 3, 6, 9]); const iB = (s: number, e: number) => Interval.ofBounds(s, e); @@ -47,6 +48,14 @@ describe('ordered set', () => { expect(OrderedSet.areIntersecting(empty, singleton10)).toBe(false); expect(OrderedSet.areIntersecting(empty, range1_4)).toBe(false); expect(OrderedSet.areIntersecting(empty, arr136)).toBe(false); + + expect(OrderedSet.areIntersecting(Interval.ofRange(2, 3), arr12369)).toBe(true); + expect(OrderedSet.areIntersecting(Interval.ofRange(2, 6), arr12369)).toBe(true); + expect(OrderedSet.areIntersecting(Interval.ofRange(2, 8), arr12369)).toBe(true); + expect(OrderedSet.areIntersecting(Interval.ofRange(4, 8), arr12369)).toBe(true); + + expect(OrderedSet.areIntersecting(Interval.ofRange(4, 5), arr12369)).toBe(false); + expect(OrderedSet.areIntersecting(Interval.ofRange(7, 8), arr12369)).toBe(false); }); it('isSubset', () => { diff --git a/src/mol-data/int/impl/ordered-set.ts b/src/mol-data/int/impl/ordered-set.ts index 5688286a7f5937e159cd0bb689789255e1814e90..6031729daf7a67e650a617ca86a20a562081b139 100644 --- a/src/mol-data/int/impl/ordered-set.ts +++ b/src/mol-data/int/impl/ordered-set.ts @@ -29,6 +29,8 @@ export function indexOf(set: OrderedSetImpl, x: number) { return I.is(set) ? I.i export function getAt(set: OrderedSetImpl, i: number) { return I.is(set) ? I.getAt(set, i) : set[i]; } export function min(set: OrderedSetImpl) { return I.is(set) ? I.min(set) : S.min(set); } export function max(set: OrderedSetImpl) { return I.is(set) ? I.max(set) : S.max(set); } +export function start(set: OrderedSetImpl) { return I.is(set) ? I.start(set) : S.start(set); } +export function end(set: OrderedSetImpl) { return I.is(set) ? I.end(set) : S.end(set); } export function hashCode(set: OrderedSetImpl) { return I.is(set) ? I.hashCode(set) : S.hashCode(set); } // TODO: possibly add more hash functions to allow for multilevel hashing. @@ -97,7 +99,10 @@ export function subtract(a: OrderedSetImpl, b: OrderedSetImpl) { function areEqualIS(a: I, b: S) { return I.size(a) === S.size(b) && I.start(a) === S.start(b) && I.end(a) === S.end(b); } function areIntersectingSI(a: S, b: I) { - return areRangesIntersecting(a, b); + if (S.size(a) === 0 || I.size(b) === 0) return false + const predAMinB = S.findPredecessorIndex(a, I.min(b)) + const predAMaxB = S.findPredecessorIndex(a, I.max(b)) + return predAMinB !== predAMaxB } function isSubsetSI(a: S, b: I) {