/** * Construct empty FV with given number of bins * @param nbins the number of bins in each dimension */ public SparseIntFV(int nbins) { values = new SparseBinSearchIntArray(nbins); }
/** * Construct from an existing array of values * @param values the values */ public SparseBinSearchIntArray(int[] values) { this(values.length); for (int i=0; i<values.length; i++) { if (values[i] != 0) set(i, values[i]); } }
@Override public int set(int key, int value) { if (key < 0 || key >= length) throw new IndexOutOfBoundsException(Integer.toString(key)); int spot = Arrays.binarySearch(keys, 0, used, key); if (spot >= 0) return values[spot] = value; else return update(-1 - spot, key, value); }
@Override public Iterable<DualEntry> unionEntries(final SparseIntArray otherArray) { if (otherArray instanceof SparseBinSearchIntArray) { return unionEntries((SparseBinSearchIntArray) otherArray); } return super.unionEntries(otherArray); }
@Override public int increment(int key, int value) { if (key < 0 || key >= length) throw new IndexOutOfBoundsException(Integer.toString(key)); int spot = Arrays.binarySearch(keys, 0, used, key); if (spot >= 0) return values[spot] += value; return update(-1 - spot, key, value); }
/** * Construct from native array * @param values the array of values */ public SparseIntFV(int[] values) { this.values = new SparseBinSearchIntArray(values); }
/** * Construct empty FV with given number of bins * @param nbins the number of bins in each dimension */ public SparseIntFV(int nbins) { values = new SparseBinSearchIntArray(nbins); }
/** * Construct from native array * @param values the array of values */ public SparseIntFV(int[] values) { this.values = new SparseBinSearchIntArray(values); }
@Override public SparseIntArray copy() { SparseBinSearchIntArray copy = new SparseBinSearchIntArray(length); copy.used = used; copy.keys = Arrays.copyOf(keys, keys.length); copy.values = Arrays.copyOf(values, values.length); return copy; }
@Override public void readBinary(DataInput in) throws IOException { values = new SparseBinSearchIntArray(0); values.readBinary(in); }
@Override public void readBinary(DataInput in) throws IOException { values = new SparseBinSearchIntArray(0); values.readBinary(in); }
@Override public void readASCII(Scanner in) throws IOException { values = new SparseBinSearchIntArray(0); values.readASCII(in); }
@Override public void readASCII(Scanner in) throws IOException { values = new SparseBinSearchIntArray(0); values.readASCII(in); }