/** * Construct a new array set from an array of items. * @param items The items to initialize the set with. The items are copied, the array is * not reused. */ public LongSortedArraySet(long[] items) { this(SortedKeyIndex.create(items)); }
/** * Pack longs into a sorted set. * @param longs An array of longs. This array is copied, not wrapped. * @return An efficient sorted set containing the numbers in {@code longs}. */ public static LongSortedArraySet packedSet(long... longs) { return SortedKeyIndex.create(longs).keySet(); }
@Test public void testSingletonLowerBound() { SortedKeyIndex keys = SortedKeyIndex.create(5); assertThat(keys.findLowerBound(0), equalTo(0)); assertThat(keys.findLowerBound(5), equalTo(0)); assertThat(keys.findLowerBound(7), equalTo(1)); }
@Test public void testSingletonUpperBound() { SortedKeyIndex keys = SortedKeyIndex.create(5); assertThat(keys.findUpperBound(0), equalTo(0)); assertThat(keys.findUpperBound(5), equalTo(1)); assertThat(keys.findUpperBound(7), equalTo(1)); }
@Test public void testUnionSizeCompatLSAS() { SortedKeyIndex lkd = SortedKeyIndex.create(5, 3, 27); assertThat(unionSize(lkd.keySet(), lkd.keySet()), equalTo(3)); }
@Test public void testSetUnionCompatLSAS() { SortedKeyIndex lkd = SortedKeyIndex.create(5, 3, 27); assertThat(setUnion(lkd.keySet(), lkd.keySet()), contains(3L, 5L, 27L)); }
@Test public void testSomeKeysUpperBound() { SortedKeyIndex keys = SortedKeyIndex.create(5, 6, 8); assertThat(keys.findUpperBound(0), equalTo(0)); assertThat(keys.findUpperBound(5), equalTo(1)); assertThat(keys.findUpperBound(6), equalTo(2)); assertThat(keys.findUpperBound(7), equalTo(2)); assertThat(keys.findUpperBound(8), equalTo(3)); assertThat(keys.findUpperBound(10), equalTo(3)); }
@Test public void testSomeKeysLowerBound() { SortedKeyIndex keys = SortedKeyIndex.create(5, 6, 8); assertThat(keys.findLowerBound(0), equalTo(0)); assertThat(keys.findLowerBound(5), equalTo(0)); assertThat(keys.findLowerBound(6), equalTo(1)); assertThat(keys.findLowerBound(7), equalTo(2)); assertThat(keys.findLowerBound(8), equalTo(2)); assertThat(keys.findLowerBound(10), equalTo(3)); }
@Test public void testSublist() { double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(1, 2, 3, 4, 5), values); assertThat(map.size(), equalTo(5)); Long2DoubleSortedMap sub = map.subMap(2, 5); assertThat(sub.size(), equalTo(3)); assertThat(sub.containsKey(2L), equalTo(true)); assertThat(sub.containsKey(1L), equalTo(false)); assertThat(sub.containsKey(5L), equalTo(false)); assertThat(sub.containsKey(4L), equalTo(true)); assertThat(sub.keySet(), contains(2L, 3L, 4L)); }
/** * Test ItemItemBuildContext when there is no rating data. */ @Test public void testEmpty() { SortedKeyIndex items = SortedKeyIndex.create(); Long2DoubleSortedMap[] ratingMap = new Long2DoubleSortedMap[0]; ItemItemBuildContext context = new ItemItemBuildContext(items, ratingMap, new Long2ObjectOpenHashMap<LongSortedSet>()); testRatingIntegrity(items, ratingMap, context); }
@Test public void testSingletonMap() { Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(42), new double[]{3.5}); assertThat(map.get(42L), equalTo(3.5)); assertThat(map.size(), equalTo(1)); assertThat(map.isEmpty(), equalTo(false)); assertThat(map.keySet(), contains(42L)); assertThat(map.values(), contains(3.5)); assertThat(map.entrySet(), hasSize(1)); assertThat(map.firstLongKey(), equalTo(42L)); assertThat(map.lastLongKey(), equalTo(42L)); Map.Entry<Long, Double> ent = map.entrySet().first(); assertThat(ent, notNullValue()); assertThat(ent.getKey(), equalTo(42L)); assertThat(ent.getValue(), equalTo(3.5)); assertThat(map.entrySet().contains(Pair.of(42L, 3.5)), equalTo(true)); assertThat(map.entrySet().contains(Pair.of(42L, 3.7)), equalTo(false)); assertThat(map.entrySet().contains(Pair.of(41L, 3.5)), equalTo(false)); assertThat(map.entrySet().first(), equalTo((Object) Pair.of(42L, 3.5))); assertThat(map.entrySet().last(), equalTo((Object) Pair.of(42L, 3.5))); }
/** * Test ItemItemBuildContext when no items have rating data. */ @Test public void testNoItemsData() { SortedKeyIndex items = SortedKeyIndex.create(1, 2, 3, 4); Long2DoubleSortedMap[] ratingMap = { Long2DoubleSortedMaps.EMPTY_MAP, Long2DoubleSortedMaps.EMPTY_MAP, Long2DoubleSortedMaps.EMPTY_MAP, Long2DoubleSortedMaps.EMPTY_MAP }; ItemItemBuildContext context = new ItemItemBuildContext(items, ratingMap, new Long2ObjectOpenHashMap<LongSortedSet>()); testRatingIntegrity(items, ratingMap, context); }
/** * Test method for {@link Ratings#itemRatingVector(java.util.Collection)}. */ @Test public void testItemRatingVector() { Collection<Rating> ratings = new ArrayList<>(); ratings.add(Rating.create(7, 5, 3.5)); RatingBuilder rb = new RatingBuilder(); ratings.add(Rating.create(3, 5, 1.5)); ratings.add(Rating.create(8, 5, 2)); Long2DoubleMap v = Ratings.itemRatingVector(ratings); assertEquals(3, v.size()); assertEquals(7, Vectors.sum(v), EPSILON); long[] keys = {3, 7, 8}; double[] values = {1.5, 3.5, 2}; Long2DoubleSortedArrayMap sv = Long2DoubleSortedArrayMap.wrap(SortedKeyIndex.create(keys), values); assertEquals(sv, v); } }
@Test public void testFastIterStartFrom() { double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedArrayMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(1, 2, 3, 4, 5), values); AbstractLong2DoubleMap.BasicEntry key = new AbstractLong2DoubleMap.BasicEntry(2, 2.0); ObjectBidirectionalIterator<Long2DoubleMap.Entry> iter = map.long2DoubleEntrySet().fastIterator(key); assertThat(iter.next().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(2L)); }
/** * Test ItemItemBuildContext when all items have rating data. */ @Test public void testAllItemsData() { SortedKeyIndex items = SortedKeyIndex.create(1, 2, 3, 4); long[] userIds = {101, 102, 103, 104}; SortedKeyIndex idx = SortedKeyIndex.create(userIds); double[] ratings1 = {4.0, 3.0, 2.5, 2.0}; double[] ratings2 = {3.0, 2.5, 4.0, 1.0}; double[] ratings3 = {5.0, 3.5, 0.5, 1.0}; double[] ratings4 = {4.5, 3.0, 3.5, 1.5}; Long2DoubleSortedArrayMap v1 = Long2DoubleSortedArrayMap.wrap(idx, ratings1); Long2DoubleSortedArrayMap v2 = Long2DoubleSortedArrayMap.wrap(idx, ratings2); Long2DoubleSortedArrayMap v3 = Long2DoubleSortedArrayMap.wrap(idx, ratings3); Long2DoubleSortedArrayMap v4 = Long2DoubleSortedArrayMap.wrap(idx, ratings4); Long2DoubleSortedArrayMap[] ratings = { v1, v2, v3, v4 }; ItemItemBuildContext context = new ItemItemBuildContext(items, ratings, new Long2ObjectOpenHashMap<LongSortedSet>()); testRatingIntegrity(items, ratings, context); }
@Test public void testIterStartFrom() { double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(1, 2, 3, 4, 5), values); AbstractLong2DoubleMap.BasicEntry key = new AbstractLong2DoubleMap.BasicEntry(2, 2.0); ObjectBidirectionalIterator<Long2DoubleMap.Entry> iter = map.long2DoubleEntrySet().iterator(key); assertThat(iter.next().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(2L)); }
@Test public void testSubMapUnpacked() { SortedKeyIndex idx = SortedKeyIndex.create(1, 2, 3, 4, 5); double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedArrayMap map = new Long2DoubleSortedArrayMap(idx, values); assertThat(map.size(), equalTo(5)); Long2DoubleSortedMap sub = map.subMap(new LongOpenHashSet(LongUtils.packedSet(2L, 10L, 4L))); assertThat(sub.size(), equalTo(2)); assertThat(sub.containsKey(2L), equalTo(true)); assertThat(sub.containsKey(1L), equalTo(false)); assertThat(sub.containsKey(5L), equalTo(false)); assertThat(sub.containsKey(4L), equalTo(true)); assertThat(sub.containsKey(3L), equalTo(false)); assertThat(sub.keySet(), contains(2L, 4L)); assertThat(sub, hasEntry(2L, 2.4)); assertThat(sub, hasEntry(4L, 4.3)); }
@Test public void testSubMap() { SortedKeyIndex idx = SortedKeyIndex.create(1, 2, 3, 4, 5); double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedArrayMap map = new Long2DoubleSortedArrayMap(idx, values); assertThat(map.size(), equalTo(5)); Long2DoubleSortedMap sub = map.subMap(LongUtils.packedSet(2L, 4L)); assertThat(sub.size(), equalTo(2)); assertThat(sub.containsKey(2L), equalTo(true)); assertThat(sub.containsKey(1L), equalTo(false)); assertThat(sub.containsKey(5L), equalTo(false)); assertThat(sub.containsKey(4L), equalTo(true)); assertThat(sub.containsKey(3L), equalTo(false)); assertThat(sub.keySet(), contains(2L, 4L)); assertThat(sub, hasEntry(2L, 2.4)); assertThat(sub, hasEntry(4L, 4.3)); }
@Test public void testSubViewLowerBound() { SortedKeyIndex keys = SortedKeyIndex.create(0, 1, 2, 3, 4, 5, 6, 7); SortedKeyIndex subk = keys.subIndex(1, 5); assertThat(subk.getLowerBound(), equalTo(1)); assertThat(subk.getUpperBound(), equalTo(5)); assertThat(subk.tryGetIndex(1), equalTo(1)); assertThat(subk.tryGetIndex(4), equalTo(4)); assertThat(subk.tryGetIndex(5), lessThan(0)); assertThat(subk.tryGetIndex(6), lessThan(0)); assertThat(subk.tryGetIndex(0), lessThan(0)); assertThat(subk.findLowerBound(1L), equalTo(1)); assertThat(subk.findLowerBound(0L), equalTo(1)); assertThat(subk.findLowerBound(2L), equalTo(2)); assertThat(subk.findLowerBound(7L), equalTo(5)); assertThat(subk.findUpperBound(1), equalTo(2)); assertThat(subk.findUpperBound(5), equalTo(5)); assertThat(subk.findUpperBound(4), equalTo(5)); } }
/** * Test method for {@link Ratings#userRatingVector(java.util.Collection)}. */ @Test public void testUserRatingVector() { Collection<Rating> ratings = Lists.newArrayList( Rating.create(5, 7, 3.5), Rating.create(5, 3, 1.5), Rating.create(5, 8, 2) ); Long2DoubleMap v = Ratings.userRatingVector(ratings); assertEquals(3, v.size()); assertEquals(7, Vectors.sum(v), EPSILON); long[] keys = {3, 7, 8}; double[] values = {1.5, 3.5, 2}; Long2DoubleSortedArrayMap sv = Long2DoubleSortedArrayMap.wrap(SortedKeyIndex.create(keys), values); assertEquals(sv, v); } }