Package org.grouplens.lenskit.collections

Examples of org.grouplens.lenskit.collections.LongKeyDomain


        Long2ObjectMap<LongSortedSet> userItemSets = new Long2ObjectOpenHashMap<LongSortedSet>();
        for (Long2ObjectMap.Entry<LongList> entry: userItems.long2ObjectEntrySet()) {
            userItemSets.put(entry.getLongKey(), LongUtils.packedSet(entry.getValue()));
        }

        LongKeyDomain items = LongKeyDomain.fromCollection(itemVectors.keySet(), true);
        SparseVector[] itemData = new SparseVector[items.domainSize()];
        for (int i = 0; i < itemData.length; i++) {
            long itemId = items.getKey(i);
            itemData[i] = itemVectors.get(itemId);
        }

        logger.debug("item data completed");
        return new ItemItemBuildContext(items, itemData, userItemSets);
View Full Code Here


        LongSortedSet itemUniverse = buildContext.getItems();

        final int nitems = itemUniverse.size();

        LongKeyDomain itemDomain = LongKeyDomain.fromCollection(itemUniverse, true);
        assert itemDomain.size() == itemDomain.domainSize();
        assert itemDomain.domainSize() == nitems;
        List<List<ScoredId>> matrix = Lists.newArrayListWithCapacity(itemDomain.domainSize());

        // working space for accumulating each row (reuse between rows)
        MutableSparseVector currentRow = MutableSparseVector.create(itemUniverse);
        Stopwatch timer = Stopwatch.createStarted();

        for (int i = 0; i < nitems; i++) {
            assert matrix.size() == i;
            final long rowItem = itemDomain.getKey(i);
            final SparseVector vec1 = buildContext.itemVector(rowItem);

            // Take advantage of sparsity if we can
            LongIterator neighbors = iterationStrategy.neighborIterator(buildContext, rowItem, false);
            currentRow.fill(0);
View Full Code Here

        logger.debug("Building item data");
        Long2ObjectMap<ScoredIdListBuilder> itemRatingData = new Long2ObjectOpenHashMap<ScoredIdListBuilder>(1000);
        Long2ObjectMap<LongSortedSet> userItems = new Long2ObjectOpenHashMap<LongSortedSet>(1000);
        buildItemRatings(itemRatingData, userItems);

        LongKeyDomain items = LongKeyDomain.fromCollection(itemRatingData.keySet(), true);
        final int n = items.domainSize();
        assert n == itemRatingData.size();
        // finalize the item data into vectors
        SparseVector[] itemRatings = new SparseVector[n];

        for (int i = 0; i < n; i++) {
            final long item = items.getKey(i);
            ScoredIdListBuilder ratings = itemRatingData.get(item);
            SparseVector v = ratings.buildVector();
            assert v.size() == ratings.size();
            itemRatings[i] = v;
            // release some memory
View Full Code Here

     *                  new vector.
     * @return the new copy of the vector.
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public MutableSparseVector withDomain(LongSet keyDomain) {
        LongKeyDomain domain = LongKeyDomain.fromCollection(keyDomain, false);
        // pass an unowned domain to avoid the extra copy
        return withDomain(domain.unowned());
    }
View Full Code Here

    }

    @Override
    public MutableSparseVector mutableCopy() {
        checkFrozen();
        LongKeyDomain mks = keys.clone();
        double[] mvs = java.util.Arrays.copyOf(values, keys.domainSize());

        // copy the channel maps
        Map<Symbol, MutableSparseVector> newChanVectors =
                new Reference2ObjectArrayMap<Symbol, MutableSparseVector>();
View Full Code Here

    @Override
    public ImmutableSparseVector immutable() {
        checkFrozen();
        // TODO Don't copy bit set if we are freezing?
        LongKeyDomain newDomain = keys.clone();
        accumulateAllKeys(newDomain);
        return immutable(false, newDomain.compactCopy().unowned());
    }
View Full Code Here

     * @param keyDomain The key set to use as the domain.
     * @return An immutable vector built from this vector's data.
     */
    private ImmutableSparseVector immutable(boolean freeze, LongKeyDomain keyDomain) {
        double[] nvs;
        LongKeyDomain newDomain = keyDomain.clone();
        if (newDomain.isCompatibleWith(keys)) {
            nvs = (freeze && values.length == newDomain.size())
                    ? values
                    : java.util.Arrays.copyOf(values, newDomain.domainSize());
            newDomain.setActive(keys.getActiveMask());
        } else {
            nvs = new double[newDomain.domainSize()];

            int i = 0;
            int j = 0;
            final int end = keys.domainSize();
            while (i < nvs.length && j < end) {
                final long ki = newDomain.getKey(i);
                final long kj = keys.getKey(j);
                if (ki == kj) {
                    nvs[i] = values[j];
                    newDomain.setActive(i, keys.indexIsActive(j));
                    i++;
                    j++;
                } else if (kj < ki) {
                    j++;
                } else {
View Full Code Here

            throw new IllegalArgumentException("key array too short");
        }
        if (!MoreArrays.isSorted(keys, 0, size)) {
            throw new IllegalArgumentException("item array not sorted");
        }
        LongKeyDomain keySet = LongKeyDomain.wrap(keys, size, true);
        return new MutableSparseVector(keySet, values);
    }
View Full Code Here

        } catch (IllegalArgumentException e) { /* expected */}
    }
   
    @Test
    public void testAddTypedSideChannel() {
        LongKeyDomain domain = LongKeyDomain.create(1, 2, 4);
        MutableTypedSideChannel<String> ts = new MutableTypedSideChannel<String>(domain);
        MutableSparseVector sv = new MutableSparseVector(domain);
        ts.put(1, "a");
       
        sv.addChannel(fooStrSym, ts);
View Full Code Here

        LongList ids = new LongArrayList(ratings.size());
        for (Rating r: ratings) {
            ids.add(dimension.getId(r));
        }

        LongKeyDomain keys = LongKeyDomain.fromCollection(ids, false);
        MutableSparseVector msv = MutableSparseVector.create(keys.domain());
        long[] timestamps = null;
        // check for fast-path, where each item has one rating
        if (keys.domainSize() < ratings.size()) {
            timestamps = new long[keys.domainSize()];
        }

        for (Rating r: ratings) {
            long id = dimension.getId(r);
            if (timestamps != null) {
                int idx = keys.getIndex(id);
                if (keys.indexIsActive(idx) && timestamps[idx] >= r.getTimestamp()) {
                    continue// we have seen a newer event - skip this.
                } else {
                    timestamps[idx] = r.getTimestamp();
                    keys.setActive(idx, true);
                }
            }

            Preference p = r.getPreference();
            if (p != null) {
View Full Code Here

TOP

Related Classes of org.grouplens.lenskit.collections.LongKeyDomain

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.