/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
@Override public int getUpperBound() { return size(); }
@Override public boolean containsKey(long id) { return tryGetIndex(id) >= 0; }
/** * Construct a new indexer. It maps each long key to its position in the list, and then be ready for more keys. * * @param keys The list of keys to store. */ public static HashKeyIndex create(LongList keys) { HashKeyIndex idx = create(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { idx.internId(iter.nextLong()); } return idx; }
@Test public void testInternId() { HashKeyIndex idx = new HashKeyIndex(); assertThat(idx.size(), equalTo(0)); assertThat(idx.tryGetIndex(42), lessThan(0)); assertThat(idx.internId(42), equalTo(0)); assertThat(idx.getKey(0), equalTo(42L)); assertThat(idx.tryGetIndex(42), equalTo(0)); }
@Override public RatingSummary get() { HashKeyIndex index = new HashKeyIndex(); DoubleArrayList sums = new DoubleArrayList(); IntArrayList counts = new IntArrayList(); assert sums.size() == counts.size(); long item = r.getItemId(); int idx = index.internId(item); if (idx >= sums.size()) { assert idx == sums.size() && idx == counts.size(); SortedKeyIndex items = SortedKeyIndex.fromCollection(index.getKeyList()); final int n = items.size(); int[] countArray = new int[n]; int oidx = index.getIndex(items.getKey(i)); countArray[i] = counts.getInt(oidx); offsets[i] = sums.getDouble(oidx) / countArray[i] - mean;
@Test public void testReinternId() { HashKeyIndex idx = new HashKeyIndex(); assertThat(idx.internId(42), equalTo(0)); assertThat(idx.internId(39), equalTo(1)); assertThat(idx.internId(42), equalTo(0)); }
public static HashKeyIndex create() { return new HashKeyIndex(); }
/** * Internal set method that takes individual indexes. * * @param ci The chunk index. * @param ei The element index. * @param user The user ID to set. * @param item The item ID to set. * @param pref The preference value to set. */ private void set(int ci, int ei, long user, long item, double pref) { users[ci][ei] = userIndex.internId(user); items[ci][ei] = itemIndex.internId(item); values[ci][ei] = pref; }
private PackedRatingData internalBuild() { return new PackedRatingData(users, items, values, nprefs, userIndex.frozenCopy(), itemIndex.frozenCopy()); }
@Override public RatingSummary get() { HashKeyIndex index = new HashKeyIndex(); DoubleArrayList sums = new DoubleArrayList(); IntArrayList counts = new IntArrayList(); assert sums.size() == counts.size(); long item = r.getItemId(); int idx = index.internId(item); if (idx >= sums.size()) { assert idx == sums.size() && idx == counts.size(); SortedKeyIndex items = SortedKeyIndex.fromCollection(index.getKeyList()); final int n = items.size(); int[] countArray = new int[n]; int oidx = index.getIndex(items.getKey(i)); countArray[i] = counts.getInt(oidx); offsets[i] = sums.getDouble(oidx) / countArray[i] - mean;
@Test public void testFromArray() { HashKeyIndex map = new HashKeyIndex(); map.internId(42); map.internId(37); map.internId(62); double[] values = { 3.5, 4.9, 1.8 }; Long2DoubleMap res = Long2DoubleSortedArrayMap.fromArray(map, values); assertThat(res.size(), equalTo(3)); assertThat(res.keySet(), contains(37L, 42L, 62L)); assertThat(res, hasEntry(37L, 4.9)); assertThat(res, hasEntry(42L, 3.5)); assertThat(res, hasEntry(62L, 1.8)); }
/** * Construct a new indexer. It maps each long key to its position in the list, and then be ready for more keys. * * @param keys The list of keys to store. */ public static HashKeyIndex create(LongList keys) { HashKeyIndex idx = create(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { idx.internId(iter.nextLong()); } return idx; }
PackedRatingDataBuilder() { itemIndex = new HashKeyIndex(); userIndex = new HashKeyIndex(); freeList = new IntHeapPriorityQueue(); allocate(INITIAL_CHUNK_COUNT); }
/** * Internal set method that takes individual indexes. * * @param ci The chunk index. * @param ei The element index. * @param user The user ID to set. * @param item The item ID to set. * @param pref The preference value to set. */ private void set(int ci, int ei, long user, long item, double pref) { users[ci][ei] = userIndex.internId(user); items[ci][ei] = itemIndex.internId(item); values[ci][ei] = pref; }
private PackedRatingData internalBuild() { return new PackedRatingData(users, items, values, nprefs, userIndex.frozenCopy(), itemIndex.frozenCopy()); }
@Test public void testImmutableCopy() { HashKeyIndex idx = new HashKeyIndex(); assertThat(idx.internId(42), equalTo(0)); assertThat(idx.internId(39), equalTo(1)); KeyIndex imm = idx.frozenCopy(); assertThat(imm.getKey(0), equalTo(42L)); assertThat(imm.getKey(1), equalTo(39L)); assertThat(imm.getIndex(42), equalTo(0)); } }
@Before public void createModel() { baseline = PrecomputedItemScorer.newBuilder() .addScore(1, 42, 3.0) .addScore(1, 39, 2.5) .addScore(1, 25, 4.2) .addScore(5, 42, 3.7) .addScore(5, 39, 2.8) .addScore(3, 42, 2.2) .addScore(3, 39, 3.2) .addScore(17, 42, 2.5) .build(); RealMatrix umat = MatrixUtils.createRealMatrix(3, 2); umat.setRow(0, new double[]{0.1, 0.3}); umat.setRow(1, new double[]{-0.2, 0.2}); umat.setRow(2, new double[]{0.0, 0.15}); HashKeyIndex uidx = new HashKeyIndex(); uidx.internId(1); uidx.internId(5); uidx.internId(3); RealMatrix imat = MatrixUtils.createRealMatrix(2, 2); imat.setRow(0, new double[]{0.52, 0.29}); imat.setRow(1, new double[]{0.3, -1.2}); HashKeyIndex iidx = new HashKeyIndex(); iidx.internId(42); iidx.internId(39); model = new MFModel(umat, imat, uidx, iidx); // scorer = new BiasedMFItemScorer(model, baseline); }
public static HashKeyIndex create() { return new HashKeyIndex(); }
@Override public int getIndex(long id) { int idx = tryGetIndex(id); if (idx < 0) { throw new IllegalArgumentException("key " + id + " not in index"); } else { return idx; } }