Skip to content

Commit 71f4cf5

Browse files
committed
refactor xxhash
1 parent 9fb71ac commit 71f4cf5

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ Description: Provides high performance container data types such
2323
those offered by other packages.
2424
License: MIT + file LICENSE
2525
URL: https://github.com/randy3k/collections
26-
Depends:
27-
R (>= 3.4.0)
28-
Suggests:
26+
Suggests:
2927
covr (>= 3.4.0),
3028
testthat (>= 2.3.1)
3129
ByteCompile: yes

src/Makevars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
SOURCES = collections.c deque.c dict.c priority_queue.c queue.c stack.c utils.c
2-
OBJECTS = tommyds/tommy.o xxhash/xxh.o $(SOURCES:.c=.o)
1+
SOURCES = collections.c deque.c dict.c priority_queue.c queue.c stack.c xxh.c utils.c
2+
OBJECTS = tommyds/tommy.o $(SOURCES:.c=.o)

src/dict.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "tommyds/tommyhashlin.h"
22
#include "dict.h"
3+
#include "xxh.h"
34
#include "utils.h"
4-
#include "xxhash/xxh.h"
55

66
#define INITIAL_SIZE 16
77
#define GROW_FACTOR 1.5
@@ -127,16 +127,20 @@ tommy_hash_t digest(SEXP key) {
127127
SEXP c = Rf_asChar(key);
128128
s = Rf_translateCharUTF8(c);
129129
return XXH3_64bits(s, strlen(s));
130+
}
130131

131-
} else if (is_hashable(key)) {
132+
if (is_hashable(key)) {
132133
return xxh_digest(key);
134+
}
133135

134-
} else if (Rf_isEnvironment(key)) {
136+
if (Rf_isEnvironment(key)) {
135137
s = R_alloc(sizeof(char), 30);
136138
sprintf((char*) s, "env<%p>", key);
137139
return XXH3_64bits(s, strlen(s));
138140

139-
} else if (Rf_isFunction(key)) {
141+
}
142+
143+
if (Rf_isFunction(key)) {
140144
SEXP key2 = PROTECT(Rf_shallow_duplicate(key));
141145
// the digest function will also hash the closure environment and attributes
142146
SET_CLOENV(key2, R_NilValue);
@@ -146,6 +150,7 @@ tommy_hash_t digest(SEXP key) {
146150
return h;
147151

148152
}
153+
149154
Rf_error("key is not hashable");
150155
}
151156

src/xxhash/xxh.c renamed to src/xxh.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ static void OutBytes(R_outpstream_t stream, void *buf, int length)
1717

1818

1919
XXH64_hash_t xxh_digest(SEXP x) {
20+
21+
if (Rf_isVectorAtomic(x) && !ALTREP(x)) {
22+
char *p;
23+
if (TYPEOF(x) == INTSXP) {
24+
p = (char*) INTEGER(x);
25+
return XXH3_64bits(p, Rf_length(x) * sizeof(int));
26+
}
27+
if (TYPEOF(x) == REALSXP) {
28+
p = (char*) REAL(x);
29+
return XXH3_64bits(p, Rf_length(x) * sizeof(double));
30+
}
31+
if (TYPEOF(x) == LGLSXP) {
32+
p = (char*) LOGICAL(x);
33+
return XXH3_64bits(p, Rf_length(x) * sizeof(int));
34+
}
35+
if (TYPEOF(x) == RAWSXP) {
36+
p = (char*) RAW(x);
37+
return XXH3_64bits(p, Rf_length(x));
38+
}
39+
}
40+
2041
XXH3_state_t* const xxh_state = XXH3_createState();
2142
XXH3_64bits_reset_withSeed(xxh_state, 0);
2243
struct R_outpstream_st stream;

src/xxhash/xxh.h renamed to src/xxh.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
#include <config.h>
66
#endif
77

8+
#include "Rversion.h"
9+
10+
#if (R_VERSION < R_Version(3, 5, 0))
11+
12+
# define ALTREP(x) false
13+
14+
#endif
15+
816
#define USE_RINTERNALS
917
#include <R.h>
1018
#include <Rinternals.h>
1119
#include <R_ext/Rdynload.h>
1220
#undef USE_RINTERNALS
1321

14-
#include "xxh3.h"
22+
#include "xxhash/xxh3.h"
1523

1624
XXH64_hash_t xxh_digest(SEXP x);
1725

0 commit comments

Comments
 (0)