@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
52 lines (49 loc) • 2.07 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
var DEFAULT_CHUNK_SIZE = 4096; // Default chunk size in bits
/**
* A dynamic bit array implementation that allows for efficient storage and manipulation of bits.
* It supports dynamic resizing and provides methods for setting and getting bit values.
* This is useful for scenarios where you need to manage a large number of boolean flags efficiently.
*/
export var DynamicBitArray = /*#__PURE__*/function () {
function DynamicBitArray() {
var chunkSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_CHUNK_SIZE;
_classCallCheck(this, DynamicBitArray);
this.chunkSize = chunkSize;
this.chunkTotalBitSize = this.chunkSize * 8;
this.bitArrays = [];
}
return _createClass(DynamicBitArray, [{
key: "set",
value: function set(index, value) {
var bitArray = this.getChunk(index);
var byteOffset = index % this.chunkTotalBitSize;
var byteIndex = Math.floor(byteOffset / 8); // Get the byte index
var bitOffset = byteOffset % 8; // Get the bit offset within the byte
if (value) {
bitArray[byteIndex] |= 1 << bitOffset; // set the bit
} else {
bitArray[byteIndex] &= ~(1 << bitOffset); // clear the bit
}
}
}, {
key: "get",
value: function get(index) {
var bitArray = this.getChunk(index);
var byteOffset = index % this.chunkTotalBitSize;
var byteIndex = Math.floor(byteOffset / 8); // Get the byte index
var bitOffset = byteOffset % 8; // Get the bit offset within the byte
return (bitArray[byteIndex] & 1 << bitOffset) !== 0; // Check if the bit is set
}
}, {
key: "getChunk",
value: function getChunk(index) {
var chunkIndex = Math.floor(index / this.chunkTotalBitSize);
if (!this.bitArrays[chunkIndex]) {
this.bitArrays[chunkIndex] = new Uint8Array(this.chunkSize);
}
return this.bitArrays[chunkIndex];
}
}]);
}();