-
Notifications
You must be signed in to change notification settings - Fork 241
feat: Add Ranges utility to
#5246
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12891e7
feat: Add Ranges utility `to`
paulgessinger 375b870
ranges::to docs
paulgessinger 8e15043
refactor: Extra arguments for container constructor
paulgessinger 8dad795
Revert "refactor: Extra arguments for container constructor"
paulgessinger 119e136
Merge branch 'main' into push-rtltwyptmopt
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // This file is part of the ACTS project. | ||
| // | ||
| // Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // 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/. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <ranges> | ||
|
|
||
| namespace Acts::Ranges { | ||
|
|
||
| /// Adaptor for converting a range to a container | ||
| /// @tparam Container the container type to convert to | ||
| template <template <typename...> class Container> | ||
| struct to_adaptor { | ||
| /// Convert a range to a container | ||
| /// @tparam Range the range type to convert | ||
| /// @param range the range to convert | ||
| /// @return the converted container | ||
| template <typename Range> | ||
| auto operator()(Range&& range) const { | ||
| using ValueType = std::ranges::range_value_t<Range>; | ||
| return Container<ValueType>(std::ranges::begin(range), | ||
| std::ranges::end(range)); | ||
| } | ||
| }; | ||
|
|
||
| /// Overload operator| for piping | ||
| /// @tparam Range the range type to convert | ||
| /// @tparam Container the container type to convert to | ||
| /// @param range the range to convert | ||
| /// @param adaptor the adaptor to use | ||
| /// @return the converted container | ||
| template <typename Range, template <typename...> class Container> | ||
| auto operator|(Range&& range, to_adaptor<Container> adaptor) { | ||
| return adaptor(std::forward<Range>(range)); | ||
| } | ||
|
|
||
| /// Create the adaptor objects | ||
| /// @tparam Container the container type to convert to | ||
| /// @return the adaptor object | ||
| template <template <typename...> class Container> | ||
| inline constexpr to_adaptor<Container> to{}; | ||
|
|
||
| } // namespace Acts::Ranges | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // This file is part of the ACTS project. | ||
| // | ||
| // Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // 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/unit_test.hpp> | ||
|
|
||
| #include "Acts/Utilities/Ranges.hpp" | ||
|
|
||
| #include <array> | ||
| #include <ranges> | ||
| #include <set> | ||
| #include <vector> | ||
|
|
||
| namespace ActsTests { | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(RangesTests) | ||
|
|
||
| BOOST_AUTO_TEST_CASE(ToVectorFromView) { | ||
| std::array<int, 6> values = {1, 2, 3, 4, 5, 6}; | ||
|
|
||
| auto evenTimesTen = | ||
| values | std::views::filter([](int value) { return value % 2 == 0; }) | | ||
| std::views::transform([](int value) { return value * 10; }); | ||
|
|
||
| auto result = evenTimesTen | Acts::Ranges::to<std::vector>; | ||
|
|
||
| std::vector<int> expected = {20, 40, 60}; | ||
| BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), | ||
| result.begin(), result.end()); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(ToSetRemovesDuplicatesAndOrders) { | ||
| std::vector<int> values = {4, 1, 4, 3, 2, 2}; | ||
|
|
||
| auto result = values | Acts::Ranges::to<std::set>; | ||
|
|
||
| std::set<int> expected = {1, 2, 3, 4}; | ||
| BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), | ||
| result.begin(), result.end()); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(ToAdaptorCallableSyntax) { | ||
| std::vector<int> values = {5, 6, 7}; | ||
|
|
||
| auto adaptor = Acts::Ranges::to_adaptor<std::vector>{}; | ||
| auto result = adaptor(values); | ||
|
|
||
| std::vector<int> expected = {5, 6, 7}; | ||
| BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), | ||
| result.begin(), result.end()); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() | ||
|
|
||
| } // namespace ActsTests |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.