/** * Construct a new vector from a key set and value array. * * @param ks The key set. Used as-is, and will be modified. Pass a clone, usually. * @param vs The value array. */ @SuppressWarnings("PMD.ArrayIsStoredDirectly") SparseVector(LongKeyDomain ks, double[] vs) { assert vs.length >= ks.domainSize(); keys = ks; keys.acquire(); values = vs; }
/** * Construct a new vector from the contents of a map. The key domain is the key set of the map. * Therefore, no new keys can be added to this vector. * * @param keyValueMap A map providing the values for the vector. */ SparseVector(Long2DoubleMap keyValueMap) { keys = LongKeyDomain.fromCollection(keyValueMap.keySet(), true); final int len = keys.domainSize(); values = new double[len]; for (int i = 0; i < len; i++) { values[i] = keyValueMap.get(keys.getKey(i)); } }
/** * Fast iterator over entries (it can reuse entry objects). * * @param state The state of entries to iterate. * @return a fast iterator over all key/value pairs * @see it.unimi.dsi.fastutil.longs.Long2DoubleMap.FastEntrySet#fastIterator() * Long2DoubleMap.FastEntrySet.fastIterator() * @since 0.11 */ public Iterator<VectorEntry> fastIterator(VectorEntry.State state) { IntIterator iter; switch (state) { case SET: iter = keys.activeIndexIterator(isMutable()); break; case UNSET: { iter = keys.clone().invert().activeIndexIterator(false); break; } case EITHER: { iter = IntIterators.fromTo(0, keys.domainSize()); break; } default: // should be impossible throw new IllegalArgumentException("invalid entry state"); } return new FastIterImpl(iter, state); }
/** * Construct a new sparse vector with a particular domain. Allocates the value storage. * * @param ks The key set. Used as-is, and will be modified. Pass a clone, usually. */ SparseVector(LongKeyDomain ks) { this(ks, new double[ks.domainSize()]); ks.setAllActive(false); }