Skip to content

Commit b3d279e

Browse files
committed
Refactor function signatures to use trailing return type syntax for improved readability
1 parent b2d72ce commit b3d279e

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

source/pbat/common/ArgSort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace common {
2929
* @return Eigen::Vector<TIndex, Eigen::Dynamic>
3030
*/
3131
template <std::integral TIndex, class FLess>
32-
Eigen::Vector<TIndex, Eigen::Dynamic> ArgSort(TIndex n, FLess less)
32+
auto ArgSort(TIndex n, FLess less) -> Eigen::Vector<TIndex, Eigen::Dynamic>
3333
{
3434
using IndexVectorType = Eigen::Vector<TIndex, Eigen::Dynamic>;
3535
IndexVectorType inds(n);

source/pbat/common/Eigen.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace common {
2525
* @return Eigen vector adaptor
2626
*/
2727
template <CContiguousArithmeticRange R>
28-
Eigen::Map<Eigen::Vector<std::ranges::range_value_t<R>, Eigen::Dynamic> const> ToEigen(R&& r)
28+
auto ToEigen(R&& r)
29+
-> Eigen::Map<Eigen::Vector<std::ranges::range_value_t<R>, Eigen::Dynamic> const>
2930
{
3031
namespace rng = std::ranges;
3132
return Eigen::Map<Eigen::Vector<std::ranges::range_value_t<R>, Eigen::Dynamic> const>(
@@ -40,11 +41,10 @@ Eigen::Map<Eigen::Vector<std::ranges::range_value_t<R>, Eigen::Dynamic> const> T
4041
* @return Eigen matrix adaptor
4142
*/
4243
template <CContiguousArithmeticMatrixRange R>
43-
Eigen::Map<Eigen::Matrix<
44+
auto ToEigen(R&& r) -> Eigen::Map<Eigen::Matrix<
4445
typename std::ranges::range_value_t<R>::Scalar,
4546
Eigen::Dynamic,
4647
Eigen::Dynamic> const>
47-
ToEigen(R&& r)
4848
{
4949
namespace rng = std::ranges;
5050
using ValueType = rng::range_value_t<R>;

source/pbat/common/Hash.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,46 +52,86 @@ std::size_t HashCombine(const Types&... args)
5252

5353
namespace std {
5454

55+
/**
56+
* @brief Hash function for pair of Index
57+
*/
5558
template <>
5659
struct hash<pair<pbat::Index, pbat::Index>>
5760
{
61+
/**
62+
* @brief Hash function for pair of Index
63+
* @param inds Pair of indices
64+
* @return std::size_t Hash value
65+
*/
5866
[[maybe_unused]] std::size_t operator()(pair<pbat::Index, pbat::Index> const& inds) const
5967
{
6068
return pbat::common::HashCombine(inds.first, inds.second);
6169
}
6270
};
6371

72+
/**
73+
* @brief Hash function for 2-tuple of Index
74+
*/
6475
template <>
6576
struct hash<tuple<pbat::Index, pbat::Index>>
6677
{
78+
/**
79+
* @brief Hash function for 2-tuple of Index
80+
* @param inds 2-tuple of indices
81+
* @return std::size_t Hash value
82+
*/
6783
[[maybe_unused]] std::size_t operator()(tuple<pbat::Index, pbat::Index> const& inds) const
6884
{
6985
return pbat::common::HashCombine(get<0>(inds), get<1>(inds));
7086
}
7187
};
7288

89+
/**
90+
* @brief Hash function for 3-tuple of Index
91+
*/
7392
template <>
7493
struct hash<tuple<pbat::Index, pbat::Index, pbat::Index>>
7594
{
95+
/**
96+
* @brief Hash function for 3-tuple of Index
97+
* @param inds 3-tuple of indices
98+
* @return std::size_t Hash value
99+
*/
76100
[[maybe_unused]] std::size_t
77101
operator()(tuple<pbat::Index, pbat::Index, pbat::Index> const& inds) const
78102
{
79103
return pbat::common::HashCombine(get<0>(inds), get<1>(inds), get<2>(inds));
80104
}
81105
};
82106

107+
/**
108+
* @brief Hash function for pbat::IndexVector<2>
109+
*/
83110
template <>
84111
struct hash<pbat::IndexVector<2>>
85112
{
113+
/**
114+
* @brief Hash function for pbat::IndexVector<2>
115+
* @param inds Index vector
116+
* @return std::size_t Hash value
117+
*/
86118
[[maybe_unused]] std::size_t operator()(pbat::IndexVector<2> const& inds) const
87119
{
88120
return pbat::common::HashCombine(inds(0), inds(1));
89121
}
90122
};
91123

124+
/**
125+
* @brief Hash function for pbat::IndexVector<3>
126+
*/
92127
template <>
93128
struct hash<pbat::IndexVector<3>>
94129
{
130+
/**
131+
* @brief Hash function for pbat::IndexVector<3>
132+
* @param inds Index vector
133+
* @return std::size_t Hash value
134+
*/
95135
[[maybe_unused]] std::size_t operator()(pbat::IndexVector<3> const& inds) const
96136
{
97137
return pbat::common::HashCombine(inds(0), inds(1), inds(2));

source/pbat/common/Indexing.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
#include <doctest/doctest.h>
66
#include <ranges>
77

8-
namespace pbat {
9-
namespace common {
10-
118
TEST_CASE("[common] Cumulative sums are computable from any integral range type")
129
{
10+
using namespace pbat;
1311
std::array<Index, 3> v{5, 10, 15};
14-
auto const cs = CumSum(v);
12+
auto const cs = common::CumSum(v);
1513
IndexVectorX csExpected(4);
1614
csExpected << 0, 5, 15, 30;
1715
CHECK_EQ(cs, csExpected);
@@ -34,7 +32,4 @@ TEST_CASE("[common] Repeat on Eigen vectors works as expected")
3432
CHECK_EQ(result.size(), expected.size());
3533
bool const bAreEqual = (result.array() == expected.array()).all();
3634
CHECK(bAreEqual);
37-
}
38-
39-
} // namespace common
40-
} // namespace pbat
35+
}

source/pbat/common/Indexing.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace common {
3131
* @return Eigen::Vector<TIndex, Eigen::Dynamic> Cumulative sum of the range
3232
*/
3333
template <CIndexRange R, std::integral TIndex = std::ranges::range_value_t<R>>
34-
Eigen::Vector<TIndex, Eigen::Dynamic> CumSum(R&& sizes)
34+
auto CumSum(R&& sizes) -> Eigen::Vector<TIndex, Eigen::Dynamic>
3535
{
3636
namespace rng = std::ranges;
3737
using IndexVectorType = Eigen::Vector<TIndex, Eigen::Dynamic>;
@@ -54,7 +54,7 @@ Eigen::Vector<TIndex, Eigen::Dynamic> CumSum(R&& sizes)
5454
* @return Eigen::Vector<TIndex, Eigen::Dynamic> Counts of each integer in the range
5555
*/
5656
template <std::integral TIndex>
57-
Eigen::Vector<TIndex, Eigen::Dynamic> Counts(auto begin, auto end, TIndex ncounts)
57+
auto Counts(auto begin, auto end, TIndex ncounts) -> Eigen::Vector<TIndex, Eigen::Dynamic>
5858
{
5959
using IndexVectorType = Eigen::Vector<TIndex, Eigen::Dynamic>;
6060
IndexVectorType counts(ncounts);
@@ -73,7 +73,7 @@ Eigen::Vector<TIndex, Eigen::Dynamic> Counts(auto begin, auto end, TIndex ncount
7373
* @return Eigen::Vector<TIndex, Eigen::Dynamic> Shuffled range of integers
7474
*/
7575
template <std::integral TIndex>
76-
Eigen::Vector<TIndex, Eigen::Dynamic> Shuffle(TIndex begin, TIndex end)
76+
auto Shuffle(TIndex begin, TIndex end) -> Eigen::Vector<TIndex, Eigen::Dynamic>
7777
{
7878
auto iota = std::views::iota(begin, end);
7979
Eigen::Vector<TIndex, Eigen::Dynamic> inds(end - begin);
@@ -101,7 +101,7 @@ template <
101101
std::integral TIndexE,
102102
class Func,
103103
class TIndex = std::common_type_t<TIndexB, TIndexE>>
104-
Eigen::Vector<TIndex, Eigen::Dynamic> Filter(TIndexB begin, TIndexE end, Func&& f)
104+
auto Filter(TIndexB begin, TIndexE end, Func&& f) -> Eigen::Vector<TIndex, Eigen::Dynamic>
105105
{
106106
auto filteredView = std::views::iota(static_cast<TIndex>(begin), static_cast<TIndex>(end)) |
107107
std::views::filter(f);
@@ -129,8 +129,8 @@ template <
129129
class TDerivedR,
130130
class TScalar = typename TDerivedX::Scalar,
131131
std::integral TIndex = typename TDerivedR::Scalar>
132-
Eigen::Vector<TScalar, Eigen::Dynamic>
133-
Repeat(Eigen::DenseBase<TDerivedX> const& x, Eigen::DenseBase<TDerivedR> const& r)
132+
auto Repeat(Eigen::DenseBase<TDerivedX> const& x, Eigen::DenseBase<TDerivedR> const& r)
133+
-> Eigen::Vector<TScalar, Eigen::Dynamic>
134134
{
135135
using VectorType = Eigen::Vector<TScalar, Eigen::Dynamic>;
136136
VectorType y(r.sum());

0 commit comments

Comments
 (0)