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