/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
/** * Initialize the training estimator. * @param snap The getEntry snapshot. * @param baseline The baseline predictor. * @param dom The getEntry domain (for clamping). */ TrainingEstimator(RatingMatrix snap, BiasModel baseline, PreferenceDomain dom) { ratings = snap.getRatings(); domain = dom; estimates = new double[ratings.size()]; final LongCollection userIds = snap.getUserIds(); LongIterator userIter = userIds.iterator(); double global = baseline.getIntercept(); for (RatingMatrixEntry r: snap.getRatings()) { double userBias = baseline.getUserBias(r.getUserId()); double itemBias = baseline.getItemBias(r.getItemId()); estimates[r.getIndex()] = global + userBias + itemBias; } }
@Override public LongIterator iterator() { return collection.iterator(); } @Override
@Override public Iterable<Neighbor> getCandidateNeighbors(final long user, LongSet items) { Long2DoubleMap urs = rvDAO.userRatingVector(user); if (urs.isEmpty()) { return Collections.emptyList(); } final Long2DoubleMap normed = similarityNormalizer.makeTransformation(user, urs) .apply(urs); assert normed != null; LongCollection qset = items; if (normed.size() < qset.size()) { qset = normed.keySet(); } final LongSet candidates = new LongOpenHashSet(); for (LongIterator iter = qset.iterator(); iter.hasNext();) { final long item = iter.nextLong(); LongSet users = snapshot.getItemUsers(item); if (users != null) { candidates.addAll(users); } } candidates.remove(user); logger.debug("Found {} candidate neighbors for user {}", candidates.size(), user); return new Iterable<Neighbor>() { @Override public Iterator<Neighbor> iterator() { return new NeighborIterator(user, normed, candidates); } }; }
/** * Get the IDs of the candidate neighbors for a user. * @param user The user. * @param userItems The user's rated items. * @param targetItems The set of target items. * @return The set of IDs of candidate neighbors. */ private LongSet findCandidateNeighbors(long user, LongSet userItems, LongCollection targetItems) { LongSet users = new LongOpenHashSet(100); LongIterator items; if (userItems.size() < targetItems.size()) { items = userItems.iterator(); } else { items = targetItems.iterator(); } while (items.hasNext()) { LongSet iusers = dao.query(CommonTypes.RATING) .withAttribute(CommonAttributes.ITEM_ID, items.nextLong()) .valueSet(CommonAttributes.USER_ID); if (iusers != null) { users.addAll(iusers); } } users.remove(user); return users; }
@Override public LongIterator iterator() { return LongIterators.unmodifiable(collection.iterator()); } @Override
/** * Creates a new array list and fills it with a given type-specific collection. * * @param c * a type-specific collection that will be used to fill the array * list. */ public LongArrayList(final LongCollection c) { this(c.size()); size = LongIterators.unwrap(c.iterator(), a); } /**
public long id() { return ids.values().iterator().nextLong(); }
@Override public boolean addAll(final LongCollection c) { boolean retVal = false; for (final LongIterator i = c.iterator(); i.hasNext();) if (add(i.nextLong())) retVal = true; return retVal; } @Override
@Override public boolean removeAll(final LongCollection c) { boolean retVal = false; for (final LongIterator i = c.iterator(); i.hasNext();) if (rem(i.nextLong())) retVal = true; return retVal; } @Override
public boolean containsAll(LongCollection c) { final LongIterator i = c.iterator(); int n = c.size(); while (n-- != 0) if (!contains(i.nextLong())) return false; return true; }
@Override public boolean containsAll(final LongCollection c) { for (final LongIterator i = c.iterator(); i.hasNext();) if (!contains(i.nextLong())) return false; return true; } @Override
public boolean containsAll(LongCollection c) { final LongIterator i = c.iterator(); int n = c.size(); while (n-- != 0) if (!contains(i.nextLong())) return false; return true; }
public boolean containsAll(LongCollection c) { final LongIterator i = c.iterator(); int n = c.size(); while (n-- != 0) if (!contains(i.nextLong())) return false; return true; }
@Override public boolean addAll(int index, final LongCollection c) { ensureIndex(index); int n = c.size(); if (n == 0) return false; grow(size + n); if (index != size) System.arraycopy(a, index, a, index + n, size - index); final LongIterator i = c.iterator(); size += n; while (n-- != 0) a[index++] = i.nextLong(); assert size <= a.length; return true; } @Override
@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()); } }
/** * Creates a new big-array big list and fills it with a given type-specific * collection. * * @param c * a type-specific collection that will be used to fill the array * list. */ public LongBigArrayBigList(final LongCollection c) { this(c.size()); for (LongIterator i = c.iterator(); i.hasNext();) add(i.nextLong()); } /**
@Override public boolean addAll(int index, final LongCollection c) { ensureIndex(index); final LongIterator i = c.iterator(); final boolean retVal = i.hasNext(); while (i.hasNext()) add(index++, i.nextLong()); return retVal; } /**
/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }