Skip to content

Commit c33786a

Browse files
committed
Merge branch 'master' of github.com:RcppCore/Rcpp
2 parents af7676a + 3ce9424 commit c33786a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

inst/include/Rcpp/internal/NAComparator.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ struct NAComparator<double> {
7272

7373
};
7474

75+
template <>
76+
struct NAComparator<Rcomplex> {
77+
inline bool operator()(Rcomplex left, Rcomplex right) const {
78+
// sort() in R says that complex numbers are first sorted by
79+
// the real parts, and then the imaginary parts.
80+
81+
// When only one of the two numbers contains NA or NaN, move
82+
// it to the right hand side.
83+
84+
// When both left and right contain NA or NaN, return true.
85+
86+
bool leftNaN = (left.r != left.r) || (left.i != left.i);
87+
bool rightNaN = (right.r != right.r) || (right.i != right.i);
88+
89+
if (!(leftNaN || rightNaN)) // if both are nice numbers
90+
{
91+
if (left.r == right.r) // if real parts are the same
92+
return left.i < right.i;
93+
else
94+
return left.r < right.r;
95+
} else
96+
return leftNaN <= rightNaN;
97+
}
98+
};
99+
75100
template <>
76101
struct NAComparator<SEXP> {
77102
inline bool operator()(SEXP left, SEXP right) const {

0 commit comments

Comments
 (0)