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

Function GenerateOffsets

cpp/src/arrow/testing/random.cc:601–654  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

599
600template <typename OffsetArrayType>
601std::shared_ptr<Array> GenerateOffsets(SeedType seed, int64_t size,
602 typename OffsetArrayType::value_type first_offset,
603 typename OffsetArrayType::value_type last_offset,
604 double null_probability, bool force_empty_nulls,
605 int64_t alignment, MemoryPool* memory_pool) {
606 using GenOpt = GenerateOptions<
607 typename OffsetArrayType::value_type,
608 std::uniform_int_distribution<typename OffsetArrayType::value_type>>;
609 GenOpt options(seed, first_offset, last_offset, null_probability);
610
611 BufferVector buffers{2};
612
613 int64_t null_count = 0;
614
615 buffers[0] = *AllocateEmptyBitmap(size, alignment, memory_pool);
616 uint8_t* null_bitmap = buffers[0]->mutable_data();
617 options.GenerateBitmap(null_bitmap, size, &null_count);
618 // Make sure the first and last entry are non-null
619 for (const int64_t offset : std::vector<int64_t>{0, size - 1}) {
620 if (!arrow::bit_util::GetBit(null_bitmap, offset)) {
621 arrow::bit_util::SetBit(null_bitmap, offset);
622 --null_count;
623 }
624 }
625
626 buffers[1] = *AllocateBuffer(sizeof(typename OffsetArrayType::value_type) * size,
627 alignment, memory_pool);
628 auto data =
629 reinterpret_cast<typename OffsetArrayType::value_type*>(buffers[1]->mutable_data());
630 options.GenerateTypedData(data, size);
631 // Ensure offsets are in increasing order
632 std::sort(data, data + size);
633 // Ensure first and last offsets are as required
634 DCHECK_GE(data[0], first_offset);
635 DCHECK_LE(data[size - 1], last_offset);
636 data[0] = first_offset;
637 data[size - 1] = last_offset;
638
639 if (force_empty_nulls) {
640 arrow::internal::BitmapReader reader(null_bitmap, 0, size);
641 for (int64_t i = 0; i < size; ++i) {
642 if (reader.IsNotSet()) {
643 // Ensure a null entry corresponds to a 0-sized list extent
644 // (note this can be neither the first nor the last list entry, see above)
645 data[i + 1] = data[i];
646 }
647 reader.Next();
648 }
649 }
650
651 auto array_data = ArrayData::Make(
652 std::make_shared<typename OffsetArrayType::TypeClass>(), size, buffers, null_count);
653 return std::make_shared<OffsetArrayType>(array_data);
654}
655
656template <typename OffsetArrayType>
657std::shared_ptr<Array> OffsetsFromLengthsArray(OffsetArrayType* lengths,

Callers

nothing calls this directly

Calls 10

AllocateEmptyBitmapFunction · 0.85
SetBitFunction · 0.85
GenerateBitmapMethod · 0.80
GenerateTypedDataMethod · 0.80
GetBitFunction · 0.50
AllocateBufferFunction · 0.50
MakeFunction · 0.50
mutable_dataMethod · 0.45
IsNotSetMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected