@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); }
/** * Query whether this set contains the specified key in its domain. * @param key The key. * @return {@code true} if the key is in the domain. */ @Override public boolean containsKey(long key) { int idx = tryGetIndex(key); return idx >= lowerBound && idx < upperBound; }
/** * Pack longs into a sorted set. * @param longs A collection of longs. * @return An efficient sorted set containing the numbers in {@code longs}. */ public static LongSortedArraySet packedSet(Collection<Long> longs) { if (longs instanceof LongSortedArraySet) { return (LongSortedArraySet) longs; } else { return SortedKeyIndex.fromCollection(longs).keySet(); } }
/** * 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()); }
@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)); }
private Long2DoubleSortedArrayMap fastSubMap(LongSortedArraySet toKeep) { SortedKeyIndex oks = toKeep.getIndex(); int tn = size(); int on = oks.size(); long[] nks = new long[Math.min(tn, on)]; double[] nvs = new double[Math.min(tn, on)]; int tlb = keys.getLowerBound(); int olb = oks.getLowerBound(); int ni = 0; long k = oks.getKey(oi + olb); int ti = keys.tryGetIndex(k); if (ti >= 0) { nks[ni] = k; tk = keys.getKey(tlb + ti); lti = ti; ok = oks.getKey(olb + oi); loi = oi; return wrap(SortedKeyIndex.wrap(nks, ni), nvs);
@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)); } }
@Override public long firstLong() { if (keys.size() > 0) { return keys.getKey(keys.getLowerBound()); } else { throw new NoSuchElementException(); } }
@Test public void testEmptyCollection() { SortedKeyIndex keys = SortedKeyIndex.fromCollection(LongLists.EMPTY_LIST); assertThat(keys.size(), equalTo(0)); assertThat(keys.size(), equalTo(0)); assertThat(keys.keySet(), hasSize(0)); assertThat(keys.getKeyList(), hasSize(0)); }
@Override public long lastLong() { if (keys.size() > 0) { return keys.getKey(keys.getUpperBound() - 1); } else { throw new NoSuchElementException(); } }
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); }
/** * 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); }
SortedKeyIndex domain = SortedKeyIndex.fromCollection(vectors.keySet()); ImmutableList.Builder<Long2DoubleMap> scoreVectors = ImmutableList.builder(); ImmutableList.Builder<Long2DoubleMap> normedVectors = ImmutableList.builder(); for (LongIterator uiter = domain.keyIterator(); uiter.hasNext();) { final long user = uiter.nextLong(); Long2DoubleMap rawV = vectors.get(user);
@Override public LongSortedSet tailSet(long key) { int start = keys.findLowerBound(key); int end = keys.getUpperBound(); return new LongSortedArraySet(keys.subIndex(start, end)); }
@Override public LongSortedSet headSet(long key) { int start = keys.getLowerBound(); int end = keys.findLowerBound(key); return new LongSortedArraySet(keys.subIndex(start, end)); }
/** * Construct a new array set from a collection of items. * * @param items The set's contents. */ public LongSortedArraySet(@Nonnull Collection<Long> items) { this(SortedKeyIndex.fromCollection(items)); }
/** * 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)); }
/** * Get the set of known items. * @return The set of known items. */ public LongSortedSet getKnownItems() { return items.keySet(); }