Little DX upgrade. Now .range() can return VOID.

It means that user don't need to always return `true` to continue iteration, but user can break the iterations by returning `false`.
This commit is contained in:
Eugene 2025-04-03 19:18:00 +03:00
parent 6c6b48a287
commit ea844221b7
3 changed files with 19 additions and 4 deletions

View File

@ -68,7 +68,6 @@ describe("Bitmap", () => {
bitmap.range((x) => { bitmap.range((x) => {
results.push(x); results.push(x);
return true;
}); });
expect(results).toEqual([2, 4, 6, 8]); expect(results).toEqual([2, 4, 6, 8]);

View File

@ -100,14 +100,14 @@ export class Bitmap implements IBitmap {
return result; return result;
} }
public range(fn: (x: number) => boolean): void { public range(fn: (x: number) => boolean | void): void {
let needContinueIterating = true; let needContinueIterating = true;
for (let i = 0; i < this.bits.length; i++) { for (let i = 0; i < this.bits.length; i++) {
let x = this.bits[i]; let x = this.bits[i];
for (let j = 0; j < 8; j++) { for (let j = 0; j < 8; j++) {
const bit = (x >> j) & 1; const bit = (x >> j) & 1;
if (bit === 0) continue; if (bit === 0) continue;
needContinueIterating = fn(i * 8 + j); needContinueIterating = fn(i * 8 + j) ?? true;
if (!needContinueIterating) break; if (!needContinueIterating) break;
} }
if (!needContinueIterating) break; if (!needContinueIterating) break;

View File

@ -1,19 +1,35 @@
export interface IBitmap { export interface IBitmap {
grow(x: number): void; grow(x: number): void;
set(x: number): void; set(x: number): void;
remove(x: number): void; remove(x: number): void;
contains(x: number): boolean; contains(x: number): boolean;
count(): number; count(): number;
and(bitmap: IBitmap): IBitmap; and(bitmap: IBitmap): IBitmap;
andNot(bitmap: IBitmap): IBitmap; andNot(bitmap: IBitmap): IBitmap;
or(bitmap: IBitmap): IBitmap; or(bitmap: IBitmap): IBitmap;
xor(bitmap: IBitmap): IBitmap; xor(bitmap: IBitmap): IBitmap;
range(fn: (x: number) => boolean): void;
range(fn: (x: number) => boolean | void): void;
filter(fn: (x: number) => boolean): void; filter(fn: (x: number) => boolean): void;
clear(): void; clear(): void;
clone(): IBitmap; clone(): IBitmap;
min(): number; min(): number;
max(): number; max(): number;
minZero(): number; minZero(): number;
maxZero(): number; maxZero(): number;
} }