Exemplo n.º 1
0
 public static SparseIntArray histogram(int[] vector, int W) {
   final SparseIntArray hist = new SparseIntArray(W);
   for (int i : vector) {
     hist.inc(i);
   }
   return hist;
 }
Exemplo n.º 2
0
 /** 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;
 }
Exemplo n.º 3
0
 @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;
 }
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
 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;
 }
Exemplo n.º 6
0
 /**
  * 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;
 }