Skip to content
Permalink
Browse files

Added decreasing parameter to sort_unique() to address RcppCore#950.

  • Loading branch information...
coatless committed Mar 20, 2019
1 parent 6d1ba4a commit b618662b7c2548435509b8c5710c965a3700e459
Showing with 47 additions and 2 deletions.
  1. +6 −0 ChangeLog
  2. +5 −0 inst/NEWS.Rd
  3. +2 −2 inst/include/Rcpp/sugar/functions/unique.h
  4. +5 −0 inst/unitTests/cpp/sugar.cpp
  5. +29 −0 inst/unitTests/runit.sugar.R
@@ -1,3 +1,9 @@
2019-03-20 James J Balamuta <balamut2@illinois.edu>

* inst/include/Rcpp/sugar/functions/unique.h: Added decreasing parameter to control sort.
* inst/unitTests/runit.sugar.R: Added sort_unique() tests
* inst/unitTests/cpp/sugar.cpp: Ditto

2019-03-19 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Roll minor version
@@ -11,6 +11,11 @@
a variable reference suppressing compiler warnings (Dirk in
\ghpr{953}) fixing \ghit{951}).
}
\item Changes in Rcpp Sugar:
\itemize{
\item Added decreasing parameter to \code{sort_unique()}
(James Balamuta in \ghpr{958} addressing \ghit{950}).
}
}
}

@@ -69,8 +69,8 @@ inline Vector<RTYPE> unique( const VectorBase<RTYPE,NA,T>& t ){
return hash.keys() ;
}
template <int RTYPE, bool NA, typename T>
inline Vector<RTYPE> sort_unique( const VectorBase<RTYPE,NA,T>& t ){
return unique<RTYPE,NA,T>( t ).sort() ;
inline Vector<RTYPE> sort_unique( const VectorBase<RTYPE,NA,T>& t , bool decreasing = false){
return unique<RTYPE,NA,T>( t ).sort(decreasing) ;
}

template <int RTYPE, bool NA, typename T, bool RHS_NA, typename RHS_T>
@@ -611,6 +611,11 @@ Rcpp::CharacterVector runit_unique_ch(Rcpp::CharacterVector x) {
return Rcpp::unique(x);
}

// [[Rcpp::export]]
Rcpp::CharacterVector runit_sort_unique_ch(Rcpp::CharacterVector x, decreasing = false) {
return Rcpp::sort_unique(x, decreasing);
}

// [[Rcpp::export]]
IntegerVector runit_table( CharacterVector x){
return table( x ) ;
@@ -687,6 +687,35 @@ if (.runThisTest) {
)
}

test.sort_unique <- function() {

x <- sample(LETTERS[1:5], 10, TRUE)
checkEquals(
sort(unique(x), decreasing = TRUE),
runit_sort_unique_ch(x, decreasing = TRUE),
"unique / character / without NA / decreasing sort"
)

checkEquals(
sort(unique(x), decreasing = FALSE),
runit_sort_unique_ch(x, decreasing = FALSE),
"unique / character / without NA / increasing sort"
)

x <- c(x, NA, NA)
checkEquals(
sort(unique(x), decreasing = TRUE, na.last = TRUE),
runit_sort_unique_ch(x, decreasing = TRUE),
"unique / character / with NA / decreasing sort"
)

checkEquals(
sort(unique(x), decreasing = FALSE, na.last = TRUE),
runit_sort_unique_ch(x, decreasing = FALSE),
"unique / character / with NA / increasing sort"
)
}

test.table <- function(){
x <- sample( letters, 1000, replace = TRUE )
checkTrue( all( runit_table(x) == table(x) ) )

0 comments on commit b618662

Please sign in to comment.
You can’t perform that action at this time.