@Override public LongCollection getUserIds() { return userIndex().getKeyList(); }
@Override public LongCollection getItemIds() { return itemIndex().getKeyList(); }
@Test public void testItemIndex() { KeyIndex ind = snap.itemIndex(); assertEquals(5, ind.size()); assertThat(ind.getKeyList(), containsInAnyOrder(7L, 8L, 9L, 10L, 11L)); }
/** * Create a map from an array and index mapping. * * @param mapping The index mapping specifying the keys. * @param values The array of values. * @return A sparse vector mapping the IDs in {@code map} to the values in {@code values}. * @throws IllegalArgumentException if {@code values} not the same size as {@code idx}. */ public static Long2DoubleSortedArrayMap fromArray(KeyIndex mapping, DoubleList values) { Preconditions.checkArgument(values.size() == mapping.size(), "value array and index have different sizes: " + values.size() + " != " + mapping.size()); final int n = values.size(); double[] nvs = new double[n]; SortedKeyIndex index = SortedKeyIndex.fromCollection(mapping.getKeyList()); for (int i = 0; i < n; i++) { long item = index.getKey(i); int origIndex = mapping.getIndex(item); nvs[i] = values.getDouble(origIndex); } return wrap(index, nvs); }
@Test public void testEmpty() { KeyIndex empty = new FrozenHashKeyIndex(LongLists.EMPTY_LIST); assertThat(empty.getKeyList(), hasSize(0)); assertThat(empty.containsKey(30), equalTo(false)); assertThat(empty.tryGetIndex(30), equalTo(-1)); try { empty.getIndex(30); fail("getting absent index should fail"); } catch (IllegalArgumentException ex) { /* expected */ } }
@Test public void testSingleton() { KeyIndex idx = new FrozenHashKeyIndex(LongLists.singleton(42)); assertThat(idx.getKeyList(), hasSize(1)); assertThat(idx.getKeyList(), contains(42L)); assertThat(idx.containsKey(30), equalTo(false)); assertThat(idx.containsKey(42), equalTo(true)); assertThat(idx.tryGetIndex(30), equalTo(-1)); assertThat(idx.tryGetIndex(42), equalTo(0)); assertThat(idx.getIndex(42), equalTo(0)); assertThat(idx.getKey(0), equalTo(42L)); try { idx.getIndex(30); fail("getting absent index should fail"); } catch (IllegalArgumentException ex) { /* expected */ } }
@Test public void testUserIndex() { KeyIndex ind = snap.userIndex(); assertEquals(6, ind.size()); assertTrue(ind.getKeyList().contains(1)); assertTrue(ind.getKeyList().contains(3)); assertTrue(ind.getKeyList().contains(4)); assertTrue(ind.getKeyList().contains(5)); assertTrue(ind.getKeyList().contains(6)); assertTrue(ind.getKeyList().contains(7)); assertEquals(0, ind.getIndex(1)); assertEquals(1, ind.getIndex(3)); assertEquals(2, ind.getIndex(4)); assertEquals(3, ind.getIndex(5)); assertEquals(4, ind.getIndex(6)); assertEquals(5, ind.getIndex(7)); assertEquals(1, ind.getKey(0)); assertEquals(3, ind.getKey(1)); assertEquals(4, ind.getKey(2)); assertEquals(5, ind.getKey(3)); assertEquals(6, ind.getKey(4)); assertEquals(7, ind.getKey(5)); }
@Override public LongCollection getItemIds() { return itemIndex().getKeyList(); }
@Override public LongCollection getUserIds() { return userIndex().getKeyList(); }
/** * Create a map from an array and index mapping. * * @param mapping The index mapping specifying the keys. * @param values The array of values. * @return A sparse vector mapping the IDs in {@code map} to the values in {@code values}. * @throws IllegalArgumentException if {@code values} not the same size as {@code idx}. */ public static Long2DoubleSortedArrayMap fromArray(KeyIndex mapping, DoubleList values) { Preconditions.checkArgument(values.size() == mapping.size(), "value array and index have different sizes: " + values.size() + " != " + mapping.size()); final int n = values.size(); double[] nvs = new double[n]; SortedKeyIndex index = SortedKeyIndex.fromCollection(mapping.getKeyList()); for (int i = 0; i < n; i++) { long item = index.getKey(i); int origIndex = mapping.getIndex(item); nvs[i] = values.get(origIndex); } return wrap(index, nvs); }