Skip to content

Commit 0c598bc

Browse files
committed
Merge pull request #408 from thirdwing/master
std::hash<Rcpp::String>. close #84
2 parents 5252c43 + f7686af commit 0c598bc

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2015-12-12 Qiang Kou <[email protected]>
2+
3+
* inst/include/Rcpp/String.h: std::hash<Rcpp::String>
4+
* inst/unitTests/cpp/wrap.cpp: Unit tests
5+
* inst/unitTests/runit.wrap.R: Unit tests
6+
17
2015-12-04 Qiang Kou <[email protected]>
28

39
* inst/include/Rcpp/vector/Matrix.h: Add math operators between matrix

inst/include/Rcpp/String.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,24 @@ namespace Rcpp {
556556

557557
} // Rcpp
558558

559+
/** hash can be in std or std::tr1 */
560+
#if defined(RCPP_USING_CXX11) || defined(HAS_TR1)
561+
namespace std
562+
{
563+
#ifndef RCPP_USING_CXX11
564+
namespace tr1 {
565+
#endif
566+
template <>
567+
struct hash<Rcpp::String>
568+
{
569+
size_t operator()(const Rcpp::String & s) const{
570+
return hash<string>()(s.get_cstring());
571+
}
572+
};
573+
#ifndef RCPP_USING_CXX11
574+
}
575+
#endif
576+
}
577+
#endif
578+
559579
#endif

inst/unitTests/cpp/wrap.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ SEXP nonnull_const_char(){
168168
return wrap(p) ;
169169
}
170170

171+
#ifdef RCPP_USING_CXX11
172+
// [[Rcpp::plugins(cpp11)]]
173+
#endif
174+
171175
// [[Rcpp::export]]
172176
IntegerVector unordered_map_string_int(){
173177
RCPP_UNORDERED_MAP< std::string, int > m ;
@@ -177,6 +181,25 @@ IntegerVector unordered_map_string_int(){
177181
return wrap(m);
178182
}
179183

184+
// [[Rcpp::export]]
185+
IntegerVector unordered_map_rcpp_string_int(StringVector v){
186+
RCPP_UNORDERED_MAP< String, int > m ;
187+
m[v[0]] = 200;
188+
m[v[1]] = 100;
189+
m[v[2]] = 300;
190+
return wrap(m);
191+
}
192+
193+
// [[Rcpp::export]]
194+
LogicalVector unordered_set_rcpp_string(StringVector x) {
195+
RCPP_UNORDERED_SET<String> seen;
196+
LogicalVector out(x.size());
197+
for (int i = 0; i < x.size(); i++) {
198+
out[i] = !seen.insert(x[i]).second;
199+
}
200+
return out;
201+
}
202+
180203
// [[Rcpp::export]]
181204
NumericVector unordered_map_string_double(){
182205
RCPP_UNORDERED_MAP<std::string,double> m ;

inst/unitTests/runit.wrap.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ if (.runThisTest) {
108108
checkEquals( res[["c"]], 300L, msg = "wrap( tr1::unordered_map<string,int>) " )
109109
}
110110

111+
test.wrap.unordered.map.rcpp.string.int <- function(){
112+
res <- unordered_map_rcpp_string_int(c("a", "b", "c"))
113+
checkEquals( res[["a"]], 200L, msg = "wrap( tr1::unordered_map<Rcpp::String,int>) " )
114+
checkEquals( res[["b"]], 100L, msg = "wrap( tr1::unordered_map<Rcpp::String,int>) " )
115+
checkEquals( res[["c"]], 300L, msg = "wrap( tr1::unordered_map<Rcpp::String,int>) " )
116+
}
117+
118+
test.unordered.set.rcpp.string <- function(){
119+
checkEquals(unordered_set_rcpp_string(c("a", "b", "c", "b")),
120+
c(FALSE, FALSE, FALSE, TRUE), msg = "wrap( tr1::unordered_set<Rcpp::String>) " )
121+
}
122+
111123
test.wrap.unordered.map.string.double <- function(){
112124
res <- unordered_map_string_double()
113125
checkEquals( res[["a"]], 200, msg = "wrap( tr1::unordered_map<string,double>) " )

0 commit comments

Comments
 (0)