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

Function ComputeStrides

cpp/src/arrow/extension/tensor_internal.cc:69–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67}
68
69Result<std::vector<int64_t>> ComputeStrides(const std::shared_ptr<DataType>& value_type,
70 std::span<const int64_t> shape,
71 std::span<const int64_t> permutation) {
72 const auto ndim = shape.size();
73 const int byte_width = value_type->byte_width();
74
75 // Use identity permutation if none provided
76 std::vector<int64_t> perm;
77 if (permutation.empty()) {
78 perm.resize(ndim);
79 std::iota(perm.begin(), perm.end(), 0);
80 } else {
81 perm.assign(permutation.begin(), permutation.end());
82 }
83
84 int64_t remaining = 0;
85 if (!shape.empty() && shape[0] > 0) {
86 remaining = byte_width;
87 for (auto i : perm) {
88 if (i > 0) {
89 if (MultiplyWithOverflow(remaining, shape[i], &remaining)) {
90 return Status::Invalid(
91 "Strides computed from shape would not fit in 64-bit integer");
92 }
93 }
94 }
95 }
96
97 std::vector<int64_t> strides;
98 if (remaining == 0) {
99 strides.assign(ndim, byte_width);
100 return strides;
101 }
102
103 strides.push_back(remaining);
104 for (auto i : perm) {
105 if (i > 0) {
106 remaining /= shape[i];
107 strides.push_back(remaining);
108 }
109 }
110 Permute(perm, &strides);
111
112 return strides;
113}
114
115Result<std::shared_ptr<Buffer>> SliceTensorBuffer(const Array& data_array,
116 const DataType& value_type,

Callers 3

DeserializeMethod · 0.85
DeserializeMethod · 0.85

Calls 11

MultiplyWithOverflowFunction · 0.85
PermuteFunction · 0.85
resizeMethod · 0.80
assignMethod · 0.80
push_backMethod · 0.80
InvalidFunction · 0.50
sizeMethod · 0.45
byte_widthMethod · 0.45
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected