Skip to content

Commit 88ebc36

Browse files
authored
Merge pull request #575 from nathan-russell/master
Fixed signed comparison in IndexHash; unit tests for unique()
2 parents 8715b35 + 0e446d3 commit 88ebc36

File tree

6 files changed

+130
-35
lines changed

6 files changed

+130
-35
lines changed

ChangeLog

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
2016-11-04 Nathan Russell <[email protected]>
2+
3+
* inst/include/Rcpp/macros/dispatch.h: Modify variadic macros
4+
to not use GNU extensions
5+
* DESCRIPTION: roll minor version
6+
7+
2016-11-03 Nathan Russell <[email protected]>
8+
9+
* inst/include/Rcpp/hash/IndexHash.h: Add casts to eliminate
10+
signed / unsigned comparison warning
11+
* inst/include/Rcpp/hash/SelfHash.h: Idem
12+
* inst/unitTests/cpp/sugar.cpp: Added unit tests for sugar function
13+
unique()
14+
* inst/unitTests/runit.sugar.R: Idem
15+
116
2016-10-30 Dirk Eddelbuettel <[email protected]>
217

318
* src/api.cpp: New capabilities field for new date(time) vectors

inst/include/Rcpp/hash/IndexHash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace Rcpp{
170170
unsigned int addr = get_addr(val) ;
171171
while (data[addr] && not_equal( src[data[addr] - 1], val)) {
172172
addr++;
173-
if (addr == m) {
173+
if (addr == static_cast<unsigned int>(m)) {
174174
addr = 0;
175175
}
176176
}
@@ -191,7 +191,7 @@ namespace Rcpp{
191191
if (src[data[addr] - 1] == value)
192192
return data[addr];
193193
addr++;
194-
if (addr == m) addr = 0;
194+
if (addr == static_cast<unsigned int>(m)) addr = 0;
195195
}
196196
return NA_INTEGER;
197197
}

inst/include/Rcpp/hash/SelfHash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace sugar{
6565
unsigned int addr = get_addr(val) ;
6666
while (data[addr] && src[data[addr] - 1] != val) {
6767
addr++;
68-
if (addr == m) addr = 0;
68+
if (addr == static_cast<unsigned int>(m)) addr = 0;
6969
}
7070
if (!data[addr]) {
7171
data[addr] = i ;
@@ -81,7 +81,7 @@ namespace sugar{
8181
if (src[data[addr] - 1] == value)
8282
return data[addr];
8383
addr++;
84-
if (addr == m) addr = 0;
84+
if (addr == static_cast<unsigned int>(m)) addr = 0;
8585
}
8686
return NA_INTEGER;
8787
}

inst/include/Rcpp/macros/dispatch.h

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// dispatch.h: Rcpp R/C++ interface class library -- macros for dispatch
44
//
55
// Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois
6-
// Copyright (C) 2016 Dirk Eddelbuettel, Romain Francois and Artem Klevtsov
6+
// Copyright (C) 2016 Dirk Eddelbuettel, Romain Francois, Artem Klevtsov and Nathan Russell
77
//
88
// This file is part of Rcpp.
99
//
@@ -23,41 +23,62 @@
2323
#ifndef Rcpp__macros__dispatch_h
2424
#define Rcpp__macros__dispatch_h
2525

26+
// The variadic macros below incorporate techniques presented by
27+
// Stack Overflow user Richard Hansen in this answer
28+
//
29+
// http://stackoverflow.com/a/11172679/1869097
30+
//
31+
// and are necessary to avoid the use of GNU compiler extensions.
32+
2633
#ifdef RCPP_USING_CXX11
27-
#define ___RCPP_HANDLE_CASE___(___RTYPE___, ___FUN___, ___OBJECT___, \
28-
___RCPPTYPE___, ...) \
29-
case ___RTYPE___: \
30-
return ___FUN___(::Rcpp::___RCPPTYPE___<___RTYPE___>(___OBJECT___), \
31-
##__VA_ARGS__);
3234

33-
#define ___RCPP_RETURN___(__FUN__, __SEXP__, __RCPPTYPE__, ...) \
34-
SEXP __TMP__ = __SEXP__; \
35-
switch (TYPEOF(__TMP__)) { \
36-
___RCPP_HANDLE_CASE___(INTSXP, __FUN__, __TMP__, __RCPPTYPE__, \
37-
##__VA_ARGS__) \
38-
___RCPP_HANDLE_CASE___(REALSXP, __FUN__, __TMP__, __RCPPTYPE__, \
39-
##__VA_ARGS__) \
40-
___RCPP_HANDLE_CASE___(RAWSXP, __FUN__, __TMP__, __RCPPTYPE__, \
41-
##__VA_ARGS__) \
42-
___RCPP_HANDLE_CASE___(LGLSXP, __FUN__, __TMP__, __RCPPTYPE__, \
43-
##__VA_ARGS__) \
44-
___RCPP_HANDLE_CASE___(CPLXSXP, __FUN__, __TMP__, __RCPPTYPE__, \
45-
##__VA_ARGS__) \
46-
___RCPP_HANDLE_CASE___(STRSXP, __FUN__, __TMP__, __RCPPTYPE__, \
47-
##__VA_ARGS__) \
48-
___RCPP_HANDLE_CASE___(VECSXP, __FUN__, __TMP__, __RCPPTYPE__, \
49-
##__VA_ARGS__) \
50-
___RCPP_HANDLE_CASE___(EXPRSXP, __FUN__, __TMP__, __RCPPTYPE__, \
51-
##__VA_ARGS__) \
52-
default: \
53-
throw std::range_error("Not a vector"); \
35+
#define ___RCPP_HANDLE_CASE___(___RTYPE___, ___FUN___, ___RCPPTYPE___, ...) \
36+
case ___RTYPE___: \
37+
return ___FUN___(::Rcpp::___RCPPTYPE___<___RTYPE___>(RCPP_MACRO_FIRST(__VA_ARGS__)) \
38+
RCPP_MACRO_REST(__VA_ARGS__));
39+
40+
41+
#define ___RCPP_RETURN___(__FUN__, __RCPPTYPE__, ...) \
42+
SEXP __TMP__ = RCPP_MACRO_FIRST(__VA_ARGS__); \
43+
switch (TYPEOF(__TMP__)) { \
44+
___RCPP_HANDLE_CASE___(INTSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
45+
___RCPP_HANDLE_CASE___(REALSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
46+
___RCPP_HANDLE_CASE___(RAWSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
47+
___RCPP_HANDLE_CASE___(LGLSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
48+
___RCPP_HANDLE_CASE___(CPLXSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
49+
___RCPP_HANDLE_CASE___(STRSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
50+
___RCPP_HANDLE_CASE___(VECSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
51+
___RCPP_HANDLE_CASE___(EXPRSXP, __FUN__, __RCPPTYPE__, __VA_ARGS__) \
52+
default: \
53+
throw std::range_error("Not a vector"); \
5454
}
5555

56-
#define RCPP_RETURN_VECTOR(_FUN_, _SEXP_, ...) \
57-
___RCPP_RETURN___(_FUN_, _SEXP_, Vector, ##__VA_ARGS__)
58-
#define RCPP_RETURN_MATRIX(_FUN_, _SEXP_, ...) \
59-
___RCPP_RETURN___(_FUN_, _SEXP_, Matrix, ##__VA_ARGS__)
56+
57+
#define RCPP_RETURN_VECTOR(_FUN_, ...) \
58+
___RCPP_RETURN___(_FUN_, Vector, __VA_ARGS__)
59+
#define RCPP_RETURN_MATRIX(_FUN_, ...) \
60+
___RCPP_RETURN___(_FUN_, Matrix, __VA_ARGS__)
61+
62+
63+
#define RCPP_MACRO_FIRST(...) RCPP_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway)
64+
#define RCPP_MACRO_FIRST_HELPER(first, ...) first
65+
66+
#define RCPP_MACRO_REST(...) RCPP_MACRO_REST_HELPER(RCPP_MACRO_NUM(__VA_ARGS__), __VA_ARGS__)
67+
#define RCPP_MACRO_REST_HELPER(qty, ...) RCPP_MACRO_REST_HELPER2(qty, __VA_ARGS__)
68+
#define RCPP_MACRO_REST_HELPER2(qty, ...) RCPP_MACRO_REST_HELPER_##qty(__VA_ARGS__)
69+
#define RCPP_MACRO_REST_HELPER_ONE(first)
70+
#define RCPP_MACRO_REST_HELPER_TWOORMORE(first, ...) , __VA_ARGS__
71+
#define RCPP_MACRO_NUM(...) \
72+
RCPP_MACRO_SELECT_25TH(__VA_ARGS__, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, \
73+
TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, \
74+
TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, \
75+
TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, \
76+
TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway)
77+
#define RCPP_MACRO_SELECT_25TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, \
78+
a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, ...) a25
79+
6080
#else
81+
6182
#define ___RCPP_HANDLE_CASE___(___RTYPE___, ___FUN___, ___OBJECT___, \
6283
___RCPPTYPE___) \
6384
case ___RTYPE___: \

inst/unitTests/cpp/sugar.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,21 @@ IntegerVector runit_self_match( CharacterVector x){
595595
return self_match( x ) ;
596596
}
597597

598+
// [[Rcpp::export]]
599+
Rcpp::IntegerVector runit_unique_int(Rcpp::IntegerVector x) {
600+
return Rcpp::unique(x);
601+
}
602+
603+
// [[Rcpp::export]]
604+
Rcpp::NumericVector runit_unique_dbl(Rcpp::NumericVector x) {
605+
return Rcpp::unique(x);
606+
}
607+
608+
// [[Rcpp::export]]
609+
Rcpp::CharacterVector runit_unique_ch(Rcpp::CharacterVector x) {
610+
return Rcpp::unique(x);
611+
}
612+
598613
// [[Rcpp::export]]
599614
IntegerVector runit_table( CharacterVector x){
600615
return table( x ) ;

inst/unitTests/runit.sugar.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,50 @@ if (.runThisTest) {
638638
x <- sample( letters, 1000, replace = TRUE )
639639
checkEquals( runit_self_match(x), match(x,unique(x)) )
640640
}
641+
642+
test.unique <- function() {
643+
x <- sample(LETTERS[1:5], 10, TRUE)
644+
checkEquals(
645+
sort(unique(x)),
646+
sort(runit_unique_ch(x)),
647+
"unique / character / without NA"
648+
)
649+
650+
x <- c(x, NA, NA)
651+
checkEquals(
652+
sort(unique(x), na.last = TRUE),
653+
sort(runit_unique_ch(x), na.last = TRUE),
654+
"unique / character / with NA"
655+
)
656+
657+
x <- sample(1:5, 10, TRUE)
658+
checkEquals(
659+
sort(unique(x)),
660+
sort(runit_unique_int(x)),
661+
"unique / integer / without NA"
662+
)
663+
664+
x <- c(x, NA, NA)
665+
checkEquals(
666+
sort(unique(x), na.last = TRUE),
667+
sort(runit_unique_int(x), na.last = TRUE),
668+
"unique / integer / with NA"
669+
)
670+
671+
x <- sample(1:5 + 0.5, 10, TRUE)
672+
checkEquals(
673+
sort(unique(x)),
674+
sort(runit_unique_dbl(x)),
675+
"unique / numeric / without NA"
676+
)
677+
678+
x <- c(x, NA, NA)
679+
checkEquals(
680+
sort(unique(x), na.last = TRUE),
681+
sort(runit_unique_dbl(x), na.last = TRUE),
682+
"unique / numeric / with NA"
683+
)
684+
}
641685

642686
test.table <- function(){
643687
x <- sample( letters, 1000, replace = TRUE )

0 commit comments

Comments
 (0)