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

Function StridedTensorCountNonZero

cpp/src/arrow/tensor.cc:546–563  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

544
545template <typename TYPE>
546int64_t StridedTensorCountNonZero(int dim_index, int64_t offset, const Tensor& tensor) {
547 using c_type = typename TYPE::c_type;
548 const c_type zero = c_type(0);
549 int64_t nnz = 0;
550 if (dim_index == tensor.ndim() - 1) {
551 for (int64_t i = 0; i < tensor.shape()[dim_index]; ++i) {
552 const auto* ptr = tensor.raw_data() + offset + i * tensor.strides()[dim_index];
553 auto& elem = *reinterpret_cast<const c_type*>(ptr);
554 if (elem != zero) ++nnz;
555 }
556 return nnz;
557 }
558 for (int64_t i = 0; i < tensor.shape()[dim_index]; ++i) {
559 nnz += StridedTensorCountNonZero<TYPE>(dim_index + 1, offset, tensor);
560 offset += tensor.strides()[dim_index];
561 }
562 return nnz;
563}
564
565template <typename TYPE>
566int64_t ContiguousTensorCountNonZero(const Tensor& tensor) {

Callers

nothing calls this directly

Calls 3

raw_dataMethod · 0.80
stridesMethod · 0.80
shapeMethod · 0.45

Tested by

no test coverage detected