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

Function MakeSimpleArray

r/src/r_to_arrow.cpp:1229–1273  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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