MCPcopy Create free account
hub / github.com/apache/arrow / vector_from_r_memory_impl

Function vector_from_r_memory_impl

r/src/r_to_arrow.cpp:1350–1403  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 12

ValueOrStopFunction · 0.85
BytesForBitsFunction · 0.85
gc_memory_poolFunction · 0.85
AllocateBufferFunction · 0.50
MakeFunction · 0.50
OKFunction · 0.50
AppendMethod · 0.45
mutable_dataMethod · 0.45
NextMethod · 0.45
SetMethod · 0.45
ClearMethod · 0.45
FinishMethod · 0.45

Tested by

no test coverage detected