Skip to content

Commit 9c981a3

Browse files
coatlesseddelbuettel
authored andcommitted
Added decreasing parameter to sort_unique() to address #950. (#958)
* Added decreasing parameter to sort_unique() to address #950. * Allow the travis build to run for 20 minutes. * Travis wait doesn't play nicely with docker without a workaround. c.f. travis-ci/travis-ci#6934 (comment) * Add type... * Use `na.last = FALSE` to disable changing orders. Note: `unique()`'s unit test use `na.last = TRUE` because sorting was done at the R level instead of the C++ level. * Remove travis debug setup and enable only one `na.last`
1 parent fa1a111 commit 9c981a3

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2019-03-20 James J Balamuta <[email protected]>
2+
3+
* inst/include/Rcpp/sugar/functions/unique.h: Added decreasing parameter to control sort.
4+
* inst/unitTests/runit.sugar.R: Added sort_unique() tests
5+
* inst/unitTests/cpp/sugar.cpp: Ditto
6+
17
2019-03-20 Dirk Eddelbuettel <[email protected]>
28

39
* src/date.cpp: Renamed from Date.cpp

inst/NEWS.Rd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
a variable reference suppressing compiler warnings (Dirk in
1212
\ghpr{953}) fixing \ghit{951}).
1313
}
14+
\item Changes in Rcpp Sugar:
15+
\itemize{
16+
\item Added decreasing parameter to \code{sort_unique()}
17+
(James Balamuta in \ghpr{958} addressing \ghit{950}).
18+
}
1419
}
1520
}
1621

inst/include/Rcpp/sugar/functions/unique.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ inline Vector<RTYPE> unique( const VectorBase<RTYPE,NA,T>& t ){
6969
return hash.keys() ;
7070
}
7171
template <int RTYPE, bool NA, typename T>
72-
inline Vector<RTYPE> sort_unique( const VectorBase<RTYPE,NA,T>& t ){
73-
return unique<RTYPE,NA,T>( t ).sort() ;
72+
inline Vector<RTYPE> sort_unique( const VectorBase<RTYPE,NA,T>& t , bool decreasing = false){
73+
return unique<RTYPE,NA,T>( t ).sort(decreasing) ;
7474
}
7575

7676
template <int RTYPE, bool NA, typename T, bool RHS_NA, typename RHS_T>

inst/unitTests/cpp/sugar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,12 @@ Rcpp::CharacterVector runit_unique_ch(Rcpp::CharacterVector x) {
611611
return Rcpp::unique(x);
612612
}
613613

614+
// [[Rcpp::export]]
615+
Rcpp::CharacterVector runit_sort_unique_ch(Rcpp::CharacterVector x,
616+
bool decreasing = false) {
617+
return Rcpp::sort_unique(x, decreasing);
618+
}
619+
614620
// [[Rcpp::export]]
615621
IntegerVector runit_table( CharacterVector x){
616622
return table( x ) ;

inst/unitTests/runit.sugar.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,36 @@ if (.runThisTest) {
687687
)
688688
}
689689

690+
test.sort_unique <- function() {
691+
692+
set.seed(123)
693+
x <- sample(LETTERS[1:5], 10, TRUE)
694+
checkEquals(
695+
sort(unique(x), decreasing = TRUE),
696+
runit_sort_unique_ch(x, decreasing = TRUE),
697+
"unique / character / without NA / decreasing sort"
698+
)
699+
700+
checkEquals(
701+
sort(unique(x), decreasing = FALSE),
702+
runit_sort_unique_ch(x, decreasing = FALSE),
703+
"unique / character / without NA / increasing sort"
704+
)
705+
706+
x <- c(x, NA, NA)
707+
checkEquals(
708+
sort(unique(x), decreasing = TRUE, na.last = FALSE),
709+
runit_sort_unique_ch(x, decreasing = TRUE),
710+
"unique / character / with NA / decreasing sort"
711+
)
712+
713+
checkEquals(
714+
sort(unique(x), decreasing = FALSE, na.last = TRUE),
715+
runit_sort_unique_ch(x, decreasing = FALSE),
716+
"unique / character / with NA / increasing sort"
717+
)
718+
}
719+
690720
test.table <- function(){
691721
x <- sample( letters, 1000, replace = TRUE )
692722
checkTrue( all( runit_table(x) == table(x) ) )

0 commit comments

Comments
 (0)