} finally {
users.close();
}
Long2ObjectMap<LongList> itemUserLists = new Long2ObjectOpenHashMap<LongList>();
LongKeyDomain domain = LongKeyDomain.fromCollection(vectors.keySet()).compactCopy(true);
assert domain.size() == domain.domainSize();
ImmutableList.Builder<ImmutableSparseVector> vecs = ImmutableList.builder();
ImmutableList.Builder<ImmutableSparseVector> nvecs = ImmutableList.builder();
for (LongIterator uiter = domain.activeSetView().iterator(); uiter.hasNext();) {
final long user = uiter.nextLong();
MutableSparseVector vec = vectors.get(user);
// save user's original vector
ImmutableSparseVector userVector = vec.immutable();
vecs.add(userVector);
// normalize user vector
normalizer.normalize(user, userVector, vec);
// and save normalized vector
nvecs.add(vec.immutable());
for (LongIterator iiter = userVector.keySet().iterator(); iiter.hasNext();) {
final long item = iiter.nextLong();
LongList itemUsers = itemUserLists.get(item);
if (itemUsers == null) {
itemUsers = new LongArrayList();
itemUserLists.put(item, itemUsers);
}
itemUsers.add(user);
}
}
Long2ObjectMap<LongSortedSet> itemUserSets = new Long2ObjectOpenHashMap<LongSortedSet>();
for (Long2ObjectMap.Entry<LongList> entry: itemUserLists.long2ObjectEntrySet()) {
itemUserSets.put(entry.getLongKey(), LongUtils.packedSet(entry.getValue()));
}
return new UserSnapshot(domain.unowned(), vecs.build(), nvecs.build(), itemUserSets);
}