public void mergeWith(SetDigest other) { hll.mergeWith(other.hll); LongBidirectionalIterator iterator = other.minhash.keySet().iterator(); while (iterator.hasNext()) { long key = iterator.nextLong(); int count = minhash.get(key) + other.minhash.get(key); minhash.put(key, Shorts.saturatedCast(count)); } while (minhash.size() > maxHashes) { minhash.remove(minhash.lastLongKey()); } }
/** * Puts the item pair into the accumulator. * * @param id1 The id of the first item. * @param itemVec1 The rating vector of the first item. * @param id2 The id of the second item. * @param itemVec2 The rating vector of the second item. */ public void putItemPair(long id1, Long2DoubleSortedMap itemVec1, long id2, Long2DoubleSortedMap itemVec2) { if (workMatrix == null) { throw new IllegalStateException("Model is already built"); } // to profit from matrix symmetry, always store by the lesser id if (id1 < id2) { int coratings = 0; double deviation = 0.0; LongIterator iter = itemVec1.keySet().iterator(); while (iter.hasNext()) { long u = iter.nextLong(); if (itemVec2.containsKey(u)) { coratings++; deviation += itemVec1.get(u) - itemVec2.get(u); } } deviation = (coratings == 0) ? Double.NaN : deviation; Pair<Long2DoubleMap, Long2IntMap> row = workMatrix.get(id1); row.getLeft().put(id2, deviation); row.getRight().put(id2, coratings); } }
LongIterator iter = itemSet.iterator(); while (iter.hasNext()) { final long item = iter.nextLong();
@Test public void testTailIter() { long[] data = {0, 42, 5, 2, 6, 1, 7}; LongSortedSet set = new LongSortedArraySet(data); LongBidirectionalIterator iter = set.iterator(2); assertTrue(iter.hasNext()); assertTrue(iter.hasPrevious()); assertEquals(5, iter.nextLong()); iter = set.iterator(2); assertEquals(2, iter.previousLong()); iter = set.iterator(-5); assertFalse(iter.hasPrevious()); iter = set.iterator(100); assertFalse(iter.hasNext()); iter = set.iterator(3); assertEquals(5, iter.nextLong()); iter = set.iterator(3); assertEquals(2, iter.previousLong()); }
@Override public LongBidirectionalIterator iterator(final long from) { return sortedSet.iterator(from); } @Override
@Override public LongBidirectionalIterator iterator() { return sortedSet.iterator(); } @Override
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 testSubsetIterator() { LongSortedSet set = LongUtils.packedSet(1L, 3L, 5L, 10L); LongSortedSet subset = set.subSet(3L, 6L); assertThat(subset, hasSize(2)); assertThat(subset, contains(3L, 5L)); LongBidirectionalIterator iter = subset.iterator(); assertThat(iter.hasPrevious(), equalTo(false)); try { iter.previousLong(); fail("previousLong should fail"); } catch (NoSuchElementException e) { /* expected */ } }
@Override public LongBidirectionalIterator iterator() { return LongIterators.unmodifiable(sortedSet.iterator()); } @Override
@Override public LongBidirectionalIterator iterator(final long from) { return LongIterators.unmodifiable(sortedSet.iterator(from)); } @Override
@Test public void testArrayCtor() { LongSortedSet set = new LongSortedArraySet(new long[]{2, 5, 6}); testSetSimple(set); LongBidirectionalIterator iter = set.iterator(); assertTrue(iter.hasNext()); assertEquals(2, iter.nextLong()); assertTrue(iter.hasPrevious()); assertEquals(2, iter.previousLong()); assertFalse(iter.hasPrevious()); }
/** * Run a battery of tests on a standard set. Used to test a variety of * construction scenarios with less code duplication. * * @param set The set {2, 5, 6}. */ private void testSetSimple(LongSortedSet set) { assertFalse(set.isEmpty()); assertEquals(3, set.size()); assertEquals(2, set.firstLong()); assertEquals(6, set.lastLong()); assertEquals(Long.valueOf(2), set.first()); assertEquals(Long.valueOf(6), set.last()); assertTrue(set.contains(2)); assertTrue(set.contains(5)); assertTrue(set.contains(6)); assertFalse(set.contains(0)); assertFalse(set.contains(42)); assertFalse(set.iterator().hasPrevious()); long[] items = LongIterators.unwrap(set.iterator()); assertEquals(2, items[0]); assertEquals(5, items[1]); assertEquals(6, items[2]); }
/** Creates a new rank structure using a bit vector. * * <p>The resulting structure keeps no reference to the original bit vector. * * @param bitVector the input bit vector. */ public SparseRank(final BitVector bitVector) { this(bitVector.length(), bitVector.count(), bitVector.asLongSet().iterator()); }
/** Creates a new select structure using a bit vector. * * <p>The resulting structure keeps no reference to the original bit vector. * * @param bitVector the input bit vector. */ public SparseSelect(final BitVector bitVector) { this(bitVector.length(), bitVector.count(), bitVector.asLongSet().iterator()); }