@Deprecated public final class MutableSparseVector extends SparseVector implements Serializable
Mutable version of sparse vector.
This class extends the sparse vector with support for imperative mutation operations on their values.
Once created the domain of potential keys remains immutable. Since the vector is sparse, keys can be added, but only within the domain the vector was constructed with. These vectors separate the concepts of the key set, which is the current set of active keys from the key domain, which is the set of potential keys. Of course, the key domain must always include the key set.
Addition and subtraction are supported, though they are modified from the mathematical operations because they never change the set of keys. Mutation operations also operate in-place to reduce the reallocation and copying required. Therefore, a common pattern is:
MutableSparseVector normalized = vector.mutableCopy(); normalized.subtract(normFactor);
Create mutable sparse vectors using the various create
static methods provided (create(java.util.Collection)
, create(java.util.Collection, double)
, create(long...)
, create(java.util.Map)
).
Modifier and Type | Method and Description |
---|---|
void |
add(double value)
Deprecated.
Add a value to all set keys in this array.
|
double |
add(long key,
double value)
Deprecated.
Add a value to the specified entry.
|
void |
add(SparseVector other)
Deprecated.
Add another rating vector to this one.
|
<K> Long2ObjectMap<K> |
addChannel(TypedSymbol<K> channelSymbol)
Deprecated.
Add a typed channel to this vector.
|
MutableSparseVector |
addChannelVector(Symbol channelSymbol)
Deprecated.
Add a channel to this vector.
|
void |
addScaled(SparseVector v,
double scale)
Deprecated.
Add a vector to this vector with a scaling factor.
|
void |
clear()
Deprecated.
Clear all values from the set.
|
MutableSparseVector |
combineWith(SparseVector o)
Deprecated.
Combine this vector with another vector by taking the union of the key domains of two vectors.
|
MutableSparseVector |
copy()
Deprecated.
Copy the rating vector.
|
static MutableSparseVector |
create(Collection<Long> domain)
Deprecated.
Create a new empty mutable sparse vector with the specified key domain.
|
static MutableSparseVector |
create(Collection<Long> domain,
double value)
Deprecated.
Create a new mutable sparse vector with the specified key domain and filled with a value.
|
static MutableSparseVector |
create(long... keys)
Deprecated.
Create a mutable sparse vector with a fixed set of keys.
|
static MutableSparseVector |
create(Map<Long,Double> content)
Deprecated.
Create a new mutable sparse vector with the specified content.
|
void |
fill(double value)
Deprecated.
Set the values for all items in the key domain to
value . |
ImmutableSparseVector |
freeze()
Deprecated.
Construct an immutable sparse vector from this vector’s data (if possible), invalidating this vector in the process.
|
<K> Long2ObjectMap<K> |
getChannel(TypedSymbol<K> channelSymbol)
Deprecated.
Fetch the channel stored under a particular typed symbol.
|
Set<TypedSymbol<?>> |
getChannelSymbols()
Deprecated.
Retrieve all symbols that map to typed side channels for this vector.
|
MutableSparseVector |
getChannelVector(Symbol channelSymbol)
Deprecated.
Get the vector associated with a particular unboxed channel.
|
Set<Symbol> |
getChannelVectorSymbols()
Deprecated.
Retrieve all symbols that map to side channels for this vector.
|
<K> Long2ObjectMap<K> |
getOrAddChannel(TypedSymbol<K> channelSymbol)
Deprecated.
Add a typed channel to the vector, even if there is already a channel with the same symbol.
|
MutableSparseVector |
getOrAddChannelVector(Symbol channelSymbol)
Deprecated.
Add a channel to the vector, even if there is already a channel with the same symbol.
|
boolean |
hasChannel(TypedSymbol<?> channelSymbol)
Deprecated.
Return whether this sparse vector has a channel stored under a particular typed symbol.
|
boolean |
hasChannelVector(Symbol channelSymbol)
Deprecated.
Return whether this sparse vector has a channel vector stored under a particular symbol.
|
ImmutableSparseVector |
immutable()
Deprecated.
Return an immutable snapshot of this sparse vector.
|
LongSortedSet |
keySet()
Deprecated.
Get the set of keys of this vector.
|
void |
multiply(double s)
Deprecated.
Multiply the vector by a scalar.
|
void |
multiply(SparseVector other)
Deprecated.
Multiply each element in the vector by the corresponding element in another vector.
|
MutableSparseVector |
mutableCopy()
Deprecated.
Return a mutable copy of this sparse vector.
|
void |
removeAllChannels()
Deprecated.
Remove all channels stored in this vector.
|
<K> Long2ObjectMap<K> |
removeChannel(TypedSymbol<K> channelSymbol)
Deprecated.
Remove the typed channel stored under a particular symbol.
|
SparseVector |
removeChannelVector(Symbol channelSymbol)
Deprecated.
Remove the channel stored under a particular symbol.
|
double |
set(long key,
double value)
Deprecated.
Set a value in the vector.
|
void |
set(SparseVector other)
Deprecated.
Set the values in this SparseVector to equal the values in other for each key that is present in both vectors.
|
double |
set(VectorEntry entry,
double value)
Deprecated.
Set the value in the vector corresponding to a vector entry.
|
MutableSparseVector |
shrinkDomain()
Deprecated.
Create a new version of this MutableSparseVector that has keyDomain equal to this vector’s key set.
|
void |
subtract(SparseVector other)
Deprecated.
Subtract another rating vector from this one.
|
void |
unset(long key)
Deprecated.
Unset the value for a key.
|
void |
unset(VectorEntry entry)
Deprecated.
Unset the value for a vector entry.
|
void |
unsetLessThan(double thresh)
Deprecated.
Unset all values less than a threshold.
|
MutableSparseVector |
withDomain(LongSet keyDomain)
Deprecated.
Create a new version of this MutableSparseVector that has a different domain from the current version of the vector.
|
static MutableSparseVector |
wrap(long[] keys,
double[] values)
Deprecated.
Wrap key and value arrays in a sparse vector.
|
static MutableSparseVector |
wrap(long[] keys,
double[] values,
int size)
Deprecated.
Wrap key and value arrays in a sparse vector.
|
static MutableSparseVector |
wrap(LongArrayList keyList,
DoubleArrayList valueList)
Deprecated.
Wrap key and value array lists in a mutable sparse vector.
|
static MutableSparseVector |
wrapUnsorted(long[] keys,
double[] values)
Deprecated.
Create a new
MutableSparseVector from unsorted key and value arrays. |
asMap, containsKey, countCommonKeys, dot, empty, equals, fast, fast, fastIntersect, fastIterator, fastIterator, get, get, get, hashCode, isEmpty, isSet, iterator, iterator, keyDomain, keysByValue, keysByValue, mean, norm, size, sum, sumAbs, toString, unsetKeySet, values, view
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static MutableSparseVector create(Collection<Long> domain)
Create a new empty mutable sparse vector with the specified key domain.
domain
- The key domain. This method is more efficient if you pass some form of LongCollection
, particularly a LongSortedSet
.public static MutableSparseVector create(Collection<Long> domain, double value)
Create a new mutable sparse vector with the specified key domain and filled with a value.
domain
- The key domain. This method is more efficient if you pass some form of LongCollection
, particularly a LongSortedSet
.value
- The value to fill the vector with.public static MutableSparseVector create(Map<Long,Double> content)
Create a new mutable sparse vector with the specified content.
content
- The content of the vector. Pass a Long2DoubleMap
for more efficiency. It may not contain any null
values.public LongSortedSet keySet()
SparseVector
Get the set of keys of this vector. It is a subset of the key domain. The keys will be in sorted order.
keySet
in class SparseVector
public MutableSparseVector withDomain(LongSet keyDomain)
Create a new version of this MutableSparseVector that has a different domain from the current version of the vector. All elements in the current vector that are also in the new keyDomain are copied over into the new vector.
Channels from the current vector are copied over to the new vector, all with the changed keyDomain.
keyDomain
- the set of keys to use for the domain of the new vector.public MutableSparseVector shrinkDomain()
Create a new version of this MutableSparseVector that has keyDomain equal to this vector’s key set. All elements in the current vector that are also in the new keyDomain are copied over into the new vector. Channels from the current vector are copied over to the new vector, all with the changed keyDomain.
Note: the domain of this vector is not changed.
public double set(long key, double value)
Set a value in the vector.
key
- The key of the value to set.value
- The value to set.Double.NaN
if the key had no value (or if the original value was Double.NaN
).IllegalArgumentException
- if the key is not in the domain for this sparse vector.public double set(@Nonnull VectorEntry entry, double value)
Set the value in the vector corresponding to a vector entry. This is used in lieu of providing a setValue
method on VectorEntry
, and changes the value in constant time. The value on the entry is also changed to reflect the new value.
Is guaranteed to work on any vector that has an identical set of keys as the vector from which the VectorEntry was created (such as the channels of that vector), but may throw an IllegalArgumentException if the keys are not an identical object even if they are the same value, to permit more efficient implementations.
entry
- The entry to update.value
- The new value.IllegalArgumentException
- if entry
does not come from this vector, or if the index in the entry is corrupt.public void fill(double value)
Set the values for all items in the key domain to value
.
value
- The value to set.public void unset(long key)
Unset the value for a key. The key remains in the key domain, but is removed from the key set.
key
- The key to unset.IllegalArgumentException
- if the key is not in the key domain.public void unset(@Nonnull VectorEntry entry)
Unset the value for a vector entry. The key remains in the domain, but is removed from the key set.
entry
- The entry to unset.public void unsetLessThan(double thresh)
Unset all values less than a threshold.
thresh
- The threshold.public void clear()
Clear all values from the set.
public double add(long key, double value)
Add a value to the specified entry. The key must be in the key set.
key
- The key whose value should be added.value
- The value to increase it by.IllegalArgumentException
- if the key is not in the key set.public void add(double value)
Add a value to all set keys in this array.
value
- The value to add.public void subtract(SparseVector other)
Subtract another rating vector from this one.
After calling this method, every element of this vector has been decreased by the corresponding element in other. Elements with no corresponding element are unchanged.
other
- The vector to subtract.public void add(SparseVector other)
Add another rating vector to this one.
After calling this method, every element of this vector has been increased by the corresponding element in other. Elements with no corresponding element are unchanged.
other
- The vector to add.public void addScaled(SparseVector v, double scale)
Add a vector to this vector with a scaling factor. It multiplies v
by the scaling factor scale
and adds it to this vector. Only keys set in both v
and this
are modified. The scaling is done on-the-fly; v
is unmodified.
v
- The vector to add to this vector.scale
- The scaling factor to be applied to the vector.public void set(SparseVector other)
Set the values in this SparseVector to equal the values in other for each key that is present in both vectors.
After calling this method, every element in this vector that has a key in other has its value set to the corresponding value in other. Elements with no corresponding key are unchanged, and elements in other that are not in this vector are not inserted.
other
- The vector to blit its values into this vectorpublic void multiply(double s)
Multiply the vector by a scalar. This multiples every element in the vector by s.
s
- The scalar to rescale the vector by.public void multiply(SparseVector other)
Multiply each element in the vector by the corresponding element in another vector. Elements not in the other vector are left unchanged.
other
- The vector to pairwise-multiply with this one.public MutableSparseVector copy()
Copy the rating vector.
public MutableSparseVector mutableCopy()
SparseVector
Return a mutable copy of this sparse vector. The key domain of the mutable vector will be the same as this vector’s key domain.
mutableCopy
in class SparseVector
public ImmutableSparseVector immutable()
SparseVector
Return an immutable snapshot of this sparse vector. The new vector’s key domain may be shrunk to remove storage of unused keys; no keys in the key set will be removed.
immutable
in class SparseVector
public ImmutableSparseVector freeze()
Construct an immutable sparse vector from this vector’s data (if possible), invalidating this vector in the process. Any subsequent use of this vector is invalid; all access must be through the returned immutable vector. The frozen vector’s key domain is equal to this vector’s key set.
immutable()
public static MutableSparseVector wrap(long[] keys, double[] values)
Wrap key and value arrays in a sparse vector.
This method allows a new vector to be constructed from pre-created arrays. After wrapping arrays in a sparse vector, client code should not modify them (particularly the keys array). The key domain of the newly created vector will be the same as the keys.
keys
- Array of entry keys. This array must be in sorted order and be duplicate-free.values
- The values for the vector, in key order.IllegalArgumentException
- if there is a problem with the provided arrays (length mismatch, keys not sorted, etc.).public static MutableSparseVector wrap(long[] keys, double[] values, int size)
Wrap key and value arrays in a sparse vector.
This method allows a new vector to be constructed from pre-created arrays. After wrapping arrays in a sparse vector, client code should not modify them (particularly the keys array). The key domain of the newly created vector will be the same as the keys.
keys
- Array of entry keys. This array must be in sorted order and be duplicate-free.values
- The values for the vector.size
- The size of the vector; only the first size entries from each array are actually used.IllegalArgumentException
- if there is a problem with the provided arrays (length mismatch, keys not sorted, etc.).public static MutableSparseVector wrap(LongArrayList keyList, DoubleArrayList valueList)
Wrap key and value array lists in a mutable sparse vector. Don’t modify the original lists once this has been called! There must be at least as many values as keys. The value list will be truncated to the length of the key list.
keyList
- The list of keysvalueList
- The list of valuespublic static MutableSparseVector create(long... keys)
Create a mutable sparse vector with a fixed set of keys. This is mostly useful for tests.
keys
- The key domain.public static MutableSparseVector wrapUnsorted(long[] keys, double[] values)
Create a new MutableSparseVector
from unsorted key and value arrays. The provided arrays will be modified and should not be used by the client after this operation has completed. The key domain of the new MutableSparseVector
will be the same as keys
.
keys
- Array of entry keys. This should be duplicate-free.values
- The values of the vector, in key order.IllegalArgumentException
- if there is a problem with the provided arrays (length mismatch, etc.).public SparseVector removeChannelVector(Symbol channelSymbol)
Remove the channel stored under a particular symbol.
channelSymbol
- the symbol under which the channel was stored in the vector.IllegalArgumentException
- if this vector does not have such a channel at this time.public <K> Long2ObjectMap<K> removeChannel(TypedSymbol<K> channelSymbol)
Remove the typed channel stored under a particular symbol.
channelSymbol
- the symbol under which the channel was stored in the vector.IllegalArgumentException
- if this vector does not have such a channel at this time.public void removeAllChannels()
Remove all channels stored in this vector.
public MutableSparseVector addChannelVector(Symbol channelSymbol)
Add a channel to this vector. The new channel will be empty, and will have the same key domain as this vector.
channelSymbol
- the symbol under which this new channel should be created.IllegalArgumentException
- if there is already a channel with that symbolpublic <K> Long2ObjectMap<K> addChannel(TypedSymbol<K> channelSymbol)
Add a typed channel to this vector. The new channel will be empty, and will have the same key domain as this vector.
channelSymbol
- the symbol under which this new channel should be created.IllegalArgumentException
- if there is already a channel with that symbolpublic MutableSparseVector getOrAddChannelVector(Symbol channelSymbol)
Add a channel to the vector, even if there is already a channel with the same symbol. If there already was such a channel it will be unchanged; otherwise a new empty channel will be created with the same key domain as this vector.
channelSymbol
- the symbol under which this new channel should be created.public <K> Long2ObjectMap<K> getOrAddChannel(TypedSymbol<K> channelSymbol)
Add a typed channel to the vector, even if there is already a channel with the same symbol. The new channel will be empty, and will have the same key domain as this vector.
channelSymbol
- the symbol under which this new channel should be created.public boolean hasChannelVector(Symbol channelSymbol)
SparseVector
Return whether this sparse vector has a channel vector stored under a particular symbol.
hasChannelVector
in class SparseVector
channelSymbol
- the symbol under which the channel was stored in the vector.public boolean hasChannel(TypedSymbol<?> channelSymbol)
SparseVector
Return whether this sparse vector has a channel stored under a particular typed symbol.
hasChannel
in class SparseVector
channelSymbol
- the typed symbol under which the channel was stored in the vector.public MutableSparseVector getChannelVector(Symbol channelSymbol)
SparseVector
Get the vector associated with a particular unboxed channel.
getChannelVector
in class SparseVector
channelSymbol
- the symbol under which the channel was/is stored in the vector.null
if there is no such channel.public <K> Long2ObjectMap<K> getChannel(TypedSymbol<K> channelSymbol)
SparseVector
Fetch the channel stored under a particular typed symbol.
getChannel
in class SparseVector
channelSymbol
- the typed symbol under which the channel was/is stored in the vector.null
if there is no such channel.public Set<Symbol> getChannelVectorSymbols()
SparseVector
Retrieve all symbols that map to side channels for this vector.
getChannelVectorSymbols
in class SparseVector
public Set<TypedSymbol<?>> getChannelSymbols()
SparseVector
Retrieve all symbols that map to typed side channels for this vector.
getChannelSymbols
in class SparseVector
public MutableSparseVector combineWith(SparseVector o)
SparseVector
Combine this vector with another vector by taking the union of the key domains of two vectors. If both vectors have values the same key, the values in o
override those from the current vector.
combineWith
in class SparseVector
o
- The other vector