/** * Get a particular entry of the vector. * * @param i the entry * @return the value of the entry. */ public float get(int i) { // The default value is 0.0 if (i >= entries.size()) { return 0.0f; } return entries.getFloat(i); }
@Override public void write(DataOutput out) throws IOException { out.writeBoolean(isSingleton); if (isSingleton) { out.writeInt(singletonIndex); out.writeFloat(singletonValue); } else { out.writeInt(entries.size()); for (int i = 0; i < entries.size(); ++i) { out.writeFloat(entries.getFloat(i)); } } }
/** * Add the vector specified. This is a vector addition that does an element-by-element addition. * * @param other the vector to add. */ public void add(FloatDenseVector other) { if (isSingleton) { throw new RuntimeException("Cannot add to singleton vector"); } if (other.isSingleton) { ensureCapacity(other.singletonIndex + 1); entries.set( other.singletonIndex, entries.getFloat(other.singletonIndex) + other.singletonValue); } else { ensureCapacity(other.entries.size()); for (int i = 0; i < other.entries.size(); ++i) { entries.set(i, entries.getFloat(i) + other.entries.getFloat(i)); } } }
@Override public void readFields(DataInput in) throws IOException { isSingleton = in.readBoolean(); if (isSingleton) { singletonIndex = in.readInt(); singletonValue = in.readFloat(); } else { int size = in.readInt(); for (int i = 0; i < size; ++i) { entries.add(in.readFloat()); } } }
/** * Resize the array to be at least the size specified. * * @param size the size of the array */ private void ensureCapacity(int size) { if (entries.size() < size) { entries.size(size); } }
/** * Set the given value to the entry with the index specified. * * @param i the entry * @param value the value to set to the entry */ public void set(int i, float value) { entries.set(i, value); }