Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make TransformRange fulfill range concept #3971

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Core/include/Acts/Utilities/TransformRange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ struct TransformRangeIterator {
/// Construct an iterator from an underlying iterator
explicit TransformRangeIterator(iterator_t iterator) : m_iterator(iterator) {}

TransformRangeIterator() = default;

/// Return a reference to the value that is transformed by the callable
/// @return Reference to the transformed value
reference operator*() { return Callable::apply(*m_iterator); }
Expand All @@ -184,6 +186,14 @@ struct TransformRangeIterator {
return *this;
}

/// Advance the iterator
/// @return Reference to the iterator
TransformRangeIterator operator++(int) {
auto tmp = *this;
++m_iterator;
return tmp;
}

/// Compare two iterators for equality
/// @param other The other iterator to compare to
bool operator==(const TransformRangeIterator& other) const {
Expand Down Expand Up @@ -219,3 +229,9 @@ struct DotGet {
};

} // namespace Acts::detail

/// @cond
template <typename Callable, typename container_t>
constexpr bool std::ranges::enable_borrowed_range<
Acts::detail::TransformRange<Callable, container_t>> = true;
/// @endcond
24 changes: 24 additions & 0 deletions Tests/UnitTests/Core/Utilities/TransformRangeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include <boost/test/tools/old/interface.hpp>
#include <boost/test/unit_test.hpp>

#include "Acts/Utilities/TransformRange.hpp"

#include <algorithm>

using namespace Acts;

BOOST_AUTO_TEST_SUITE(TransformRangeTests)
Expand Down Expand Up @@ -99,6 +102,14 @@ BOOST_AUTO_TEST_CASE(TransformRangeDeref) {
static_assert(std::is_same_v<decltype(*(++r.begin())), int&>);
checkSameAddresses(v, r);

std::vector<int> unpacked;
std::ranges::transform(r, std::back_inserter(unpacked),
[](auto val) { return val; });
std::vector<int> exp = {1, 2, 4};

BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), unpacked.begin(),
unpacked.end());

auto cr = detail::TransformRange{detail::ConstDereference{}, raw_v};
static_assert(std::is_same_v<decltype(cr)::value_type, const int>);
static_assert(std::is_same_v<decltype(cr)::reference, const int&>);
Expand All @@ -108,6 +119,13 @@ BOOST_AUTO_TEST_CASE(TransformRangeDeref) {
static_assert(std::is_same_v<decltype(*cr.begin()), const int&>);
static_assert(std::is_same_v<decltype(*(++cr.begin())), const int&>);
checkSameAddresses(v, r);

unpacked.clear();
std::ranges::transform(cr, std::back_inserter(unpacked),
[](auto val) { return val; });

BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), unpacked.begin(),
unpacked.end());
}
}

Expand All @@ -127,6 +145,12 @@ BOOST_AUTO_TEST_CASE(TransformRangeDeref) {
static_assert(std::is_same_v<decltype(*r.begin()), const int&>);
static_assert(std::is_same_v<decltype(*(++r.begin())), const int&>);
checkSameAddresses(v, r);

std::vector<int> unpacked;
std::ranges::transform(r, std::back_inserter(unpacked),
[](auto val) { return val; });

BOOST_CHECK(unpacked == std::vector<int>({1, 2, 3}));
}

std::vector<const int*> raw_v;
Expand Down
Loading