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
1516#endif
1617#endif
1718
18-
1919typedef struct item {
2020 SEXP key ;
2121 int value ;
2222 tommy_node node ;
2323} item ;
2424
25-
2625static 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-
3735static_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-
4744static_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-
5753static_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-
6661tommy_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-
10496static 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-
135126static 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-
141131static 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-
162151SEXP 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-
186174static 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-
210197static 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-
236222void _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-
251236SEXP 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-
287271SEXP 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-
333316SEXP 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-
342324SEXP 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-
361342SEXP 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-
381361SEXP dict_clear (SEXP self ) {
382362 set_int_value (self , "n" , 0 );
383363 set_int_value (self , "m" , INITIAL_SIZE );
0 commit comments