@Override public int size() { return keySet().size(); } }
@Override public long lastLongKey() { return keySet().lastLong(); }
@Override public long firstLongKey() { return keySet().firstLong(); }
/** * Return a subset of this map containing only the keys that appear in another set. * @param toKeep The set of keys to keep. * @return A copy of this map containing only those keys that appear in {@code keys}. */ public Long2DoubleSortedArrayMap subMap(LongSet toKeep) { if (toKeep == keySet()) { return this; } if (toKeep instanceof LongSortedArraySet) { return fastSubMap((LongSortedArraySet) toKeep); } else { return slowSubMap(toKeep); } }
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); }
@Test public void testSubMapIndexes() { Long2DoubleMap map = new Long2DoubleOpenHashMap(); map.put(1, 1.0); map.put(2, 2.0); map.put(3, 3.0); map.put(4, 4.0); Long2DoubleSortedArrayMap sam = Long2DoubleSortedArrayMap.create(map); Long2DoubleSortedArrayMap s2 = sam.subMap(2, 4); assertThat(s2.keySet(), contains(2L, 3L)); assertThat(s2.getKeyByIndex(0), equalTo(2L)); assertThat(s2.getKeyByIndex(1), equalTo(3L)); assertThat(s2.getValueByIndex(0), equalTo(2.0)); assertThat(s2.getValueByIndex(1), equalTo(3.0)); } }
/** * Add a vector to another (scaled) vector and a scalar. The result is \\(x_i + s_y y_i + o\\). * * @param x The source vector. * @param y The addition vector. {@link Long2DoubleFunction#defaultReturnValue()} is assumed for missing values. * @param sy The scale by which elements of {@code y} are multipled. * @param o The offset to add. * @return A vector with the same keys as {@code x}, transformed by the specified linear formula. */ public static Long2DoubleMap combine(Long2DoubleMap x, Long2DoubleFunction y, double sy, double o) { SortedKeyIndex idx = SortedKeyIndex.fromCollection(x.keySet()); final int n = idx.size(); double[] values = new double[n]; if (x instanceof Long2DoubleSortedArrayMap) { // TODO make this fast for two sorted maps Long2DoubleSortedArrayMap sx = (Long2DoubleSortedArrayMap) x; assert idx == sx.keySet().getIndex(); for (int i = 0; i < n; i++) { values[i] = sx.getValueByIndex(i) + y.get(idx.getKey(i)) * sy + o; } } else { for (int i = 0; i < n; i++) { long k = idx.getKey(i); values[i] = x.get(k) + y.get(k) * sy + o; } } return Long2DoubleSortedArrayMap.wrap(idx, values); }
@Override public long lastLongKey() { return keySet().lastLong(); }
@Override public int size() { return keySet().size(); } }
@Override public long firstLongKey() { return keySet().firstLong(); }
/** * Return a subset of this map containing only the keys that appear in another set. * @param toKeep The set of keys to keep. * @return A copy of this map containing only those keys that appear in {@code keys}. */ public Long2DoubleSortedArrayMap subMap(LongSet toKeep) { if (toKeep == keySet()) { return this; } if (toKeep instanceof LongSortedArraySet) { return fastSubMap((LongSortedArraySet) toKeep); } else { return slowSubMap(toKeep); } }
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); }
/** * Add a vector to another (scaled) vector and a scalar. The result is \\(x_i + s_y y_i + o\\). * * @param x The source vector. * @param y The addition vector. {@link Long2DoubleFunction#defaultReturnValue()} is assumed for missing values. * @param sy The scale by which elements of {@code y} are multipled. * @param o The offset to add. * @return A vector with the same keys as {@code x}, transformed by the specified linear formula. */ public static Long2DoubleMap combine(Long2DoubleMap x, Long2DoubleFunction y, double sy, double o) { SortedKeyIndex idx = SortedKeyIndex.fromCollection(x.keySet()); final int n = idx.size(); double[] values = new double[n]; if (x instanceof Long2DoubleSortedArrayMap) { // TODO make this fast for two sorted maps Long2DoubleSortedArrayMap sx = (Long2DoubleSortedArrayMap) x; assert idx == sx.keySet().getIndex(); for (int i = 0; i < n; i++) { values[i] = sx.getValueByIndex(i) + y.get(idx.getKey(i)) * sy + o; } } else { for (int i = 0; i < n; i++) { long k = idx.getKey(i); values[i] = x.get(k) + y.get(k) * sy + o; } } return Long2DoubleSortedArrayMap.wrap(idx, values); }