/** * Compute the size of the union of two sets. * @param a The first set. * @param b The second set. * @return The size of the union of the two sets. */ public static int unionSize(LongSortedSet a, LongSortedSet b) { return a.size() + b.size() - intersectSize(a, b); }
@Override public double similarity(Long2DoubleMap vec1, Long2DoubleMap vec2) { double s = delegate.similarity(vec1, vec2); int n = LongUtils.intersectSize(vec1.keySet(), vec2.keySet()); s *= n; return s / max(n, threshold); }
@Test public void testIntersectSizeEmpty() { assertThat(intersectSize(LongSortedSets.EMPTY_SET, LongSortedSets.EMPTY_SET), equalTo(0)); }
/** * Compute the size of the intersection of two sets. * @param a The first set. * @param b The second set. * @return The size of the intersection of the two sets. */ public static int intersectSize(LongSet a, LongSet b) { if (a instanceof LongSortedSet && b instanceof LongSortedSet) { return intersectSize((LongSortedSet) a, (LongSortedSet) b); } else { int n = 0; LongIterator iter = a.iterator(); while (iter.hasNext()) { long x = iter.nextLong(); if (b.contains(x)) { n += 1; } } return n; } }
@Test public void testIntersectSizeDisjoint() { assertThat(intersectSize(LongSortedSets.singleton(52), LongSortedSets.singleton(30)), equalTo(0)); assertThat(intersectSize(LongSortedSets.singleton(30), LongSortedSets.singleton(52)), equalTo(0)); }
@Test public void testIntersectSizeSingleton() { assertThat(intersectSize(LongSortedSets.singleton(52), LongSortedSets.singleton(52)), equalTo(1)); }
@Test public void testIntersectSizePackedSets() { assertThat(intersectSize(packedSet(1L, 3L, 5L, 7L), packedSet(2L, 3L, 4L, 5L, 6L)), equalTo(2)); }
/** * Compute the size of the union of two sets. * @param a The first set. * @param b The second set. * @return The size of the union of the two sets. */ public static int unionSize(LongSortedSet a, LongSortedSet b) { return a.size() + b.size() - intersectSize(a, b); }
@Override public double similarity(Long2DoubleMap vec1, Long2DoubleMap vec2) { double s = delegate.similarity(vec1, vec2); int n = LongUtils.intersectSize(vec1.keySet(), vec2.keySet()); s *= n; return s / max(n, threshold); }
/** * Compute the size of the intersection of two sets. * @param a The first set. * @param b The second set. * @return The size of the intersection of the two sets. */ public static int intersectSize(LongSet a, LongSet b) { if (a instanceof LongSortedSet && b instanceof LongSortedSet) { return intersectSize((LongSortedSet) a, (LongSortedSet) b); } else { int n = 0; LongIterator iter = a.iterator(); while (iter.hasNext()) { long x = iter.nextLong(); if (b.contains(x)) { n += 1; } } return n; } }