Add force setting to MinCovered

This commit is contained in:
Andras Schmelczer 2025-04-08 23:00:44 +01:00
parent dc124ace20
commit 0abd50ac0c
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 21 additions and 4 deletions

View file

@ -45,4 +45,16 @@ describe("CoveredValues", () => {
covered.add(6); covered.add(6);
expect(covered.min).toBe(6); expect(covered.min).toBe(6);
}); });
test("should handle force setting min value", () => {
const covered = new CoveredValues(5);
covered.add(7);
covered.add(8);
covered.add(9);
expect(covered.min).toBe(5);
covered.min = 6;
expect(covered.min).toBe(6);
covered.add(10);
expect(covered.min).toBe(10);
});
}); });

View file

@ -18,6 +18,15 @@ export class CoveredValues {
public constructor(private minValue: number) {} public constructor(private minValue: number) {}
public get min(): number {
return this.minValue;
}
public set min(value: number) {
this.minValue = Math.max(value, this.minValue);
this.seenValues = this.seenValues.filter((v) => v > value);
}
public add(value: number): void { public add(value: number): void {
if (value < this.minValue) { if (value < this.minValue) {
return; return;
@ -44,8 +53,4 @@ export class CoveredValues {
this.minValue++; this.minValue++;
} }
} }
public get min(): number {
return this.minValue;
}
} }