예제 #1
0
 static public int[] difference(int a[], int b[]) {
   int[] r = new int[a.length];
   int cnt = 0;
   for (int i=0; i<a.length; i++) {
     if (!contains(b, a[i])) r[cnt++] = a[i];
   }
   return Arrays.copyOf(r, cnt);
 }
예제 #2
0
 public static byte [] unzipBytes(byte [] bs, Compression cmp) {
   InputStream is = null;
   int off = 0;
   try {
     switch(cmp) {
     case NONE: // No compression
       return bs;
     case ZIP: {
       ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bs));
       ZipEntry ze = zis.getNextEntry(); // Get the *FIRST* entry
       // There is at least one entry in zip file and it is not a directory.
       if( ze != null && !ze.isDirectory() ) {
         is = zis;
         break;
       }
       zis.close();
       return bs; // Don't crash, ignore file if cannot unzip
     }
     case GZIP:
       is = new GZIPInputStream(new ByteArrayInputStream(bs));
       break;
     default:
       assert false:"cmp = " + cmp;
     }
     // If reading from a compressed stream, estimate we can read 2x uncompressed
     assert( is != null ):"is is NULL, cmp = " + cmp;
     bs = new byte[bs.length * 2];
     // Now read from the (possibly compressed) stream
     while( off < bs.length ) {
       int len = is.read(bs, off, bs.length - off);
       if( len < 0 )
         break;
       off += len;
       if( off == bs.length ) { // Dataset is uncompressing alot! Need more space...
         if( bs.length >= ValueArray.CHUNK_SZ )
           break; // Already got enough
         bs = Arrays.copyOf(bs, bs.length * 2);
       }
     }
   } catch( IOException ioe ) { // Stop at any io error
     Log.err(ioe);
   } finally {
     Utils.close(is);
   }
   return bs;
 }
예제 #3
0
  /**
   * Concat arrays in one.
   *
   * @param arrays Arrays.
   * @return Summary array.
   */
  public static int[] concat(int[]... arrays) {
    assert arrays != null;
    assert arrays.length > 1;

    int len = 0;

    for (int[] a : arrays) len += a.length;

    int[] r = Arrays.copyOf(arrays[0], len);

    for (int i = 1, shift = 0; i < arrays.length; i++) {
      shift += arrays[i - 1].length;
      System.arraycopy(arrays[i], 0, r, shift, arrays[i].length);
    }

    return r;
  }
예제 #4
0
 public static int[] remove(int[] a, int i) {
   int[] tmp = Arrays.copyOf(a,a.length-1);
   System.arraycopy(a,i+1,tmp,i,tmp.length-i);
   return tmp;
 }
예제 #5
0
 public static <T> T[] append(T[] a, T... b) {
   if( a==null ) return b;
   T[] tmp = Arrays.copyOf(a,a.length+b.length);
   System.arraycopy(b,0,tmp,a.length,b.length);
   return tmp;
 }
예제 #6
0
 public static long[][][] append(long[][][] a, long[][] e) {
   a = Arrays.copyOf(a,a.length+1);
   a[a.length-1] = e;
   return a;
 }
예제 #7
0
 public static double[] append(double[] a, double e) {
   a = Arrays.copyOf(a,a.length+1);
   a[a.length-1] = e;
   return a;
 }