@Override public int size() { return keys.size(); }
@Override public int size() { return SortedKeyIndex.this.size(); }
@Override public int size() { return keys.size(); }
/** * Construct a new rating summary. * @param mean The global mean rating. * @param keys The item IDs. * @param offsets The item mean offsets. * @param counts The item rating counts. */ RatingSummary(double mean, SortedKeyIndex keys, double[] offsets, int[] counts) { Preconditions.checkArgument(offsets.length == keys.size(), "offset array length"); Preconditions.checkArgument(counts.length == keys.size(), "count array length"); globalMean = mean; itemIndex = keys; itemOffsets = offsets; itemCounts = counts; }
@Override public long firstLong() { if (keys.size() > 0) { return keys.getKey(keys.getLowerBound()); } else { throw new NoSuchElementException(); } }
/** * Create a map that maps a group of items to the same value. * @param keys The keys. * @param value The value. * @return A map that contains all `keys`, mapping each of them to `value`. */ public static Long2DoubleMap constantDoubleMap(Set<Long> keys, double value) { // TODO Implement this using a flyweight wrapper SortedKeyIndex idx = SortedKeyIndex.fromCollection(keys); double[] values = new double[idx.size()]; Arrays.fill(values, value); return Long2DoubleSortedArrayMap.wrap(idx, values); }
@Override public long lastLong() { if (keys.size() > 0) { return keys.getKey(keys.getUpperBound() - 1); } else { throw new NoSuchElementException(); } }
/** * Construct a new item-item model. * * @param nbrs The item neighborhoods. The item neighborhood lists are not copied. */ public SimilarityMatrixModel(Map<Long,Long2DoubleMap> nbrs) { itemDomain = SortedKeyIndex.fromCollection(nbrs.keySet()); int n = itemDomain.size(); assert n == nbrs.size(); ImmutableList.Builder<Long2DoubleMap> neighbors = ImmutableList.builder(); for (int i = 0; i < n; i++) { neighbors.add(LongUtils.frozenMap(nbrs.get(itemDomain.getKey(i)))); } neighborhoods = neighbors.build(); }
/** * Create a new map with existing data. * @param data Use {@link #create(Map)} instead, as it can avoid copying maps that are already packed. */ @Deprecated public Long2DoubleSortedArrayMap(Map<Long,Double> data) { Long2DoubleFunction vf = LongUtils.asLong2DoubleFunction(data); keys = SortedKeyIndex.fromCollection(data.keySet()); int size = keys.size(); values = new double[size]; for (int i = 0; i < size; i++) { values[i] = vf.get(keys.getKey(i)); } }
@Override public long getLong(int i) { Preconditions.checkElementIndex(i, SortedKeyIndex.this.size()); return getKey(getLowerBound() + i); } }
@Override public Long2DoubleMap getUserBiases(LongSet users) { SortedKeyIndex index = SortedKeyIndex.fromCollection(users); final int n = index.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { values[i] = getUserBias(index.getKey(i)); } return Long2DoubleSortedArrayMap.wrap(index, values); }
public Long2DoubleSortedMap clampVector(Map<Long,Double> scores) { SortedKeyIndex keys = SortedKeyIndex.fromCollection(scores.keySet()); Long2DoubleFunction baseVals = LongUtils.asLong2DoubleMap(scores); double[] values = new double[keys.size()]; for (int i = 0; i < values.length; i++) { long item = keys.getKey(i); values[i] = clampValue(baseVals.get(item)); } return Long2DoubleSortedArrayMap.wrap(keys, values); }
/** * Multiply each element of a vector by a scalar. * @param vector The vector. * @param value The scalar to multiply. * @return A new vector consisting of the same keys as `vector`, with `value` multipled by each. */ @Nonnull public static Long2DoubleMap multiplyScalar(Long2DoubleMap vector, double value) { // TODO Consier implementing this in terms of transform SortedKeyIndex idx = SortedKeyIndex.fromCollection(vector.keySet()); int n = idx.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { values[i] = vector.get(idx.getKey(i)) * value; } return Long2DoubleSortedArrayMap.wrap(idx, values); }
@Nullable @Override public Long2DoubleMap apply(@Nullable Long2DoubleMap input) { if (input == null) return null; Map<Long,Double> base = baselineScorer.score(user, input.keySet()); SortedKeyIndex idx = SortedKeyIndex.fromCollection(input.keySet()); int n = idx.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { long k = idx.getKey(i); Double bp = base.get(k); double bpv = bp != null ? bp : 0; values[i] = input.get(idx.getKey(i)) - bpv; } return Long2DoubleSortedArrayMap.wrap(idx, values); }
@Override public Long2DoubleMap unapply(Long2DoubleMap input) { if (input == null) return null; Map<Long,Double> base = baselineScorer.score(user, input.keySet()); SortedKeyIndex idx = SortedKeyIndex.fromCollection(input.keySet()); int n = idx.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { long k = idx.getKey(i); Double bp = base.get(k); double bpv = bp != null ? bp : 0; values[i] = input.get(idx.getKey(i)) + bpv; } return Long2DoubleSortedArrayMap.wrap(idx, values); }
@Inject public PopularityRankItemScorer(final InteractionStatistics stats) { statistics = stats; long[] items = stats.getKnownItems().toLongArray(); LongArrays.quickSort(items, (l1, l2) -> Integer.compare(stats.getInteractionCount(l2), stats.getInteractionCount(l1))); Long2IntMap ranks = LongUtils.itemRanks(LongArrayList.wrap(items)); SortedKeyIndex keys = SortedKeyIndex.fromCollection(ranks.keySet()); int n = keys.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { values[i] = 1.0 - ranks.get(keys.getKey(i)) / ((double) n); } rankScores = Long2DoubleSortedArrayMap.wrap(keys, values); }
@Test public void testEmptyArray() { long[] rawKeys = {}; SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, 0); assertThat(keys.size(), equalTo(0)); assertThat(keys.size(), equalTo(0)); assertThat(keys.keySet(), hasSize(0)); assertThat(keys.getKeyList(), hasSize(0)); assertThat(keys.tryGetIndex(42), lessThan(0)); }
@Theory public void testSingleton(KeyData data) { assumeThat(data, notNullValue()); long key = data.getKey(); // key to use long low = data.getLow(); // unused low key long high = data.getAfter(1); // unused high key long[] rawKeys = {key}; SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, 1); assertThat(keys.size(), equalTo(1)); assertThat(keys.size(), equalTo(1)); assertThat(keys.keySet(), hasSize(1)); assertThat(keys.tryGetIndex(key), equalTo(0)); assertThat(keys.tryGetIndex(low), lessThan(0)); assertThat(keys.tryGetIndex(high), lessThan(0)); assertThat(keys.getKeyList(), contains(key)); }