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

Method Convert

cpp/src/arrow/tensor/coo_converter.cc:171–225  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169 : tensor_(tensor), index_value_type_(index_value_type), pool_(pool) {}
170
171 Status Convert() {
172 RETURN_NOT_OK(::arrow::internal::CheckSparseIndexMaximumValue(index_value_type_,
173 tensor_.shape()));
174
175 const int index_elsize = index_value_type_->byte_width();
176 const int value_elsize = tensor_.type()->byte_width();
177
178 const int64_t ndim = tensor_.ndim();
179 ARROW_ASSIGN_OR_RAISE(int64_t nonzero_count, tensor_.CountNonZero());
180
181 ARROW_ASSIGN_OR_RAISE(auto indices_buffer,
182 AllocateBuffer(index_elsize * ndim * nonzero_count, pool_));
183 uint8_t* indices = indices_buffer->mutable_data();
184
185 ARROW_ASSIGN_OR_RAISE(auto values_buffer,
186 AllocateBuffer(value_elsize * nonzero_count, pool_));
187 uint8_t* values = values_buffer->mutable_data();
188
189 const uint8_t* tensor_data = tensor_.raw_data();
190 if (ndim <= 1) {
191 const int64_t count = ndim == 0 ? 1 : tensor_.shape()[0];
192 for (int64_t i = 0; i < count; ++i) {
193 if (std::any_of(tensor_data, tensor_data + value_elsize, IsNonZero)) {
194 AssignIndex(indices, i, index_elsize);
195 std::copy_n(tensor_data, value_elsize, values);
196
197 indices += index_elsize;
198 values += value_elsize;
199 }
200 tensor_data += value_elsize;
201 }
202 } else if (tensor_.is_row_major()) {
203 DISPATCH(CONVERT_ROW_MAJOR_TENSOR, index_elsize, value_elsize, indices, values,
204 nonzero_count);
205 } else if (tensor_.is_column_major()) {
206 DISPATCH(CONVERT_COLUMN_MAJOR_TENSOR, index_elsize, value_elsize, indices, values,
207 nonzero_count);
208 } else {
209 DISPATCH(CONVERT_STRIDED_TENSOR, index_elsize, value_elsize, indices, values,
210 nonzero_count);
211 }
212
213 // make results
214 const std::vector<int64_t> indices_shape = {nonzero_count, ndim};
215 std::vector<int64_t> indices_strides;
216 RETURN_NOT_OK(internal::ComputeRowMajorStrides(
217 checked_cast<const FixedWidthType&>(*index_value_type_), indices_shape,
218 &indices_strides));
219 auto coords = std::make_shared<Tensor>(index_value_type_, std::move(indices_buffer),
220 indices_shape, indices_strides);
221 ARROW_ASSIGN_OR_RAISE(sparse_index, SparseCOOIndex::Make(coords, true));
222 data = std::move(values_buffer);
223
224 return Status::OK();
225 }
226
227 std::shared_ptr<SparseCOOIndex> sparse_index;
228 std::shared_ptr<Buffer> data;

Callers 1

Calls 12

ComputeRowMajorStridesFunction · 0.85
raw_dataMethod · 0.80
is_row_majorMethod · 0.80
is_column_majorMethod · 0.80
ARROW_ASSIGN_OR_RAISEFunction · 0.50
MakeFunction · 0.50
OKFunction · 0.50
shapeMethod · 0.45
byte_widthMethod · 0.45
typeMethod · 0.45
mutable_dataMethod · 0.45

Tested by

no test coverage detected