@Override public DateTimeColumn unique() { LongSet ints = new LongOpenHashSet(data.size()); for (long i : data) { ints.add(i); } DateTimeColumn column = emptyCopy(ints.size()); column.setName(name() + " Unique values"); column.data = LongArrayList.wrap(ints.toLongArray()); return column; }
/** * Create a key set with some keys. All keys are initially active. * @param keys The keys. * @return The key set. */ public static SortedKeyIndex create(long... keys) { // the delegation goes this way to minimize the number of array copies return fromCollection(LongArrayList.wrap(keys)); }
@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); }
/** * Check that we score items but do not provide scores for items * the user has previously rated. User 5 has rated only item 8 * previously. */ @Test public void testItemScorerNoRating() { long[] items = {7, 8}; ItemItemScorer scorer = session.get(ItemItemScorer.class); assertThat(scorer, notNullValue()); Map<Long, Double> scores = scorer.score(5, LongArrayList.wrap(items)); assertThat(scores, notNullValue()); assertThat(scores.size(), equalTo(1)); assertThat(scores.get(7L), not(notANumber())); assertThat(scores.containsKey(8L), equalTo(false)); }
/** * Check that we score items but do not provide scores for items * the user has previously rated. User 5 has rated only item 8 * previously. */ @Test public void testItemScorerChannels() { long[] items = {7, 8}; ItemItemScorer scorer = session.get(ItemItemScorer.class); assertThat(scorer, notNullValue()); Map<Long, Double> scores = scorer.score(5, LongArrayList.wrap(items)); assertThat(scores, notNullValue()); assertThat(scores.size(), equalTo(1)); assertThat(scores.containsKey(8L), equalTo(false)); long[] items2 = {7, 8, 9}; scorer = session.get(ItemItemScorer.class); assertThat(scorer, notNullValue()); ResultMap details = scorer.scoreWithDetails(2, LongArrayList.wrap(items2)); Result r = details.get(9); assertThat(r, notNullValue()); ItemItemResult score = r.as(ItemItemResult.class); assertThat(score, notNullValue()); assertThat(score.getNeighborhoodSize(), equalTo(3)); }
/** * Check that we score items but do not provide scores for items * the user has previously rated. */ @Test public void testGlobalItemScorerNoRating() { long[] queryItems = {1, 10}; long[] items = {5, 10}; ItemItemItemBasedItemScorer scorer = session.get(ItemItemItemBasedItemScorer.class); assertThat(scorer, notNullValue()); ResultMap scores = scorer.scoreRelatedItemsWithDetails(LongArrayList.wrap(queryItems), LongArrayList.wrap(items)); assertThat(scores, notNullValue()); assertThat(scores.size(), equalTo(2)); Result r5 = scores.get(5); assertThat(r5, notNullValue()); assertThat(r5.getScore(), not(notANumber())); // assertThat(scores.get(10), equalTo(0.0)); }
@Test public void testTwoItemRanks() { Long2IntMap ranks = itemRanks(LongArrayList.wrap(new long[]{1,2})); assertThat(ranks.keySet(), containsInAnyOrder(1L, 2L)); assertThat(ranks, hasEntry(1L, 0)); assertThat(ranks, hasEntry(2L, 1)); }
/** * 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); }
@Test public void testThreeItemRanks() { Long2IntMap ranks = itemRanks(LongArrayList.wrap(new long[]{1,2,3})); assertThat(ranks.keySet(), containsInAnyOrder(1L, 2L, 3L)); assertThat(ranks, hasEntry(1L, 0)); assertThat(ranks, hasEntry(2L, 1)); assertThat(ranks, hasEntry(3L, 2)); }
/** * Create a key set with some keys. All keys are initially active. * @param keys The keys. * @return The key set. */ public static SortedKeyIndex create(long... keys) { // the delegation goes this way to minimize the number of array copies return fromCollection(LongArrayList.wrap(keys)); }
/** * Create a key set with some keys. All keys are initially active. * @param keys The keys. * @return The key set. */ public static LongKeyDomain create(long... keys) { // the delegation goes this way to minimize the number of array copies return fromCollection(LongArrayList.wrap(keys)); }
@Override public String toString() { final StringBuffer s = new StringBuffer(); s.append("["); for (int i = 0; i < n; i++) { if (i != 0) s.append(", "); s.append(LongArrayList.wrap(getArray(i)).toString()); } s.append("]"); return s.toString(); } /**
/** Utility method to load a compressed offset file into a list. * * @param in the input bit stream providing the offsets (see {@link BitStreamIndexWriter}). * @param T the number of terms indexed. * @return a list of longs backed by an array; the list has * an additional final element of index <code>T</code> that gives the number * of bytes of the index file. */ public static LongList readOffsets( final InputBitStream in, final int T ) throws IOException { final long[] offset = new long[ T + 1 ]; LOGGER.debug( "Loading offsets..." ); offset[ 0 ] = in.readLongGamma(); for( int i = 0; i < T; i++ ) offset[ i + 1 ] = in.readLongGamma() + offset[ i ]; LOGGER.debug( "Completed." ); return LongArrayList.wrap( offset ); }
@Override public boolean retainAll(Collection<?> c) { if (c instanceof LongCollection) { return retainAll((LongCollection) c); } else { long[] longs = LongIterators.unwrap(LongIterators.asLongIterator(Iterators.filter(c.iterator(), Long.class))); Arrays.sort(longs); return retainAll(LongArrayList.wrap(longs).iterator()); } } }
@Override public boolean removeAll(Collection<?> c) { if (c instanceof LongCollection) { return removeAll((LongCollection) c); } else { long[] longs = LongIterators.unwrap(LongIterators.asLongIterator(Iterators.filter(c.iterator(), Long.class))); Arrays.sort(longs); return removeAll(LongArrayList.wrap(longs).iterator()); } }
@Override public boolean retainAll(LongCollection c) { if (c instanceof LongSortedSet) { return retainAll(c.iterator()); } else { long[] longs = c.toLongArray(); Arrays.sort(longs); return retainAll(LongArrayList.wrap(longs).iterator()); } }
@Override public boolean removeAll(LongCollection c) { if (c instanceof LongSortedSet) { return removeAll(c.iterator()); } else { long[] longs = c.toLongArray(); Arrays.sort(longs); return removeAll(LongArrayList.wrap(longs).iterator()); } }
@Override public KmerNode next() { KmerNode node = underlying.next(); assert(matchesExpected(node.width() * node.weight(), LongArrayList.wrap(new long[] { node.firstKmer() }), node.firstStart(), node.firstEnd())); return node; } }
@Override public DateTimeColumn unique() { LongSet ints = new LongOpenHashSet(data.size()); for (long i : data) { ints.add(i); } return DateTimeColumn.create(name() + " Unique values", LongArrayList.wrap(ints.toLongArray())); }
@Override public DateTimeColumn unique() { LongSet ints = new LongOpenHashSet(data.size()); for (long i : data) { ints.add(i); } DateTimeColumn column = emptyCopy(ints.size()); column.setName(name() + " Unique values"); column.data = LongArrayList.wrap(ints.toLongArray()); return column; }