Skip to content

Commit ebd40f0

Browse files
committed
Need a specialized 'equals' to handle NA, NaN in Hasher
1 parent 904956e commit ebd40f0

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

inst/include/Rcpp/hash/IndexHash.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ namespace Rcpp{
159159
#endif
160160
}
161161

162-
inline bool not_equal(STORAGE lhs, STORAGE rhs) {
163-
return ::Rcpp::traits::comparator_type<STORAGE>()(lhs, rhs);
162+
inline bool not_equal(const STORAGE& lhs, const STORAGE& rhs) {
163+
return ! ::Rcpp::traits::equal_type<STORAGE>()(lhs, rhs);
164164
}
165165

166166
bool add_value(int i){
@@ -169,15 +169,17 @@ namespace Rcpp{
169169
int addr = get_addr(val) ;
170170
while (data[addr] && not_equal( src[data[addr] - 1], val)) {
171171
addr++;
172-
if (addr == m) addr = 0;
172+
if (addr == m) {
173+
addr = 0;
174+
}
173175
}
176+
174177
if (!data[addr]){
175178
data[addr] = i ;
176179
size_++ ;
177180

178181
return true ;
179182
}
180-
181183
return false;
182184
}
183185

inst/include/Rcpp/traits/comparator_type.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ inline int StrCmp(SEXP x, SEXP y) {
4646
if (x == NA_STRING) return (y == NA_STRING ? 0 : 1);
4747
if (y == NA_STRING) return -1;
4848
if (x == y) return 0; // same string in cache
49-
return strcmp(CHAR(x), CHAR(y));
49+
return strcmp(char_nocheck(x), char_nocheck(y));
5050
}
5151

5252
template <typename T>
@@ -91,7 +91,21 @@ struct comparator_type<SEXP> {
9191
inline bool operator()(SEXP left, SEXP right) const {
9292
return StrCmp(left, right) < 0;
9393
}
94-
};
94+
};
95+
96+
template <typename T>
97+
struct equal_type {
98+
inline bool operator()(T left, T right) const {
99+
return left == right;
100+
}
101+
};
102+
103+
template <>
104+
struct equal_type<double> {
105+
inline bool operator()(double left, double right) const {
106+
return memcmp(&left, &right, sizeof(double)) == 0;
107+
}
108+
};
95109

96110
}
97111
}

0 commit comments

Comments
 (0)