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

Function MakeSimpleArray

r/src/r_to_arrow.cpp:1230–1274  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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