[Rcpp-commits] r3974 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Nov 15 17:39:43 CET 2012


Author: romain
Date: 2012-11-15 17:39:43 +0100 (Thu, 15 Nov 2012)
New Revision: 3974

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
Log:
sugar %in% 

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-15 13:26:11 UTC (rev 3973)
+++ pkg/Rcpp/ChangeLog	2012-11-15 16:39:43 UTC (rev 3974)
@@ -9,6 +9,8 @@
         in terms of stl algorithms.
         * include/Rcpp/vector/Vector.h: class no_init that can be used to create
         an uninitialized vector. e.g. IntegerVector out = no_init(10) ;
+        * include/Rcpp/sugar/functions/unique.h: sugar version of %in% using 
+        unordered_set
 
 2012-11-14  JJ Allaire <jj at rstudio.org>
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h	2012-11-15 13:26:11 UTC (rev 3973)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h	2012-11-15 16:39:43 UTC (rev 3974)
@@ -106,7 +106,43 @@
    
 } ;
 
+template <typename SET>
+class InSet {
+public:
+    InSet( const SET& hash_ ) : hash(hash_), end(hash_.end()){}
+    
+    template <typename T>
+    inline int operator()(T value){
+        return hash.find(value) != end ;    
+    }
+    
+private:
+    const SET& hash ;
+    typename SET::const_iterator end ;
+} ;
 
+template <int RTYPE, typename TABLE_T>
+class In {
+public:
+    In( const TABLE_T& table) : hash( get_const_begin(table), get_const_end(table) ){}
+    
+    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 ;
+    }
+    
+private:
+    typedef typename RCPP_UNORDERED_SET< typename traits::storage_type<RTYPE>::type > SET ;
+    SET hash ;
+    
+} ;
 
 
 } // sugar
@@ -120,6 +156,11 @@
 	return sugar::Unique<RTYPE,T>( t.get_ref() ).get_sorted() ;
 }
 
+template <int RTYPE, bool NA, typename T, bool RHS_NA, typename RHS_T>
+inline LogicalVector in( const VectorBase<RTYPE,NA,T>& x, const VectorBase<RTYPE,RHS_NA,RHS_T>& table ){
+    sugar::In<RTYPE,RHS_T> obj(table.get_ref()) ;
+    return obj.get( x.get_ref() );
+}
 
 
 } // Rcpp



More information about the Rcpp-commits mailing list