format
This commit is contained in:
parent
594ec641a4
commit
5ae03c47da
|
|
@ -214,9 +214,9 @@ describe("Bitmap", () => {
|
||||||
|
|
||||||
expect(bitmap.toString()).toEqual(resultString);
|
expect(bitmap.toString()).toEqual(resultString);
|
||||||
expect(String(bitmap)).toEqual(resultString);
|
expect(String(bitmap)).toEqual(resultString);
|
||||||
expect(`bitmap debug: ${bitmap}`).toEqual(`bitmap debug: ${resultString}`)
|
expect(`bitmap debug: ${bitmap}`).toEqual(`bitmap debug: ${resultString}`);
|
||||||
|
|
||||||
const bitmap2 = Bitmap.fromString(bitmap.toString());
|
const bitmap2 = Bitmap.fromString(bitmap.toString());
|
||||||
expect(bitmap2.toString()).toEqual(bitmap.toString());
|
expect(bitmap2.toString()).toEqual(bitmap.toString());
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -71,11 +71,11 @@ export class Bitmap {
|
||||||
* preserving byte order and padding leading zeros to maintain alignment.
|
* preserving byte order and padding leading zeros to maintain alignment.
|
||||||
*
|
*
|
||||||
* @returns {string} Binary string representing the full contents of the bitmap.
|
* @returns {string} Binary string representing the full contents of the bitmap.
|
||||||
*/
|
*/
|
||||||
public toString(): string {
|
public toString(): string {
|
||||||
return Array.from(this.bits)
|
return Array.from(this.bits)
|
||||||
.map(x => x.toString(2).padStart(8, '0'))
|
.map((x) => x.toString(2).padStart(8, "0"))
|
||||||
.join(' ');
|
.join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -86,10 +86,10 @@ export class Bitmap {
|
||||||
* @param {string} s - Binary string produced by `toString()` or compatible formatter.
|
* @param {string} s - Binary string produced by `toString()` or compatible formatter.
|
||||||
* @returns {Bitmap} New Bitmap instance containing the parsed bits.
|
* @returns {Bitmap} New Bitmap instance containing the parsed bits.
|
||||||
* @throws {Error} If the input length is not divisible by 8.
|
* @throws {Error} If the input length is not divisible by 8.
|
||||||
*/
|
*/
|
||||||
static fromString(s: string): Bitmap {
|
static fromString(s: string): Bitmap {
|
||||||
// allow "00000001 00000010", "0000000100000010", etc.
|
// allow "00000001 00000010", "0000000100000010", etc.
|
||||||
const cleaned = s.replace(/\s+/g, '');
|
const cleaned = s.replace(/\s+/g, "");
|
||||||
|
|
||||||
if (cleaned.length % 8 !== 0) {
|
if (cleaned.length % 8 !== 0) {
|
||||||
throw new Error("Bitmap string must be aligned to bytes (len % 8 == 0)");
|
throw new Error("Bitmap string must be aligned to bytes (len % 8 == 0)");
|
||||||
|
|
@ -122,12 +122,12 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the bit at index `x` to 1.
|
* Set the bit at index `x` to 1.
|
||||||
* Automatically grows the bitmap if necessary.
|
* Automatically grows the bitmap if necessary.
|
||||||
* **Mutates this bitmap** in-place.
|
* **Mutates this bitmap** in-place.
|
||||||
* @param {number} x - Bit index (0-based).
|
* @param {number} x - Bit index (0-based).
|
||||||
* @returns {this} The instance itself (for method chaining).
|
* @returns {this} The instance itself (for method chaining).
|
||||||
*/
|
*/
|
||||||
public set(x: number): this {
|
public set(x: number): this {
|
||||||
const idx = getBitPositionIndex(x);
|
const idx = getBitPositionIndex(x);
|
||||||
if (idx >= this.bits.length) this.grow(x);
|
if (idx >= this.bits.length) this.grow(x);
|
||||||
|
|
@ -136,12 +136,12 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the bit at index `x` (set to 0).
|
* Clear the bit at index `x` (set to 0).
|
||||||
* No-op if the index is outside current capacity.
|
* No-op if the index is outside current capacity.
|
||||||
* **Mutates this bitmap** in-place.
|
* **Mutates this bitmap** in-place.
|
||||||
* @param {number} x - Bit index (0-based).
|
* @param {number} x - Bit index (0-based).
|
||||||
* @returns {this} The instance itself (for method chaining).
|
* @returns {this} The instance itself (for method chaining).
|
||||||
*/
|
*/
|
||||||
public remove(x: number): this {
|
public remove(x: number): this {
|
||||||
const idx = getBitPositionIndex(x);
|
const idx = getBitPositionIndex(x);
|
||||||
if (idx >= this.bits.length) return this;
|
if (idx >= this.bits.length) return this;
|
||||||
|
|
@ -178,11 +178,11 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bitwise AND with another bitmap.
|
* Bitwise AND with another bitmap.
|
||||||
* **Mutates this bitmap** — performs the operation in-place and clears extra bytes if needed.
|
* **Mutates this bitmap** — performs the operation in-place and clears extra bytes if needed.
|
||||||
* @param {Bitmap} bitmap - Other bitmap to AND with.
|
* @param {Bitmap} bitmap - Other bitmap to AND with.
|
||||||
* @returns {this} The instance itself (for method chaining).
|
* @returns {this} The instance itself (for method chaining).
|
||||||
*/
|
*/
|
||||||
public and(bitmap: Bitmap): this {
|
public and(bitmap: Bitmap): this {
|
||||||
const other = bitmap.bits;
|
const other = bitmap.bits;
|
||||||
const minlen = Math.min(this.bits.length, other.length);
|
const minlen = Math.min(this.bits.length, other.length);
|
||||||
|
|
@ -197,11 +197,11 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bitwise AND NOT (this = this & ~bitmap).
|
* Bitwise AND NOT (this = this & ~bitmap).
|
||||||
* **Mutates this bitmap** in-place.
|
* **Mutates this bitmap** in-place.
|
||||||
* @param {Bitmap} bitmap - Bitmap whose set bits will be subtracted.
|
* @param {Bitmap} bitmap - Bitmap whose set bits will be subtracted.
|
||||||
* @returns {this} The instance itself (for method chaining).
|
* @returns {this} The instance itself (for method chaining).
|
||||||
*/
|
*/
|
||||||
public andNot(bitmap: Bitmap): this {
|
public andNot(bitmap: Bitmap): this {
|
||||||
const other = bitmap.bits;
|
const other = bitmap.bits;
|
||||||
const minlen = Math.min(this.bits.length, other.length);
|
const minlen = Math.min(this.bits.length, other.length);
|
||||||
|
|
@ -213,11 +213,11 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bitwise OR (union) with another bitmap.
|
* Bitwise OR (union) with another bitmap.
|
||||||
* **Mutates this bitmap** in-place and grows it if necessary.
|
* **Mutates this bitmap** in-place and grows it if necessary.
|
||||||
* @param {Bitmap} bitmap - Other bitmap to OR with.
|
* @param {Bitmap} bitmap - Other bitmap to OR with.
|
||||||
* @returns {this} The instance itself (for method chaining).
|
* @returns {this} The instance itself (for method chaining).
|
||||||
*/
|
*/
|
||||||
public or(bitmap: Bitmap): this {
|
public or(bitmap: Bitmap): this {
|
||||||
const other = bitmap.bits;
|
const other = bitmap.bits;
|
||||||
|
|
||||||
|
|
@ -236,11 +236,11 @@ export class Bitmap {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Bitwise XOR with another bitmap.
|
* Bitwise XOR with another bitmap.
|
||||||
* **Mutates this bitmap** in-place and grows it if necessary.
|
* **Mutates this bitmap** in-place and grows it if necessary.
|
||||||
* @param {Bitmap} bitmap - Other bitmap to XOR with.
|
* @param {Bitmap} bitmap - Other bitmap to XOR with.
|
||||||
* @returns {this} The instance itself (for method chaining).
|
* @returns {this} The instance itself (for method chaining).
|
||||||
*/
|
*/
|
||||||
public xor(bitmap: Bitmap): this {
|
public xor(bitmap: Bitmap): this {
|
||||||
const other = bitmap.bits;
|
const other = bitmap.bits;
|
||||||
|
|
||||||
|
|
@ -280,11 +280,11 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove bits for which the predicate returns `false`.
|
* Remove bits for which the predicate returns `false`.
|
||||||
* **Mutates this bitmap** in-place.
|
* **Mutates this bitmap** in-place.
|
||||||
* @param {(bitIndex: number) => boolean} fn - Predicate; bit is cleared if `false` is returned.
|
* @param {(bitIndex: number) => boolean} fn - Predicate; bit is cleared if `false` is returned.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
public filter(fn: (x: number) => boolean): void {
|
public filter(fn: (x: number) => boolean): void {
|
||||||
for (let i = 0; i < this.bits.length; i++) {
|
for (let i = 0; i < this.bits.length; i++) {
|
||||||
for (let j = 0; j < 8; j++) {
|
for (let j = 0; j < 8; j++) {
|
||||||
|
|
@ -297,10 +297,10 @@ export class Bitmap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all bits in the bitmap (set everything to 0).
|
* Clear all bits in the bitmap (set everything to 0).
|
||||||
* **Mutates this bitmap** in-place.
|
* **Mutates this bitmap** in-place.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
public clear(): void {
|
public clear(): void {
|
||||||
for (let i = 0; i < this.bits.length; i++) {
|
for (let i = 0; i < this.bits.length; i++) {
|
||||||
this.bits[i] = 0;
|
this.bits[i] = 0;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue