/** * 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)); } }
/** * Check whether an entry is set. * * @param entry The entry. * @return {@code true} if the entry is set in this vector. * @throws IllegalArgumentException if the entry is not from this vector or another vector sharing * the same key domain. Only vectors and their side channels share key domains for the * purposes of this check. */ public boolean isSet(VectorEntry entry) { final SparseVector evec = entry.getVector(); final int eind = entry.getIndex(); if (evec == null) { throw new IllegalArgumentException("entry is not associated with a vector"); } else if (!keys.isCompatibleWith(evec.keys)) { throw new IllegalArgumentException("entry does not have safe key domain"); } assert entry.getKey() == keys.getKey(eind); return keys.indexIsActive(eind); }
/** * Get the value for the entry's key. * * @param entry A {@code VectorEntry} with the key to look up * @return the key's value * @throws IllegalArgumentException if the entry is unset, or if it is not from this vector or * another vector sharing the same key domain. Only vectors and their side channels share key * domains for the purposes of this check. */ public double get(VectorEntry entry) { final SparseVector evec = entry.getVector(); final int eind = entry.getIndex(); if (evec == null) { throw new IllegalArgumentException("entry is not associated with a vector"); } else if (!evec.keys.isCompatibleWith(keys)) { throw new IllegalArgumentException("entry does not have safe key domain"); } assert entry.getKey() == keys.getKey(eind); if (keys.indexIsActive(eind)) { return values[eind]; } else { throw new IllegalArgumentException("Key " + entry.getKey() + " is not set"); } }