|
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- |
| 2 | +/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */ |
| 3 | +// |
| 4 | +// NAComparator.h: Rcpp R/C++ interface class library -- comparator |
| 5 | +// |
| 6 | +// Copyright (C) 2012-2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey |
| 7 | +// |
| 8 | +// This file is part of Rcpp. |
| 9 | +// |
| 10 | +// Rcpp is free software: you can redistribute it and/or modify it |
| 11 | +// under the terms of the GNU General Public License as published by |
| 12 | +// the Free Software Foundation, either version 2 of the License, or |
| 13 | +// (at your option) any later version. |
| 14 | +// |
| 15 | +// Rcpp is distributed in the hope that it will be useful, but |
| 16 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +// GNU General Public License for more details. |
| 19 | +// |
| 20 | +// You should have received a copy of the GNU General Public License |
| 21 | +// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + |
| 23 | +#ifndef Rcpp__traits__NAComparator__h |
| 24 | +#define Rcpp__traits__NAComparator__h |
| 25 | + |
| 26 | +namespace Rcpp{ |
| 27 | +namespace traits{ |
| 28 | + |
| 29 | +inline bool Rcpp_IsNA(double x) { |
| 30 | + return memcmp( |
| 31 | + &x, |
| 32 | + &NA_REAL, |
| 33 | + sizeof(double) |
| 34 | + ) == 0; |
| 35 | +} |
| 36 | + |
| 37 | +inline bool Rcpp_IsNaN(double x) { |
| 38 | + return memcmp( |
| 39 | + &x, |
| 40 | + &R_NaN, |
| 41 | + sizeof(double) |
| 42 | + ) == 0; |
| 43 | +} |
| 44 | + |
| 45 | +inline int StrCmp(SEXP x, SEXP y) { |
| 46 | + if (x == NA_STRING) return (y == NA_STRING ? 0 : 1); |
| 47 | + if (y == NA_STRING) return -1; |
| 48 | + if (x == y) return 0; // same string in cache |
| 49 | + return strcmp(char_nocheck(x), char_nocheck(y)); |
| 50 | +} |
| 51 | + |
| 52 | +template <typename T> |
| 53 | +struct NAComparator { |
| 54 | + inline bool operator()(T left, T right) const { |
| 55 | + return left < right; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +template <> |
| 60 | +struct NAComparator<int> { |
| 61 | + inline bool operator()(int left, int right) const { |
| 62 | + if (left == NA_INTEGER) return false; |
| 63 | + if (right == NA_INTEGER) return true; |
| 64 | + return left < right; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +template <> |
| 69 | +struct NAComparator<double> { |
| 70 | + inline bool operator()(double left, double right) const { |
| 71 | + |
| 72 | + bool leftNaN = (left != left); |
| 73 | + bool rightNaN = (right != right); |
| 74 | + |
| 75 | + // this branch inspired by data.table: see |
| 76 | + // https://github.com/arunsrinivasan/datatable/commit/1a3e476d3f746e18261662f484d2afa84ac7a146#commitcomment-4885242 |
| 77 | + if (Rcpp_IsNaN(right) and Rcpp_IsNA(left)) return true; |
| 78 | + |
| 79 | + if (leftNaN != rightNaN) { |
| 80 | + return leftNaN < rightNaN; |
| 81 | + } else { |
| 82 | + return left < right; |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +}; |
| 88 | + |
| 89 | +template <> |
| 90 | +struct NAComparator<SEXP> { |
| 91 | + inline bool operator()(SEXP left, SEXP right) const { |
| 92 | + return StrCmp(left, right) < 0; |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +} |
| 97 | +} |
| 98 | + |
| 99 | +#endif |
0 commit comments