public static SparseIntArray histogram(int[] vector, int W) { final SparseIntArray hist = new SparseIntArray(W); for (int i : vector) { hist.inc(i); } return hist; }
public static SparseIntArray fromBinary(InputStream stream, int W) throws IOException { final DataInputStream dis = new DataInputStream(stream); final SparseIntArray arr = new SparseIntArray(W); while (dis.available() > 0) { try { arr.inc(dis.readInt()); } catch (EOFException x) { break; } } return arr; }
public static SparseIntArray fromFrequencyString(String string, int n) throws VectorFormatException { final String[] entries = string.split("\\s+"); final SparseIntArray array = new SparseIntArray(n); for (String entry : entries) { try { array.inc(Integer.parseInt(entry)); } catch (NumberFormatException x) { throw new VectorFormatException(x); } } return array; }