Skip to content

Commit 76b4ec8

Browse files
committed
fix: change to char
1 parent ef8b4b8 commit 76b4ec8

File tree

2 files changed

+29
-54
lines changed

2 files changed

+29
-54
lines changed

src/dict.c

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#include "tommyds/tommyhashlin.h"
21
#include "dict.h"
3-
#include "xxh.h"
2+
3+
#include "tommyds/tommyhashlin.h"
44
#include "utils.h"
5+
#include "xxh.h"
56

67
#define INITIAL_SIZE 16
78
#define GROW_FACTOR 1.5
@@ -15,14 +16,12 @@
1516
#endif
1617
#endif
1718

18-
1919
typedef struct item {
2020
SEXP key;
2121
int value;
2222
tommy_node node;
2323
} item;
2424

25-
2625
static void free_ht(SEXP ht_xptr) {
2726
tommy_hashlin* ht = R_ExternalPtrAddr(ht_xptr);
2827

@@ -33,7 +32,6 @@ static void free_ht(SEXP ht_xptr) {
3332
}
3433
}
3534

36-
3735
static_inline int holes_pop(SEXP self) {
3836
SEXP holes = PROTECT(get_sexp_value(self, "holes"));
3937
SEXP pop = PROTECT(get_sexp_value(holes, "pop"));
@@ -43,7 +41,6 @@ static_inline int holes_pop(SEXP self) {
4341
return n;
4442
}
4543

46-
4744
static_inline void holes_push(SEXP self, int index) {
4845
SEXP holes = PROTECT(get_sexp_value(self, "holes"));
4946
SEXP push = PROTECT(get_sexp_value(holes, "push"));
@@ -53,7 +50,6 @@ static_inline void holes_push(SEXP self, int index) {
5350
UNPROTECT(4);
5451
}
5552

56-
5753
static_inline void holes_clear(SEXP self) {
5854
SEXP holes = PROTECT(get_sexp_value(self, "holes"));
5955
SEXP clear = PROTECT(get_sexp_value(holes, "clear"));
@@ -62,16 +58,14 @@ static_inline void holes_clear(SEXP self) {
6258
UNPROTECT(3);
6359
}
6460

65-
6661
tommy_hash_t key_to_u64(SEXP key) {
67-
6862
if (is_hashable(key)) {
6963
return xxh_digest(key);
7064
}
7165

7266
if (Rf_isEnvironment(key)) {
73-
static char ch[50];
74-
snprintf(ch, 50, "<environment: %p>", (void*) key);
67+
char ch[128];
68+
snprintf(ch, sizeof(ch), "<environment: %p>", (void*)key);
7569
return XXH3_64bits(ch, strlen(ch));
7670
}
7771

@@ -88,7 +82,6 @@ tommy_hash_t key_to_u64(SEXP key) {
8882
Rf_error("key is not hashable");
8983
}
9084

91-
9285
// SEXP dict_hash(SEXP key) {
9386
// tommy_hash_t h = key_to_u64(key);
9487
// char* p = R_alloc(17, sizeof(char));
@@ -100,7 +93,6 @@ tommy_hash_t key_to_u64(SEXP key) {
10093
// return Rf_mkString(p);
10194
// }
10295

103-
10496
static tommy_hashlin* init_hashlin(SEXP self, SEXP ht_xptr) {
10597
tommy_hashlin* ht;
10698
ht = malloc(sizeof(tommy_hashlin));
@@ -121,7 +113,7 @@ static tommy_hashlin* init_hashlin(SEXP self, SEXP ht_xptr) {
121113
c = VECTOR_ELT(ks, i);
122114
if (Rf_isNull(c)) continue;
123115
h = key_to_u64(c);
124-
s = (item*) malloc(sizeof(item));
116+
s = (item*)malloc(sizeof(item));
125117
s->key = c;
126118
s->value = i + 1;
127119
tommy_hashlin_insert(ht, &s->node, s, h);
@@ -131,16 +123,14 @@ static tommy_hashlin* init_hashlin(SEXP self, SEXP ht_xptr) {
131123
return ht;
132124
}
133125

134-
135126
static int compare(const void* arg, const void* obj) {
136127
// return 0 if match
137-
return !R_compute_identical((SEXP) arg, (SEXP) ((item*) obj)->key, 16);
128+
return !R_compute_identical((SEXP)arg, (SEXP)((item*)obj)->key, 16);
138129
}
139130

140-
141131
static int _dict_index_get(SEXP self, SEXP ht_xptr, SEXP _key, tommy_hash_t h) {
142-
tommy_hashlin *ht;
143-
item *s;
132+
tommy_hashlin* ht;
133+
item* s;
144134
int index;
145135

146136
PROTECT(ht_xptr);
@@ -158,7 +148,6 @@ static int _dict_index_get(SEXP self, SEXP ht_xptr, SEXP _key, tommy_hash_t h) {
158148
return index;
159149
}
160150

161-
162151
SEXP dict_get(SEXP self, SEXP _key) {
163152
SEXP ht_xptr = PROTECT(get_sexp_value(self, "ht_xptr"));
164153
tommy_hash_t h = key_to_u64(_key);
@@ -182,7 +171,6 @@ SEXP dict_get(SEXP self, SEXP _key) {
182171
return res;
183172
}
184173

185-
186174
static void grow(SEXP self, int m) {
187175
SEXP ks = PROTECT(get_sexp_value(self, "ks"));
188176
SEXP vs = PROTECT(get_sexp_value(self, "vs"));
@@ -197,7 +185,7 @@ static void grow(SEXP self, int m) {
197185
SET_VECTOR_ELT(ks2, i, VECTOR_ELT(ks, i));
198186
SET_VECTOR_ELT(vs2, i, VECTOR_ELT(vs, i));
199187
}
200-
for(i = nks; i < m; i++) {
188+
for (i = nks; i < m; i++) {
201189
SET_VECTOR_ELT(ks2, i, R_NilValue);
202190
SET_VECTOR_ELT(vs2, i, R_NilValue);
203191
}
@@ -206,7 +194,6 @@ static void grow(SEXP self, int m) {
206194
UNPROTECT(4);
207195
}
208196

209-
210197
static void shrink(SEXP self, int m) {
211198
SEXP ks = PROTECT(get_sexp_value(self, "ks"));
212199
SEXP vs = PROTECT(get_sexp_value(self, "vs"));
@@ -223,7 +210,7 @@ static void shrink(SEXP self, int m) {
223210
SET_VECTOR_ELT(vs2, j, VECTOR_ELT(vs, i));
224211
j++;
225212
}
226-
for(i = j; i < m; i++) {
213+
for (i = j; i < m; i++) {
227214
SET_VECTOR_ELT(ks2, i, R_NilValue);
228215
SET_VECTOR_ELT(vs2, i, R_NilValue);
229216
}
@@ -232,22 +219,20 @@ static void shrink(SEXP self, int m) {
232219
UNPROTECT(4);
233220
}
234221

235-
236222
void _dict_index_set(SEXP self, SEXP ht_xptr, SEXP _key, tommy_hash_t h, int index) {
237223
tommy_hashlin* ht;
238-
item *s;
224+
item* s;
239225

240226
ht = R_ExternalPtrAddr(ht_xptr);
241227
if (ht == NULL) {
242228
ht = init_hashlin(self, ht_xptr);
243229
}
244-
s = (item*) malloc(sizeof(item));
230+
s = (item*)malloc(sizeof(item));
245231
s->key = _key;
246232
s->value = index;
247233
tommy_hashlin_insert(ht, &s->node, s, h);
248234
}
249235

250-
251236
SEXP dict_set(SEXP self, SEXP _key, SEXP value) {
252237
SEXP ht_xptr = PROTECT(get_sexp_value(self, "ht_xptr"));
253238
tommy_hash_t h = key_to_u64(_key);
@@ -265,7 +250,7 @@ SEXP dict_set(SEXP self, SEXP _key, SEXP value) {
265250
}
266251
int m = get_int_value(self, "m");
267252
if (index > m) {
268-
int m2 = (int) ceil(GROW_FACTOR * m);
253+
int m2 = (int)ceil(GROW_FACTOR * m);
269254
grow(self, m2);
270255
set_int_value(self, "m", m2);
271256
}
@@ -283,10 +268,9 @@ SEXP dict_set(SEXP self, SEXP _key, SEXP value) {
283268
return Rf_ScalarInteger(idx);
284269
}
285270

286-
287271
SEXP dict_remove(SEXP self, SEXP _key, SEXP _silent) {
288-
tommy_hashlin *ht;
289-
item *s;
272+
tommy_hashlin* ht;
273+
item* s;
290274
int index;
291275
int silent = Rf_asInteger(_silent);
292276

@@ -329,7 +313,6 @@ SEXP dict_remove(SEXP self, SEXP _key, SEXP _silent) {
329313
return R_NilValue;
330314
}
331315

332-
333316
SEXP dict_has(SEXP self, SEXP _key) {
334317
SEXP ht_xptr = PROTECT(get_sexp_value(self, "ht_xptr"));
335318
tommy_hash_t h = key_to_u64(_key);
@@ -338,7 +321,6 @@ SEXP dict_has(SEXP self, SEXP _key) {
338321
return Rf_ScalarLogical(index >= 1);
339322
}
340323

341-
342324
SEXP dict_keys(SEXP self) {
343325
SEXP ks = PROTECT(get_sexp_value(self, "ks"));
344326
int n = get_int_value(self, "n");
@@ -357,7 +339,6 @@ SEXP dict_keys(SEXP self) {
357339
return keys;
358340
}
359341

360-
361342
SEXP dict_values(SEXP self) {
362343
SEXP ks = PROTECT(get_sexp_value(self, "ks"));
363344
SEXP vs = PROTECT(get_sexp_value(self, "vs"));
@@ -377,7 +358,6 @@ SEXP dict_values(SEXP self) {
377358
return values;
378359
}
379360

380-
381361
SEXP dict_clear(SEXP self) {
382362
set_int_value(self, "n", 0);
383363
set_int_value(self, "m", INITIAL_SIZE);

src/xxh.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "xxh.h"
22

3-
43
int is_hashable(SEXP key) {
54
if (Rf_isNull(key)) {
65
return 1;
7-
}else if (Rf_isVectorAtomic(key)) {
6+
} else if (Rf_isVectorAtomic(key)) {
87
if (!is_hashable(ATTRIB(key))) {
98
return 0;
109
}
@@ -42,23 +41,21 @@ int is_hashable(SEXP key) {
4241
return 0;
4342
}
4443

45-
4644
// much of the following is derived from the fastdigest package but adapt to xxh
4745

4846
static char* buf1;
4947

5048
static void OutChar(R_outpstream_t stream, int c) {
51-
XXH3_state_t* const xxh_state = (XXH3_state_t* const) stream->data;
52-
buf1[0] = (char) c;
49+
XXH3_state_t* const xxh_state = (XXH3_state_t* const)stream->data;
50+
buf1[0] = (char)c;
5351
XXH3_64bits_update(xxh_state, buf1, 1);
5452
}
5553

56-
static void OutBytes(R_outpstream_t stream, void *buf, int length) {
57-
XXH3_state_t* const xxh_state = (XXH3_state_t* const) stream->data;
54+
static void OutBytes(R_outpstream_t stream, void* buf, int length) {
55+
XXH3_state_t* const xxh_state = (XXH3_state_t* const)stream->data;
5856
XXH3_64bits_update(xxh_state, buf, length);
5957
}
6058

61-
6259
XXH64_hash_t xxh_serialized_digest(SEXP x) {
6360
XXH3_state_t* const xxh_state = XXH3_createState();
6461
XXH3_64bits_reset(xxh_state);
@@ -67,8 +64,7 @@ XXH64_hash_t xxh_serialized_digest(SEXP x) {
6764
int version = 2;
6865

6966
buf1 = malloc(1);
70-
R_InitOutPStream(&stream, (R_pstream_data_t) xxh_state, type, version,
71-
OutChar, OutBytes, NULL, R_NilValue);
67+
R_InitOutPStream(&stream, (R_pstream_data_t)xxh_state, type, version, OutChar, OutBytes, NULL, R_NilValue);
7268

7369
R_Serialize(x, &stream);
7470

@@ -78,21 +74,20 @@ XXH64_hash_t xxh_serialized_digest(SEXP x) {
7874
return res;
7975
}
8076

81-
8277
XXH64_hash_t xxh_digest(SEXP x) {
8378
if (Rf_length(x) >= 0 && Rf_isVectorAtomic(x)) {
8479
// note: always materialize ALTREP
85-
char *p;
80+
char* p;
8681
if (TYPEOF(x) == STRSXP) {
8782
if (Rf_length(x) == 1) {
88-
p = (char *) Rf_translateCharUTF8(Rf_asChar(x));
83+
p = (char*)Rf_translateCharUTF8(Rf_asChar(x));
8984
return XXH3_64bits(p, strlen(p));
9085
} else {
9186
XXH3_state_t* const xxh_state = XXH3_createState();
9287
XXH3_64bits_reset(xxh_state);
9388
R_xlen_t n = Rf_length(x);
9489
for (R_xlen_t i = 0; i < n; i++) {
95-
p = (char *) Rf_translateCharUTF8(STRING_ELT(x, i));
90+
p = (char*)Rf_translateCharUTF8(STRING_ELT(x, i));
9691
XXH3_64bits_update(xxh_state, p, strlen(p));
9792
}
9893
XXH64_hash_t res = XXH3_64bits_digest(xxh_state);
@@ -101,19 +96,19 @@ XXH64_hash_t xxh_digest(SEXP x) {
10196
}
10297
}
10398
if (TYPEOF(x) == INTSXP) {
104-
p = (char*) INTEGER(x);
99+
p = (char*)INTEGER(x);
105100
return XXH3_64bits(p, Rf_length(x) * sizeof(int));
106101
}
107102
if (TYPEOF(x) == REALSXP) {
108-
p = (char*) REAL(x);
103+
p = (char*)REAL(x);
109104
return XXH3_64bits(p, Rf_length(x) * sizeof(double));
110105
}
111106
if (TYPEOF(x) == LGLSXP) {
112-
p = (char*) LOGICAL(x);
107+
p = (char*)LOGICAL(x);
113108
return XXH3_64bits(p, Rf_length(x) * sizeof(int));
114109
}
115110
if (TYPEOF(x) == RAWSXP) {
116-
p = (char*) RAW(x);
111+
p = (char*)RAW(x);
117112
return XXH3_64bits(p, Rf_length(x));
118113
}
119114
}

0 commit comments

Comments
 (0)