[Rcpp-devel] [Rcpp-commits] r282 - in pkg: inst src src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 5 17:50:36 CET 2010
Author: romain
Date: 2010-01-05 17:50:36 +0100 (Tue, 05 Jan 2010)
New Revision: 282
Added:
pkg/src/RawVector.cpp
pkg/src/Rcpp/RawVector.h
Modified:
pkg/inst/ChangeLog
pkg/src/IntegerVector.cpp
pkg/src/NumericVector.cpp
pkg/src/Rcpp.h
pkg/src/Rcpp/IntegerVector.h
pkg/src/Rcpp/NumericVector.h
Log:
+ Rcpp::RawVector
Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog 2010-01-05 15:24:52 UTC (rev 281)
+++ pkg/inst/ChangeLog 2010-01-05 16:50:36 UTC (rev 282)
@@ -9,12 +9,17 @@
they can be used in STL algorithms
* src/IntegerVector.cpp : implementation
* inst/unitTests/runit.IntegerVector.R: unit tests
-
+
* src/Rcpp/NumericVector.h : same as above, but for numeric
vectors (REALSXP)
* src/NumericVector.cpp : implementation
* inst/unitTests/runit.NumericVector.R: unit tests
-
+
+ * src/Rcpp/RawVector.h : same as above, but for numeric
+ vectors (RAWSXP)
+ * src/RawVector.cpp : implementation
+ * inst/unitTests/runit.RawVector.R: unit tests
+
* src/RcppCommon.h: improve the conditional compiling logic
with macros HAS_VARIADIC_TEMPLATES and HAS_INIT_LISTS instead
of CXX0X. This ensures the package can be compiled with
Modified: pkg/src/IntegerVector.cpp
===================================================================
--- pkg/src/IntegerVector.cpp 2010-01-05 15:24:52 UTC (rev 281)
+++ pkg/src/IntegerVector.cpp 2010-01-05 16:50:36 UTC (rev 282)
@@ -22,14 +22,22 @@
#include <RcppCommon.h>
#include <Rcpp/RObject.h>
#include <Rcpp/IntegerVector.h>
+#include <algorithm>
namespace Rcpp{
IntegerVector::IntegerVector(SEXP x) throw(not_compatible) : RObject() {
- if( TYPEOF( x ) == INTSXP ){
- setSEXP( x ) ;
- } else {
- throw not_compatible( "cannot convert to intrger vector" ) ;
+ switch( TYPEOF( x ) ){
+ case INTSXP:
+ setSEXP( x ) ;
+ break ;
+ case REALSXP:
+ case LGLSXP:
+ case RAWSXP:
+ setSEXP( Rf_coerceVector( x, INTSXP) ) ;
+ break ;
+ default:
+ throw not_compatible( "cannot convert to intrger vector" ) ;
}
}
Modified: pkg/src/NumericVector.cpp
===================================================================
--- pkg/src/NumericVector.cpp 2010-01-05 15:24:52 UTC (rev 281)
+++ pkg/src/NumericVector.cpp 2010-01-05 16:50:36 UTC (rev 282)
@@ -26,10 +26,17 @@
namespace Rcpp{
NumericVector::NumericVector(SEXP x) throw(not_compatible) : RObject() {
- if( TYPEOF( x ) == REALSXP ){
- setSEXP( x ) ;
- } else {
- throw not_compatible( "cannot convert to intrger vector" ) ;
+ switch( TYPEOF( x ) ){
+ case REALSXP:
+ setSEXP( x ) ;
+ break ;
+ case INTSXP:
+ case LGLSXP:
+ case RAWSXP:
+ setSEXP( Rf_coerceVector( x, REALSXP) ) ;
+ break ;
+ default:
+ throw not_compatible( "cannot convert to numeric vector" ) ;
}
}
Added: pkg/src/RawVector.cpp
===================================================================
--- pkg/src/RawVector.cpp (rev 0)
+++ pkg/src/RawVector.cpp 2010-01-05 16:50:36 UTC (rev 282)
@@ -0,0 +1,75 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RawVector.h: Rcpp R/C++ interface class library -- integer vectors
+//
+// Copyright (C) 2010 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/>.
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+#include <Rcpp/RawVector.h>
+#include <algorithm>
+
+namespace Rcpp{
+
+ RawVector::RawVector(SEXP x) throw(not_compatible) : RObject() {
+ switch( TYPEOF( x ) ){
+ case RAWSXP:
+ setSEXP( x ) ;
+ break ;
+ case INTSXP:
+ case REALSXP:
+ case LGLSXP:
+ setSEXP( Rf_coerceVector( x, RAWSXP) ) ;
+ break ;
+ default:
+ throw not_compatible( "cannot convert to intrger vector" ) ;
+ }
+ }
+
+ RawVector::RawVector(int size) : RObject() {
+ setSEXP( Rf_allocVector(RAWSXP, size) ) ;
+ }
+
+#ifdef HAS_INIT_LISTS
+ RawVector::RawVector( std::initializer_list<int> list ) {
+ SEXP x = PROTECT( Rf_allocVector( RAWSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), RAW(x) );
+ setSEXP(x) ;
+ UNPROTECT( 1 ); /* x */
+ }
+ RawVector::RawVector( std::initializer_list<Rbyte> list ) {
+ /* FIXME: we need to take care of coercion, so
+ transform is probably better */
+ SEXP x = PROTECT( Rf_allocVector( RAWSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), RAW(x) );
+ setSEXP(x) ;
+ UNPROTECT( 1 ); /* x */
+ }
+#endif
+
+Rbyte& RawVector::operator[]( int i ) const {
+ return RAW(m_sexp)[i] ;
+}
+Rbyte* RawVector::begin() const {
+ return RAW(m_sexp) ;
+}
+Rbyte* RawVector::end() const {
+ return RAW(m_sexp) + LENGTH(m_sexp);
+}
+
+} // namespace
Modified: pkg/src/Rcpp/IntegerVector.h
===================================================================
--- pkg/src/Rcpp/IntegerVector.h 2010-01-05 15:24:52 UTC (rev 281)
+++ pkg/src/Rcpp/IntegerVector.h 2010-01-05 16:50:36 UTC (rev 282)
@@ -56,6 +56,9 @@
int& operator[]( int i ) const ;
int* begin() const ;
int* end() const ;
+
+ typedef int* iterator ;
+
} ;
} // namespace
Modified: pkg/src/Rcpp/NumericVector.h
===================================================================
--- pkg/src/Rcpp/NumericVector.h 2010-01-05 15:24:52 UTC (rev 281)
+++ pkg/src/Rcpp/NumericVector.h 2010-01-05 16:50:36 UTC (rev 282)
@@ -57,6 +57,8 @@
double* begin() const ;
double* end() const ;
+ typedef double* iterator ;
+
} ;
} // namespace
Added: pkg/src/Rcpp/RawVector.h
===================================================================
--- pkg/src/Rcpp/RawVector.h (rev 0)
+++ pkg/src/Rcpp/RawVector.h 2010-01-05 16:50:36 UTC (rev 282)
@@ -0,0 +1,66 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RawVector.h: Rcpp R/C++ interface class library -- raw vectors
+//
+// Copyright (C) 2010 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/>.
+
+#ifndef Rcpp_RawVector_h
+#define Rcpp_RawVector_h
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#include <algorithm>
+#endif
+
+namespace Rcpp{
+
+class RawVector : public RObject {
+public:
+
+ RawVector(SEXP x) throw(not_compatible);
+ RawVector( int size) ;
+
+#ifdef HAS_INIT_LISTS
+ RawVector( std::initializer_list<Rbyte> list ) ;
+ RawVector( std::initializer_list<int> list ) ;
+#endif
+
+ /**
+ * the length of the vector, uses Rf_length
+ */
+ inline int length() const { return Rf_length( m_sexp ) ; }
+
+ /**
+ * alias of length
+ */
+ inline int size() const { return Rf_length( m_sexp ) ; }
+
+ Rbyte& operator[]( int i ) const ;
+ Rbyte* begin() const ;
+ Rbyte* end() const ;
+
+ typedef Rbyte* iterator ;
+
+} ;
+
+} // namespace
+
+#endif
Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h 2010-01-05 15:24:52 UTC (rev 281)
+++ pkg/src/Rcpp.h 2010-01-05 16:50:36 UTC (rev 282)
@@ -58,5 +58,6 @@
#include <Rcpp/Function.h>
#include <Rcpp/IntegerVector.h>
#include <Rcpp/NumericVector.h>
+#include <Rcpp/RawVector.h>
#endif
_______________________________________________
Rcpp-commits mailing list
Rcpp-commits at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-commits
More information about the Rcpp-devel
mailing list