/** * Package-private helper that clears the contents of this vector */ final void clear() { if (isCompressed()) { values = null; indices = null; } elements.clear(); }
/** * Package-private helper that clears the contents of this vector */ final void clear() { if (isCompressed()) { values = null; indices = null; } elements.clear(); }
/** * Package-private helper that clears the contents of this vector */ final void clear() { if (isCompressed()) { values = null; indices = null; } elements.clear(); }
@Override public int countNonZeros() { if (isCompressed()) { int nnz = 0; for (double v : values) { if (v != 0.0) { ++nnz; } } return nnz; } else { return elements.size(); } }
@Override public int countNonZeros() { if (isCompressed()) { int nnz = 0; for (double v : values) { if (v != 0.0) { ++nnz; } } return nnz; } else { return elements.size(); } }
@Override public int countNonZeros() { if (isCompressed()) { int nnz = 0; for (double v : values) { if (v != 0.0) { ++nnz; } } return nnz; } else { return elements.size(); } }
/** * The compressed representation should allow for quicker mathematical * operations, but does not permit editing the values in the vector. This * transitions from the uncompressed to the compressed form. If already * compressed, this does nothing. */ final public void compress() { if (isCompressed()) { return; } int nnz = elements.size(); values = new double[nnz]; indices = new int[nnz]; int idx = 0; for (Map.Entry<Integer, MutableDouble> e : elements.entrySet()) { indices[idx] = e.getKey(); values[idx] = e.getValue().value; ++idx; } elements.clear(); }
/** * The compressed representation should allow for quicker mathematical * operations, but does not permit editing the values in the vector. This * transitions from the uncompressed to the compressed form. If already * compressed, this does nothing. */ final public void compress() { if (isCompressed()) { return; } int nnz = elements.size(); values = new double[nnz]; indices = new int[nnz]; int idx = 0; for (Map.Entry<Integer, MutableDouble> e : elements.entrySet()) { indices[idx] = e.getKey(); values[idx] = e.getValue().value; ++idx; } elements.clear(); }
/** * The compressed representation should allow for quicker mathematical * operations, but does not permit editing the values in the vector. This * transitions from the uncompressed to the compressed form. If already * compressed, this does nothing. */ final public void compress() { if (isCompressed()) { return; } int nnz = elements.size(); values = new double[nnz]; indices = new int[nnz]; int idx = 0; for (Map.Entry<Integer, MutableDouble> e : elements.entrySet()) { indices[idx] = e.getKey(); values[idx] = e.getValue().value; ++idx; } elements.clear(); }
/** * The compressed representation should allow for quicker mathematical * operations, but does not permit editing the values in the vector. This * transitions from the compressed to the uncompressed form. If already * uncompressed, this does nothing. */ final public void decompress() { if (!isCompressed()) { return; } elements.clear(); for (int i = 0; i < values.length; ++i) { final double value = values[i]; if (value != 0.0) { elements.put(indices[i], new MutableDouble(value)); } } indices = null; values = null; }
/** * The compressed representation should allow for quicker mathematical * operations, but does not permit editing the values in the vector. This * transitions from the compressed to the uncompressed form. If already * uncompressed, this does nothing. */ final public void decompress() { if (!isCompressed()) { return; } elements.clear(); for (int i = 0; i < values.length; ++i) { final double value = values[i]; if (value != 0.0) { elements.put(indices[i], new MutableDouble(value)); } } indices = null; values = null; }
/** * Copy constructor -- creates a deep copy of the input sparse vector. * * @param v The sparse vector to copy */ public SparseVector( final SparseVector v) { this.dimensionality = v.dimensionality; if (!v.isCompressed()) { elements = new TreeMap<>(v.elements); // Need to copy over all the values. for (Map.Entry<Integer, MutableDouble> entry : this.elements.entrySet()) { entry.setValue(new MutableDouble(entry.getValue())); } values = null; indices = null; } else { elements = new TreeMap<>(); values = Arrays.copyOf(v.values, v.values.length); indices = Arrays.copyOf(v.indices, v.indices.length); } }
/** * The compressed representation should allow for quicker mathematical * operations, but does not permit editing the values in the vector. This * transitions from the compressed to the uncompressed form. If already * uncompressed, this does nothing. */ final public void decompress() { if (!isCompressed()) { return; } elements.clear(); for (int i = 0; i < values.length; ++i) { final double value = values[i]; if (value != 0.0) { elements.put(indices[i], new MutableDouble(value)); } } indices = null; values = null; }
/** * Copy constructor -- creates a deep copy of the input sparse vector. * * @param v The sparse vector to copy */ public SparseVector( final SparseVector v) { this.dimensionality = v.dimensionality; if (!v.isCompressed()) { elements = new TreeMap<>(v.elements); // Need to copy over all the values. for (Map.Entry<Integer, MutableDouble> entry : this.elements.entrySet()) { entry.setValue(new MutableDouble(entry.getValue())); } values = null; indices = null; } else { elements = new TreeMap<>(); values = Arrays.copyOf(v.values, v.values.length); indices = Arrays.copyOf(v.indices, v.indices.length); } }
/** * Copy constructor -- creates a deep copy of the input sparse vector. * * @param v The sparse vector to copy */ public SparseVector( final SparseVector v) { this.dimensionality = v.dimensionality; if (!v.isCompressed()) { elements = new TreeMap<>(v.elements); // Need to copy over all the values. for (Map.Entry<Integer, MutableDouble> entry : this.elements.entrySet()) { entry.setValue(new MutableDouble(entry.getValue())); } values = null; indices = null; } else { elements = new TreeMap<>(); values = Arrays.copyOf(v.values, v.values.length); indices = Arrays.copyOf(v.indices, v.indices.length); } }
@Override final public SparseVector clone() { final SparseVector clone = (SparseVector) super.clone(); if (!this.isCompressed()) { clone.elements = new TreeMap<>(this.elements); // Need to copy over all the values. for (Map.Entry<Integer, MutableDouble> entry : clone.elements.entrySet()) { entry.setValue(new MutableDouble(entry.getValue())); } clone.values = null; clone.indices = null; } else { clone.elements = new TreeMap<>(); clone.values = Arrays.copyOf(this.values, this.values.length); clone.indices = Arrays.copyOf(this.indices, this.indices.length); } return clone; }
@Override final public SparseVector clone() { final SparseVector clone = (SparseVector) super.clone(); if (!this.isCompressed()) { clone.elements = new TreeMap<>(this.elements); // Need to copy over all the values. for (Map.Entry<Integer, MutableDouble> entry : clone.elements.entrySet()) { entry.setValue(new MutableDouble(entry.getValue())); } clone.values = null; clone.indices = null; } else { clone.elements = new TreeMap<>(); clone.values = Arrays.copyOf(this.values, this.values.length); clone.indices = Arrays.copyOf(this.indices, this.indices.length); } return clone; }
@Override final public SparseVector clone() { final SparseVector clone = (SparseVector) super.clone(); if (!this.isCompressed()) { clone.elements = new TreeMap<>(this.elements); // Need to copy over all the values. for (Map.Entry<Integer, MutableDouble> entry : clone.elements.entrySet()) { entry.setValue(new MutableDouble(entry.getValue())); } clone.values = null; clone.indices = null; } else { clone.elements = new TreeMap<>(); clone.values = Arrays.copyOf(this.values, this.values.length); clone.indices = Arrays.copyOf(this.indices, this.indices.length); } return clone; }