-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: use c++20 concept to define insert function for different …
…types of seed collections (#3367) Taking from @<!---->CouthuresJeremy 's PR I'm here proposing a possible alternative that would not even be a breaking change (unless I'm missing something). This will use c++20 concepts to define different versions of an `insert` function that will add a seed to the output collection. This will work with 'back_insert_operator' (as we currently do) as well as collection that support `push_back` and/or `insert` blocked by - #3345 - #3377 edit: Now this is breaking since we do not support back_insert_operator option anymore
- Loading branch information
1 parent
b4c888d
commit 88c1223
Showing
11 changed files
with
177 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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) 2024 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 http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include <ranges> | ||
|
||
namespace Acts::detail { | ||
|
||
// Define some concepts | ||
template <typename external_t> | ||
concept isCollectionThatSupportsPushBack = | ||
std::ranges::range<external_t> && requires { | ||
typename external_t::value_type; | ||
} && requires(external_t coll, typename external_t::value_type val) { | ||
coll.push_back(val); | ||
}; | ||
|
||
template <typename external_t> | ||
concept isCollectionThatSupportsInsert = | ||
std::ranges::range<external_t> && requires { | ||
typename external_t::value_type; | ||
} && requires(external_t coll, typename external_t::value_type val) { | ||
coll.insert(std::ranges::end(coll), val); | ||
}; | ||
|
||
// Define some functions | ||
template <typename value_t> | ||
void pushBackOrInsertAtEnd( | ||
Acts::detail::isCollectionThatSupportsPushBack auto& storage, | ||
value_t&& value) { | ||
storage.push_back(std::forward<value_t>(value)); | ||
} | ||
|
||
template <std::ranges::range storage_t, typename value_t> | ||
requires(!Acts::detail::isCollectionThatSupportsPushBack<storage_t> && | ||
Acts::detail::isCollectionThatSupportsInsert< | ||
storage_t>) void pushBackOrInsertAtEnd(storage_t& storage, | ||
value_t&& value) { | ||
storage.insert(std::ranges::end(storage), std::forward<value_t>(value)); | ||
} | ||
|
||
} // namespace Acts::detail |
This file contains 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 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 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 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,85 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2024 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 http://mozilla.org/MPL/2.0/. | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include "Acts/Seeding/detail/UtilityFunctions.hpp" | ||
|
||
#include <iterator> | ||
#include <list> | ||
#include <set> | ||
#include <string> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
namespace Acts::Test { | ||
|
||
BOOST_AUTO_TEST_CASE(pushBackOrInsertAtEnd_vector) { | ||
std::vector<std::size_t> coll; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 2ul); | ||
BOOST_CHECK(coll.size() == 1ul); | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 5ul); | ||
BOOST_CHECK(coll.size() == 2ul); | ||
std::size_t val = 1ul; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, val); | ||
BOOST_CHECK(coll.size() == 3ul); | ||
|
||
BOOST_CHECK_EQUAL(coll[0], 2ul); | ||
BOOST_CHECK_EQUAL(coll[1], 5ul); | ||
BOOST_CHECK_EQUAL(coll[2], 1ul); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(pushBackOrInsertAtEnd_list) { | ||
std::list<std::size_t> coll; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 2ul); | ||
BOOST_CHECK(coll.size() == 1ul); | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 5ul); | ||
BOOST_CHECK(coll.size() == 2ul); | ||
std::size_t val = 1ul; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, val); | ||
BOOST_CHECK(coll.size() == 3ul); | ||
|
||
BOOST_CHECK_EQUAL(coll.front(), 2ul); | ||
coll.pop_front(); | ||
BOOST_CHECK_EQUAL(coll.front(), 5ul); | ||
coll.pop_front(); | ||
BOOST_CHECK_EQUAL(coll.front(), 1ul); | ||
coll.pop_front(); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(pushBackOrInsertAtEnd_set) { | ||
std::set<std::size_t> coll; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 2ul); | ||
BOOST_CHECK(coll.size() == 1ul); | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 5ul); | ||
BOOST_CHECK(coll.size() == 2ul); | ||
std::size_t val = 1ul; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, val); | ||
BOOST_CHECK(coll.size() == 3ul); | ||
|
||
BOOST_CHECK(coll.find(2ul) != coll.end()); | ||
BOOST_CHECK(coll.find(5ul) != coll.end()); | ||
BOOST_CHECK(coll.find(1ul) != coll.end()); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(pushBackOrInsertAtEnd_unordered_set) { | ||
std::unordered_set<std::size_t> coll; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 2ul); | ||
BOOST_CHECK(coll.size() == 1ul); | ||
Acts::detail::pushBackOrInsertAtEnd(coll, 5ul); | ||
BOOST_CHECK(coll.size() == 2ul); | ||
std::size_t val = 1ul; | ||
Acts::detail::pushBackOrInsertAtEnd(coll, val); | ||
BOOST_CHECK(coll.size() == 3ul); | ||
|
||
BOOST_CHECK(coll.find(2ul) != coll.end()); | ||
BOOST_CHECK(coll.find(5ul) != coll.end()); | ||
BOOST_CHECK(coll.find(1ul) != coll.end()); | ||
} | ||
|
||
} // namespace Acts::Test |
Oops, something went wrong.