[Rcpp-commits] r526 - in pkg: . inst inst/unitTests src src/Rcpp/internal

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 31 12:35:28 CET 2010


Author: romain
Date: 2010-01-31 12:35:27 +0100 (Sun, 31 Jan 2010)
New Revision: 526

Modified:
   pkg/NEWS
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.wrap.R
   pkg/src/Rcpp/internal/wrap.h
   pkg/src/RcppCommon.cpp
   pkg/src/RcppCommon.h
Log:
tr1::unordered_(multi)map<string,T> becomes wrap()able

Modified: pkg/NEWS
===================================================================
--- pkg/NEWS	2010-01-31 10:48:13 UTC (rev 525)
+++ pkg/NEWS	2010-01-31 11:35:27 UTC (rev 526)
@@ -6,6 +6,10 @@
     o	std::map<std::string,T> becomes wrap<>()'able
 
     o	std::multimap<std::string,T> becomes wrap<>()'able
+    
+    o	std::tr1::unordered_map<std::string,T> becomes wrap<>()'able
+    
+    o	std::tr1::unordered_multimap<std::string,T> becomes wrap<>()'able
 
 0.7.4	2010-01-30
 

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-31 10:48:13 UTC (rev 525)
+++ pkg/inst/ChangeLog	2010-01-31 11:35:27 UTC (rev 526)
@@ -4,7 +4,8 @@
 	multiset<T> where T can be wrap()'ed -> vector or list
 	map<string,T> where T can be wrap()'ed -> named vector or named list
 	multimap<string,T> where T can be wrap()'ed -> named vector or named list
-
+	tr1::unordered_map<string,T> where T can be wrap()'ed -> named vector or named list
+	
 2010-01-30  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.7.4

Modified: pkg/inst/unitTests/runit.wrap.R
===================================================================
--- pkg/inst/unitTests/runit.wrap.R	2010-01-31 10:48:13 UTC (rev 525)
+++ pkg/inst/unitTests/runit.wrap.R	2010-01-31 11:35:27 UTC (rev 526)
@@ -223,3 +223,123 @@
 		msg = "wrap( multimap<string,vector<int>>) " )
 }
 
+
+
+# tr1::unordered_map
+
+if( Rcpp:::capabilities()[["tr1 unordered maps"]] ) {
+	
+test.wrap.unordered.map.string.int <- function(){
+	
+	funx <- cfunction(signature(), 
+	'
+	std::tr1::unordered_map< std::string, int > m ;
+	m["b"] = 100;
+  	m["a"] = 200;
+  	m["c"] = 300;
+  	return wrap(m) ;
+	', 
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	
+	res <- funx()
+	checkEquals( res[["a"]], 200L,  msg = "wrap( tr1::unordered_map<string,int>) " )
+	checkEquals( res[["b"]], 100L,  msg = "wrap( tr1::unordered_map<string,int>) " )
+	checkEquals( res[["c"]], 300L,  msg = "wrap( tr1::unordered_map<string,int>) " )
+}
+
+test.wrap.unordered.map.string.double <- function(){
+	
+	funx <- cfunction(signature(), 
+	'
+	std::tr1::unordered_map<std::string,double> m ;
+	m["b"] = 100;
+  	m["a"] = 200;
+  	m["c"] = 300;
+  	return wrap(m) ;
+	', 
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	
+	res <- funx()
+	checkEquals( res[["a"]], 200,  msg = "wrap( tr1::unordered_map<string,double>) " )
+	checkEquals( res[["b"]], 100,  msg = "wrap( tr1::unordered_map<string,double>) " )
+	checkEquals( res[["c"]], 300,  msg = "wrap( tr1::unordered_map<string,double>) " )
+}
+
+test.wrap.unordered.map.string.bool <- function(){
+	
+	funx <- cfunction(signature(), 
+	'
+	std::tr1::unordered_map<std::string,bool> m ;
+	m["b"] = true;
+  	m["a"] = false;
+  	m["c"] = true;
+  	return wrap(m) ;
+	', 
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	
+	res <- funx()
+	checkEquals( res[["a"]], FALSE,  msg = "wrap( tr1::unordered_map<string,bool>) " )
+	checkEquals( res[["b"]], TRUE ,  msg = "wrap( tr1::unordered_map<string,bool>) " )
+	checkEquals( res[["c"]], TRUE ,  msg = "wrap( tr1::unordered_map<string,bool>) " )
+}
+
+test.wrap.unordered.map.string.Rbyte <- function(){
+	
+	funx <- cfunction(signature(), 
+	'
+	std::tr1::unordered_map<std::string,Rbyte> m ;
+	m["b"] = (Rbyte)0;
+  	m["a"] = (Rbyte)1;
+  	m["c"] = (Rbyte)2;
+  	return wrap(m) ;
+	', 
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	
+	res <- funx()
+	checkEquals( res[["a"]], as.raw(1),  msg = "wrap( tr1::unordered_map<string,Rbyte>) " )
+	checkEquals( res[["b"]], as.raw(0),  msg = "wrap( tr1::unordered_map<string,Rbyte>) " )
+	checkEquals( res[["c"]], as.raw(2),  msg = "wrap( tr1::unordered_map<string,Rbyte>) " )
+}
+
+test.wrap.unordered.map.string.string <- function(){
+	
+	funx <- cfunction(signature(), 
+	'
+	std::tr1::unordered_map<std::string,std::string> m ;
+	m["b"] = "foo" ;
+  	m["a"] = "bar" ;
+  	m["c"] = "bling" ;
+  	return wrap(m) ;
+	', 
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	
+	res <- funx()
+	checkEquals( res[["a"]], "bar"   ,  msg = "wrap( tr1::unordered_map<string,string>) " )
+	checkEquals( res[["b"]], "foo"   ,  msg = "wrap( tr1::unordered_map<string,string>) " )
+	checkEquals( res[["c"]], "bling" ,  msg = "wrap( tr1::unordered_map<string,string>) " )
+}
+
+test.wrap.unordered.map.string.generic <- function(){
+	
+	funx <- cfunction(signature(), 
+	'
+	std::tr1::unordered_map< std::string,std::vector<int> > m ;
+	std::vector<int> b ; b.push_back(1) ; b.push_back(2) ; m["b"] = b ;
+  	std::vector<int> a ; a.push_back(1) ; a.push_back(2) ; a.push_back(2) ; m["a"] = a ;
+  	std::vector<int> c ; c.push_back(1) ; c.push_back(2) ; c.push_back(2) ; c.push_back(2) ; m["c"] = c ;
+  	return wrap(m) ;
+	', 
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	
+	res <- funx()
+	checkEquals( res[["a"]], c(1L,2L,2L) ,  msg = "wrap( tr1::unordered_map<string,vector<int>>) " )
+	checkEquals( res[["b"]], c(1L,2L) ,  msg = "wrap( tr1::unordered_map<string,vector<int>>) " )
+	checkEquals( res[["c"]], c(1L,2L,2L,2L) ,  msg = "wrap( tr1::unordered_map<string,vector<int>>) " )
+	
+}
+	
+} # if( Rcpp:::capabilities("tr1 unordered maps") )
+
+
+
+

Modified: pkg/src/Rcpp/internal/wrap.h
===================================================================
--- pkg/src/Rcpp/internal/wrap.h	2010-01-31 10:48:13 UTC (rev 525)
+++ pkg/src/Rcpp/internal/wrap.h	2010-01-31 11:35:27 UTC (rev 526)
@@ -96,6 +96,11 @@
 template <typename T> struct wrap_type_traits< std::multiset<T> > { typedef wrap_type_stl_container_tag category ; } ;
 template <typename T> struct wrap_type_traits< std::map<std::string,T> > { typedef wrap_type_stl_container_tag category ; } ;
 template <typename T> struct wrap_type_traits< std::multimap<std::string,T> > { typedef wrap_type_stl_container_tag category ; } ;
+#ifdef HAS_TR1_UNORDERED_MAP
+template <typename T> struct wrap_type_traits< std::tr1::unordered_map<std::string,T> > { typedef wrap_type_stl_container_tag category ; } ;
+template <typename T> struct wrap_type_traits< std::tr1::unordered_multimap<std::string,T> > { typedef wrap_type_stl_container_tag category ; } ;
+#endif
+
 // #ifdef HAS_INIT_LISTS
 // template <typename T> struct wrap_type_traits< std::initializer_list<T> > { typedef wrap_type_stl_container_tag category ; } ;
 // #endif

Modified: pkg/src/RcppCommon.cpp
===================================================================
--- pkg/src/RcppCommon.cpp	2010-01-31 10:48:13 UTC (rev 525)
+++ pkg/src/RcppCommon.cpp	2010-01-31 11:35:27 UTC (rev 526)
@@ -42,8 +42,8 @@
 }
 
 SEXP capabilities(){
-	SEXP cap = PROTECT( Rf_allocVector( LGLSXP, 3) ) ;
-	SEXP names = PROTECT( Rf_allocVector( STRSXP, 3 ) ) ;
+	SEXP cap = PROTECT( Rf_allocVector( LGLSXP, 4) ) ;
+	SEXP names = PROTECT( Rf_allocVector( STRSXP, 4 ) ) ;
 #ifdef HAS_VARIADIC_TEMPLATES
 	LOGICAL(cap)[0] = TRUE ;
 #else
@@ -54,10 +54,19 @@
 #else
 	LOGICAL(cap)[1] = FALSE ;
 #endif
+	/* exceptions are allways supported */
 	LOGICAL(cap)[2] = TRUE ;
+
+#ifdef HAS_TR1_UNORDERED_MAP
+	LOGICAL(cap)[3] = TRUE ;
+#else
+	LOGICAL(cap)[3] = FALSE ;
+#endif
+
 	SET_STRING_ELT(names, 0, Rf_mkChar("variadic templates") ) ;
 	SET_STRING_ELT(names, 1, Rf_mkChar("initializer lists") ) ;
 	SET_STRING_ELT(names, 2, Rf_mkChar("exception handling") ) ;
+	SET_STRING_ELT(names, 3, Rf_mkChar("tr1 unordered maps") ) ;
 	Rf_setAttrib( cap, R_NamesSymbol, names ) ;
 	UNPROTECT(2) ;
 	return cap ;

Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h	2010-01-31 10:48:13 UTC (rev 525)
+++ pkg/src/RcppCommon.h	2010-01-31 11:35:27 UTC (rev 526)
@@ -25,8 +25,8 @@
 #define RcppCommon_h
 
 #ifdef __GNUC__
+	#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
 	#ifdef __GXX_EXPERIMENTAL_CXX0X__
-		#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
 		#if GCC_VERSION >= 40300
 			#define HAS_VARIADIC_TEMPLATES
 		#endif
@@ -34,6 +34,11 @@
 			#define HAS_INIT_LISTS
 		#endif
 	#endif
+	// FIXME: [romain] I did not actually check, tr1::unordered_map was 
+	// probably introduced before GCC 4.3
+	#if GCC_VERSION >= 40300
+		#define HAS_TR1_UNORDERED_MAP
+	#endif
 #endif
 
 #include <exception>
@@ -54,6 +59,10 @@
 #include <initializer_list>
 #endif
 
+#ifdef HAS_TR1_UNORDERED_MAP
+#include <tr1/unordered_map>
+#endif
+
 // include R headers, but set R_NO_REMAP and access everything via Rf_ prefixes
 #define R_NO_REMAP
 #include <R.h>



More information about the Rcpp-commits mailing list