00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <RcppVectorView.h>
00024
00025 template <typename T>
00026 RcppVectorView<T>::RcppVectorView(SEXP vec) {
00027 if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
00028 throw std::range_error("RcppVectorView: invalid numeric vector in constructor");
00029 len = Rf_length(vec);
00030 if (Rf_isInteger(vec)) {
00031 v = (T *)(INTEGER(vec));
00032 } else if (Rf_isReal(vec)) {
00033 v = (T *)(REAL(vec));
00034 }
00035 }
00036
00037 template <typename T>
00038 inline int RcppVectorView<T>::size() const {
00039 return len;
00040 }
00041
00042 template <typename T>
00043 inline T RcppVectorView<T>::operator()(int i) const {
00044 if (i < 0 || i >= len) {
00045 std::ostringstream oss;
00046 oss << "RcppVectorView: subscript out of range: " << i;
00047 throw std::range_error(oss.str());
00048 }
00049 return v[i];
00050 }
00051
00052 template class RcppVectorView<int>;
00053 template class RcppVectorView<double>;
00054