Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 3x 3x 11x 6x 5x 5x 6x 9x 3x 6x 21x 3x 18x 28x 1x 2x 2x 14x 14x 2x 12x | import { MatrixIterator, DanMatrix } from '..';
/**
* The class DanMatrixRowsIterator implements MatrixIterator interface and Iterable interface
*/
export class DanMatrixRowsIterator<T> implements MatrixIterator<Array<T>>, Iterable<Array<T>> {
// the matrix
private _matrix: DanMatrix<T>;
// index of the current row, or -1 if the iterator was not yet started
private _rowIndex: number;
/**
* The public class constructor
* @param {DanMatrix<T>} matrix the DanMatrix object
*/
public constructor(matrix: DanMatrix<T>) {
if (!(matrix instanceof DanMatrix)) {
throw new Error('Wrong input');
}
this._matrix = matrix;
this._initFields();
}
/**
* Init the class fields
*/
private _initFields(): void {
this._rowIndex = -1;
}
/**
* Get the current row, or return undefined if the iterator was not yet started
* @returns {Array<T>|undefined}
*/
current(): Array<T> | undefined {
// the iterator was not yet started
if (this._rowIndex < 0) {
return undefined;
}
return this._matrix.getRowAt(this._rowIndex);
}
/**
* Get the next row, or return undefined if the iterator's end was reached
* @returns {Array<T>|undefined}
*/
next(): Array<T> | undefined {
// return undefined if there are no rows left
if (!this.hasNext()) {
return undefined;
}
return this._matrix.getRowAt(++this._rowIndex);
}
/**
* Check if the iterator can return more rows
* @returns {boolean} true if the iterator can return more rows, false if there are no rows left
*/
hasNext(): boolean {
return this._rowIndex < this._matrix.rowsNum() - 1;
}
/**
* Restart the iterator
*/
rewind(): void {
this._initFields();
}
/**
* A zero-argument function that returns an object, conforming to the [Iterator Protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol).
* In Javascript/Typescript in order to be iterable, an object must implement the "@@iterator" method
* @returns {Iterator<Array<T>>} an iterator object.
*/
[Symbol.iterator](): Iterator<Array<T>> {
const iteratorInstance: DanMatrixRowsIterator<T> = this;
return {
next() {
const nextElement = iteratorInstance.next();
if (nextElement === undefined) {
return {
value: undefined,
done: true
};
} else {
return {
value: nextElement,
done: false
};
}
}
};
}
}
|