00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <RcppMatrixView.h>
00024
00025 template <typename T>
00026 RcppMatrixView<T>::RcppMatrixView(SEXP mat) {
00027 if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
00028 throw std::range_error("RcppMatrixView: invalid numeric matrix in constructor");
00029
00030 SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
00031 d1 = INTEGER(dimAttr)[0];
00032 d2 = INTEGER(dimAttr)[1];
00033 if (Rf_isInteger(mat)) {
00034 a = (T *)(INTEGER(mat));
00035 } else if (Rf_isReal(mat)) {
00036 a = (T *)(REAL(mat));
00037 }
00038 }
00039
00040 template <typename T>
00041 inline int RcppMatrixView<T>::dim1() const {
00042 return d1;
00043 }
00044
00045 template <typename T>
00046 inline int RcppMatrixView<T>::dim2() const {
00047 return d2;
00048 }
00049
00050 template <typename T>
00051 inline int RcppMatrixView<T>::rows() const {
00052 return d1;
00053 }
00054
00055 template <typename T>
00056 inline int RcppMatrixView<T>::cols() const {
00057 return d2;
00058 }
00059
00060 template <typename T>
00061 inline T RcppMatrixView<T>::operator()(int i, int j) const {
00062 if (i < 0 || i >= d1 || j < 0 || j >= d2) {
00063 std::ostringstream oss;
00064 oss << "RcppMatrixView: subscripts out of range: " << i << ", " << j;
00065 throw std::range_error(oss.str());
00066 }
00067 return a[i + d1 * j];
00068 }
00069
00070 template class RcppMatrixView<int>;
00071 template class RcppMatrixView<double>;
00072
00073