package water.fvec;


import water.H2O;
import water.util.UnsafeUtils;

import java.util.Iterator;

//NA sparse double chunk
public class CNAXDChunk extends CNAXIChunk {
  
  protected CNAXDChunk(int len, int valsz, byte [] buf){super(len,valsz,buf);}

  // extract fp value from an (byte)offset
  protected final double getFValue(int off){
    if(valsz() == 8) return UnsafeUtils.get8d(_mem, off + ridsz());
    throw H2O.fail();
  }

  @Override protected long at8_impl(int idx) {
    int off = findOffset(idx);
    if(getId(off) != idx) throw new IllegalArgumentException("at8_abs but value is missing");
    double d = getFValue(off);
    return (long)d;
  }
  @Override protected double atd_impl(int idx) {
    int off = findOffset(idx);
    if(getId(off) != idx)return Double.NaN;
    return getFValue(off);
  }

  @Override public NewChunk inflate_impl(NewChunk nc) {
    nc.setSparseNA();
    nc.set_len(_len);
    nc.set_sparseLen(_sparseLen);
    nc.alloc_doubles(_sparseLen);
    nc.alloc_indices(_sparseLen);
    int off = _OFF;
    for( int i = 0; i < _sparseLen; ++i, off += ridsz() + valsz()) {
      nc.indices()[i] = getId(off);
      nc.doubles()[i] = getFValue(off);
    }
    return nc;
  }

  public Iterator<Value> values(){
    return new SparseIterator(new Value(){
      @Override public final long asLong() {return (long) getFValue(_off);}
      @Override public final double asDouble() {return getFValue(_off);}
      @Override public final boolean isNA(){return false;}
    });
  }
  @Override
  public boolean hasFloat() {return true;}


}

@@ -1560,10 +1560,627 @@
 rue;%7D%0A%0A%0A
+  @Override public int asSparseDoubles(double %5B%5D vals, int%5B%5D ids) %7B%0A    if(vals.length %3C _sparseLen) throw new IllegalArgumentException();%0A    int off = _OFF;%0A    final int inc = 8 + _ridsz;%0A    if(_ridsz == 2)%7B%0A      for (int i = 0; i %3C _sparseLen; ++i, off += inc) %7B%0A        ids%5Bi%5D = UnsafeUtils.get2(_mem,off);%0A        vals%5Bi%5D = UnsafeUtils.get8d(_mem,off+2);%0A      %7D%0A    %7D else if(_ridsz == 4)%7B%0A      for (int i = 0; i %3C _sparseLen; ++i, off += inc) %7B%0A        ids%5Bi%5D = UnsafeUtils.get4(_mem,off);%0A        vals%5Bi%5D = UnsafeUtils.get8d(_mem,off+4);%0A      %7D%0A    %7D else throw H2O.unimpl();%0A    return _sparseLen;%0A  %7D%0A%0A
 %7D%0A

Fix in sparse bulk interface, CNAXD must override asSparseDoubles