[Rcpp-commits] r458 - in pkg: inst src src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 26 09:54:00 CET 2010
Author: romain
Date: 2010-01-26 09:53:59 +0100 (Tue, 26 Jan 2010)
New Revision: 458
Modified:
pkg/inst/ChangeLog
pkg/src/Rcpp/SimpleVector.h
pkg/src/Rcpp/VectorBase.h
pkg/src/VectorBase.cpp
Log:
VectorBase gains an offset method
Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog 2010-01-26 08:23:15 UTC (rev 457)
+++ pkg/inst/ChangeLog 2010-01-26 08:53:59 UTC (rev 458)
@@ -3,6 +3,16 @@
* src/Rcpp/r_cast.h: new template function to handle casts
from one SEXP to another. This is mostly useful internally
+ * src/Rcpp/VectorBase.h: VectorBase gains a offset method
+ that is responsible to calculate the correct offset based
+ on variable number of size_t arguments. currently the number
+ of arguments can be 1 (vector indexing) and 2 (matrix indexing)
+ but arbitrary number of arguments will be added later
+ for arbitrary array-like indexing.
+
+ * src/Rcpp/SimpleVector.h: outsourcing offset calculation
+ to VectorBase::offset
+
2010-01-25 Romain Francois <francoisromain at free.fr>
* src/Rcpp/wrap.h: wrap is back at being a template. The
Modified: pkg/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/src/Rcpp/SimpleVector.h 2010-01-26 08:23:15 UTC (rev 457)
+++ pkg/src/Rcpp/SimpleVector.h 2010-01-26 08:53:59 UTC (rev 458)
@@ -53,19 +53,12 @@
inline CTYPE* begin() const{ return start ; }
inline CTYPE* end() const{ return start+Rf_length(m_sexp); }
- CTYPE& operator()( const size_t& i) throw(RObject::index_out_of_bounds){
- if( i >= static_cast<size_t>(Rf_length(m_sexp)) ) throw RObject::index_out_of_bounds() ;
- return start[i] ;
+ inline CTYPE& operator()( const size_t& i) throw(RObject::index_out_of_bounds){
+ return start[ offset(i) ] ;
}
- CTYPE& operator()( const size_t& i, const size_t& j) throw(VectorBase::not_a_matrix,RObject::index_out_of_bounds){
- /* TODO: factor this code out into a Offset class otr something */
- if( !Rf_isMatrix(m_sexp) ) throw VectorBase::not_a_matrix() ;
- int *dim = INTEGER( Rf_getAttrib( m_sexp, R_DimSymbol ) ) ;
- size_t nrow = static_cast<size_t>(dim[0]) ;
- size_t ncol = static_cast<size_t>(dim[1]) ;
- if( i >= nrow || j >= ncol ) throw RObject::index_out_of_bounds() ;
- return start[ i + nrow*j ] ;
+ inline CTYPE& operator()( const size_t& i, const size_t& j) throw(VectorBase::not_a_matrix,RObject::index_out_of_bounds){
+ return start[ offset(i,j) ] ;
}
protected:
Modified: pkg/src/Rcpp/VectorBase.h
===================================================================
--- pkg/src/Rcpp/VectorBase.h 2010-01-26 08:23:15 UTC (rev 457)
+++ pkg/src/Rcpp/VectorBase.h 2010-01-26 08:53:59 UTC (rev 458)
@@ -24,6 +24,7 @@
#include <RcppCommon.h>
#include <Rcpp/RObject.h>
+#include <Rcpp/r_cast.h>
namespace Rcpp{
@@ -49,6 +50,15 @@
*/
inline int size() const { return ::Rf_length( m_sexp ) ; }
+ /**
+ * offset based on the dimensions of this vector
+ */
+ size_t offset(const size_t& i, const size_t& j) const throw(not_a_matrix,RObject::index_out_of_bounds) ;
+
+ size_t offset(const size_t& i) throw(RObject::index_out_of_bounds) ;
+
+ /* TODO: 3 dimensions, ... n dimensions through variadic templates */
+
} ;
template <int sexptype, typename T> T* get_pointer(SEXP x){ throw std::exception( "not implemented" ) ; return static_cast<T*>(0); }
Modified: pkg/src/VectorBase.cpp
===================================================================
--- pkg/src/VectorBase.cpp 2010-01-26 08:23:15 UTC (rev 457)
+++ pkg/src/VectorBase.cpp 2010-01-26 08:53:59 UTC (rev 458)
@@ -28,6 +28,21 @@
VectorBase::VectorBase() : RObject() {} ;
VectorBase::~VectorBase(){}
+ size_t VectorBase::offset( const size_t& i, const size_t& j) const throw(not_a_matrix,RObject::index_out_of_bounds) {
+ if( !Rf_isMatrix(m_sexp) ) throw VectorBase::not_a_matrix() ;
+ /* we need to extract the dimensions */
+ int *dim = INTEGER( Rf_getAttrib( m_sexp, R_DimSymbol ) ) ;
+ size_t nrow = static_cast<size_t>(dim[0]) ;
+ size_t ncol = static_cast<size_t>(dim[1]) ;
+ if( i >= nrow || j >= ncol ) throw RObject::index_out_of_bounds() ;
+ return i + nrow*j ;
+ }
+
+ size_t VectorBase::offset(const size_t& i) throw(RObject::index_out_of_bounds){
+ if( i >= static_cast<size_t>(Rf_length(m_sexp)) ) throw RObject::index_out_of_bounds() ;
+ return i ;
+ }
+
template<> double* get_pointer<REALSXP,double>(SEXP x){ return REAL(x) ; }
template<> int* get_pointer<INTSXP,int>(SEXP x){ return INTEGER(x) ; }
template<> int* get_pointer<LGLSXP,int>(SEXP x){ return LOGICAL(x) ; }
More information about the Rcpp-commits
mailing list