Skip to content

Commit 7fd2ee4

Browse files
committed
cleanup
1 parent 0102580 commit 7fd2ee4

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

include/highfive/mdspan.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@ struct mdspan_inspector {
2727

2828
// mdspan is trivially copyable if the value type is trivially copyable
2929
// and the layout is contiguous (which is the default)
30-
static constexpr bool is_trivially_copyable =
30+
static constexpr bool is_trivially_copyable =
3131
std::is_trivially_copyable<value_type>::value &&
3232
inspector<value_type>::is_trivially_nestable &&
3333
std::is_same_v<typename MdspanType::layout_type, std::layout_right>;
3434
static constexpr bool is_trivially_nestable = false;
3535

36-
private:
36+
private:
37+
using index_type = typename extents_type::index_type;
38+
3739
// Helper to access the first element (at index 0 in all dimensions)
3840
static auto get_first_element(const type& val) {
39-
std::array<typename extents_type::index_type, ndim> indices{};
41+
std::array<index_type, ndim> indices{};
4042
return val[indices];
4143
}
4244

43-
public:
45+
public:
4446
static size_t getRank(const type& val) {
4547
// Check if mdspan is empty (has zero size)
4648
if (val.size() == 0) {
@@ -113,8 +115,8 @@ struct mdspan_inspector {
113115
auto subsize = compute_total_size(subdims);
114116

115117
// Create index array for iteration
116-
std::array<typename extents_type::index_type, ndim> indices{};
117-
118+
std::array<index_type, ndim> indices{};
119+
118120
// Helper function to iterate through all elements
119121
auto iterate = [&](auto& self, size_t dim) -> void {
120122
if (dim == ndim) {
@@ -123,7 +125,8 @@ struct mdspan_inspector {
123125
m += subsize;
124126
} else {
125127
// Recursive case: iterate over current dimension
126-
for (indices[dim] = 0; indices[dim] < static_cast<typename extents_type::index_type>(val.extent(dim)); ++indices[dim]) {
128+
const auto n = static_cast<index_type>(val.extent(dim));
129+
for (indices[dim] = 0; indices[dim] < n; ++indices[dim]) {
127130
self(self, dim + 1);
128131
}
129132
}
@@ -146,8 +149,8 @@ struct mdspan_inspector {
146149
auto subsize = compute_total_size(subdims);
147150

148151
// Create index array for iteration
149-
std::array<typename extents_type::index_type, ndim> indices{};
150-
152+
std::array<index_type, ndim> indices{};
153+
151154
// Helper function to iterate through all elements
152155
auto iterate = [&](auto& self, size_t dim) -> void {
153156
if (dim == ndim) {
@@ -156,15 +159,15 @@ struct mdspan_inspector {
156159
vec_align += subsize;
157160
} else {
158161
// Recursive case: iterate over current dimension
159-
for (indices[dim] = 0; indices[dim] < static_cast<typename extents_type::index_type>(dims[dim]); ++indices[dim]) {
162+
const auto n = static_cast<index_type>(dims[dim]);
163+
for (indices[dim] = 0; indices[dim] < n; ++indices[dim]) {
160164
self(self, dim + 1);
161165
}
162166
}
163167
};
164168

165169
iterate(iterate, 0);
166170
}
167-
168171
};
169172

170173
// Specialization for std::mdspan
@@ -183,4 +186,3 @@ struct inspector<std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>
183186

184187
} // namespace details
185188
} // namespace HighFive
186-

src/examples/read_write_std_mdspan.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int main(void) {
3535
constexpr size_t rows = 3;
3636
constexpr size_t cols = 4;
3737
std::vector<double> values(rows * cols);
38-
38+
3939
// Fill the data
4040
for (size_t i = 0; i < rows; ++i) {
4141
for (size_t j = 0; j < cols; ++j) {
@@ -60,15 +60,13 @@ int main(void) {
6060
// that memory:
6161
auto dims = dataset.getDimensions();
6262
auto values = std::vector<double>(dataset.getElementCount());
63-
63+
6464
// Create an mdspan view over the preallocated memory using CTAD
65-
auto view = std::mdspan(values.data(),
66-
std::extents{dims[0], dims[1]});
65+
auto view = std::mdspan(values.data(), std::extents{dims[0], dims[1]});
6766

6867
// ... now we can read into the preallocated memory:
6968
dataset.read(view);
7069
}
7170

7271
return 0;
7372
}
74-

tests/unit/data_generator.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -718,30 +718,31 @@ struct MdspanContainerTraits {
718718
ContainerTraits<value_type>::sanitize_dims(dims, axis + rank);
719719
}
720720

721-
private:
722-
static std::array<typename extents_type::index_type, rank> extract_local_indices(const std::vector<size_t>& indices) {
721+
private:
722+
static std::array<typename extents_type::index_type, rank> extract_local_indices(
723+
const std::vector<size_t>& indices) {
723724
std::array<typename extents_type::index_type, rank> local_indices;
724725
for (size_t i = 0; i < rank; ++i) {
725726
local_indices[i] = static_cast<typename extents_type::index_type>(indices[i]);
726727
}
727728
return local_indices;
728729
}
729730

730-
template<size_t... Is>
731-
static extents_type create_extents_impl(const std::vector<size_t>& dims, std::index_sequence<Is...>) {
732-
return extents_type{dims[Is]...};
733-
}
734-
735731
static extents_type create_extents(const std::vector<size_t>& dims) {
736-
return create_extents_impl(dims, std::make_index_sequence<rank>{});
732+
auto impl = [&dims]<size_t... Is>(std::index_sequence<Is...>) {
733+
return extents_type{dims[Is]...};
734+
};
735+
return impl(std::make_index_sequence<rank>{});
737736
}
738737
};
739738

740739
template <class ElementType, class Extents, class LayoutPolicy, class AccessorPolicy>
741740
struct ContainerTraits<std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>>
742-
: public MdspanContainerTraits<std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>> {
741+
: public MdspanContainerTraits<
742+
std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>> {
743743
private:
744-
using super = MdspanContainerTraits<std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>>;
744+
using super =
745+
MdspanContainerTraits<std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>>;
745746

746747
public:
747748
using container_type = typename super::container_type;

0 commit comments

Comments
 (0)