public NewChunk( Vec vec, int cidx, long[] mantissa, int[] exponent, int[] indices, double[] doubles) { _vec = vec; _cidx = cidx; _ls = mantissa; _xs = exponent; _id = indices; _ds = doubles; if (_ls != null && sparseLen() == 0) set_sparseLen(set_len(_ls.length)); if (_xs != null && sparseLen() == 0) set_sparseLen(set_len(_xs.length)); if (_id != null && sparseLen() == 0) set_sparseLen(set_len(_id.length)); if (_ds != null && sparseLen() == 0) set_sparseLen(set_len(_ds.length)); }
protected void cancel_sparse() { if (sparseLen() != _len) { if (_is != null) { int[] is = MemoryManager.malloc4(_len); for (int i = 0; i < _len; i++) is[i] = -1; for (int i = 0; i < sparseLen(); i++) is[_id[i]] = _is[i]; _is = is; } else if (_ds == null) { int[] xs = MemoryManager.malloc4(_len); long[] ls = MemoryManager.malloc8(_len); for (int i = 0; i < sparseLen(); ++i) { xs[_id[i]] = _xs[i]; ls[_id[i]] = _ls[i]; } _xs = xs; _ls = ls; } else { double[] ds = MemoryManager.malloc8d(_len); for (int i = 0; i < sparseLen(); ++i) ds[_id[i]] = _ds[i]; _ds = ds; } set_sparseLen(_len); } _id = null; }
// Append all of 'nc' onto the current NewChunk. Kill nc. public void add(NewChunk nc) { assert _cidx >= 0; assert sparseLen() <= _len; assert nc.sparseLen() <= nc._len : "_len = " + nc.sparseLen() + ", _len2 = " + nc._len; if (nc._len == 0) return; if (_len == 0) { _ls = nc._ls; nc._ls = null; _xs = nc._xs; nc._xs = null; _id = nc._id; nc._id = null; _ds = nc._ds; nc._ds = null; _is = nc._is; nc._is = null; _ss = nc._ss; nc._ss = null; set_sparseLen(nc.sparseLen()); set_len(nc._len); return; } if (nc.sparse() != sparse()) { // for now, just make it dense cancel_sparse(); nc.cancel_sparse(); } if (_ds != null) throw H2O.fail(); while (sparseLen() + nc.sparseLen() >= _xs.length) _xs = MemoryManager.arrayCopyOf(_xs, _xs.length << 1); _ls = MemoryManager.arrayCopyOf(_ls, _xs.length); System.arraycopy(nc._ls, 0, _ls, sparseLen(), nc.sparseLen()); System.arraycopy(nc._xs, 0, _xs, sparseLen(), nc.sparseLen()); if (_id != null) { assert nc._id != null; _id = MemoryManager.arrayCopyOf(_id, _xs.length); System.arraycopy(nc._id, 0, _id, sparseLen(), nc.sparseLen()); for (int i = sparseLen(); i < sparseLen() + nc.sparseLen(); ++i) _id[i] += _len; } else assert nc._id == null; set_sparseLen(sparseLen() + nc.sparseLen()); set_len(_len + nc._len); nc._ls = null; nc._xs = null; nc._id = null; nc.set_sparseLen(nc.set_len(0)); assert sparseLen() <= _len; }
// Append a UUID, stored in _ls & _ds public void addUUID(long lo, long hi) { if (_ls == null || _ds == null || sparseLen() >= _ls.length) append2slowUUID(); _ls[sparseLen()] = lo; _ds[sparseLen()] = Double.longBitsToDouble(hi); set_sparseLen(sparseLen() + 1); set_len(_len + 1); assert sparseLen() <= _len; }
// Append a String, stored in _ss & _is public void addStr(ValueString str) { if (_id == null || str != null) { if (_is == null || sparseLen() >= _is.length) { append2slowstr(); addStr(str); assert sparseLen() <= _len; return; } if (str != null) { if (_id != null) _id[sparseLen()] = _len; _is[sparseLen()] = _sslen; set_sparseLen(sparseLen() + 1); append_ss(str); } else if (_id == null) { _is[sparseLen()] = CStrChunk.NA; set_sparseLen(sparseLen() + 1); } } set_len(_len + 1); assert sparseLen() <= _len; }
// Append a string, store in _ss & _is public void addStr(Object str) { if (_id == null || str != null) { if (_is == null || sparseLen() >= _is.length) { append2slowstr(); addStr(str); assert sparseLen() <= _len; return; } if (str != null) { if (_id != null) _id[sparseLen()] = _len; _is[sparseLen()] = _sslen; set_sparseLen(sparseLen() + 1); if (str instanceof BufferedString) append_ss((BufferedString) str); else // this spares some callers from an unneeded conversion to BufferedString first append_ss((String) str); } else if (_id == null) { _is[sparseLen()] = CStrChunk.NA; set_sparseLen(sparseLen() + 1); } } set_len(_len + 1); assert sparseLen() <= _len; }
@Override public NewChunk inflate_impl(NewChunk nc) { double dx = Math.log10(_scale); assert water.util.PrettyPrint.fitsIntoInt(dx); nc.set_sparseLen(0); nc.set_len(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; }
// Fast-path append long data void append2(long l, int x) { if (_id == null || l != 0) { if (_ls == null || sparseLen() == _ls.length) { append2slow(); // again call append2 since calling append2slow might have changed things (eg might have // switched to sparse and l could be 0) append2(l, x); return; } _ls[sparseLen()] = l; _xs[sparseLen()] = x; if (_id != null) _id[sparseLen()] = _len; set_sparseLen(sparseLen() + 1); } set_len(_len + 1); assert sparseLen() <= _len; }
// Fast-path append double data public void addNum(double d) { if (isUUID() || isString()) { addNA(); return; } if (_id == null || d != 0) { if (_ls != null) switch_to_doubles(); if (_ds == null || sparseLen() >= _ds.length) { append2slowd(); // call addNum again since append2slow might have flipped to sparse addNum(d); assert sparseLen() <= _len; return; } if (_id != null) _id[sparseLen()] = _len; _ds[sparseLen()] = d; set_sparseLen(sparseLen() + 1); } set_len(_len + 1); assert sparseLen() <= _len; }
protected void set_sparse(int nzeros) { if (sparseLen() == nzeros && _len != 0) return; if (_id != null) { // we have sparse representation but some 0s in it! int[] id = MemoryManager.malloc4(nzeros); int j = 0; if (_ds != null) { double[] ds = MemoryManager.malloc8d(nzeros); for (int i = 0; i < sparseLen(); ++i) { if (_ds[i] != 0) { ds[j] = _ds[i]; id[j] = _id[i]; ++j; } } _ds = ds; } else if (_is != null) { int[] is = MemoryManager.malloc4(nzeros); for (int i = 0; i < sparseLen(); i++) { if (_is[i] != -1) { is[j] = _is[i]; id[j] = id[i]; ++j; } } } else { long[] ls = MemoryManager.malloc8(nzeros); int[] xs = MemoryManager.malloc4(nzeros); for (int i = 0; i < sparseLen(); ++i) { if (_ls[i] != 0) { ls[j] = _ls[i]; xs[j] = _xs[i]; id[j] = _id[i]; ++j; } } _ls = ls; _xs = xs; } _id = id; assert j == nzeros; set_sparseLen(nzeros); return; } assert sparseLen() == _len : "_len = " + sparseLen() + ", _len2 = " + _len + ", nzeros = " + nzeros; int zs = 0; if (_is != null) { assert nzeros < _is.length; _id = MemoryManager.malloc4(_is.length); for (int i = 0; i < sparseLen(); i++) { if (_is[i] == -1) zs++; else { _is[i - zs] = _is[i]; _id[i - zs] = i; } } } else if (_ds == null) { if (_len == 0) { _ls = new long[0]; _xs = new int[0]; _id = new int[0]; set_sparseLen(0); return; } else { assert nzeros < sparseLen(); _id = alloc_indices(_ls.length); for (int i = 0; i < sparseLen(); ++i) { if (_ls[i] == 0 && _xs[i] == 0) ++zs; else { _ls[i - zs] = _ls[i]; _xs[i - zs] = _xs[i]; _id[i - zs] = i; } } } } else { assert nzeros < _ds.length; _id = alloc_indices(_ds.length); for (int i = 0; i < sparseLen(); ++i) { if (_ds[i] == 0) ++zs; else { _ds[i - zs] = _ds[i]; _id[i - zs] = i; } } } assert zs == (sparseLen() - nzeros); set_sparseLen(nzeros); }
// Pre-sized newchunks. public NewChunk(Vec vec, int cidx, int len) { this(vec, cidx); _ds = new double[len]; Arrays.fill(_ds, Double.NaN); set_sparseLen(set_len(len)); }