33// hash.h: Rcpp R/C++ interface class library -- hashing utility, inspired
44// from Simon's fastmatch package
55//
6- // Copyright (C) 2010, 2011 Simon Urbanek
7- // Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
6+ // Copyright (C) 2010, 2011 Simon Urbanek
7+ // Copyright (C) 2012 - 2024 Dirk Eddelbuettel and Romain Francois
8+ // Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
89//
910// This file is part of Rcpp.
1011//
@@ -60,10 +61,16 @@ namespace sugar{
6061 std::vector<int > indices ;
6162 int size_ ;
6263
64+ STORAGE normalize (STORAGE val) const { return val; }
65+
66+ inline bool not_equal (const STORAGE& lhs, const STORAGE& rhs) {
67+ return ! internal::NAEquals<STORAGE>()(normalize (lhs), rhs);
68+ }
69+
6370 int add_value_get_index (int i){
64- STORAGE val = src[i++] ;
71+ STORAGE val = normalize ( src[i++]) ;
6572 unsigned int addr = get_addr (val) ;
66- while (data[addr] && src[data[addr] - 1 ] != val) {
73+ while (data[addr] && not_equal ( src[data[addr] - 1 ], val) ) {
6774 addr++;
6875 if (addr == static_cast <unsigned int >(m)) addr = 0 ;
6976 }
@@ -90,6 +97,15 @@ namespace sugar{
9097 unsigned int get_addr (STORAGE value) const ;
9198 } ;
9299
100+ template <>
101+ inline double SelfHash<REALSXP>::normalize(double val) const {
102+ /* double is a bit tricky - we nave to normalize 0.0, NA and NaN */
103+ if (val == 0.0 ) val = 0.0 ;
104+ if (internal::Rcpp_IsNA (val)) val = NA_REAL;
105+ else if (internal::Rcpp_IsNaN (val)) val = R_NaN;
106+ return val;
107+ }
108+
93109 template <>
94110 inline unsigned int SelfHash<INTSXP>::get_addr(int value) const {
95111 return RCPP_HASH (value) ;
@@ -102,10 +118,6 @@ namespace sugar{
102118 unsigned int u[2 ];
103119 };
104120 union dint_u val_u;
105- /* double is a bit tricky - we nave to normalize 0.0, NA and NaN */
106- if (val == 0.0 ) val = 0.0 ;
107- if (internal::Rcpp_IsNA (val)) val = NA_REAL;
108- else if (internal::Rcpp_IsNaN (val)) val = R_NaN;
109121 val_u.d = val;
110122 addr = RCPP_HASH (val_u.u [0 ] + val_u.u [1 ]);
111123 return addr ;
0 commit comments