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

Function MakeSimpleArray

r/src/r_to_arrow.cpp:1237–1281  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1235// the R object, via an RBuffer, hence be zero copy
1236template <int RTYPE, typename RVector, typename Type>
1237std::shared_ptr<Array> MakeSimpleArray(SEXP x) {
1238 using value_type = typename arrow::TypeTraits<Type>::ArrayType::value_type;
1239 RVector vec(x);
1240 auto n = vec.size();
1241 auto p_vec_start = reinterpret_cast<const value_type*>(DATAPTR_RO(vec));
1242 auto p_vec_end = p_vec_start + n;
1243 std::vector<std::shared_ptr<Buffer>> buffers{nullptr,
1244 std::make_shared<RBuffer<RVector>>(vec)};
1245
1246 int null_count = 0;
1247
1248 auto first_na = std::find_if(p_vec_start, p_vec_end, is_NA<value_type>);
1249 if (first_na < p_vec_end) {
1250 auto null_bitmap =
1251 ValueOrStop(AllocateBuffer(bit_util::BytesForBits(n), gc_memory_pool()));
1252 internal::FirstTimeBitmapWriter bitmap_writer(null_bitmap->mutable_data(), 0, n);
1253
1254 // first loop to clear all the bits before the first NA
1255 auto j = std::distance(p_vec_start, first_na);
1256 int i = 0;
1257 for (; i < j; i++, bitmap_writer.Next()) {
1258 bitmap_writer.Set();
1259 }
1260
1261 auto p_vec = first_na;
1262 // then finish
1263 for (; i < n; i++, bitmap_writer.Next(), ++p_vec) {
1264 if (is_NA<value_type>(*p_vec)) {
1265 bitmap_writer.Clear();
1266 null_count++;
1267 } else {
1268 bitmap_writer.Set();
1269 }
1270 }
1271
1272 bitmap_writer.Finish();
1273 buffers[0] = std::move(null_bitmap);
1274 }
1275
1276 auto data = ArrayData::Make(std::make_shared<Type>(), LENGTH(x), std::move(buffers),
1277 null_count, 0 /*offset*/);
1278
1279 // return the right Array class
1280 return std::make_shared<typename TypeTraits<Type>::ArrayType>(data);
1281}
1282
1283std::shared_ptr<arrow::Array> vec_to_arrow__reuse_memory(SEXP x) {
1284 auto type = TYPEOF(x);

Callers

nothing calls this directly

Calls 11

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

Tested by

no test coverage detected