[Rcpp-commits] r3983 - in pkg/Rcpp/inst: . include/Rcpp/sugar/functions
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Nov 16 13:32:22 CET 2012
Author: romain
Date: 2012-11-16 13:32:21 +0100 (Fri, 16 Nov 2012)
New Revision: 3983
Modified:
pkg/Rcpp/inst/NEWS.Rd
pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h
Log:
setequal, ... and updated NEWS.Rd
Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd 2012-11-16 11:29:50 UTC (rev 3982)
+++ pkg/Rcpp/inst/NEWS.Rd 2012-11-16 12:32:21 UTC (rev 3983)
@@ -2,6 +2,28 @@
\title{News for Package 'Rcpp'}
\newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
+\section{Changes in Rcpp version 0.10.1 (future)}{
+ \itemize{
+ \item Changes in Rcpp sugar:
+ \itemize{
+ \item New functions: \code{setdiff}, \code{union_}, \code{intersect}
+ \code{setequal}, \code{in}, \code{min}, \code{max}, \code{range},
+ \code{match}
+ }
+ \item Changes in Rcpp API:
+ \itemize{
+ \item The \code{Vector} template class (hence \code{NumericVector}
+ ...) get the \code{is_na} and the \code{get_na} static methods.
+ \item New helper class \code{no_init} that can be used to
+ create a vector without initializing its data, e.g. :
+ \code{ IntegerVector out = no_init(n) ; }
+ }
+ \item Changes in R code :
+ \itemize{
+ \item New function \code{areMacrosDefined}
+ }
+ }
+}
\section{Changes in Rcpp version 0.10.0 (2012-11-13)}{
\itemize{
\item Support for C++11 style attributes (embedded in comments) to enable
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h 2012-11-16 11:29:50 UTC (rev 3982)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h 2012-11-16 12:32:21 UTC (rev 3983)
@@ -66,6 +66,37 @@
SET rhs_set ;
} ;
+
+ template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+ class SetEqual {
+ public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ SetEqual( const LHS_T& lhs, const RHS_T& rhs) :
+ lhs_set( get_const_begin(lhs), get_const_end(lhs) ),
+ rhs_set( get_const_begin(rhs), get_const_end(rhs) )
+ {
+
+ std::for_each( rhs_set.begin(), rhs_set.end(), RemoveFromSet<SET>(lhs_set) ) ;
+ }
+
+ bool get() const {
+ if( lhs_set.size() != rhs_set.size() ) return false ;
+
+ ITERATOR it = lhs_set.begin(), end = lhs_set.end(), rhs_end = rhs_set.end() ;
+ for( ; it != end; ){
+ if( rhs_set.find(*it++) == rhs_end ) return false ;
+ }
+ return true ;
+ }
+
+ private:
+ typedef RCPP_UNORDERED_SET<STORAGE> SET ;
+ typedef typename SET::const_iterator ITERATOR ;
+ SET lhs_set ;
+ SET rhs_set ;
+
+ } ;
template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
class Intersect {
@@ -100,7 +131,32 @@
} ;
+ template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+ class Union {
+ public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ Union( const LHS_T& lhs, const RHS_T& rhs) :
+ result( get_const_begin(lhs), get_const_end(lhs) )
+ {
+ result.insert( get_const_begin(rhs), get_const_end(rhs) ) ;
+ }
+
+ Vector<RTYPE> get() const {
+ int n = result.size() ;
+ Vector<RTYPE> out = no_init(n) ;
+ std::copy( result.begin(), result.end(), get_const_begin(out) ) ;
+ return out ;
+ }
+
+ private:
+ typedef RCPP_UNORDERED_SET<STORAGE> SET ;
+ typedef typename SET::const_iterator ITERATOR ;
+ SET result ;
+
+ } ;
+
} // sugar
template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
@@ -109,10 +165,20 @@
}
template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+inline bool setequal( const VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, const VectorBase<RTYPE,RHS_NA,RHS_T>& rhs ){
+ return sugar::SetEqual<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs.get_ref(), rhs.get_ref() ).get() ;
+}
+
+template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
inline Vector<RTYPE> intersect( const VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, const VectorBase<RTYPE,RHS_NA,RHS_T>& rhs ){
return sugar::Intersect<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs.get_ref(), rhs.get_ref() ).get() ;
}
+// we cannot use "union" because it is a keyword
+template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+inline Vector<RTYPE> union_( const VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, const VectorBase<RTYPE,RHS_NA,RHS_T>& rhs ){
+ return sugar::Union<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs.get_ref(), rhs.get_ref() ).get() ;
+}
More information about the Rcpp-commits
mailing list