| 1347 | // leverage concurrency. Maybe some refactoring needed. |
| 1348 | template <typename RVector, typename Type> |
| 1349 | bool vector_from_r_memory_impl(SEXP x, const std::shared_ptr<DataType>& type, |
| 1350 | std::vector<std::shared_ptr<arrow::ChunkedArray>>& columns, |
| 1351 | int j, RTasks& tasks) { |
| 1352 | RVector vec(x); |
| 1353 | using value_type = typename arrow::TypeTraits<Type>::ArrayType::value_type; |
| 1354 | auto buffer = std::make_shared<RBuffer<RVector>>(vec); |
| 1355 | |
| 1356 | tasks.Append(true, [buffer, x, &columns, j]() { |
| 1357 | std::vector<std::shared_ptr<Buffer>> buffers{nullptr, buffer}; |
| 1358 | |
| 1359 | auto n = XLENGTH(x); |
| 1360 | auto p_x_start = reinterpret_cast<const value_type*>(DATAPTR_RO(x)); |
| 1361 | auto p_x_end = p_x_start + n; |
| 1362 | |
| 1363 | int null_count = 0; |
| 1364 | auto first_na = std::find_if(p_x_start, p_x_end, is_NA<value_type>); |
| 1365 | |
| 1366 | if (first_na < p_x_end) { |
| 1367 | auto null_bitmap = |
| 1368 | ValueOrStop(AllocateBuffer(bit_util::BytesForBits(n), gc_memory_pool())); |
| 1369 | internal::FirstTimeBitmapWriter bitmap_writer(null_bitmap->mutable_data(), 0, n); |
| 1370 | |
| 1371 | // first loop to clear all the bits before the first NA |
| 1372 | auto k = std::distance(p_x_start, first_na); |
| 1373 | int i = 0; |
| 1374 | for (; i < k; i++, bitmap_writer.Next()) { |
| 1375 | bitmap_writer.Set(); |
| 1376 | } |
| 1377 | |
| 1378 | auto p_vec = first_na; |
| 1379 | // then finish |
| 1380 | for (; i < n; i++, bitmap_writer.Next(), ++p_vec) { |
| 1381 | if (is_NA<value_type>(*p_vec)) { |
| 1382 | bitmap_writer.Clear(); |
| 1383 | null_count++; |
| 1384 | } else { |
| 1385 | bitmap_writer.Set(); |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | bitmap_writer.Finish(); |
| 1390 | buffers[0] = std::move(null_bitmap); |
| 1391 | } |
| 1392 | |
| 1393 | auto data = ArrayData::Make(std::make_shared<Type>(), n, std::move(buffers), |
| 1394 | null_count, 0 /*offset*/); |
| 1395 | auto array = std::make_shared<typename TypeTraits<Type>::ArrayType>(data); |
| 1396 | columns[j] = std::make_shared<arrow::ChunkedArray>(array); |
| 1397 | |
| 1398 | return Status::OK(); |
| 1399 | }); |
| 1400 | |
| 1401 | return true; |
| 1402 | } |
| 1403 | |
| 1404 | bool vector_from_r_memory(SEXP x, const std::shared_ptr<DataType>& type, |
| 1405 | std::vector<std::shared_ptr<arrow::ChunkedArray>>& columns, |
nothing calls this directly
no test coverage detected