logger.debug("Packing preference snapshot"); PackedRatingDataBuilder bld = new PackedRatingDataBuilder(); int idx = bld.add(uid, iid, r.getDoubleValue()); imap.put(iid, idx); logger.debug("Packed {} ratings", bld.size()); bld.shuffle(random); PackedRatingData data = bld.build();
/** * Build the packed rating data. This first moves records from the end to fill * any released but not reused slots. * * @return The packed rating data structure. */ @Override public PackedRatingData build() { repack(); trim(); return internalBuild(); } }
/** * Shuffle the data. This uses a Fischer-Yates shuffle to uniformly permute * (subject to limitations of the PRNG) the data. The arrays are repacked * to eliminate free slots prior to shuffling. * * @param rng The random number generator to use. */ void shuffle(Random rng) { repack(); // do a reverse Fisher-Yates shuffle on the arrays final int np = nprefs; for (int i = 0; i < np - 1; i++) { // swap w/ j s.t. i <= j < end int j = i + rng.nextInt(np - i); assert j >= i; assert j < np; swap(i, j); } }
@Test public void testAddPreference() { int idx = bld.add(10, 39, 3.5); assertThat(idx, equalTo(0)); assertThat(bld.size(), equalTo(1)); PackedRatingData data = bld.build(); assertThat(data.size(), equalTo(1)); RatingMatrixEntry p2 = data.getEntry(0); assertThat(p2.getIndex(), equalTo(0)); assertThat(p2.getUserIndex(), equalTo(0)); assertThat(p2.getItemIndex(), equalTo(0)); assertThat(p2.getUserId(), equalTo(10L)); assertThat(p2.getItemId(), equalTo(39L)); assertThat(p2.getValue(), equalTo(3.5)); }
PackedRatingData tmpPack = internalBuild(); set(i, pref.getUserId(), pref.getItemId(), pref.getValue());
@Before public void createBuilder() { bld = new PackedRatingDataBuilder(); }
/** * Set the preference data at a particular index. * * @param idx The index. * @param user The user ID to set. * @param item The item ID to set. * @param pref The preference value to set. */ public void set(int idx, long user, long item, double pref) { Preconditions.checkElementIndex(idx, nprefs); final int ci = chunk(idx); final int ei = element(idx); set(ci, ei, user, item, pref); }
/** * Shuffle the data. This uses a Fischer-Yates shuffle to uniformly permute * (subject to limitations of the PRNG) the data. The arrays are repacked * to eliminate free slots prior to shuffling. * * @param rng The random number generator to use. */ void shuffle(Random rng) { repack(); // do a reverse Fisher-Yates shuffle on the arrays final int np = nprefs; for (int i = 0; i < np - 1; i++) { // swap w/ j s.t. i <= j < end int j = i + rng.nextInt(np - i); assert j >= i; assert j < np; swap(i, j); } }
PackedRatingData tmpPack = internalBuild(); set(i, pref.getUserId(), pref.getItemId(), pref.getValue());
PackedRatingDataBuilder() { itemIndex = new HashKeyIndex(); userIndex = new HashKeyIndex(); freeList = new IntHeapPriorityQueue(); allocate(INITIAL_CHUNK_COUNT); }
/** * Set the preference data at a particular index. * * @param idx The index. * @param user The user ID to set. * @param item The item ID to set. * @param pref The preference value to set. */ public void set(int idx, long user, long item, double pref) { Preconditions.checkElementIndex(idx, nprefs); final int ci = chunk(idx); final int ei = element(idx); set(ci, ei, user, item, pref); }
logger.debug("Packing preference snapshot"); PackedRatingDataBuilder bld = new PackedRatingDataBuilder(); int idx = bld.add(uid, iid, r.getDoubleValue()); imap.put(iid, idx); logger.debug("Packed {} ratings", bld.size()); bld.shuffle(random); PackedRatingData data = bld.build();
/** * Build the packed rating data. This first moves records from the end to fill * any released but not reused slots. * * @return The packed rating data structure. */ @Override public PackedRatingData build() { repack(); trim(); return internalBuild(); } }
PackedRatingDataBuilder() { itemIndex = new HashKeyIndex(); userIndex = new HashKeyIndex(); freeList = new IntHeapPriorityQueue(); allocate(INITIAL_CHUNK_COUNT); }