Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

std::hash<Rcpp::String>. close #84#408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
eddelbuettel merged 1 commit intoRcppCore:masterfromthirdwing:master
Dec 12, 2015
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsChangeLog
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinst/include/Rcpp/String.h
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinst/unitTests/cpp/wrap.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinst/unitTests/runit.wrap.R
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp