@gmod/cram
Version:
read CRAM files with pure Javascript
40 lines • 1.4 kB
JavaScript
import CramCodec from "./_base.js";
import { CramUnimplementedError } from "../../errors.js";
export default class BetaCodec extends CramCodec {
constructor(parameters, dataType) {
super(parameters, dataType);
if (this.dataType !== 'int') {
throw new CramUnimplementedError(`${this.dataType} decoding not yet implemented by BETA codec`);
}
}
decode(_slice, coreDataBlock, _blocksByContentId, cursors) {
return decodeBetaInline(coreDataBlock.content, cursors.coreBlock, this.parameters.length, this.parameters.offset);
}
}
/**
* Optimized beta decoder with inlined bit reading.
*/
function decodeBetaInline(data, cursor, numBits, offset) {
let { bytePosition, bitPosition } = cursor;
// Fast path: reading exactly 8 bits when byte-aligned
if (numBits === 8 && bitPosition === 7) {
const val = data[bytePosition];
cursor.bytePosition = bytePosition + 1;
return val - offset;
}
// General case
let val = 0;
for (let i = 0; i < numBits; i++) {
val <<= 1;
val |= (data[bytePosition] >> bitPosition) & 1;
bitPosition -= 1;
if (bitPosition < 0) {
bytePosition += 1;
bitPosition = 7;
}
}
cursor.bytePosition = bytePosition;
cursor.bitPosition = bitPosition;
return val - offset;
}
//# sourceMappingURL=beta.js.map