Skip to content

Commit d419b67

Browse files
committed
fix(BitMatrix): fix bug where getting indexes by column results in RangeError
Fixes #2
1 parent 0a73830 commit d419b67

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

tests/BitMatrixSpec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,22 @@ describe("BitMatrix", function () {
293293
expect(bs.getRowIndexes(31)).toEqual([6, 31]);
294294
expect(bs.getRowIndexes(32)).toEqual([6, 32]);
295295
});
296+
it("be able to get set bits per column when column count < row count", function () {
297+
const bm = new BitMatrix(100, 10);
298+
bm.set(2, 1, true);
299+
bm.set(3, 9, true);
300+
expect(bm.getIndexes(true)[0]).toEqual([]);
301+
expect(bm.getIndexes(true)[1]).toEqual([2]);
302+
expect(bm.getIndexes(true)[9]).toEqual([3]);
303+
bm.set(4, 8, true);
304+
expect(bm.getIndexes(true)[0]).toEqual([]);
305+
expect(bm.getIndexes(true)[1]).toEqual([2]);
306+
expect(bm.getIndexes(true)[8]).toEqual([4]);
307+
expect(bm.getIndexes(true)[9]).toEqual([3]);
308+
bm.set(4, 8, false);
309+
expect(bm.getIndexes(true)[0]).toEqual([]);
310+
expect(bm.getIndexes(true)[1]).toEqual([2]);
311+
expect(bm.getIndexes(true)[8]).toEqual([]);
312+
expect(bm.getIndexes(true)[9]).toEqual([3]);
313+
});
296314
});

ts/BitMatrix.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class BitMatrix implements IBitMatrix {
5454
let result: number[][] = [];
5555
let index: number = 0;
5656
if (resultPerColumn) {
57-
while (index < this.rowCount) {
57+
while (index < this.colCount) {
5858
result.push(this.getColIndexes(index));
5959
index++;
6060
}
@@ -180,11 +180,11 @@ export class BitMatrix implements IBitMatrix {
180180
}
181181

182182
private validateIndex(rowIndex: number, colIndex: number) {
183-
if (rowIndex > this.rowCount - 1 || rowIndex < 0) {
184-
throw new RangeError("Row index is incorrect.");
183+
if (rowIndex >= this.rowCount || rowIndex < 0) {
184+
throw new RangeError(`Row index is incorrect. Maximum allowed index: ${this.rowCount - 1}. Actual index ${rowIndex}`);
185185
}
186-
if (colIndex > this.colCount - 1 || colIndex < 0) {
187-
throw new RangeError("Column index is incorrect.");
186+
if (colIndex >= this.colCount || colIndex < 0) {
187+
throw new RangeError(`Column index is incorrect. Maximum allowed index: ${this.colCount - 1}. Actual index ${colIndex}`);
188188
}
189189
}
190190
}

0 commit comments

Comments
 (0)