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:1357–1410  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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