[Rcpp-commits] r4115 - in pkg/Rcpp: . inst/include/Rcpp/api/meat inst/include/Rcpp/vector src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Dec 8 21:17:45 CET 2012
Author: romain
Date: 2012-12-08 21:17:44 +0100 (Sat, 08 Dec 2012)
New Revision: 4115
Removed:
pkg/Rcpp/src/posixt.cpp
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/api/meat/Matrix.h
pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
pkg/Rcpp/src/Date.cpp
pkg/Rcpp/src/Reference.cpp
Log:
more meat in Matrix
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2012-12-08 14:27:33 UTC (rev 4114)
+++ pkg/Rcpp/ChangeLog 2012-12-08 20:17:44 UTC (rev 4115)
@@ -2,7 +2,9 @@
* src/Timer.cpp: implementation of Timer
* include/Rcpp/Benchmark/Timer.h: internal performance timer, based on
- the code from the microbenchmark package.
+ the code from the microbenchmark package
+ * include/Rcpp/api/meat/Matrix.h: move more in Matrix meat
+ * src/Date.cpp: import the code from posixt.cpp
2012-12-07 JJ Allaire <jj at rstudio.org>
Modified: pkg/Rcpp/inst/include/Rcpp/api/meat/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/api/meat/Matrix.h 2012-12-08 14:27:33 UTC (rev 4114)
+++ pkg/Rcpp/inst/include/Rcpp/api/meat/Matrix.h 2012-12-08 20:17:44 UTC (rev 4115)
@@ -25,6 +25,15 @@
namespace Rcpp{
template <int RTYPE>
+ Matrix<RTYPE>::Matrix(SEXP x) : VECTOR(), nrows(0) {
+ if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
+ SEXP y = r_cast<RTYPE>( x ) ;
+ VECTOR::setSEXP( y );
+ nrows = VECTOR::dims()[0] ;
+ }
+
+
+ template <int RTYPE>
Matrix<RTYPE>::Matrix( const Dimension& dims) : VECTOR(), nrows(dims[0]) {
if( dims.size() != 2 ) throw not_compatible("not a matrix") ;
VECTOR::setSEXP( Rf_allocMatrix( RTYPE, dims[0], dims[1] ) ) ;
@@ -49,6 +58,131 @@
template <int RTYPE>
Matrix<RTYPE>::Matrix( const int& n) : VECTOR( Dimension( n, n ) ), nrows(n) {}
+ template <int RTYPE>
+ Matrix<RTYPE>::Matrix( const Matrix& other) : VECTOR(), nrows(other.nrows) {
+ SEXP x = other.asSexp() ;
+ if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
+ VECTOR::setSEXP( x ) ;
+ }
+
+ template <int RTYPE>
+ template <bool NA, typename MAT>
+ Matrix<RTYPE>::Matrix( const MatrixBase<RTYPE,NA,MAT>& other ) : VECTOR(), nrows(other.nrow()) {
+ int nc = other.ncol() ;
+ RObject::setSEXP( Rf_allocMatrix( RTYPE, nrows, nc ) ) ;
+ import_matrix_expression<NA,MAT>( other, nrows, nc ) ;
+ }
+
+ template <int RTYPE>
+ Matrix<RTYPE>& Matrix<RTYPE>::operator=(const Matrix& other) {
+ SEXP x = other.asSexp() ;
+ if( ! ::Rf_isMatrix(x) ) not_compatible("not a matrix") ;
+ VECTOR::setSEXP( x ) ;
+ nrows = other.nrows ;
+ return *this ;
+ }
+
+ template <int RTYPE>
+ template <typename U>
+ void Matrix<RTYPE>::fill_diag( const U& u){
+ fill_diag__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;
+ }
+
+ template <int RTYPE>
+ template <typename U>
+ Matrix<RTYPE> Matrix<RTYPE>::diag( int size, const U& diag_value ){
+ Matrix res(size,size) ;
+ res.fill_diag( diag_value ) ;
+ return res ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Proxy Matrix<RTYPE>::operator[]( int i ){
+ return static_cast< Vector<RTYPE>* >( this )->operator[]( i ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::const_Proxy Matrix<RTYPE>::operator[]( int i ) const {
+ return static_cast< const Vector<RTYPE>* >( this )->operator[]( i ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Proxy Matrix<RTYPE>::operator()( const size_t& i, const size_t& j) {
+ return static_cast< Vector<RTYPE>* >( this )->operator[]( offset( i, j ) ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::const_Proxy Matrix<RTYPE>::operator()( const size_t& i, const size_t& j) const {
+ return static_cast< const Vector<RTYPE>* >( this )->operator[]( offset( i, j ) ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Row Matrix<RTYPE>::operator()( int i, internal::NamedPlaceHolder ){
+ return Row( *this, i ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Column Matrix<RTYPE>::operator()( internal::NamedPlaceHolder, int i ){
+ return Column( *this, i ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Column Matrix<RTYPE>::operator()( internal::NamedPlaceHolder, int i ) const {
+ return Column( *this, i ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Sub Matrix<RTYPE>::operator()( const Range& row_range, const Range& col_range){
+ return Sub( const_cast<Matrix&>(*this), row_range, col_range ) ;
+ }
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Sub Matrix<RTYPE>::operator()( internal::NamedPlaceHolder, const Range& col_range){
+ return Sub( const_cast<Matrix&>(*this), Range(0,nrow()-1) , col_range ) ;
+ }
+
+ template <int RTYPE>
+ inline typename Matrix<RTYPE>::Sub Matrix<RTYPE>::operator()( const Range& row_range, internal::NamedPlaceHolder ){
+ return Sub( const_cast<Matrix&>(*this), row_range, Range(0,ncol()-1) ) ;
+ }
+
+ template <int RTYPE>
+ template <typename U>
+ void Matrix<RTYPE>::fill_diag__dispatch( traits::false_type, const U& u){
+ SEXP elem = PROTECT( converter_type::get( u ) ) ;
+ int n = Matrix::ncol() ;
+ int offset = n +1 ;
+ iterator it( VECTOR::begin()) ;
+ for( int i=0; i<n; i++){
+ *it = ::Rf_duplicate( elem );
+ it += offset;
+ }
+ UNPROTECT(1); // elem
+ }
+
+ template <int RTYPE>
+ template <typename U>
+ void Matrix<RTYPE>::fill_diag__dispatch( traits::true_type, const U& u){
+ stored_type elem = converter_type::get( u ) ;
+ int n = Matrix::ncol() ;
+ int offset = n + 1 ;
+ iterator it( VECTOR::begin()) ;
+ for( int i=0; i<n; i++){
+ *it = elem ;
+ it += offset;
+ }
+ }
+
+ template <int RTYPE>
+ template <bool NA, typename MAT>
+ void Matrix<RTYPE>::import_matrix_expression( const MatrixBase<RTYPE,NA,MAT>& other, int nr, int nc ){
+ iterator start = VECTOR::begin() ;
+ for( int j=0; j<nc; j++){
+ for( int i=0; i<nr; i++, ++start){
+ *start = other(i,j) ;
+ }
+ }
+ }
+
} // namespace Rcpp
#endif
Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h 2012-12-08 14:27:33 UTC (rev 4114)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h 2012-12-08 20:17:44 UTC (rev 4115)
@@ -26,9 +26,14 @@
template <int RTYPE>
class Matrix : public Vector<RTYPE>, public MatrixBase<RTYPE,true, Matrix<RTYPE> > {
+ int nrows ;
+
public:
struct r_type : traits::integral_constant<int,RTYPE>{} ;
struct can_have_na : traits::true_type{} ;
+ typedef MatrixRow<RTYPE> Row ;
+ typedef MatrixColumn<RTYPE> Column ;
+ typedef SubMatrix<RTYPE> Sub ;
typedef Vector<RTYPE> VECTOR ;
typedef typename VECTOR::iterator iterator ;
@@ -40,158 +45,25 @@
Matrix() : VECTOR() {}
- Matrix(SEXP x) : VECTOR(), nrows(0) {
- if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
- SEXP y = r_cast<RTYPE>( x ) ;
- VECTOR::setSEXP( y );
- nrows = VECTOR::dims()[0] ;
- }
+ Matrix(SEXP x) ;
+ Matrix( const int& n) ;
Matrix( const Dimension& dims) ;
-
Matrix( const int& nrows_, const int& ncols) ;
template <typename Iterator>
Matrix( const int& nrows_, const int& ncols, Iterator start ) ;
- Matrix( const int& n) ;
+ Matrix( const Matrix& other) ;
- Matrix( const Matrix& other) : VECTOR(), nrows(other.nrows) {
- SEXP x = other.asSexp() ;
- if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
- VECTOR::setSEXP( x ) ;
- }
-
- Matrix& operator=(const Matrix& other) {
- SEXP x = other.asSexp() ;
- if( ! ::Rf_isMatrix(x) ) not_compatible("not a matrix") ;
- VECTOR::setSEXP( x ) ;
- nrows = other.nrows ;
- return *this ;
- }
-
template <bool NA, typename MAT>
- Matrix( const MatrixBase<RTYPE,NA,MAT>& other ) : VECTOR(), nrows(other.nrow()) {
- int nc = other.ncol() ;
- RObject::setSEXP( Rf_allocMatrix( RTYPE, nrows, nc ) ) ;
- import_matrix_expression<NA,MAT>( other, nrows, nc ) ;
- }
-
- // defined later
+ Matrix( const MatrixBase<RTYPE,NA,MAT>& other ) ;
+
Matrix( const SubMatrix<RTYPE>& ) ;
+
+ Matrix& operator=(const Matrix& other) ;
Matrix& operator=( const SubMatrix<RTYPE>& ) ;
-private:
-
- template <bool NA, typename MAT>
- void import_matrix_expression( const MatrixBase<RTYPE,NA,MAT>& other, int nr, int nc ){
- iterator start = VECTOR::begin() ;
- for( int j=0; j<nc; j++){
- for( int i=0; i<nr; i++, ++start){
- *start = other(i,j) ;
- }
- }
- }
-
-public:
-
- template <typename U>
- void fill_diag( const U& u){
- fill_diag__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;
- }
-
- template <typename U>
- static Matrix diag( int size, const U& diag_value ){
- Matrix res(size,size) ;
- res.fill_diag( diag_value ) ;
- return res ;
- }
-
- inline Proxy operator[]( int i ){
- return static_cast< Vector<RTYPE>* >( this )->operator[]( i ) ;
- }
-
- inline const_Proxy operator[]( int i ) const {
- return static_cast< const Vector<RTYPE>* >( this )->operator[]( i ) ;
- }
-
- inline Proxy operator()( const size_t& i, const size_t& j) {
- return static_cast< Vector<RTYPE>* >( this )->operator[](
- offset( i, j )
- ) ;
- }
-
- inline const_Proxy operator()( const size_t& i, const size_t& j) const {
- return static_cast< const Vector<RTYPE>* >( this )->operator[](
- offset( i, j )
- ) ;
- }
-
- typedef MatrixRow<RTYPE> Row ;
- typedef MatrixColumn<RTYPE> Column ;
- typedef SubMatrix<RTYPE> Sub ;
-
- inline Row operator()( int i, internal::NamedPlaceHolder ){
- return Row( *this, i ) ;
- }
-
- inline Column operator()( internal::NamedPlaceHolder, int i ){
- return Column( *this, i ) ;
- }
- inline Column operator()( internal::NamedPlaceHolder, int i ) const {
- return Column( *this, i ) ;
- }
-
- inline Sub operator()( const Range& row_range, const Range& col_range){
- return Sub( const_cast<Matrix&>(*this), row_range, col_range ) ;
- }
-
- inline Sub operator()( internal::NamedPlaceHolder, const Range& col_range){
- return Sub( const_cast<Matrix&>(*this), Range(0,nrow()-1) , col_range ) ;
- }
-
- inline Sub operator()( const Range& row_range, internal::NamedPlaceHolder ){
- return Sub( const_cast<Matrix&>(*this), row_range, Range(0,ncol()-1) ) ;
- }
-
-
-private:
-
- inline int offset( int i, int j) const {
- return i + nrows * j ;
- }
-
- virtual void update(){
- RCPP_DEBUG_1( "%s::update", DEMANGLE(Matrix) ) ;
- VECTOR::update_vector() ;
- }
-
- template <typename U>
- void fill_diag__dispatch( traits::false_type, const U& u){
- SEXP elem = PROTECT( converter_type::get( u ) ) ;
- int n = Matrix::ncol() ;
- int offset = n +1 ;
- iterator it( VECTOR::begin()) ;
- for( int i=0; i<n; i++){
- *it = ::Rf_duplicate( elem );
- it += offset;
- }
- UNPROTECT(1); // elem
- }
-
- template <typename U>
- void fill_diag__dispatch( traits::true_type, const U& u){
- stored_type elem = converter_type::get( u ) ;
- int n = Matrix::ncol() ;
- int offset = n + 1 ;
- iterator it( VECTOR::begin()) ;
- for( int i=0; i<n; i++){
- *it = elem ;
- it += offset;
- }
- }
-
-public:
inline int ncol() const {
return VECTOR::dims()[1];
}
@@ -212,10 +84,43 @@
inline const_iterator end() const{ return VECTOR::end() ; }
inline iterator begin() { return VECTOR::begin() ; }
inline iterator end() { return VECTOR::end() ; }
+
+ template <typename U> void fill_diag( const U& u) ;
+
+ template <typename U> static Matrix diag( int size, const U& diag_value ) ;
+ inline Proxy operator[]( int i ) ;
+ inline const_Proxy operator[]( int i ) const ;
+
+ inline Proxy operator()( const size_t& i, const size_t& j) ;
+ inline const_Proxy operator()( const size_t& i, const size_t& j) const ;
+
+ inline Row operator()( int i, internal::NamedPlaceHolder ) ;
+ inline Column operator()( internal::NamedPlaceHolder, int i ) ;
+ inline Column operator()( internal::NamedPlaceHolder, int i ) const ;
+ inline Sub operator()( const Range& row_range, const Range& col_range) ;
+ inline Sub operator()( internal::NamedPlaceHolder, const Range& col_range) ;
+ inline Sub operator()( const Range& row_range, internal::NamedPlaceHolder ) ;
+
+
private:
- int nrows ;
+
+ inline int offset( int i, int j) const { return i + nrows * j ; }
+
+ virtual void update(){
+ RCPP_DEBUG_1( "%s::update", DEMANGLE(Matrix) ) ;
+ VECTOR::update_vector() ;
+ }
+ template <typename U>
+ void fill_diag__dispatch( traits::false_type, const U& u) ;
+
+ template <typename U>
+ void fill_diag__dispatch( traits::true_type, const U& u) ;
+
+ template <bool NA, typename MAT>
+ void import_matrix_expression( const MatrixBase<RTYPE,NA,MAT>& other, int nr, int nc ) ;
+
} ;
Modified: pkg/Rcpp/src/Date.cpp
===================================================================
--- pkg/Rcpp/src/Date.cpp 2012-12-08 14:27:33 UTC (rev 4114)
+++ pkg/Rcpp/src/Date.cpp 2012-12-08 20:17:44 UTC (rev 4115)
@@ -28,7 +28,8 @@
#include <Rcpp/Date.h>
#include <Rcpp/Function.h>
#include <time.h> // for gmtime
-
+#include <Rcpp/internal/posixt.h>
+
namespace Rcpp {
static struct tm * gmtime_(const time_t * const timep); // see below
@@ -168,6 +169,32 @@
bool operator<=(const Date &d1, const Date& d2) { return d1.m_d <= d2.m_d; }
bool operator!=(const Date &d1, const Date& d2) { return d1.m_d != d2.m_d; }
+ namespace internal{
+
+ SEXP getPosixClasses(){
+ SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP,2));
+ SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXct"));
+ SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXt"));
+ UNPROTECT(1) ;
+ return datetimeclass ;
+ }
+
+ SEXP new_posixt_object( double d){
+ SEXP x = PROTECT( Rf_ScalarReal( d ) ) ;
+ Rf_setAttrib(x, R_ClassSymbol, getPosixClasses() );
+ UNPROTECT(1);
+ return x ;
+ }
+
+ SEXP new_date_object( double d){
+ SEXP x = PROTECT(Rf_ScalarReal( d ) ) ;
+ Rf_setAttrib(x, R_ClassSymbol, Rf_mkString("Date"));
+ UNPROTECT(1);
+ return x;
+ }
+
+ }
+
template <> SEXP wrap(const Date &date) {
return internal::new_date_object( date.getDate() ) ;
}
Modified: pkg/Rcpp/src/Reference.cpp
===================================================================
--- pkg/Rcpp/src/Reference.cpp 2012-12-08 14:27:33 UTC (rev 4114)
+++ pkg/Rcpp/src/Reference.cpp 2012-12-08 20:17:44 UTC (rev 4115)
@@ -2,7 +2,7 @@
//
// S4.cpp: Rcpp R/C++ interface class library -- S4 objects
//
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -21,7 +21,6 @@
#include <Rcpp/Reference.h>
#include <Rcpp/exceptions.h>
-#include <Rcpp/Vector.h>
namespace Rcpp {
Deleted: pkg/Rcpp/src/posixt.cpp
===================================================================
--- pkg/Rcpp/src/posixt.cpp 2012-12-08 14:27:33 UTC (rev 4114)
+++ pkg/Rcpp/src/posixt.cpp 2012-12-08 20:17:44 UTC (rev 4115)
@@ -1,54 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// posixt.cpp: Rcpp R/C++ interface class library -- Date type
-//
-// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
-//
-// This file is part of Rcpp.
-//
-// Rcpp is free software: you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 2 of the License, or
-// (at your option) any later version.
-//
-// Rcpp is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
-
-#define USE_RINTERNALS
-#include <Rinternals.h>
-
-#include <Rcpp/internal/posixt.h>
-
-namespace Rcpp{
-namespace internal{
-
-SEXP getPosixClasses(){
- SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP,2));
- SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXct"));
- SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXt"));
- UNPROTECT(1) ;
- return datetimeclass ;
-}
-
-SEXP new_posixt_object( double d){
- SEXP x = PROTECT( Rf_ScalarReal( d ) ) ;
- Rf_setAttrib(x, R_ClassSymbol, getPosixClasses() );
- UNPROTECT(1);
- return x ;
-}
-
-SEXP new_date_object( double d){
- SEXP x = PROTECT(Rf_ScalarReal( d ) ) ;
- Rf_setAttrib(x, R_ClassSymbol, Rf_mkString("Date"));
- UNPROTECT(1);
- return x;
-}
-
-
-}
-}
More information about the Rcpp-commits
mailing list