@Override public boolean contains(Object o) { if (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry) o; long key = e instanceof Entry ? ((Entry) e).getLongKey() : (Long) e.getKey(); int idx = keys.tryGetIndex(key); if (idx >= 0) { return e.getValue().equals(values[idx]); } } return false; }
/** * Convert a map entry to a basic result. * @param entry The map entry. * @return The basic result. */ public static BasicResult fromEntry(Map.Entry<Long,Double> entry) { if (entry instanceof Long2DoubleMap.Entry) { Long2DoubleMap.Entry e = (Long2DoubleMap.Entry) entry; return create(e.getLongKey(), e.getDoubleValue()); } else { return create(entry.getKey(), entry.getValue()); } }
@Test public void testIterStartFrom() { double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(1, 2, 3, 4, 5), values); AbstractLong2DoubleMap.BasicEntry key = new AbstractLong2DoubleMap.BasicEntry(2, 2.0); ObjectBidirectionalIterator<Long2DoubleMap.Entry> iter = map.long2DoubleEntrySet().iterator(key); assertThat(iter.next().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(2L)); }
@Test public void testSimpleTruncate() { long[] keys = {1, 2, 3, 4, 5}; double[] values = {1.0, 2.0, 3.0, 4.0, 5.0}; Long2DoubleSortedArrayMap v = Long2DoubleSortedArrayMap.wrapUnsorted(keys, values); VectorTruncator truncator = new TopNTruncator(3, null); Long2DoubleMap v2 = truncator.truncate(v); long i = 3; for (Long2DoubleMap.Entry e: v2.long2DoubleEntrySet()) { assertThat(e.getLongKey(), equalTo(i)); assertThat(e.getDoubleValue(), closeTo(i, EPSILON)); i++; } assertThat(i, equalTo(6L)); } }
@Test public void testTruncate() { long[] keys = {1, 2, 3, 4}; double[] values = {1.0, 2.0, 3.0, 4.0}; Long2DoubleSortedArrayMap v = Long2DoubleSortedArrayMap.wrapUnsorted(keys, values); VectorTruncator truncator = new ThresholdTruncator(new RealThreshold(3.5)); Long2DoubleMap v2 = truncator.truncate(v); int numSeen = 0; for (Long2DoubleMap.Entry e: v2.long2DoubleEntrySet()) { assertThat(e.getLongKey(), equalTo(4L)); assertThat(e.getDoubleValue(), closeTo(4.0, EPSILON)); numSeen++; } assertThat(numSeen, equalTo(1)); } }
@Override public double getUserBias(long user) { Long2DoubleMap vec = dao.userRatingVector(user); if (vec.isEmpty()) { return 0; } else { double sum = 0; double mean = getIntercept(); for (Long2DoubleMap.Entry e: vec.long2DoubleEntrySet()) { sum += e.getDoubleValue() - mean - getItemBias(e.getLongKey()); } return sum / vec.size(); } }
@Override public ObjectSortedSet<Entry> subSet(Entry from, Entry to) { return subMap(from.getLongKey(), to.getLongKey()).long2DoubleEntrySet(); }
@Override public ObjectSortedSet<Entry> tailSet(Entry entry) { return tailMap(entry.getLongKey()).long2DoubleEntrySet(); }
@Override public ObjectSortedSet<Entry> headSet(Entry entry) { return headMap(entry.getLongKey()).long2DoubleEntrySet(); }
@Override public ObjectBidirectionalIterator<Entry> iterator(Entry entry) { return new EntryIter(entry.getLongKey()); }
@Override public ObjectBidirectionalIterator<Entry> fastIterator(Entry entry) { return new EntryIter(entry.getLongKey()); }
@Override public BiConsumer<Long2ObjectMap<Long2DoubleAccumulator>, IdBox<Long2DoubleMap>> accumulator() { return (acc, row) -> { long i1 = row.getId(); for (Long2DoubleMap.Entry e: Long2DoubleMaps.fastIterable(row.getValue())) { long i2 = e.getLongKey(); double sim = e.getDoubleValue(); acc.computeIfAbsent(i1, i -> newAccumulator()) .put(i2, sim); acc.computeIfAbsent(i2, i -> newAccumulator()) .put(i1, sim); } }; }
@Override public Long2DoubleMap truncate(Long2DoubleMap v) { Long2DoubleMap res = new Long2DoubleOpenHashMap(v.size()); for (Long2DoubleMap.Entry e: Vectors.fastEntries(v)) { if (threshold.retain(e.getDoubleValue())) { res.put(e.getLongKey(), e.getDoubleValue()); } } return res; } }
/** * Add all items from a map to the accumulator. * @param map The map. */ default void putAll(Long2DoubleMap map) { for (Long2DoubleMap.Entry e: Long2DoubleMaps.fastIterable(map)) { put(e.getLongKey(), e.getDoubleValue()); } }
@Test public void testFastIterStartFrom() { double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedArrayMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(1, 2, 3, 4, 5), values); AbstractLong2DoubleMap.BasicEntry key = new AbstractLong2DoubleMap.BasicEntry(2, 2.0); ObjectBidirectionalIterator<Long2DoubleMap.Entry> iter = map.long2DoubleEntrySet().fastIterator(key); assertThat(iter.next().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(2L)); }
@Override public Long2DoubleMap truncate(Long2DoubleMap v) { TopNLong2DoubleAccumulator accumulator = new TopNLong2DoubleAccumulator(n); for (Long2DoubleMap.Entry e : Vectors.fastEntries(v)) { double x = e.getDoubleValue(); if (threshold == null || threshold.retain(x)) { accumulator.put(e.getLongKey(), x); } } return accumulator.finishMap(); } }
@Override public ObjectSortedSet<Long2DoubleMap.Entry> subSet(Long2DoubleMap.Entry entry, Long2DoubleMap.Entry k1) { return new BasicResultMap(delegate.subMap(entry.getLongKey(), k1.getLongKey())) .scoreMap() .long2DoubleEntrySet(); }
@Override public double similarity(Long2DoubleMap vec1, Long2DoubleMap vec2) { MutualInformationAccumulator accum = new MutualInformationAccumulator(quantizer.getCount()); for (Long2DoubleMap.Entry e: vec1.long2DoubleEntrySet()) { long k = e.getLongKey(); if (vec2.containsKey(k)) { accum.count(quantizer.index(e.getDoubleValue()), quantizer.index(vec2.get(k))); } } return accum.getMutualInformation(); }
@Override public ObjectSortedSet<Long2DoubleMap.Entry> tailSet(Long2DoubleMap.Entry entry) { return new BasicResultMap(delegate.tailMap(entry.getLongKey())) .scoreMap() .long2DoubleEntrySet(); }
@Override public ObjectSortedSet<Long2DoubleMap.Entry> headSet(Long2DoubleMap.Entry entry) { return new BasicResultMap(delegate.headMap(entry.getLongKey())) .scoreMap() .long2DoubleEntrySet(); }