package water.fvec;

import java.util.Arrays;
import water.*;

/**
 * The scale/bias function, where data is in SIGNED bytes before scaling.
 */
public class C1SChunk extends Chunk {
  static final int OFF=8+8;
  public double _scale;
  long _bias;
  C1SChunk( byte[] bs, long bias, double scale ) { _mem=bs; _start = -1; _len = _mem.length-OFF;
    _bias = bias; _scale = scale;
    UDP.set8d(_mem,0,scale);
    UDP.set8 (_mem,8,bias );
  }
  @Override protected final long at8_impl( int i ) {
    long res = 0xFF&_mem[i+OFF];
    if( res == C1Chunk._NA ) throw new IllegalArgumentException("at8 but value is missing");
    return (long)((res+_bias)*_scale);
  }
  @Override protected final double atd_impl( int i ) {
    long res = 0xFF&_mem[i+OFF];
    return (res == C1Chunk._NA)?Double.NaN:(res+_bias)*_scale;
  }
  @Override protected final boolean isNA_impl( int i ) { return (0xFF&_mem[i+OFF]) == C1Chunk._NA; }
  @Override boolean set_impl(int i, long l) {
    long res = (long)(l/_scale)-_bias; // Compressed value
    double d = (res+_bias)*_scale;     // Reverse it
    if( (long)d != l ) return false;   // Does not reverse cleanly?
    if( !(0 <= res && res < 255) ) return false; // Out-o-range for a byte array
    _mem[i+OFF] = (byte)res;
    return true;
  }
  @Override boolean set_impl(int i, double d) { return false; }
  @Override boolean set_impl(int i, float f ) { return false; }
  @Override boolean setNA_impl(int idx) { _mem[idx+OFF] = (byte)C1Chunk._NA; return true; }
  @Override boolean hasFloat() { return _scale < 1.0 || _scale > Long.MAX_VALUE; }
  @Override public AutoBuffer write(AutoBuffer bb) { return bb.putA1(_mem,_mem.length); }
  @Override public C1SChunk read(AutoBuffer bb) {
    _mem = bb.bufClose();
    _start = -1;
    _len = _mem.length-OFF;
    _scale= UDP.get8d(_mem,0);
    _bias = UDP.get8 (_mem,8);
    return this;
  }
  @Override NewChunk inflate_impl(NewChunk nc) {
    double dx = Math.log10(_scale);
    assert PrettyPrint.fitsIntoInt(dx);
    nc.set_len(nc.set_sparseLen(0));
    final int len = len();
    for( int i=0; i<len; i++ ) {
      int res = 0xFF&_mem[i+OFF];
      if( res == C1Chunk._NA ) nc.addNA();
      else nc.addNum((long)(res+_bias),(int)dx);
    }
    return nc;
  }
  public int pformat_len0() { return hasFloat() ? pformat_len0(_scale,3) : super.pformat_len0(); }
  public String  pformat0() { return hasFloat() ? "% 8.2e" : super.pformat0(); }
}

package water.fvec;

import java.util.Arrays;
import water.*;

/**
 * The scale/bias function, where data is in SIGNED bytes before scaling.
 */
public class C1SChunk extends Chunk {
  static final int OFF=8+8;
  public double _scale;
  long _bias;
  C1SChunk( byte[] bs, long bias, double scale ) { _mem=bs; _start = -1; _len = _mem.length-OFF;
    _bias = bias; _scale = scale;
    UDP.set8d(_mem,0,scale);
    UDP.set8 (_mem,8,bias );
  }
  @Override protected final long at8_impl( int i ) {
    long res = 0xFF&_mem[i+OFF];
    if( res == C1Chunk._NA ) throw new IllegalArgumentException("at8 but value is missing");
    return (long)((res+_bias)*_scale);
  }
  @Override protected final double atd_impl( int i ) {
    long res = 0xFF&_mem[i+OFF];
    return (res == C1Chunk._NA)?Double.NaN:(res+_bias)*_scale;
  }
  @Override protected final boolean isNA_impl( int i ) { return (0xFF&_mem[i+OFF]) == C1Chunk._NA; }
  @Override boolean set_impl(int i, long l) {
    long res = (long)(l/_scale)-_bias; // Compressed value
    double d = (res+_bias)*_scale;     // Reverse it
    if( (long)d != l ) return false;   // Does not reverse cleanly?
    if( !(0 <= res && res < 255) ) return false; // Out-o-range for a byte array
    _mem[i+OFF] = (byte)res;
    return true;
  }
  @Override boolean set_impl(int i, double d) { return false; }
  @Override boolean set_impl(int i, float f ) { return false; }
  @Override boolean setNA_impl(int idx) { _mem[idx+OFF] = (byte)C1Chunk._NA; return true; }
  @Override boolean hasFloat() { return _scale < 1.0 || _scale > Long.MAX_VALUE; }
  @Override public AutoBuffer write(AutoBuffer bb) { return bb.putA1(_mem,_mem.length); }
  @Override public C1SChunk read(AutoBuffer bb) {
    _mem = bb.bufClose();
    _start = -1;
    _len = _mem.length-OFF;
    _scale= UDP.get8d(_mem,0);
    _bias = UDP.get8 (_mem,8);
    return this;
  }
  @Override NewChunk inflate_impl(NewChunk nc) {
    double dx = Math.log10(_scale);
    assert PrettyPrint.fitsIntoInt(dx);
    nc.set_len(nc.set_sparseLen(0));
    final int len = len();
    for( int i=0; i<len; i++ ) {
      int res = 0xFF&_mem[i+OFF];
      if( res == C1Chunk._NA ) nc.addNA();
      else nc.addNum((res+_bias),(int)dx);
    }
    return nc;
  }
  public int pformat_len0() { return hasFloat() ? pformat_len0(_scale,3) : super.pformat_len0(); }
  public String  pformat0() { return hasFloat() ? "% 8.2e" : super.pformat0(); }
}

remove redundant cast