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:
parent
6c6b48a287
commit
ea844221b7
|
|
@ -68,7 +68,6 @@ describe("Bitmap", () => {
|
|||
|
||||
bitmap.range((x) => {
|
||||
results.push(x);
|
||||
return true;
|
||||
});
|
||||
|
||||
expect(results).toEqual([2, 4, 6, 8]);
|
||||
|
|
|
|||
|
|
@ -100,14 +100,14 @@ export class Bitmap implements IBitmap {
|
|||
return result;
|
||||
}
|
||||
|
||||
public range(fn: (x: number) => boolean): void {
|
||||
public range(fn: (x: number) => boolean | void): void {
|
||||
let needContinueIterating = true;
|
||||
for (let i = 0; i < this.bits.length; i++) {
|
||||
let x = this.bits[i];
|
||||
for (let j = 0; j < 8; j++) {
|
||||
const bit = (x >> j) & 1;
|
||||
if (bit === 0) continue;
|
||||
needContinueIterating = fn(i * 8 + j);
|
||||
needContinueIterating = fn(i * 8 + j) ?? true;
|
||||
if (!needContinueIterating) break;
|
||||
}
|
||||
if (!needContinueIterating) break;
|
||||
|
|
|
|||
18
src/types.ts
18
src/types.ts
|
|
@ -1,19 +1,35 @@
|
|||
export interface IBitmap {
|
||||
grow(x: number): void;
|
||||
|
||||
set(x: number): void;
|
||||
|
||||
remove(x: number): void;
|
||||
|
||||
contains(x: number): boolean;
|
||||
|
||||
count(): number;
|
||||
|
||||
and(bitmap: IBitmap): IBitmap;
|
||||
|
||||
andNot(bitmap: IBitmap): IBitmap;
|
||||
|
||||
or(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;
|
||||
|
||||
clear(): void;
|
||||
|
||||
clone(): IBitmap;
|
||||
|
||||
min(): number;
|
||||
|
||||
max(): number;
|
||||
|
||||
minZero(): number;
|
||||
|
||||
maxZero(): number;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue