#include "matrix_proxy.h"


matrix_proxy::matrix_proxy(matrix_expression& src, const range& rows, const range& cols)
  :  _M(src), _rows(rows), _cols(cols) { }

// cannot do traditional assignment on proxies, since we cannot rebind the source matrix.
// We will only support value-based assignments
matrix_proxy& matrix_proxy::operator=(const matrix_proxy& other) {
  if (this != &other)
    matrix_expression::operator=(other);
  return *this;
}

int matrix_proxy::numRows() const {
  return _rows.size();
}

int matrix_proxy::numColumns() const {
  return _cols.size();
}

// read-only version of indexing operator
double matrix_proxy::operator()(int r, int c) const {
  int actualRow = _rows.start() + r * _rows.stride();
  int actualCol = _cols.start() + c * _cols.stride();
  return _M(actualRow, actualCol);
}

// write-access version of indexing operator
double& matrix_proxy::operator()(int r, int c) {
  int actualRow = _rows.start() + r * _rows.stride();
  int actualCol = _cols.start() + c * _cols.stride();
  return _M(actualRow, actualCol);
}
