All files / src/iterators danMatrixColumnsIterator.ts

100% Statements 21/21
100% Branches 5/5
100% Functions 8/8
100% Lines 21/21

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 963x         3x                     11x 6x   5x 5x             6x                 7x 3x   4x                 17x 3x   14x               22x             1x                 2x   2x   12x 12x 2x         10x                  
import { MatrixIterator, DanMatrix } from '..';
 
/**
 * The class DanMatrixColumnsIterator implements MatrixIterator interface and Iterable interface
 */
export class DanMatrixColumnsIterator<T> implements MatrixIterator<Array<T>>, Iterable<Array<T>> {
  // the matrix
  private _matrix: DanMatrix<T>;
  // index of the current column, or -1 if the iterator was not yet started
  private _columnIndex: 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._columnIndex = -1;
  }
 
  /**
   * Get the current column, 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._columnIndex < 0) {
      return undefined;
    }
    return this._matrix.getColumnAt(this._columnIndex);
  }
 
  /**
   * Get the next column, or return undefined if the iterator's end was reached
   * @returns {Array<T>|undefined}
   */
  next(): Array<T> | undefined {
    // return undefined if there are no columns left
    if (!this.hasNext()) {
      return undefined;
    }
    return this._matrix.getColumnAt(++this._columnIndex);
  }
 
  /**
   * Check if the iterator can return more columns
   * @returns {boolean} true if the iterator can return more columns, false if there are no columns left
   */
  hasNext(): boolean {
    return this._columnIndex < this._matrix.colsNum() - 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: DanMatrixColumnsIterator<T> = this;
 
    return {
      next() {
        const nextElement = iteratorInstance.next();
        if (nextElement === undefined) {
          return {
            value: undefined,
            done: true
          };
        } else {
          return {
            value: nextElement,
            done: false
          };
        }
      }
    };
  }
}