Skip to content

Commit ff3ea71

Browse files
committed
Rename length to non_zero_length in SparseTensor
1 parent f782303 commit ff3ea71

File tree

7 files changed

+35
-35
lines changed

7 files changed

+35
-35
lines changed

cpp/src/arrow/compare.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ struct SparseTensorEqualsImpl<SparseIndexType, SparseIndexType> {
800800
const SparseTensor<SparseIndexType>& right) {
801801
DCHECK(left.type()->id() == right.type()->id());
802802
DCHECK(left.shape() == right.shape());
803-
DCHECK(left.length() == right.length());
803+
DCHECK(left.non_zero_length() == right.non_zero_length());
804804

805805
const auto& left_index = checked_cast<const SparseIndexType&>(*left.sparse_index());
806806
const auto& right_index = checked_cast<const SparseIndexType&>(*right.sparse_index());
@@ -816,7 +816,7 @@ struct SparseTensorEqualsImpl<SparseIndexType, SparseIndexType> {
816816
const uint8_t* left_data = left.data()->data();
817817
const uint8_t* right_data = right.data()->data();
818818

819-
return memcmp(left_data, right_data, static_cast<size_t>(byte_width * left.length()));
819+
return memcmp(left_data, right_data, static_cast<size_t>(byte_width * left.non_zero_length()));
820820
}
821821
};
822822

@@ -852,7 +852,7 @@ bool SparseTensorEquals(const SparseTensorBase& left, const SparseTensorBase& ri
852852
return true;
853853
} else if (left.shape() != right.shape()) {
854854
return false;
855-
} else if (left.length() != right.length()) {
855+
} else if (left.non_zero_length() != right.non_zero_length()) {
856856
return false;
857857
}
858858

cpp/src/arrow/ipc/metadata-internal.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,9 @@ Status MakeSparseTensor(FBB& fbb, const SparseTensorBase& sparse_tensor,
863863
const BufferMetadata& data_metadata = buffers[num_index_buffers];
864864
flatbuf::Buffer data(data_metadata.offset, data_metadata.length);
865865

866-
int64_t length = sparse_tensor.length();
866+
const int64_t non_zero_length = sparse_tensor.non_zero_length();
867867

868-
*offset = flatbuf::CreateSparseTensor(fbb, fb_type_type, fb_type, fb_shape, length,
868+
*offset = flatbuf::CreateSparseTensor(fbb, fb_type_type, fb_type, fb_shape, non_zero_length,
869869
fb_sparse_index_type, fb_sparse_index, &data);
870870

871871
return Status::OK();
@@ -1037,7 +1037,7 @@ Status GetTensorMetadata(const Buffer& metadata, std::shared_ptr<DataType>* type
10371037

10381038
Status GetSparseTensorMetadata(const Buffer& metadata, std::shared_ptr<DataType>* type,
10391039
std::vector<int64_t>* shape,
1040-
std::vector<std::string>* dim_names, int64_t* length,
1040+
std::vector<std::string>* dim_names, int64_t* non_zero_length,
10411041
SparseTensorFormat::type* sparse_tensor_format_id) {
10421042
auto message = flatbuf::GetMessage(metadata.data());
10431043
if (message->header_type() != flatbuf::MessageHeader_SparseTensor) {
@@ -1062,7 +1062,7 @@ Status GetSparseTensorMetadata(const Buffer& metadata, std::shared_ptr<DataType>
10621062
}
10631063
}
10641064

1065-
*length = sparse_tensor->length();
1065+
*non_zero_length = sparse_tensor->non_zero_length();
10661066

10671067
switch (sparse_tensor->sparseIndex_type()) {
10681068
case flatbuf::SparseTensorIndex_SparseTensorIndexCOO:

cpp/src/arrow/ipc/read-write-test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ void TestSparseTensorRoundTrip::CheckSparseTensorRoundTrip<SparseCOOIndex>(
872872

873873
const auto& sparse_index = checked_cast<const SparseCOOIndex&>(*tensor.sparse_index());
874874
const int64_t indices_length = elem_size * sparse_index.indices()->size();
875-
const int64_t data_length = elem_size * tensor.length();
875+
const int64_t data_length = elem_size * tensor.non_zero_length();
876876
const int64_t expected_body_length = indices_length + data_length;
877877
ASSERT_EQ(expected_body_length, body_length);
878878

@@ -905,7 +905,7 @@ void TestSparseTensorRoundTrip::CheckSparseTensorRoundTrip<SparseCSRIndex>(
905905
const auto& sparse_index = checked_cast<const SparseCSRIndex&>(*tensor.sparse_index());
906906
const int64_t indptr_length = elem_size * sparse_index.indptr()->size();
907907
const int64_t indices_length = elem_size * sparse_index.indices()->size();
908-
const int64_t data_length = elem_size * tensor.length();
908+
const int64_t data_length = elem_size * tensor.non_zero_length();
909909
const int64_t expected_body_length = indptr_length + indices_length + data_length;
910910
ASSERT_EQ(expected_body_length, body_length);
911911

cpp/src/arrow/ipc/reader.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -730,23 +730,23 @@ Status ReadTensor(const Message& message, std::shared_ptr<Tensor>* out) {
730730
namespace {
731731

732732
Status ReadSparseCOOIndex(const flatbuf::SparseTensor* sparse_tensor, int64_t ndim,
733-
int64_t length, io::RandomAccessFile* file,
733+
int64_t non_zero_length, io::RandomAccessFile* file,
734734
std::shared_ptr<SparseIndex>* out) {
735735
auto* sparse_index = sparse_tensor->sparseIndex_as_SparseTensorIndexCOO();
736736
auto* indices_buffer = sparse_index->indicesBuffer();
737737
std::shared_ptr<Buffer> indices_data;
738738
RETURN_NOT_OK(
739739
file->ReadAt(indices_buffer->offset(), indices_buffer->length(), &indices_data));
740-
std::vector<int64_t> shape({length, ndim});
740+
std::vector<int64_t> shape({non_zero_length, ndim});
741741
const int64_t elsize = sizeof(int64_t);
742-
std::vector<int64_t> strides({elsize, elsize * length});
742+
std::vector<int64_t> strides({elsize, elsize * non_zero_length});
743743
*out = std::make_shared<SparseCOOIndex>(
744744
std::make_shared<SparseCOOIndex::CoordsTensor>(indices_data, shape, strides));
745745
return Status::OK();
746746
}
747747

748748
Status ReadSparseCSRIndex(const flatbuf::SparseTensor* sparse_tensor, int64_t ndim,
749-
int64_t length, io::RandomAccessFile* file,
749+
int64_t non_zero_length, io::RandomAccessFile* file,
750750
std::shared_ptr<SparseIndex>* out) {
751751
auto* sparse_index = sparse_tensor->sparseIndex_as_SparseMatrixIndexCSR();
752752

@@ -761,7 +761,7 @@ Status ReadSparseCSRIndex(const flatbuf::SparseTensor* sparse_tensor, int64_t nd
761761
file->ReadAt(indices_buffer->offset(), indices_buffer->length(), &indices_data));
762762

763763
std::vector<int64_t> indptr_shape({ndim + 1});
764-
std::vector<int64_t> indices_shape({length});
764+
std::vector<int64_t> indices_shape({non_zero_length});
765765
*out = std::make_shared<SparseCSRIndex>(
766766
std::make_shared<SparseCSRIndex::IndexTensor>(indptr_data, indptr_shape),
767767
std::make_shared<SparseCSRIndex::IndexTensor>(indices_data, indices_shape));
@@ -771,7 +771,7 @@ Status ReadSparseCSRIndex(const flatbuf::SparseTensor* sparse_tensor, int64_t nd
771771
Status MakeSparseTensorWithSparseCOOIndex(
772772
const std::shared_ptr<DataType>& type, const std::vector<int64_t>& shape,
773773
const std::vector<std::string>& dim_names,
774-
const std::shared_ptr<SparseCOOIndex>& sparse_index, int64_t length,
774+
const std::shared_ptr<SparseCOOIndex>& sparse_index, int64_t non_zero_length,
775775
const std::shared_ptr<Buffer>& data, std::shared_ptr<SparseTensorBase>* out) {
776776
auto* sparse_tensor =
777777
new SparseTensor<SparseCOOIndex>(sparse_index, type, data, shape, dim_names);
@@ -782,7 +782,7 @@ Status MakeSparseTensorWithSparseCOOIndex(
782782
Status MakeSparseTensorWithSparseCSRIndex(
783783
const std::shared_ptr<DataType>& type, const std::vector<int64_t>& shape,
784784
const std::vector<std::string>& dim_names,
785-
const std::shared_ptr<SparseCSRIndex>& sparse_index, int64_t length,
785+
const std::shared_ptr<SparseCSRIndex>& sparse_index, int64_t non_zero_length,
786786
const std::shared_ptr<Buffer>& data, std::shared_ptr<SparseTensorBase>* out) {
787787
auto* sparse_tensor =
788788
new SparseTensor<SparseCSRIndex>(sparse_index, type, data, shape, dim_names);
@@ -797,11 +797,11 @@ Status ReadSparseTensor(const Buffer& metadata, io::RandomAccessFile* file,
797797
std::shared_ptr<DataType> type;
798798
std::vector<int64_t> shape;
799799
std::vector<std::string> dim_names;
800-
int64_t length;
800+
int64_t non_zero_length;
801801
SparseTensorFormat::type sparse_tensor_format_id;
802802

803803
RETURN_NOT_OK(internal::GetSparseTensorMetadata(metadata, &type, &shape, &dim_names,
804-
&length, &sparse_tensor_format_id));
804+
&non_zero_length, &sparse_tensor_format_id));
805805

806806
auto message = flatbuf::GetMessage(metadata.data());
807807
auto sparse_tensor = reinterpret_cast<const flatbuf::SparseTensor*>(message->header());
@@ -817,17 +817,17 @@ Status ReadSparseTensor(const Buffer& metadata, io::RandomAccessFile* file,
817817
switch (sparse_tensor_format_id) {
818818
case SparseTensorFormat::COO:
819819
RETURN_NOT_OK(
820-
ReadSparseCOOIndex(sparse_tensor, shape.size(), length, file, &sparse_index));
820+
ReadSparseCOOIndex(sparse_tensor, shape.size(), non_zero_length, file, &sparse_index));
821821
return MakeSparseTensorWithSparseCOOIndex(
822822
type, shape, dim_names, std::dynamic_pointer_cast<SparseCOOIndex>(sparse_index),
823-
length, data, out);
823+
non_zero_length, data, out);
824824

825825
case SparseTensorFormat::CSR:
826826
RETURN_NOT_OK(
827-
ReadSparseCSRIndex(sparse_tensor, shape.size(), length, file, &sparse_index));
827+
ReadSparseCSRIndex(sparse_tensor, shape.size(), non_zero_length, file, &sparse_index));
828828
return MakeSparseTensorWithSparseCSRIndex(
829829
type, shape, dim_names, std::dynamic_pointer_cast<SparseCSRIndex>(sparse_index),
830-
length, data, out);
830+
non_zero_length, data, out);
831831

832832
default:
833833
return Status::Invalid("Unsupported sparse index format");

cpp/src/arrow/sparse_tensor-test.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ TEST(TestSparseCOOTensor, CreationEmptyTensor) {
4545
std::vector<std::string> dim_names = {"foo", "bar", "baz"};
4646
SparseTensor<SparseCOOIndex> st2(int64(), shape, dim_names);
4747

48-
ASSERT_EQ(0, st1.length());
49-
ASSERT_EQ(0, st2.length());
48+
ASSERT_EQ(0, st1.non_zero_length());
49+
ASSERT_EQ(0, st2.non_zero_length());
5050

5151
ASSERT_EQ(24, st1.size());
5252
ASSERT_EQ(24, st2.size());
@@ -73,7 +73,7 @@ TEST(TestSparseCOOTensor, CreationFromNumericTensor) {
7373

7474
CheckSparseIndexFormatType(SparseTensorFormat::COO, st1);
7575

76-
ASSERT_EQ(12, st1.length());
76+
ASSERT_EQ(12, st1.non_zero_length());
7777
ASSERT_TRUE(st1.is_mutable());
7878

7979
ASSERT_EQ("foo", st2.dim_name(0));
@@ -136,7 +136,7 @@ TEST(TestSparseCOOTensor, CreationFromTensor) {
136136
SparseTensor<SparseCOOIndex> st1(tensor1);
137137
SparseTensor<SparseCOOIndex> st2(tensor2);
138138

139-
ASSERT_EQ(12, st1.length());
139+
ASSERT_EQ(12, st1.non_zero_length());
140140
ASSERT_TRUE(st1.is_mutable());
141141

142142
ASSERT_EQ("foo", st2.dim_name(0));
@@ -200,7 +200,7 @@ TEST(TestSparseCSRMatrix, CreationFromNumericTensor2D) {
200200

201201
CheckSparseIndexFormatType(SparseTensorFormat::CSR, st1);
202202

203-
ASSERT_EQ(12, st1.length());
203+
ASSERT_EQ(12, st1.non_zero_length());
204204
ASSERT_TRUE(st1.is_mutable());
205205

206206
ASSERT_EQ("foo", st2.dim_name(0));

cpp/src/arrow/sparse_tensor.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ namespace arrow {
3232

3333
class ARROW_EXPORT SparseIndex {
3434
public:
35-
explicit SparseIndex(SparseTensorFormat::type format_id, int64_t length)
36-
: format_id_(format_id), length_(length) {}
35+
explicit SparseIndex(SparseTensorFormat::type format_id, int64_t non_zero_length)
36+
: format_id_(format_id), non_zero_length_(non_zero_length) {}
3737

3838
virtual ~SparseIndex() = default;
3939

4040
SparseTensorFormat::type format_id() const { return format_id_; }
41-
int64_t length() const { return length_; }
41+
int64_t non_zero_length() const { return non_zero_length_; }
4242

4343
virtual std::string ToString() const = 0;
4444

4545
protected:
4646
SparseTensorFormat::type format_id_;
47-
int64_t length_;
47+
int64_t non_zero_length_;
4848
};
4949

5050
template <typename SparseIndexType>
5151
class SparseIndexBase : public SparseIndex {
5252
public:
53-
explicit SparseIndexBase(int64_t length)
54-
: SparseIndex(SparseIndexType::format_id, length) {}
53+
explicit SparseIndexBase(int64_t non_zero_length)
54+
: SparseIndex(SparseIndexType::format_id, non_zero_length) {}
5555
};
5656

5757
// ----------------------------------------------------------------------
@@ -137,7 +137,7 @@ class ARROW_EXPORT SparseTensorBase {
137137
bool is_mutable() const { return data_->is_mutable(); }
138138

139139
/// Total number of non-zero cells in the sparse tensor
140-
int64_t length() const { return sparse_index_ ? sparse_index_->length() : 0; }
140+
int64_t non_zero_length() const { return sparse_index_ ? sparse_index_->non_zero_length() : 0; }
141141

142142
bool Equals(const SparseTensorBase& other) const;
143143

format/Tensor.fbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ table SparseTensor {
134134
shape: [TensorDim];
135135

136136
/// The number of non-zero values in a sparse tensor.
137-
length: long;
137+
non_zero_length: long;
138138

139139
/// Sparse tensor index
140140
sparseIndex: SparseTensorIndex;

0 commit comments

Comments
 (0)