/** * Get a view of the index as a set. * @return The set of keys. */ public LongSortedArraySet keySet() { return new LongSortedArraySet(this); }
@Nullable @Override public Entity lookup(long id) { if (idSet.contains(id)) { return Entities.create(entityType, id); } else { return null; } }
@Override public Iterator<Entity> iterator() { return new EntityIterator(entityType, idSet.iterator()); }
@Test public void testEmptySet() { LongSortedArraySet set = emptySet(); assertTrue(set.isEmpty()); assertEquals(0, set.size()); LongBidirectionalIterator iter = set.iterator(); assertNotNull(iter); assertFalse(iter.hasNext()); assertFalse(iter.hasPrevious()); assertFalse(set.contains(42)); assertFalse(set.contains(Long.valueOf(42))); }
@Test public void testEmptySetSubset() { LongSortedArraySet set = emptySet(); LongSortedSet sset = set.headSet(50); assertTrue(sset.isEmpty()); assertEquals(0, sset.size()); assertFalse(set.iterator().hasNext()); assertFalse(set.iterator().hasPrevious()); assertFalse(set.contains(42)); assertFalse(set.contains(Long.valueOf(42))); }
@Override public int size() { return idSet.size(); }
public LongSet randomUnseen(TestUser user, int n) { return allItems.randomSubset(random, n, user.getSeenItems()); } }
/** * Create a key set from a collection of keys. * * @param keys The key collection. * @return The key set. */ public static SortedKeyIndex fromCollection(Collection<Long> keys) { if (keys instanceof LongSortedArraySet) { return ((LongSortedArraySet) keys).getIndex(); } else { return fromIterator(keys.size(), keys.iterator()); } }
BareEntityCollection(EntityType et, LongSortedArraySet ids) { entityType = et; idSet = ids; Hasher hash = Hashing.md5().newHasher(); idSet.forEach((LongConsumer) hash::putLong); contentHash = hash.hash(); }
@Override public long firstLongKey() { return keySet().firstLong(); }
@Override public long lastLongKey() { return keySet().lastLong(); }
/** * 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); }
@Override public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) .append("type", entityType) .append("size", idSet.size()) .build(); }
@Test public void testRandomSubsetPickOnly() { LongSortedArraySet singleton = LongUtils.packedSet(42); LongSortedSet sample = singleton.randomSubset(new Random(), 1); assertThat(sample, contains(42L)); sample = singleton.randomSubset(new Random(), 10); assertThat(sample, contains(42L)); }
private Long2DoubleSortedArrayMap fastSubMap(LongSortedArraySet toKeep) { SortedKeyIndex oks = toKeep.getIndex(); int tn = size(); int on = oks.size();
BareEntityCollection(EntityType et, LongSortedArraySet ids) { entityType = et; idSet = ids; Hasher hash = Hashing.md5().newHasher(); idSet.forEach((LongConsumer) hash::putLong); contentHash = hash.hash(); }
@Override public long firstLongKey() { return keySet().firstLong(); }
@Override public long lastLongKey() { return keySet().lastLong(); }
/** * 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); }
@SuppressWarnings("unchecked") private LongSortedArraySet emptySet() { return new LongSortedArraySet(Collections.EMPTY_LIST); }