Skip to content

Commit 1fb8865

Browse files
authored
Merge pull request #527 from nathan-russell/master
Added decreasing option for Vector sort (#525)
2 parents 1dc7a84 + c134304 commit 1fb8865

File tree

5 files changed

+88
-16
lines changed

5 files changed

+88
-16
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2016-08-01 Nathan Russell <[email protected]>
2+
3+
* inst/include/Rcpp/vector/Vector.h: Added decreasing option for Vector
4+
sort
5+
* inst/include/Rcpp/internal/NAComparator.h: Idem
6+
* inst/unitTests/cpp/Vector.cpp: Idem
7+
* inst/unitTests/runit.Vector.R: Idem
8+
19
2016-07-31 Qiang Kou <[email protected]>
210

311
* inst/examples/SugarPerformance/sugarBenchmarks.R: Remove usage of Rf_eval

inst/include/Rcpp/internal/NAComparator.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#ifndef Rcpp__internal__NAComparator__h
2424
#define Rcpp__internal__NAComparator__h
2525

26-
namespace Rcpp{
26+
namespace Rcpp {
2727

2828
namespace internal {
2929

@@ -104,8 +104,16 @@ struct NAComparator<SEXP> {
104104
}
105105
};
106106

107-
}
108107

109-
}
108+
template <typename T>
109+
struct NAComparatorGreater {
110+
inline bool operator()(T left, T right) const {
111+
return NAComparator<T>()(right, left);
112+
}
113+
};
114+
115+
} // internal
116+
117+
} // Rcpp
110118

111-
#endif
119+
#endif // Rcpp__internal__NAComparator__h

inst/include/Rcpp/vector/Vector.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,31 @@ class Vector :
382382
);
383383
}
384384

385-
Vector& sort(){
385+
Vector& sort(bool decreasing = false) {
386386
// sort() does not apply to List, RawVector or ExpressionVector.
387387
//
388388
// The function below does nothing for qualified Vector types,
389389
// and is undefined for other types. Hence there will be a
390390
// compiler error when sorting List, RawVector or ExpressionVector.
391391
internal::Sort_is_not_allowed_for_this_type<RTYPE>::do_nothing();
392392

393-
typename traits::storage_type<RTYPE>::type* start = internal::r_vector_start<RTYPE>( Storage::get__() ) ;
394-
std::sort(
395-
start,
396-
start + size(),
397-
internal::NAComparator<typename traits::storage_type<RTYPE>::type >()
398-
) ;
399-
return *this ;
393+
typename traits::storage_type<RTYPE>::type* start = internal::r_vector_start<RTYPE>( Storage::get__() );
394+
395+
if (!decreasing) {
396+
std::sort(
397+
start,
398+
start + size(),
399+
internal::NAComparator<typename traits::storage_type<RTYPE>::type>()
400+
);
401+
} else {
402+
std::sort(
403+
start,
404+
start + size(),
405+
internal::NAComparatorGreater<typename traits::storage_type<RTYPE>::type>()
406+
);
407+
}
408+
409+
return *this;
400410
}
401411

402412
template <typename InputIterator>

inst/unitTests/cpp/Vector.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,22 +747,42 @@ CharacterVector CharacterVector_test_const_proxy(const CharacterVector x){
747747

748748
// [[Rcpp::export]]
749749
NumericVector sort_numeric(NumericVector x) {
750-
return x.sort();
750+
return x.sort();
751751
}
752752

753753
// [[Rcpp::export]]
754754
IntegerVector sort_integer(IntegerVector x) {
755-
return x.sort();
755+
return x.sort();
756756
}
757757

758758
// [[Rcpp::export]]
759759
CharacterVector sort_character(CharacterVector x) {
760-
return x.sort();
760+
return x.sort();
761761
}
762762

763763
// [[Rcpp::export]]
764764
LogicalVector sort_logical(LogicalVector x) {
765-
return x.sort();
765+
return x.sort();
766+
}
767+
768+
// [[Rcpp::export]]
769+
NumericVector sort_numeric_desc(NumericVector x) {
770+
return x.sort(true);
771+
}
772+
773+
// [[Rcpp::export]]
774+
IntegerVector sort_integer_desc(IntegerVector x) {
775+
return x.sort(true);
776+
}
777+
778+
// [[Rcpp::export]]
779+
CharacterVector sort_character_desc(CharacterVector x) {
780+
return x.sort(true);
781+
}
782+
783+
// [[Rcpp::export]]
784+
LogicalVector sort_logical_desc(LogicalVector x) {
785+
return x.sort(true);
766786
}
767787

768788
// [[Rcpp::export]]

inst/unitTests/runit.Vector.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,32 @@ if (.runThisTest) {
672672
checkIdentical( sort_logical(lgcl), sort(lgcl, na.last=TRUE) )
673673
}
674674

675+
test.sort_desc <- function() {
676+
num <- setNames(c(1, -1, 4, NA, 5, NaN), letters[1:5])
677+
checkIdentical(
678+
sort_numeric_desc(num),
679+
sort(num, decreasing = TRUE, na.last = FALSE)
680+
)
681+
682+
int <- as.integer(num)
683+
checkIdentical(
684+
sort_integer_desc(int),
685+
sort(int, decreasing = TRUE, na.last = FALSE)
686+
)
687+
688+
char <- setNames(sample(letters, 5), LETTERS[1:5])
689+
checkIdentical(
690+
sort_character_desc(char),
691+
sort(char, decreasing = TRUE, na.last = FALSE)
692+
)
693+
694+
lgcl <- as.logical(int)
695+
checkIdentical(
696+
sort_logical_desc(lgcl),
697+
sort(lgcl, decreasing = TRUE, na.last = FALSE)
698+
)
699+
}
700+
675701
test.List.assign.SEXP <- function() {
676702
l <- list(1, 2, 3)
677703
other <- list_sexp_assign(l)

0 commit comments

Comments
 (0)