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

Function GenerateOffsets

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

Source from the content-addressed store, hash-verified

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