// Reused by both MR and Spark for performing zero out
  public static IndexRange getSelectedRangeForZeroOut(
      Pair<Long, FrameBlock> in,
      int blockRowFactor,
      int blockColFactor,
      IndexRange indexRange,
      long lSrcRowIndex,
      long lDestRowIndex) {
    int iRowStart, iRowEnd, iColStart, iColEnd;

    if (indexRange.rowStart <= lDestRowIndex) iRowStart = 0;
    else iRowStart = (int) (indexRange.rowStart - in.getKey());
    iRowEnd = (int) Math.min(indexRange.rowEnd - lSrcRowIndex, blockRowFactor) - 1;

    iColStart = UtilFunctions.computeCellInBlock(indexRange.colStart, blockColFactor);
    iColEnd = UtilFunctions.computeCellInBlock(indexRange.colEnd, blockColFactor);

    return new IndexRange(iRowStart, iRowEnd, iColStart, iColEnd);
  }
  // Reused by both MR and Spark for performing zero out
  public static IndexRange getSelectedRangeForZeroOut(
      IndexedMatrixValue in, int blockRowFactor, int blockColFactor, IndexRange indexRange) {
    IndexRange tempRange = new IndexRange(-1, -1, -1, -1);

    long topBlockRowIndex = UtilFunctions.computeBlockIndex(indexRange.rowStart, blockRowFactor);
    int topRowInTopBlock = UtilFunctions.computeCellInBlock(indexRange.rowStart, blockRowFactor);
    long bottomBlockRowIndex = UtilFunctions.computeBlockIndex(indexRange.rowEnd, blockRowFactor);
    int bottomRowInBottomBlock =
        UtilFunctions.computeCellInBlock(indexRange.rowEnd, blockRowFactor);

    long leftBlockColIndex = UtilFunctions.computeBlockIndex(indexRange.colStart, blockColFactor);
    int leftColInLeftBlock = UtilFunctions.computeCellInBlock(indexRange.colStart, blockColFactor);
    long rightBlockColIndex = UtilFunctions.computeBlockIndex(indexRange.colEnd, blockColFactor);
    int rightColInRightBlock = UtilFunctions.computeCellInBlock(indexRange.colEnd, blockColFactor);

    // no overlap
    if (in.getIndexes().getRowIndex() < topBlockRowIndex
        || in.getIndexes().getRowIndex() > bottomBlockRowIndex
        || in.getIndexes().getColumnIndex() < leftBlockColIndex
        || in.getIndexes().getColumnIndex() > rightBlockColIndex) {
      tempRange.set(-1, -1, -1, -1);
      return tempRange;
    }

    // get the index range inside the block
    tempRange.set(0, in.getValue().getNumRows() - 1, 0, in.getValue().getNumColumns() - 1);
    if (topBlockRowIndex == in.getIndexes().getRowIndex()) tempRange.rowStart = topRowInTopBlock;
    if (bottomBlockRowIndex == in.getIndexes().getRowIndex())
      tempRange.rowEnd = bottomRowInBottomBlock;
    if (leftBlockColIndex == in.getIndexes().getColumnIndex())
      tempRange.colStart = leftColInLeftBlock;
    if (rightBlockColIndex == in.getIndexes().getColumnIndex())
      tempRange.colEnd = rightColInRightBlock;

    return tempRange;
  }
  /**
   * @param in
   * @param ixrange
   * @param brlen
   * @param bclen
   * @param rlen
   * @param clen
   * @param outlist
   * @throws DMLRuntimeException
   */
  public static void performShift(
      Pair<Long, FrameBlock> in,
      IndexRange ixrange,
      int brlenLeft,
      int clenLeft /*, int bclen*/,
      long rlen,
      long clen,
      ArrayList<Pair<Long, FrameBlock>> outlist)
      throws DMLRuntimeException {
    Long ix = in.getKey();
    FrameBlock fb = in.getValue();
    long start_lhs_globalRowIndex = ixrange.rowStart + (ix - 1);
    long start_lhs_globalColIndex = ixrange.colStart;
    long end_lhs_globalRowIndex = start_lhs_globalRowIndex + fb.getNumRows() - 1;
    long end_lhs_globalColIndex = ixrange.colEnd;

    long start_lhs_rowIndex = UtilFunctions.computeBlockIndex(start_lhs_globalRowIndex, brlenLeft);
    long end_lhs_rowIndex = UtilFunctions.computeBlockIndex(end_lhs_globalRowIndex, brlenLeft);

    for (long leftRowIndex = start_lhs_rowIndex; leftRowIndex <= end_lhs_rowIndex; leftRowIndex++) {

      // Calculate global index of right hand side block
      long lhs_rl = Math.max((leftRowIndex - 1) * brlenLeft + 1, start_lhs_globalRowIndex);
      long lhs_ru = Math.min(leftRowIndex * brlenLeft, end_lhs_globalRowIndex);
      long lhs_cl = start_lhs_globalColIndex;
      long lhs_cu = end_lhs_globalColIndex;

      int lhs_lrl = UtilFunctions.computeCellInBlock(lhs_rl, brlenLeft);
      int lhs_lru = UtilFunctions.computeCellInBlock(lhs_ru, brlenLeft);
      int lhs_lcl = (int) lhs_cl - 1;
      int lhs_lcu = (int) lhs_cu - 1;

      long rhs_rl = lhs_rl - (ixrange.rowStart - 1) - (ix - 1);
      long rhs_ru = rhs_rl + (lhs_ru - lhs_rl);
      long rhs_cl = lhs_cl - ixrange.colStart + 1;
      long rhs_cu = rhs_cl + (lhs_cu - lhs_cl);

      // local indices are 0 (zero) based.
      int rhs_lrl = (int) (UtilFunctions.computeCellInBlock(rhs_rl, fb.getNumRows()));
      int rhs_lru = (int) (UtilFunctions.computeCellInBlock(rhs_ru, fb.getNumRows()));
      int rhs_lcl = (int) rhs_cl - 1;
      int rhs_lcu = (int) rhs_cu - 1;

      FrameBlock slicedRHSBlk =
          fb.sliceOperations(rhs_lrl, rhs_lru, rhs_lcl, rhs_lcu, new FrameBlock());

      int lbclen = clenLeft;

      List<ValueType> schemaPartialLeft = Collections.nCopies(lhs_lcl, ValueType.STRING);
      List<ValueType> schemaRHS =
          UtilFunctions.getSubSchema(fb.getSchema(), rhs_lcl, rhs_lcl - lhs_lcl + lhs_lcu);
      List<ValueType> schema = new ArrayList<ValueType>(schemaPartialLeft);
      schema.addAll(schemaRHS);
      List<ValueType> schemaPartialRight =
          Collections.nCopies(lbclen - schema.size(), ValueType.STRING);
      schema.addAll(schemaPartialRight);
      FrameBlock resultBlock = new FrameBlock(schema);
      int iRHSRows =
          (int)
              (leftRowIndex <= rlen / brlenLeft
                  ? brlenLeft
                  : rlen - (rlen / brlenLeft) * brlenLeft);
      resultBlock.ensureAllocatedColumns(iRHSRows);

      resultBlock =
          resultBlock.leftIndexingOperations(
              slicedRHSBlk, lhs_lrl, lhs_lru, lhs_lcl, lhs_lcu, new FrameBlock());
      outlist.add(new Pair<Long, FrameBlock>((leftRowIndex - 1) * brlenLeft + 1, resultBlock));
    }
  }
  /**
   * @param in
   * @param ixrange
   * @param brlen
   * @param bclen
   * @param rlen
   * @param clen
   * @param outlist
   * @throws DMLRuntimeException
   */
  public static void performShift(
      IndexedMatrixValue in,
      IndexRange ixrange,
      int brlen,
      int bclen,
      long rlen,
      long clen,
      ArrayList<IndexedMatrixValue> outlist)
      throws DMLRuntimeException {
    MatrixIndexes ix = in.getIndexes();
    MatrixBlock mb = (MatrixBlock) in.getValue();

    long start_lhs_globalRowIndex = ixrange.rowStart + (ix.getRowIndex() - 1) * brlen;
    long start_lhs_globalColIndex = ixrange.colStart + (ix.getColumnIndex() - 1) * bclen;
    long end_lhs_globalRowIndex = start_lhs_globalRowIndex + mb.getNumRows() - 1;
    long end_lhs_globalColIndex = start_lhs_globalColIndex + mb.getNumColumns() - 1;

    long start_lhs_rowIndex = UtilFunctions.computeBlockIndex(start_lhs_globalRowIndex, brlen);
    long end_lhs_rowIndex = UtilFunctions.computeBlockIndex(end_lhs_globalRowIndex, brlen);
    long start_lhs_colIndex = UtilFunctions.computeBlockIndex(start_lhs_globalColIndex, bclen);
    long end_lhs_colIndex = UtilFunctions.computeBlockIndex(end_lhs_globalColIndex, bclen);

    for (long leftRowIndex = start_lhs_rowIndex; leftRowIndex <= end_lhs_rowIndex; leftRowIndex++) {
      for (long leftColIndex = start_lhs_colIndex;
          leftColIndex <= end_lhs_colIndex;
          leftColIndex++) {

        // Calculate global index of right hand side block
        long lhs_rl = Math.max((leftRowIndex - 1) * brlen + 1, start_lhs_globalRowIndex);
        long lhs_ru = Math.min(leftRowIndex * brlen, end_lhs_globalRowIndex);
        long lhs_cl = Math.max((leftColIndex - 1) * bclen + 1, start_lhs_globalColIndex);
        long lhs_cu = Math.min(leftColIndex * bclen, end_lhs_globalColIndex);

        int lhs_lrl = UtilFunctions.computeCellInBlock(lhs_rl, brlen);
        int lhs_lru = UtilFunctions.computeCellInBlock(lhs_ru, brlen);
        int lhs_lcl = UtilFunctions.computeCellInBlock(lhs_cl, bclen);
        int lhs_lcu = UtilFunctions.computeCellInBlock(lhs_cu, bclen);

        long rhs_rl = lhs_rl - ixrange.rowStart + 1;
        long rhs_ru = rhs_rl + (lhs_ru - lhs_rl);
        long rhs_cl = lhs_cl - ixrange.colStart + 1;
        long rhs_cu = rhs_cl + (lhs_cu - lhs_cl);

        int rhs_lrl = UtilFunctions.computeCellInBlock(rhs_rl, brlen);
        int rhs_lru = UtilFunctions.computeCellInBlock(rhs_ru, brlen);
        int rhs_lcl = UtilFunctions.computeCellInBlock(rhs_cl, bclen);
        int rhs_lcu = UtilFunctions.computeCellInBlock(rhs_cu, bclen);

        MatrixBlock slicedRHSBlk =
            mb.sliceOperations(rhs_lrl, rhs_lru, rhs_lcl, rhs_lcu, new MatrixBlock());

        int lbrlen = UtilFunctions.computeBlockSize(rlen, leftRowIndex, brlen);
        int lbclen = UtilFunctions.computeBlockSize(clen, leftColIndex, bclen);
        MatrixBlock resultBlock = new MatrixBlock(lbrlen, lbclen, false);
        resultBlock =
            resultBlock.leftIndexingOperations(
                slicedRHSBlk, lhs_lrl, lhs_lru, lhs_lcl, lhs_lcu, null, UpdateType.COPY);
        outlist.add(
            new IndexedMatrixValue(new MatrixIndexes(leftRowIndex, leftColIndex), resultBlock));
      }
    }
  }
  /**
   * @param val
   * @param range
   * @param brlen
   * @param bclen
   * @param outlist
   * @throws DMLRuntimeException
   */
  public static void performSlice(
      IndexedMatrixValue in,
      IndexRange ixrange,
      int brlen,
      int bclen,
      ArrayList<IndexedMatrixValue> outlist)
      throws DMLRuntimeException {
    long cellIndexTopRow = UtilFunctions.computeCellIndex(in.getIndexes().getRowIndex(), brlen, 0);
    long cellIndexBottomRow =
        UtilFunctions.computeCellIndex(
            in.getIndexes().getRowIndex(), brlen, in.getValue().getNumRows() - 1);
    long cellIndexLeftCol =
        UtilFunctions.computeCellIndex(in.getIndexes().getColumnIndex(), bclen, 0);
    long cellIndexRightCol =
        UtilFunctions.computeCellIndex(
            in.getIndexes().getColumnIndex(), bclen, in.getValue().getNumColumns() - 1);

    long cellIndexOverlapTop = Math.max(cellIndexTopRow, ixrange.rowStart);
    long cellIndexOverlapBottom = Math.min(cellIndexBottomRow, ixrange.rowEnd);
    long cellIndexOverlapLeft = Math.max(cellIndexLeftCol, ixrange.colStart);
    long cellIndexOverlapRight = Math.min(cellIndexRightCol, ixrange.colEnd);

    // check if block is outside the indexing range
    if (cellIndexOverlapTop > cellIndexOverlapBottom
        || cellIndexOverlapLeft > cellIndexOverlapRight) {
      return;
    }

    IndexRange tmpRange =
        new IndexRange(
            UtilFunctions.computeCellInBlock(cellIndexOverlapTop, brlen),
            UtilFunctions.computeCellInBlock(cellIndexOverlapBottom, brlen),
            UtilFunctions.computeCellInBlock(cellIndexOverlapLeft, bclen),
            UtilFunctions.computeCellInBlock(cellIndexOverlapRight, bclen));

    int rowCut = UtilFunctions.computeCellInBlock(ixrange.rowStart, brlen);
    int colCut = UtilFunctions.computeCellInBlock(ixrange.colStart, bclen);

    int rowsInLastBlock = (int) ((ixrange.rowEnd - ixrange.rowStart + 1) % brlen);
    if (rowsInLastBlock == 0) rowsInLastBlock = brlen;
    int colsInLastBlock = (int) ((ixrange.colEnd - ixrange.colStart + 1) % bclen);
    if (colsInLastBlock == 0) colsInLastBlock = bclen;

    long resultBlockIndexTop =
        UtilFunctions.computeBlockIndex(cellIndexOverlapTop - ixrange.rowStart + 1, brlen);
    long resultBlockIndexBottom =
        UtilFunctions.computeBlockIndex(cellIndexOverlapBottom - ixrange.rowStart + 1, brlen);
    long resultBlockIndexLeft =
        UtilFunctions.computeBlockIndex(cellIndexOverlapLeft - ixrange.colStart + 1, bclen);
    long resultBlockIndexRight =
        UtilFunctions.computeBlockIndex(cellIndexOverlapRight - ixrange.colStart + 1, bclen);

    int boundaryRlen = brlen;
    int boundaryClen = bclen;
    long finalBlockIndexBottom =
        UtilFunctions.computeBlockIndex(ixrange.rowEnd - ixrange.rowStart + 1, brlen);
    long finalBlockIndexRight =
        UtilFunctions.computeBlockIndex(ixrange.colEnd - ixrange.colStart + 1, bclen);
    if (resultBlockIndexBottom == finalBlockIndexBottom) boundaryRlen = rowsInLastBlock;
    if (resultBlockIndexRight == finalBlockIndexRight) boundaryClen = colsInLastBlock;

    // allocate space for the output value
    for (long r = resultBlockIndexTop; r <= resultBlockIndexBottom; r++)
      for (long c = resultBlockIndexLeft; c <= resultBlockIndexRight; c++) {
        IndexedMatrixValue out = new IndexedMatrixValue(new MatrixIndexes(), new MatrixBlock());
        out.getIndexes().setIndexes(r, c);
        outlist.add(out);
      }

    // execute actual slice operation
    in.getValue()
        .sliceOperations(
            outlist, tmpRange, rowCut, colCut, brlen, bclen, boundaryRlen, boundaryClen);
  }