Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2015-12-12 Qiang Kou <qkou@umail.iu.edu>

* inst/include/Rcpp/String.h: std::hash<Rcpp::String>
* inst/unitTests/cpp/wrap.cpp: Unit tests
* inst/unitTests/runit.wrap.R: Unit tests

2015-12-04 Qiang Kou <qkou@umail.iu.edu>

* inst/include/Rcpp/vector/Matrix.h: Add math operators between matrix
Expand Down
20 changes: 20 additions & 0 deletions inst/include/Rcpp/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,24 @@ namespace Rcpp {

} // Rcpp

/** hash can be in std or std::tr1 */
#if defined(RCPP_USING_CXX11) || defined(HAS_TR1)
namespace std
{
#ifndef RCPP_USING_CXX11
namespace tr1 {
#endif
template <>
struct hash<Rcpp::String>
{
size_t operator()(const Rcpp::String & s) const{
return hash<string>()(s.get_cstring());
}
};
#ifndef RCPP_USING_CXX11
}
#endif
}
#endif

#endif
23 changes: 23 additions & 0 deletions inst/unitTests/cpp/wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ SEXP nonnull_const_char(){
return wrap(p) ;
}

#ifdef RCPP_USING_CXX11
// [[Rcpp::plugins(cpp11)]]
#endif

// [[Rcpp::export]]
IntegerVector unordered_map_string_int(){
RCPP_UNORDERED_MAP< std::string, int > m ;
Expand All @@ -177,6 +181,25 @@ IntegerVector unordered_map_string_int(){
return wrap(m);
}

// [[Rcpp::export]]
IntegerVector unordered_map_rcpp_string_int(StringVector v){
RCPP_UNORDERED_MAP< String, int > m ;
m[v[0]] = 200;
m[v[1]] = 100;
m[v[2]] = 300;
return wrap(m);
}

// [[Rcpp::export]]
LogicalVector unordered_set_rcpp_string(StringVector x) {
RCPP_UNORDERED_SET<String> seen;
LogicalVector out(x.size());
for (int i = 0; i < x.size(); i++) {
out[i] = !seen.insert(x[i]).second;
}
return out;
}

// [[Rcpp::export]]
NumericVector unordered_map_string_double(){
RCPP_UNORDERED_MAP<std::string,double> m ;
Expand Down
12 changes: 12 additions & 0 deletions inst/unitTests/runit.wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ if (.runThisTest) {
checkEquals( res[["c"]], 300L, msg = "wrap( tr1::unordered_map<string,int>) " )
}

test.wrap.unordered.map.rcpp.string.int <- function(){
res <- unordered_map_rcpp_string_int(c("a", "b", "c"))
checkEquals( res[["a"]], 200L, msg = "wrap( tr1::unordered_map<Rcpp::String,int>) " )
checkEquals( res[["b"]], 100L, msg = "wrap( tr1::unordered_map<Rcpp::String,int>) " )
checkEquals( res[["c"]], 300L, msg = "wrap( tr1::unordered_map<Rcpp::String,int>) " )
}

test.unordered.set.rcpp.string <- function(){
checkEquals(unordered_set_rcpp_string(c("a", "b", "c", "b")),
c(FALSE, FALSE, FALSE, TRUE), msg = "wrap( tr1::unordered_set<Rcpp::String>) " )
}

test.wrap.unordered.map.string.double <- function(){
res <- unordered_map_string_double()
checkEquals( res[["a"]], 200, msg = "wrap( tr1::unordered_map<string,double>) " )
Expand Down