public static byte[] rowCompMat2CompByteArray(long[] riCompressedMat,Precision precision){
  final byte maxByte=-1;
  List<Byte> preByteArray=new ArrayList<Byte>();
  boolean switchedToShorts=false;
  boolean switchedToInts=false;
  ByteBuffer buff=ByteBuffer.allocate(4);
  buff.putInt((int)riCompressedMat[0]);
  for (  byte b : buff.array()) {
    preByteArray.add(b);
  }
  int numNonZero=0;
  for (int i=1, n=riCompressedMat.length; i < n; i++) {
    if (numNonZero < riCompressedMat[0]) {
      if (riCompressedMat[i] == -1) {
        for (int k=0; k < 4; k++) {
          preByteArray.add(maxByte);
        }
        switchedToShorts=false;
        switchedToInts=false;
        continue;
      }
      if (i > 1) {
        if (riCompressedMat[i - 1] >= riCompressedMat[i]) {
          for (int k=0; k < 4; k++) {
            preByteArray.add(maxByte);
          }
          switchedToShorts=false;
          switchedToInts=false;
        }
      }
      if (!switchedToShorts && !switchedToInts) {
        if (riCompressedMat[i] > Byte.MAX_VALUE * 2) {
          switchedToShorts=true;
          preByteArray.add(maxByte);
          if (riCompressedMat[i] > Short.MAX_VALUE * 2) {
            switchedToInts=true;
            preByteArray.add(maxByte);
            preByteArray.add(maxByte);
          }
        }
 else {
          preByteArray.add((byte)(((int)riCompressedMat[i]) << 24 >>> 24));
          numNonZero++;
        }
      }
      if (switchedToShorts && !switchedToInts) {
        if (riCompressedMat[i] > Short.MAX_VALUE * 2) {
          switchedToInts=true;
          preByteArray.add(maxByte);
          preByteArray.add(maxByte);
        }
 else {
          ByteBuffer b=ByteBuffer.allocate(2);
          b.putShort((short)(((int)riCompressedMat[i]) << 16 >>> 16));
          numNonZero++;
          for (          byte sec : b.array()) {
            preByteArray.add(sec);
          }
        }
      }
      if (switchedToInts) {
        ByteBuffer b=ByteBuffer.allocate(4);
        b.putInt((int)riCompressedMat[i]);
        numNonZero++;
        for (        byte sec : b.array()) {
          preByteArray.add(sec);
        }
      }
    }
 else {
      byte[] bytes=precision.asByteArray(riCompressedMat[i]);
      for (      byte b : bytes) {
        preByteArray.add(b);
      }
    }
  }
  byte[] byteArr=new byte[preByteArray.size()];
  int i=0;
  for (  Byte b : preByteArray) {
    byteArr[i++]=b.byteValue();
  }
  return byteArr;
}

public static byte [ ] rowCompMat2CompByteArray ( long [ ] riCompressedMat , Precision precision ) { final byte maxByte = - 1 ; List < Byte > preByteArray = new ArrayList < Byte > ( ) ; boolean switchedToShorts = false ; boolean switchedToInts = false ; ByteBuffer buff = ByteBuffer . allocate ( 4 ) ; buff . putInt ( ( int ) riCompressedMat [ 0 ] ) ; for ( byte b : buff . array ( ) ) { preByteArray . add ( b ) ; } int numNonZero = 0 ; for ( int i = 1 , n = riCompressedMat . length ; i < n ; i ++ ) { if ( numNonZero < riCompressedMat [ 0 ] ) { if ( riCompressedMat [ i ] == - 1 ) { for ( int k = 0 ; k < 4 ; k ++ ) { preByteArray . add ( maxByte ) ; } switchedToShorts = false ; switchedToInts = false ; continue ; } if ( i > 1 ) { if ( riCompressedMat [ i - 1 ] >= riCompressedMat [ i ] ) { for ( int k = 0 ; k < 4 ; k ++ ) { preByteArray . add ( maxByte ) ; } switchedToShorts = false ; switchedToInts = false ; } } if ( ! switchedToShorts && ! switchedToInts ) { if ( riCompressedMat [ i ] > Byte . MAX_VALUE * 2 ) { switchedToShorts = true ; preByteArray . add ( maxByte ) ; if ( riCompressedMat [ i ] > Short . MAX_VALUE * 2 ) { switchedToInts = true ; preByteArray . add ( maxByte ) ; preByteArray . add ( maxByte ) ; } } else { preByteArray . add ( ( byte ) ( ( ( int ) riCompressedMat [ i ] ) << 24 > > > 24 ) ) ; numNonZero ++ ; } } if ( switchedToShorts && ! switchedToInts ) { if ( riCompressedMat [ i ] > Short . MAX_VALUE * 2 ) { switchedToInts = true ; preByteArray . add ( maxByte ) ; preByteArray . add ( maxByte ) ; } else { ByteBuffer b = ByteBuffer . allocate ( 2 ) ; b . putShort ( ( short ) ( ( ( int ) riCompressedMat [ i ] ) << 16 > > > 16 ) ) ; numNonZero ++ ; for ( byte sec : b . array ( ) ) { preByteArray . add ( sec ) ; } } } if ( switchedToInts ) { ByteBuffer b = ByteBuffer . allocate ( 4 ) ; b . putInt ( ( int ) riCompressedMat [ i ] ) ; numNonZero ++ ; for ( byte sec : b . array ( ) ) { preByteArray . add ( sec ) ; } } } else { byte [ ] bytes = precision . asByteArray ( riCompressedMat [ i ] ) ; for ( byte b : bytes ) { preByteArray . add ( b ) ; } } } byte [ ] byteArr = new byte [ preByteArray . size ( ) ] ; int i = 0 ; for ( Byte b : preByteArray ) { byteArr [ i ++ ] = b . byteValue ( ) ; } return byteArr ; }
converts a complete row compressed represenation of a matrix into a byte array . further compresses the column index values values by using the smallest number of discrete bytes to store each index value . ex : if an index has a value less than 255 it is stored in a single byte , otherwise it is stored as a short , and so on if the index is greater than 65535 .
