[Rcpp-commits] r4019 - in pkg/Rcpp: inst/include inst/include/Rcpp inst/include/Rcpp/internal inst/include/Rcpp/sugar inst/include/Rcpp/sugar/functions inst/include/Rcpp/vector src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Nov 22 17:11:21 CET 2012
Author: romain
Date: 2012-11-22 17:11:21 +0100 (Thu, 22 Nov 2012)
New Revision: 4019
Modified:
pkg/Rcpp/inst/include/Rcpp/as.h
pkg/Rcpp/inst/include/Rcpp/internal/export.h
pkg/Rcpp/inst/include/Rcpp/internal/r_vector.h
pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
pkg/Rcpp/inst/include/Rcpp/sugar/sets.h
pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
pkg/Rcpp/inst/include/Rcpp/vector/traits.h
pkg/Rcpp/inst/include/RcppCommon.h
pkg/Rcpp/src/RcppCommon.cpp
Log:
Vector::sort for inplace sorting of elements in a vector
Modified: pkg/Rcpp/inst/include/Rcpp/as.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/as.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/as.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -31,7 +31,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
SEXP y = PROTECT( r_cast<RTYPE>(x) );
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
- T res = caster<STORAGE,T>( *r_vector_start<RTYPE,STORAGE>( y ) ) ;
+ T res = caster<STORAGE,T>( *r_vector_start<RTYPE>( y ) ) ;
UNPROTECT(1) ;
return res ;
}
Modified: pkg/Rcpp/inst/include/Rcpp/internal/export.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/export.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/internal/export.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -3,7 +3,7 @@
//
// export.h: Rcpp R/C++ interface class library --
//
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -33,7 +33,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE,STORAGE>(y) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
std::copy( start, start + ::Rf_length(y), first ) ;
UNPROTECT(1) ;
}
@@ -43,7 +43,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE,STORAGE>(y) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
std::transform( start, start + ::Rf_length(y) , first, caster<STORAGE,value_type> ) ;
UNPROTECT(1) ;
}
@@ -83,7 +83,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE,STORAGE>(y) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
R_len_t size = ::Rf_length(y) ;
for( R_len_t i=0; i<size; i++){
res[i] = start[i] ;
@@ -96,7 +96,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE,STORAGE>(y) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
R_len_t size = ::Rf_length(y) ;
for( R_len_t i=0; i<size; i++){
res[i] = caster<STORAGE,value_type>(start[i]) ;
Modified: pkg/Rcpp/inst/include/Rcpp/internal/r_vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/r_vector.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/internal/r_vector.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -3,7 +3,7 @@
//
// r_vector.h: Rcpp R/C++ interface class library -- information about R vectors
//
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -26,13 +26,15 @@
namespace Rcpp{
namespace internal{
-template<int RTYPE, typename CTYPE> CTYPE* r_vector_start(SEXP x){ return static_cast<CTYPE*>(0) ; }
-template<> int* r_vector_start<INTSXP,int>(SEXP x) ;
-template<> int* r_vector_start<LGLSXP,int>(SEXP x) ;
-template<> double* r_vector_start<REALSXP,double>(SEXP x) ;
-template<> Rbyte* r_vector_start<RAWSXP,Rbyte>(SEXP x) ;
-template<> Rcomplex* r_vector_start<CPLXSXP,Rcomplex>(SEXP x) ;
+template<int RTYPE>
+typename Rcpp::traits::storage_type<RTYPE>::type* r_vector_start(SEXP x){ return get_vector_ptr(x) ; }
+template<> int* r_vector_start<INTSXP>(SEXP x) ;
+template<> int* r_vector_start<LGLSXP>(SEXP x) ;
+template<> double* r_vector_start<REALSXP>(SEXP x) ;
+template<> Rbyte* r_vector_start<RAWSXP>(SEXP x) ;
+template<> Rcomplex* r_vector_start<CPLXSXP>(SEXP x) ;
+
/**
* The value 0 statically casted to the appropriate type for
* the given SEXP type
@@ -50,7 +52,7 @@
*/
template<int RTYPE> void r_init_vector(SEXP x){
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
- CTYPE* start=r_vector_start<RTYPE,CTYPE>(x) ;
+ CTYPE* start=r_vector_start<RTYPE>(x) ;
std::fill( start, start + Rf_length(x), get_zero<RTYPE,CTYPE>() ) ;
}
/**
Modified: pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/wrap.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/internal/wrap.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -58,8 +58,7 @@
size_t size = std::distance( first, last ) ;
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
- std::transform( first, last,
- r_vector_start< RTYPE, typename ::Rcpp::traits::storage_type<RTYPE>::type >(x),
+ std::transform( first, last, r_vector_start<RTYPE>(x),
caster< T, typename ::Rcpp::traits::storage_type<RTYPE>::type >
) ;
UNPROTECT(1) ;
@@ -74,7 +73,7 @@
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
int __trip_count = size >> 2 ;
- STORAGE* start = r_vector_start<RTYPE,STORAGE>(x) ;
+ STORAGE* start = r_vector_start<RTYPE>(x) ;
int i = 0 ;
for ( ; __trip_count > 0 ; --__trip_count) {
start[i] = first[i] ; i++ ;
@@ -103,7 +102,7 @@
size_t size = std::distance( first, last ) ;
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
- std::copy( first, last, r_vector_start<RTYPE, typename ::Rcpp::traits::storage_type<RTYPE>::type >(x) ) ;
+ std::copy( first, last, r_vector_start<RTYPE>(x) ) ;
UNPROTECT(1) ;
return wrap_extra_steps<T>( x ) ;
}
@@ -192,7 +191,7 @@
SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
SEXP names = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
- CTYPE* start = r_vector_start<RTYPE,CTYPE>(x) ;
+ CTYPE* start = r_vector_start<RTYPE>(x) ;
size_t i =0;
std::string buf ;
for( ; i<size; i++, ++first){
@@ -221,7 +220,7 @@
SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
SEXP names = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
- CTYPE* start = r_vector_start<RTYPE,CTYPE>(x) ;
+ CTYPE* start = r_vector_start<RTYPE>(x) ;
size_t i =0;
std::string buf ;
for( ; i<size; i++, ++first){
@@ -346,7 +345,7 @@
inline SEXP primitive_wrap__impl__cast( const T& object, ::Rcpp::traits::false_type ){
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
SEXP x = PROTECT( Rf_allocVector( RTYPE, 1 ) );
- r_vector_start<RTYPE, typename ::Rcpp::traits::storage_type<RTYPE>::type >(x)[0] = object ;
+ r_vector_start<RTYPE>(x)[0] = object ;
UNPROTECT(1);
return x;
}
@@ -359,7 +358,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE_TYPE ;
SEXP x = PROTECT( Rf_allocVector( RTYPE, 1 ) );
- r_vector_start<RTYPE,STORAGE_TYPE>(x)[0] = caster<T,STORAGE_TYPE>(object) ;
+ r_vector_start<RTYPE>(x)[0] = caster<T,STORAGE_TYPE>(object) ;
UNPROTECT(1);
return x;
}
@@ -482,7 +481,7 @@
SEXP res = PROTECT( Rf_allocVector( RTYPE, nr*nc ) );
int k=0 ;
- STORAGE* p = r_vector_start< RTYPE, STORAGE >(res) ;
+ STORAGE* p = r_vector_start< RTYPE>(res) ;
for( int j=0; j<nc; j++)
for( int i=0; i<nr; i++, k++)
p[k] = object(i,j) ;
@@ -568,7 +567,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<elem_type>::rtype ;
SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
- CTYPE* start = r_vector_start<RTYPE,CTYPE>(x) ;
+ CTYPE* start = r_vector_start<RTYPE>(x) ;
for( int i=0; i<size; i++){
start[i] = object.get(i) ;
}
@@ -583,7 +582,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<elem_type>::rtype ;
SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
- CTYPE* start = r_vector_start<RTYPE,CTYPE>(x) ;
+ CTYPE* start = r_vector_start<RTYPE>(x) ;
for( int i=0; i<size; i++){
start[i] = caster<elem_type,CTYPE>( object.get(i) );
}
@@ -745,7 +744,7 @@
inline SEXP primitive_rowmajor_wrap__dispatch( InputIterator first, int nrow, int ncol, ::Rcpp::traits::false_type ){
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
SEXP out = PROTECT( ::Rf_allocVector( RTYPE, nrow * ncol ) );
- value_type* ptr = r_vector_start<RTYPE,value_type>( out );
+ value_type* ptr = r_vector_start<RTYPE>( out );
int i=0, j=0 ;
for( j=0; j<ncol; j++){
for( i=0; i<nrow; i++, ++first ){
@@ -764,7 +763,7 @@
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
SEXP out = PROTECT( ::Rf_allocVector( RTYPE, nrow * ncol ) );
- STORAGE* ptr = r_vector_start<RTYPE,STORAGE>( out );
+ STORAGE* ptr = r_vector_start<RTYPE>( out );
int i=0, j=0 ;
for( j=0; j<ncol; j++){
for( i=0; i<nrow; i++, ++first ){
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -70,8 +70,7 @@
template <typename T>
IntegerVector match( const T& obj ){
- IntegerVector out( obj.begin(), obj.end(), Finder(hash) ) ;
- return out ;
+ return IntegerVector( obj.begin(), obj.end(), Finder(hash) ) ;
}
private:
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -36,9 +36,7 @@
return Vector<RTYPE>( set.begin(), set.end() ) ;
}
Vector<RTYPE> get_sorted( ) {
- Vector<RTYPE> out( set.begin(), set.end() ) ;
- std::sort( out.begin(), out.end() ) ;
- return out ;
+ return Vector<RTYPE>( set.begin(), set.end() ).sort() ;
}
private:
@@ -64,10 +62,7 @@
return CharacterVector( set.begin(), set.end() ) ;
}
CharacterVector get_sorted( ) {
- CharacterVector out( set.begin(), set.end() ) ;
- SEXP* p_out = get_string_ptr(out) ;
- std::sort( p_out, p_out + set.size(), StringCompare() );
- return out ;
+ return CharacterVector( set.begin(), set.end() ).sort() ;
}
private:
@@ -86,19 +81,11 @@
}
CharacterVector get( ) {
- CharacterVector out(set.size()) ;
- std::copy( set.begin(), set.end(), get_string_ptr(out) ) ;
- return out ;
+ return CharacterVector( set.begin(), set.end() ) ;
}
CharacterVector get_sorted( ) {
- int n = set.size() ;
- CharacterVector out(n) ;
- SEXP* p_out = get_string_ptr(out) ;
- std::copy( set.begin(), set.end(), p_out ) ;
- std::sort( p_out, p_out+n, StringCompare() ) ;
-
- return out ;
+ return CharacterVector( set.begin(), set.end() ).sort() ;
}
private:
@@ -106,13 +93,12 @@
} ;
-template <typename SET>
+template <typename SET, typename STORAGE>
class InSet {
public:
InSet( const SET& hash_ ) : hash(hash_), end(hash_.end()){}
- template <typename T>
- inline int operator()(T value){
+ inline int operator()(STORAGE value){
return hash.find(value) != end ;
}
@@ -128,18 +114,12 @@
template <typename T>
LogicalVector get( const T& x) const {
- int n = x.size() ;
- LogicalVector out = no_init(n) ;
- std::transform(
- get_const_begin(x), get_const_end(x),
- out.begin(),
- InSet<SET>(hash)
- ) ;
- return out ;
+ return LogicalVector( x.begin(), x.end(), InSet<SET,STORAGE>(hash) ) ;
}
private:
- typedef typename RCPP_UNORDERED_SET< typename traits::storage_type<RTYPE>::type > SET ;
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ typedef typename RCPP_UNORDERED_SET<STORAGE> SET ;
SET hash ;
} ;
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sets.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sets.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sets.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -33,17 +33,4 @@
#define RCPP_UNORDERED_MAP std::map
#endif
-namespace Rcpp{
-namespace sugar{
-
-class StringCompare {
-public:
- inline bool operator()( SEXP x, SEXP y){
- return strcmp( char_nocheck(x), char_nocheck(y) ) < 0 ;
- }
-} ;
-
-} // sugar
-} // Rcpp
-
#endif
Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -416,6 +416,15 @@
return NameProxy( *this, name ) ;
}
+ Vector& sort(){
+ std::sort(
+ internal::r_vector_start<RTYPE>(m_sexp),
+ internal::r_vector_start<RTYPE>(m_sexp) + size(),
+ typename traits::comparator_type<RTYPE>::type()
+ ) ;
+ return *this ;
+ }
+
template <typename InputIterator>
void assign( InputIterator first, InputIterator last){
/* FIXME: we can do better than this r_cast to avoid
Modified: pkg/Rcpp/inst/include/Rcpp/vector/traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/traits.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/Rcpp/vector/traits.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -36,7 +36,7 @@
r_vector_cache() : start(0){} ;
void update( const VECTOR& v ) {
- start = ::Rcpp::internal::r_vector_start<RTYPE,storage_type>(v.asSexp()) ;
+ start = ::Rcpp::internal::r_vector_start<RTYPE>(v.asSexp()) ;
RCPP_DEBUG_3( " cache<%d>::update( <%p> ), start = <%p>", RTYPE, reinterpret_cast<void*>(v.asSexp()), reinterpret_cast<void*>(start) ) ;
}
inline iterator get() const { return start; }
Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/inst/include/RcppCommon.h 2012-11-22 16:11:21 UTC (rev 4019)
@@ -298,6 +298,7 @@
#include <Rcpp/traits/storage_type.h>
#include <Rcpp/traits/r_sexptype_traits.h>
#include <Rcpp/traits/storage_type.h>
+#include <Rcpp/traits/comparator_type.h>
#include <Rcpp/traits/r_type_traits.h>
#include <Rcpp/traits/un_pointer.h>
#include <Rcpp/traits/is_pointer.h>
Modified: pkg/Rcpp/src/RcppCommon.cpp
===================================================================
--- pkg/Rcpp/src/RcppCommon.cpp 2012-11-22 14:10:49 UTC (rev 4018)
+++ pkg/Rcpp/src/RcppCommon.cpp 2012-11-22 16:11:21 UTC (rev 4019)
@@ -3,7 +3,7 @@
// RcppCommon.cpp: R/C++ interface class library -- common functions
//
// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -124,11 +124,11 @@
namespace Rcpp{
namespace internal{
- template<> int* r_vector_start<INTSXP,int>(SEXP x){ return INTEGER(x) ; }
- template<> int* r_vector_start<LGLSXP,int>(SEXP x){ return LOGICAL(x) ; }
- template<> double* r_vector_start<REALSXP,double>(SEXP x){ return REAL(x) ; }
- template<> Rbyte* r_vector_start<RAWSXP,Rbyte>(SEXP x){ return RAW(x) ; }
- template<> Rcomplex* r_vector_start<CPLXSXP,Rcomplex>(SEXP x){ return COMPLEX(x) ; }
+ template<> int* r_vector_start<INTSXP>(SEXP x){ return INTEGER(x) ; }
+ template<> int* r_vector_start<LGLSXP>(SEXP x){ return LOGICAL(x) ; }
+ template<> double* r_vector_start<REALSXP>(SEXP x){ return REAL(x) ; }
+ template<> Rbyte* r_vector_start<RAWSXP>(SEXP x){ return RAW(x) ; }
+ template<> Rcomplex* r_vector_start<CPLXSXP>(SEXP x){ return COMPLEX(x) ; }
template<> void r_init_vector<VECSXP>(SEXP x){}
template<> void r_init_vector<EXPRSXP>(SEXP x){}
More information about the Rcpp-commits
mailing list