11#include "data.table.h"
22
3- void nafillDouble (double * x , uint_fast64_t nx , unsigned int type , double fill , bool nan_is_na , ans_t * ans , bool verbose )
4- {
5- double tic = 0.0 ;
3+ void nafillDouble (double * x , uint_fast64_t nx , unsigned int type , double fill , bool nan_is_na , ans_t * ans , bool verbose ) {
4+ double tic = 0.0 ;
65 if (verbose )
76 tic = omp_get_wtime ();
8- if (type == 0 ) { // const
7+ if (type == 0 ) { // const
98 if (nan_is_na ) {
10- for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
9+ for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
1110 ans -> dbl_v [i ] = ISNAN (x [i ]) ? fill : x [i ];
1211 }
1312 } else {
14- for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
13+ for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
1514 ans -> dbl_v [i ] = ISNA (x [i ]) ? fill : x [i ];
1615 }
1716 }
18- } else if (type == 1 ) { // locf
17+ } else if (type == 1 ) { // locf
1918 if (nan_is_na ) {
2019 ans -> dbl_v [0 ] = ISNAN (x [0 ]) ? fill : x [0 ];
21- for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
22- ans -> dbl_v [i ] = ISNAN (x [i ]) ? ans -> dbl_v [i - 1 ] : x [i ];
20+ for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
21+ ans -> dbl_v [i ] = ISNAN (x [i ]) ? ans -> dbl_v [i - 1 ] : x [i ];
2322 }
2423 } else {
2524 ans -> dbl_v [0 ] = ISNA (x [0 ]) ? fill : x [0 ];
26- for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
27- ans -> dbl_v [i ] = ISNA (x [i ]) ? ans -> dbl_v [i - 1 ] : x [i ];
25+ for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
26+ ans -> dbl_v [i ] = ISNA (x [i ]) ? ans -> dbl_v [i - 1 ] : x [i ];
2827 }
2928 }
30- } else if (type == 2 ) { // nocb
29+ } else if (type == 2 ) { // nocb
3130 if (nan_is_na ) {
32- ans -> dbl_v [nx - 1 ] = ISNAN (x [nx - 1 ]) ? fill : x [nx - 1 ];
33- for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
34- ans -> dbl_v [i ] = ISNAN (x [i ]) ? ans -> dbl_v [i + 1 ] : x [i ];
31+ ans -> dbl_v [nx - 1 ] = ISNAN (x [nx - 1 ]) ? fill : x [nx - 1 ];
32+ for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
33+ ans -> dbl_v [i ] = ISNAN (x [i ]) ? ans -> dbl_v [i + 1 ] : x [i ];
3534 }
3635 } else {
37- ans -> dbl_v [nx - 1 ] = ISNA (x [nx - 1 ]) ? fill : x [nx - 1 ];
38- for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
39- ans -> dbl_v [i ] = ISNA (x [i ]) ? ans -> dbl_v [i + 1 ] : x [i ];
36+ ans -> dbl_v [nx - 1 ] = ISNA (x [nx - 1 ]) ? fill : x [nx - 1 ];
37+ for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
38+ ans -> dbl_v [i ] = ISNA (x [i ]) ? ans -> dbl_v [i + 1 ] : x [i ];
4039 }
4140 }
4241 }
4342 if (verbose )
44- snprintf (ans -> message [0 ], 500 , _ ("%s: took %.3fs\n" ), __func__ , omp_get_wtime () - tic );
43+ snprintf (ans -> message [0 ], 500 , _ ("%s: took %.3fs\n" ), __func__ , omp_get_wtime ()- tic );
4544}
46-
47- void nafillInteger (int32_t * x , uint_fast64_t nx , unsigned int type , int32_t fill , ans_t * ans , bool verbose )
48- {
49- double tic = 0.0 ;
45+ void nafillInteger (int32_t * x , uint_fast64_t nx , unsigned int type , int32_t fill , ans_t * ans , bool verbose ) {
46+ double tic = 0.0 ;
5047 if (verbose )
5148 tic = omp_get_wtime ();
52- if (type == 0 ) { // const
53- for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
54- ans -> int_v [i ] = x [i ] == NA_INTEGER ? fill : x [i ];
49+ if (type == 0 ) { // const
50+ for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
51+ ans -> int_v [i ] = x [i ]== NA_INTEGER ? fill : x [i ];
5552 }
56- } else if (type == 1 ) { // locf
57- ans -> int_v [0 ] = x [0 ] == NA_INTEGER ? fill : x [0 ];
58- for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
59- ans -> int_v [i ] = x [i ] == NA_INTEGER ? ans -> int_v [i - 1 ] : x [i ];
53+ } else if (type == 1 ) { // locf
54+ ans -> int_v [0 ] = x [0 ]== NA_INTEGER ? fill : x [0 ];
55+ for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
56+ ans -> int_v [i ] = x [i ]== NA_INTEGER ? ans -> int_v [i - 1 ] : x [i ];
6057 }
61- } else if (type == 2 ) { // nocb
62- ans -> int_v [nx - 1 ] = x [nx - 1 ] == NA_INTEGER ? fill : x [nx - 1 ];
63- for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
64- ans -> int_v [i ] = x [i ] == NA_INTEGER ? ans -> int_v [i + 1 ] : x [i ];
58+ } else if (type == 2 ) { // nocb
59+ ans -> int_v [nx - 1 ] = x [nx - 1 ] == NA_INTEGER ? fill : x [nx - 1 ];
60+ for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
61+ ans -> int_v [i ] = x [i ]== NA_INTEGER ? ans -> int_v [i + 1 ] : x [i ];
6562 }
6663 }
6764 if (verbose )
68- snprintf (ans -> message [0 ], 500 , _ ("%s: took %.3fs\n" ), __func__ , omp_get_wtime () - tic );
65+ snprintf (ans -> message [0 ], 500 , _ ("%s: took %.3fs\n" ), __func__ , omp_get_wtime ()- tic );
6966}
70-
71- void nafillInteger64 (int64_t * x , uint_fast64_t nx , unsigned int type , int64_t fill , ans_t * ans , bool verbose )
72- {
73- double tic = 0.0 ;
67+ void nafillInteger64 (int64_t * x , uint_fast64_t nx , unsigned int type , int64_t fill , ans_t * ans , bool verbose ) {
68+ double tic = 0.0 ;
7469 if (verbose )
7570 tic = omp_get_wtime ();
76- if (type == 0 ) { // const
77- for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
78- ans -> int64_v [i ] = x [i ] == NA_INTEGER64 ? fill : x [i ];
71+ if (type == 0 ) { // const
72+ for (uint_fast64_t i = 0 ; i < nx ; i ++ ) {
73+ ans -> int64_v [i ] = x [i ]== NA_INTEGER64 ? fill : x [i ];
7974 }
80- } else if (type == 1 ) { // locf
81- ans -> int64_v [0 ] = x [0 ] == NA_INTEGER64 ? fill : x [0 ];
82- for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
83- ans -> int64_v [i ] = x [i ] == NA_INTEGER64 ? ans -> int64_v [i - 1 ] : x [i ];
75+ } else if (type == 1 ) { // locf
76+ ans -> int64_v [0 ] = x [0 ]== NA_INTEGER64 ? fill : x [0 ];
77+ for (uint_fast64_t i = 1 ; i < nx ; i ++ ) {
78+ ans -> int64_v [i ] = x [i ]== NA_INTEGER64 ? ans -> int64_v [i - 1 ] : x [i ];
8479 }
85- } else if (type == 2 ) { // nocb
86- ans -> int64_v [nx - 1 ] = x [nx - 1 ] == NA_INTEGER64 ? fill : x [nx - 1 ];
87- for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
88- ans -> int64_v [i ] = x [i ] == NA_INTEGER64 ? ans -> int64_v [i + 1 ] : x [i ];
80+ } else if (type == 2 ) { // nocb
81+ ans -> int64_v [nx - 1 ] = x [nx - 1 ] == NA_INTEGER64 ? fill : x [nx - 1 ];
82+ for (int_fast64_t i = nx - 2 ; i >= 0 ; i -- ) {
83+ ans -> int64_v [i ] = x [i ]== NA_INTEGER64 ? ans -> int64_v [i + 1 ] : x [i ];
8984 }
9085 }
9186 if (verbose )
92- snprintf (ans -> message [0 ], 500 , _ ("%s: took %.3fs\n" ), __func__ , omp_get_wtime () - tic );
87+ snprintf (ans -> message [0 ], 500 , _ ("%s: took %.3fs\n" ), __func__ , omp_get_wtime ()- tic );
9388}
9489
9590/*
9691 OpenMP is being used here to parallelize the loop that fills missing values
9792 over columns of the input data. This includes handling different data types
9893 and applying the designated filling method to each column in parallel.
9994*/
100- SEXP nafillR (SEXP obj , SEXP type , SEXP fill , SEXP nan_is_na_arg , SEXP inplace , SEXP cols )
101- {
102- int protecti = 0 ;
95+ SEXP nafillR (SEXP obj , SEXP type , SEXP fill , SEXP nan_is_na_arg , SEXP inplace , SEXP cols ) {
96+ int protecti = 0 ;
10397 const bool verbose = GetVerbose ();
10498
10599 if (!xlength (obj ))
106100 return (obj );
107101
108- double tic = 0.0 ;
102+ double tic = 0.0 ;
109103 if (verbose )
110104 tic = omp_get_wtime ();
111105
@@ -128,8 +122,8 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
128122 SEXP ricols = PROTECT (colnamesInt (obj , cols , /* check_dups= */ ScalarLogical (TRUE), /* skip_absent= */ ScalarLogical (FALSE))); protecti ++ ; // nafill cols=NULL which turns into seq_along(obj)
129123 x = PROTECT (allocVector (VECSXP , length (ricols ))); protecti ++ ;
130124 int * icols = INTEGER (ricols );
131- for (int i = 0 ; i < length (ricols ); i ++ ) {
132- SEXP this_col = VECTOR_ELT (obj , icols [i ] - 1 );
125+ for (int i = 0 ; i < length (ricols ); i ++ ) {
126+ SEXP this_col = VECTOR_ELT (obj , icols [i ]- 1 );
133127 if (!isReal (this_col ) && !isInteger (this_col ))
134128 error (_ ("'x' argument must be numeric type, or list/data.table of numeric types" ));
135129 SET_VECTOR_ELT (x , i , this_col );
@@ -141,14 +135,14 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
141135 int64_t * * i64x = (int64_t * * )R_alloc (nx , sizeof (* i64x ));
142136 uint_fast64_t * inx = (uint_fast64_t * )R_alloc (nx , sizeof (* inx ));
143137 SEXP ans = R_NilValue ;
144- ans_t * vans = (ans_t * )R_alloc (nx , sizeof (* vans ));
145- for (R_len_t i = 0 ; i < nx ; i ++ ) {
138+ ans_t * vans = (ans_t * )R_alloc (nx , sizeof (* vans ));
139+ for (R_len_t i = 0 ; i < nx ; i ++ ) {
146140 const SEXP xi = VECTOR_ELT (x , i );
147141 inx [i ] = xlength (xi );
148142 // not sure why these pointers are being constructed like this; TODO: simplify structure // A: because they are used in `ans_t` struct, strictly speaking, one of them, the expected one, is used in that struct.
149143 if (isReal (xi )) {
150144 dx [i ] = REAL (xi );
151- i64x [i ] = (int64_t * )REAL (xi );
145+ i64x [i ] = (int64_t * )REAL (xi );
152146 ix [i ] = NULL ;
153147 } else {
154148 ix [i ] = INTEGER (xi );
@@ -158,19 +152,19 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
158152 }
159153 if (!binplace ) {
160154 ans = PROTECT (allocVector (VECSXP , nx )); protecti ++ ;
161- for (R_len_t i = 0 ; i < nx ; i ++ ) {
155+ for (R_len_t i = 0 ; i < nx ; i ++ ) {
162156 SET_VECTOR_ELT (ans , i , allocVector (TYPEOF (VECTOR_ELT (x , i )), inx [i ]));
163157 const SEXP ansi = VECTOR_ELT (ans , i );
164- const void * p = isReal (ansi ) ? (void * )REAL (ansi ) : (void * )INTEGER (ansi );
165- vans [i ] = ((ans_t ) { .dbl_v = (double * )p , .int_v = (int * )p , .int64_v = (int64_t * )p , .status = 0 , .message = { "\0" ,"\0" ,"\0" ,"\0" } });
158+ const void * p = isReal (ansi ) ? (void * )REAL (ansi ) : (void * )INTEGER (ansi );
159+ vans [i ] = ((ans_t ) { .dbl_v = (double * )p , .int_v = (int * )p , .int64_v = (int64_t * )p , .status = 0 , .message = { "\0" ,"\0" ,"\0" ,"\0" } });
166160 }
167161 } else {
168- for (R_len_t i = 0 ; i < nx ; i ++ ) {
169- vans [i ] = ((ans_t ) { .dbl_v = dx [i ], .int_v = ix [i ], .int64_v = i64x [i ], .status = 0 , .message = { "\0" ,"\0" ,"\0" ,"\0" } });
162+ for (R_len_t i = 0 ; i < nx ; i ++ ) {
163+ vans [i ] = ((ans_t ) { .dbl_v = dx [i ], .int_v = ix [i ], .int64_v = i64x [i ], .status = 0 , .message = { "\0" ,"\0" ,"\0" ,"\0" } });
170164 }
171165 }
172166
173- unsigned int itype = -1 ;
167+ unsigned int itype = -1 ;
174168 if (!strcmp (CHAR (STRING_ELT (type , 0 )), "const" ))
175169 itype = 0 ;
176170 else if (!strcmp (CHAR (STRING_ELT (type , 0 )), "locf" ))
@@ -180,14 +174,13 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
180174 else
181175 internal_error (__func__ , "invalid %s argument in %s function should have been caught earlier" , "type" , "nafillR" ); // # nocov
182176
183- bool hasFill = !isLogical (fill ) || LOGICAL (fill )[0 ] != NA_LOGICAL ;
184- bool * isInt64 = (bool * )R_alloc (nx , sizeof (* isInt64 ));
185- for (R_len_t i = 0 ; i < nx ; i ++ )
177+ bool hasFill = !isLogical (fill ) || LOGICAL (fill )[0 ]!= NA_LOGICAL ;
178+ bool * isInt64 = (bool * )R_alloc (nx , sizeof (* isInt64 ));
179+ for (R_len_t i = 0 ; i < nx ; i ++ )
186180 isInt64 [i ] = INHERITS (VECTOR_ELT (x , i ), char_integer64 );
187- const void * * fillp = (const void * * )R_alloc (nx , sizeof (* fillp )); // fill is (or will be) a list of length nx of matching types, scalar values for each column, this pointer points to each of those columns data pointers
181+ const void * * fillp = (const void * * )R_alloc (nx , sizeof (* fillp )); // fill is (or will be) a list of length nx of matching types, scalar values for each column, this pointer points to each of those columns data pointers
188182 if (hasFill ) {
189-
190- if (nx != length (fill ) && length (fill ) != 1 ) {
183+ if (nx != length (fill ) && length (fill )!= 1 ) {
191184 if (itype == 0 ) {
192185 error (_ ("fill must be a vector of length 1 or a list of length of x. Consider fcoalesce() to specify element-wise replacements." ));
193186 } else {
@@ -197,42 +190,42 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
197190 if (!isNewList (fill )) {
198191 SEXP fill1 = fill ;
199192 fill = PROTECT (allocVector (VECSXP , nx )); protecti ++ ;
200- for (int i = 0 ; i < nx ; i ++ )
193+ for (int i = 0 ; i < nx ; ++ i )
201194 SET_VECTOR_ELT (fill , i , fill1 );
202195 }
203196 if (!isNewList (fill ))
204197 internal_error (__func__ , "'fill' should be recycled as list already" ); // # nocov
205- for (R_len_t i = 0 ; i < nx ; i ++ ) {
198+ for (R_len_t i = 0 ; i < nx ; i ++ ) {
206199 SET_VECTOR_ELT (fill , i , coerceAs (VECTOR_ELT (fill , i ), VECTOR_ELT (x , i ), ScalarLogical (TRUE)));
207200 fillp [i ] = SEXPPTR_RO (VECTOR_ELT (fill , i )); // do like this so we can use in parallel region
208201 }
209202 }
210203 #pragma omp parallel for if (nx>1) num_threads(getDTthreads(nx, true))
211- for (R_len_t i = 0 ; i < nx ; i ++ ) {
204+ for (R_len_t i = 0 ; i < nx ; i ++ ) {
212205 switch (TYPEOF (VECTOR_ELT (x , i ))) {
213- case REALSXP : {
206+ case REALSXP : {
214207 if (isInt64 [i ]) {
215- nafillInteger64 (i64x [i ], inx [i ], itype , hasFill ? ((int64_t * )fillp [i ])[0 ] : NA_INTEGER64 , & vans [i ], verbose );
208+ nafillInteger64 (i64x [i ], inx [i ], itype , hasFill ? ((int64_t * )fillp [i ])[0 ] : NA_INTEGER64 , & vans [i ], verbose );
216209 } else {
217- nafillDouble (dx [i ], inx [i ], itype , hasFill ? ((double * )fillp [i ])[0 ] : NA_REAL , nan_is_na , & vans [i ], verbose );
210+ nafillDouble (dx [i ], inx [i ], itype , hasFill ? ((double * )fillp [i ])[0 ] : NA_REAL , nan_is_na , & vans [i ], verbose );
218211 }
219212 } break ;
220- case INTSXP : {
221- nafillInteger (ix [i ], inx [i ], itype , hasFill ? ((int32_t * )fillp [i ])[0 ] : NA_INTEGER , & vans [i ], verbose );
213+ case INTSXP : {
214+ nafillInteger (ix [i ], inx [i ], itype , hasFill ? ((int32_t * )fillp [i ])[0 ] : NA_INTEGER , & vans [i ], verbose );
222215 } break ;
223216 }
224217 }
225218
226219 if (!binplace ) {
227- for (R_len_t i = 0 ; i < nx ; i ++ ) {
220+ for (R_len_t i = 0 ; i < nx ; i ++ ) {
228221 if (!isNull (ATTRIB (VECTOR_ELT (x , i ))))
229222 copyMostAttrib (VECTOR_ELT (x , i ), VECTOR_ELT (ans , i ));
230223 }
231224 SEXP obj_names = getAttrib (obj , R_NamesSymbol ); // copy names
232225 if (!isNull (obj_names )) {
233226 SEXP ans_names = PROTECT (allocVector (STRSXP , length (ans ))); protecti ++ ;
234- for (int i = 0 ; i < length (ricols ); i ++ )
235- SET_STRING_ELT (ans_names , i , STRING_ELT (obj_names , icols [i ] - 1 ));
227+ for (int i = 0 ; i < length (ricols ); i ++ )
228+ SET_STRING_ELT (ans_names , i , STRING_ELT (obj_names , icols [i ]- 1 ));
236229 setAttrib (ans , R_NamesSymbol , ans_names );
237230 }
238231 }
@@ -243,7 +236,7 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
243236 Rprintf (Pl_ (nx ,
244237 "%s: parallel processing of %d column took %.3fs\n" ,
245238 "%s: parallel processing of %d columns took %.3fs\n" ),
246- __func__ , nx , omp_get_wtime () - tic );
239+ __func__ , nx , omp_get_wtime ()- tic );
247240
248241 UNPROTECT (protecti );
249242 if (binplace ) {
0 commit comments