@Override public SparseIntArray clone() { return new SparseIntArray(n, defaultValue, this); }
@Override public Vector<Integer> make(int n, double defaultValue) { return new SparseIntArray(n, (int) defaultValue); }
public static SparseIntArray histogram(int[] vector, int W) { final SparseIntArray hist = new SparseIntArray(W); for (int i : vector) { hist.inc(i); } return hist; }
@Override public Vector<Integer> make(double[] data) { final SparseIntArray arr = new SparseIntArray(data.length); for (int i = 0; i < data.length; i++) { if (data[i] != 0.0) { arr.put(i, data[i]); } } return arr; }
/** * Convert a dense array to a sparse array */ public static SparseIntArray fromArray(int[] arr) { SparseIntArray sa = new SparseIntArray(arr.length); for (int i = 0; i < arr.length; i++) { if (arr[i] != 0) { sa.put(i, arr[i]); } } return sa; }
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 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; }
/** * Create from a string of the form "idx=val,...,idx=val" * * @param string The string * @param n The length of the array * @param defaultValue The default value * @param sep The seperator * @return The array object * @throws SparseIntArrayFormatException If the string is not formatted as a * sparse array */ public static SparseIntArray fromString(String string, int length, int defaultValue, String sep) throws VectorFormatException { String[] entries = string.split(sep); final SparseIntArray SparseIntArray = new SparseIntArray(length); for (String entry : entries) { if (entry.matches("\\s*")) { continue; } String[] values = entry.split("="); if (values.length != 2) { throw new VectorFormatException("Bad sparse array value " + entry); } SparseIntArray.put(new Integer(values[0]), new Integer(values[1])); } return SparseIntArray; }
public static Vector<Integer> filter(final IntSet salients, final Vector<Integer> source) { final SparseIntArray filtered = new SparseIntArray(source.length()); for (int i : salients) { filtered.add(i, source.value(i)); } return filtered; }
/** * Convert a dense double array to a sparse matrix * * @param arrs The arrays, each element must be non-null and have same * length * @return The sparse matrix */ public static SparseMatrix<Integer> fromArray(int[][] arrs) { if (arrs.length == 0) { return new SparseMatrix<Integer>(0, 0, AS_INTS); } final SparseMatrix<Integer> mat = new SparseMatrix<Integer>(arrs.length, arrs[0].length, AS_INTS); for (int i = 0; i < arrs.length; i++) { assert (arrs[i].length == arrs[0].length); for (int j = 0; j < arrs[i].length; j++) { if (arrs[i][j] != 0.0) { if (mat.arr[i] == null) { mat.arr[i] = new SparseIntArray(arrs[i].length); } mat.arr[i].put(j, arrs[i][j]); } } } return mat; }
@Override public Vector<Integer> subvector(int offset, int length) { final SparseIntArray sv = new SparseIntArray(n); final ObjectIterator<Entry> iter = int2IntEntrySet().fastIterator(); while(iter.hasNext()) { final Entry e = iter.next(); if(e.getIntKey() >= offset && e.getIntKey() < offset + length) { sv.put(e.getIntKey(), e.getIntValue()); } } return sv; }