// Compute compressed boolean buffer private byte[] bufB(int bpv) { assert bpv == 1 || bpv == 2 : "Only bit vectors with/without NA are supported"; final int off = CBSChunk.OFF; int clen = off + CBSChunk.clen(_len2, bpv); byte bs[] = new byte[clen]; // Save the gap = number of unfilled bits and bpv value bs[0] = (byte) (((_len2 * bpv) & 7) == 0 ? 0 : (8 - ((_len2 * bpv) & 7))); bs[1] = (byte) bpv; if (_len2 != _len) { // Sparse bitvector? assert bpv == 1; // No NAs for (int i = 0; i < _len; i++) { int row = _xs[i]; bs[(row >> 3) + off] = CBSChunk.write1b(bs[(row >> 3) + off], (byte) 1, row & 7); } return bs; } // Dense bitvector int boff = 0; byte b = 0; int idx = CBSChunk.OFF; for (int i = 0; i < _len; i++) { if (bpv == 1) { assert !isNA(i); b = CBSChunk.write1b(b, (byte) _ls[i], boff); } else { byte val = isNA(i) ? CBSChunk._NA : (byte) _ls[i]; b = CBSChunk.write2b(b, val, boff); } boff += bpv; if (boff > 8 - bpv) { bs[idx] = b; boff = 0; b = 0; idx++; } } assert bs[0] == (byte) (boff == 0 ? 0 : 8 - boff); // Flush last byte if (boff > 0) bs[idx++] = b; return bs; }