@Override public long getLongKey() { return keys.getKey(index); }
@Override public long previousLong() { if (position > lowerBound) { // since position is *next* thing to return by 'next', it is one past *last* thing returned. position -= 1; return getKey(position); } else { throw new NoSuchElementException(); } } }
@Override public long nextLong() { if (position < upperBound) { // position is the next thing to return by 'next' long k = getKey(position); position += 1; return k; } else { throw new NoSuchElementException(); } }
/** * Get a key by its position in the map. Used for optimizing certain operations. * @param i The index. * @return The key at position {@code i}. */ public long getKeyByIndex(int i) { return keys.getKey(i + keys.getLowerBound()); }
private Entry entry(int idx) { return new BasicEntry(keys.getKey(idx), values[idx]); }
@Override public long firstLong() { if (keys.size() > 0) { return keys.getKey(keys.getLowerBound()); } else { throw new NoSuchElementException(); } }
@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)); } }
@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 long getLong(int i) { Preconditions.checkElementIndex(i, SortedKeyIndex.this.size()); return getKey(getLowerBound() + i); } }
@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); }
@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); }
/** * Add a scalar to each element of a vector. * @param vec The vector to rescale. * @param val The value to add. * @return A new map with every value in {@code vec} increased by {@code val}. */ public static Long2DoubleMap addScalar(Long2DoubleMap vec, double val) { SortedKeyIndex keys = SortedKeyIndex.fromCollection(vec.keySet()); final int n = keys.size(); double[] values = new double[n]; if (vec instanceof Long2DoubleSortedArrayMap) { Long2DoubleSortedArrayMap sorted = (Long2DoubleSortedArrayMap) vec; for (int i = 0; i < n; i++) { assert sorted.getKeyByIndex(i) == keys.getKey(i); values[i] = sorted.getValueByIndex(i) + val; } } else { for (int i = 0; i < n; i++) { values[i] = vec.get(keys.getKey(i)) + val; } } return Long2DoubleSortedArrayMap.wrap(keys, 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); }
/** * Transform the values of a vector. * * @param input The vector to transform. * @param function The transformation to apply. * @return A new vector that is the result of applying `function` to each value in `input`. */ public static Long2DoubleMap transform(Long2DoubleMap input, DoubleUnaryOperator function) { // FIXME Improve performance when input is also sorted SortedKeyIndex idx = SortedKeyIndex.fromCollection(input.keySet()); int n = idx.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { values[i] = function.applyAsDouble(input.get(idx.getKey(i))); } return Long2DoubleSortedArrayMap.wrap(idx, values); }
private Long2DoubleSortedArrayMap slowSubMap(LongSet toKeep) { LongSortedSet kept = LongUtils.setIntersect(keySet(), toKeep); double[] nvs = new double[kept.size()]; int i = keys.getLowerBound(); int j = 0; LongIterator iter = kept.iterator(); while (iter.hasNext()) { long key = iter.nextLong(); while (keys.getKey(i) < key) { i++; } nvs[j] = values[i]; j++; i++; } return wrap(SortedKeyIndex.fromCollection(kept), nvs); }
/** * Construct a new interaction statistics object. * * @param type The counted entity type. * @param counts A map of item interaction counts. */ public InteractionStatistics(EntityType type, Long2IntMap counts) { entityType = type; items = SortedKeyIndex.fromCollection(counts.keySet()); int n = items.size(); interactionCounts = new int[n]; for (int i = 0; i < n; i++) { interactionCounts[i] = counts.get(items.getKey(i)); } long[] iarray = items.keySet().toLongArray(); LongArrays.quickSort(iarray, (l1, l2) -> Doubles.compare(counts.get(l2), counts.get(l1))); itemList = LongArrayList.wrap(iarray); }