| 289 | } |
| 290 | |
| 291 | Result<std::shared_ptr<Tensor>> MakeTensorFromSparseCOOTensor( |
| 292 | MemoryPool* pool, const SparseCOOTensor* sparse_tensor) { |
| 293 | const auto& sparse_index = |
| 294 | checked_cast<const SparseCOOIndex&>(*sparse_tensor->sparse_index()); |
| 295 | const auto& coords = sparse_index.indices(); |
| 296 | const auto* coords_data = coords->raw_data(); |
| 297 | |
| 298 | const int index_elsize = coords->type()->byte_width(); |
| 299 | |
| 300 | const auto& value_type = checked_cast<const FixedWidthType&>(*sparse_tensor->type()); |
| 301 | const int value_elsize = value_type.byte_width(); |
| 302 | ARROW_ASSIGN_OR_RAISE(auto values_buffer, |
| 303 | AllocateBuffer(value_elsize * sparse_tensor->size(), pool)); |
| 304 | auto values = values_buffer->mutable_data(); |
| 305 | std::fill_n(values, value_elsize * sparse_tensor->size(), 0); |
| 306 | |
| 307 | std::vector<int64_t> strides; |
| 308 | RETURN_NOT_OK(ComputeRowMajorStrides(value_type, sparse_tensor->shape(), &strides)); |
| 309 | |
| 310 | const auto* raw_data = sparse_tensor->raw_data(); |
| 311 | const int ndim = sparse_tensor->ndim(); |
| 312 | |
| 313 | for (int64_t i = 0; i < sparse_tensor->non_zero_length(); ++i) { |
| 314 | int64_t offset = 0; |
| 315 | |
| 316 | for (int j = 0; j < ndim; ++j) { |
| 317 | auto index = static_cast<int64_t>( |
| 318 | SparseTensorConverterMixin::GetIndexValue(coords_data, index_elsize)); |
| 319 | offset += index * strides[j]; |
| 320 | coords_data += index_elsize; |
| 321 | } |
| 322 | |
| 323 | std::copy_n(raw_data, value_elsize, values + offset); |
| 324 | raw_data += value_elsize; |
| 325 | } |
| 326 | |
| 327 | return std::make_shared<Tensor>(sparse_tensor->type(), std::move(values_buffer), |
| 328 | sparse_tensor->shape(), strides, |
| 329 | sparse_tensor->dim_names()); |
| 330 | } |
| 331 | |
| 332 | } // namespace internal |
| 333 | } // namespace arrow |
no test coverage detected