UNPKG

@gmod/cram

Version:

read CRAM files with pure Javascript

46 lines 1.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const _base_ts_1 = __importDefault(require("./_base.js")); const errors_ts_1 = require("../../errors.js"); class BetaCodec extends _base_ts_1.default { constructor(parameters, dataType) { super(parameters, dataType); if (this.dataType !== 'int') { throw new errors_ts_1.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); } } exports.default = BetaCodec; /** * 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