class SparseMatrix extends Matrix {

  @Override
  @SuppressWarnings("squid:ForLoopCounterChangedCheck")
  protected final void forEach(final ElementVisitorFunction func) {
    final boolean[] copied = new boolean[1];
    for (int i = 0; i < this.numRows; ++i) {
      final int iCopy = i;
      final double[] rowValues = this.values[i];
      final int[] rowCols = this.cols[i];
      int size = this.maxIndices[i];
      for (int j = 0; j < size; ++j) {
        final int jCopy = j;
        copied[0] = false;
        func.visit(
            i,
            rowCols[j],
            rowValues[j],
            v -> {
              if (0.0 == v) {
                copied[0] = this.remove(iCopy, jCopy);
              } else {
                rowValues[jCopy] = v;
              }
            });

        if (copied[0]) {
          // Same index again since the array shrank.
          size = this.maxIndices[i];
          --j;
        }
      }
    }
  }

  public  SparseMatrix(final int numRows, final int numCols, final int initialCapacityPerRow);

}