[Rcpp-commits] r3981 - 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 12:10:00 CET 2012


Author: romain
Date: 2012-11-16 12:10:00 +0100 (Fri, 16 Nov 2012)
New Revision: 3981

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h
Log:
intersect

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-16 10:15:18 UTC (rev 3980)
+++ pkg/Rcpp/ChangeLog	2012-11-16 11:10:00 UTC (rev 3981)
@@ -2,7 +2,8 @@
 
         * include/Rcpp/vector/Vector.h : added static methods Vector::is_na and
         Vector::get_na
-        * inst/include/Rcpp/sugar/functions/setdiff.h: initial version of setdiff
+        * inst/include/Rcpp/sugar/functions/setdiff.h: initial version of 
+        setdiff and intersect
 
 2012-11-15  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h	2012-11-16 10:15:18 UTC (rev 3980)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/setdiff.h	2012-11-16 11:10:00 UTC (rev 3981)
@@ -67,6 +67,40 @@
         
     } ;
     
+    template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+    class Intersect {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Intersect( const LHS_T& lhs, const RHS_T& rhs) : 
+            intersect()
+        {
+            
+            SET lhs_set( get_const_begin(lhs), get_const_end(lhs) ) ; 
+            SET rhs_set( get_const_begin(rhs), get_const_end(rhs) ) ; 
+            
+            ITERATOR end = lhs_set.end() ;
+            ITERATOR rhs_end = rhs_set.end() ;
+            for( ITERATOR it=lhs_set.begin(); it != end; it++){
+                if( rhs_set.find(*it) != rhs_end ) intersect.insert(*it) ;
+            }
+        }
+        
+        Vector<RTYPE> get() const {
+            int n = intersect.size() ;
+            Vector<RTYPE> out = no_init(n) ;
+            std::copy( intersect.begin(), intersect.end(), get_const_begin(out) ) ;
+            return out ;
+        }
+        
+    private:
+        typedef RCPP_UNORDERED_SET<STORAGE> SET ;
+        typedef typename SET::const_iterator ITERATOR ;
+        SET intersect ;
+        
+    } ;
+    
+    
 } // sugar
 
 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
@@ -74,6 +108,14 @@
     return sugar::SetDiff<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() ;
+}
+
+
+
+
 } // Rcpp
 #endif
 



More information about the Rcpp-commits mailing list