Skip to content

Commit

Permalink
Rename overload selection tags and refine docstring wording
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Dec 2, 2024
1 parent 6dadc4a commit 9837182
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
50 changes: 27 additions & 23 deletions include/podio/LinkNavigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ namespace detail::links {
};

/// Simple struct tag for overload selection in LinkNavigator below
struct LookupFromTag {};
struct ReturnFromTag {};
/// Simple struct tag for overload selection in LinkNavigator below
struct LookupToTag {};
struct ReturnToTag {};
} // namespace detail::links

/// Tag variable to select the lookup of *From* objects that are linked from a
/// *To* object in podio::LinkNavigator::getLinked
static constexpr detail::links::LookupFromTag LookupFrom;
/// Tag variable to select the lookup of *To* objects that are linked from a
/// Tag variable to select the lookup of *From* objects have links with a *To*
/// object in podio::LinkNavigator::getLinked
static constexpr detail::links::ReturnFromTag ReturnFrom;
/// Tag variable to select the lookup of *To* objects that have links with a
/// *From* object in podio::LinkNavigator::getLinked
static constexpr detail::links::LookupToTag LookupTo;
static constexpr detail::links::ReturnToTag ReturnTo;

/// A helper class to more easily handle one-to-many links.
///
Expand Down Expand Up @@ -69,7 +69,8 @@ class LinkNavigator {
LinkNavigator& operator=(LinkNavigator&&) = default;
~LinkNavigator() = default;

/// Get all the objects and weights that are linked to the passed object
/// Get all the *From* objects and weights that have links with the passed
/// object
///
/// You will get this overload if you pass the podio::LookupFrom tag as second
/// argument
Expand All @@ -78,12 +79,12 @@ class LinkNavigator {
/// to construct this instance of the LinkNavigator has the same From and To
/// types.
///
/// @param object The object that is labeled *to* in the link
/// @param object The object that is labeled *To* in the link
/// @param . tag variable for selecting this overload
///
/// @returns A vector of all objects and their weights that are linked to
/// @returns A vector of all objects and their weights that have links with
/// the passed object
std::vector<WeightedObject<FromT>> getLinked(const ToT& object, podio::detail::links::LookupFromTag) const {
std::vector<WeightedObject<FromT>> getLinked(const ToT& object, podio::detail::links::ReturnFromTag) const {
const auto& [begin, end] = m_to2from.equal_range(object);
std::vector<WeightedObject<FromT>> result;
result.reserve(std::distance(begin, end));
Expand All @@ -94,22 +95,24 @@ class LinkNavigator {
return result;
}

/// Get all the objects and weights that are linked to the passed object
/// Get all the *From* objects and weights that have links with the passed
/// object
///
/// @note This overload will automatically do the right thing (TM) in case the
/// LinkCollection that has been passed to construct this LinkNavigator has
/// different From and To types.
///
/// @param object The object that is labeled *to* in the link
/// @param object The object that is labeled *To* in the link
///
/// @returns A vector of all objects and their weights that are linked to
/// @returns A vector of all objects and their weights that have links with
/// the passed object
template <typename ToU = ToT>
std::enable_if_t<!std::is_same_v<FromT, ToU>, std::vector<WeightedObject<FromT>>> getLinked(const ToT& object) const {
return getLinked(object, podio::LookupFrom);
return getLinked(object, podio::ReturnFrom);
}

/// Get all the objects and weights that are linked to the passed object
/// Get all the *To* objects and weights that have links with the passed
/// object
///
/// You will get this overload if you pass the podio::LookupTo tag as second
/// argument
Expand All @@ -118,12 +121,12 @@ class LinkNavigator {
/// to construct this instance of the LinkNavigator has the same From and To
/// types.
///
/// @param object The object that is labeled *from* in the link
/// @param object The object that is labeled *From* in the link
/// @param . tag variable for selecting this overload
///
/// @returns A vector of all objects and their weights that are linked to
/// @returns A vector of all objects and their weights that have links with
/// the passed object
std::vector<WeightedObject<ToT>> getLinked(const FromT& object, podio::detail::links::LookupToTag) const {
std::vector<WeightedObject<ToT>> getLinked(const FromT& object, podio::detail::links::ReturnToTag) const {
const auto& [begin, end] = m_from2to.equal_range(object);
std::vector<WeightedObject<ToT>> result;
result.reserve(std::distance(begin, end));
Expand All @@ -134,19 +137,20 @@ class LinkNavigator {
return result;
}

/// Get all the objects and weights that are linked to the passed object
/// Get all the *To* objects and weights that have links with the passed
/// object
///
/// @note This overload will automatically do the right thing (TM) in case the
/// LinkCollection that has been passed to construct this LinkNavigator has
/// different From and To types.
///
/// @param object The object that is labeled *from* in the link
/// @param object The object that is labeled *From* in the link
///
/// @returns A vector of all objects and their weights that are linked to
/// @returns A vector of all objects and their weights that have links with
/// the passed object
template <typename FromU = FromT>
std::enable_if_t<!std::is_same_v<FromU, ToT>, std::vector<WeightedObject<ToT>>> getLinked(const FromT& object) const {
return getLinked(object, podio::LookupTo);
return getLinked(object, podio::ReturnTo);
}

private:
Expand Down
8 changes: 4 additions & 4 deletions tests/unittests/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,24 +539,24 @@ TEST_CASE("LinkNavigator same types", "[links]") {
link.setWeight(0.66f);

auto navigator = podio::LinkNavigator{linkColl};
auto linkedClusters = navigator.getLinked(clusters[1], podio::LookupTo);
auto linkedClusters = navigator.getLinked(clusters[1], podio::ReturnTo);
REQUIRE(linkedClusters.size() == 1);
REQUIRE(linkedClusters[0].o == clusters[2]);
REQUIRE(linkedClusters[0].weight == 0.66f);

linkedClusters = navigator.getLinked(clusters[1], podio::LookupTo);
linkedClusters = navigator.getLinked(clusters[1], podio::ReturnTo);
REQUIRE(linkedClusters.size() == 1);
REQUIRE(linkedClusters[0].o == clusters[0]);
REQUIRE(linkedClusters[0].weight == 0.5f);

using Catch::Matchers::UnorderedEquals;
using podio::detail::links::WeightedObject;
using WeightedObjVec = std::vector<WeightedObject<ExampleCluster>>;
linkedClusters = navigator.getLinked(clusters[0], podio::LookupTo);
linkedClusters = navigator.getLinked(clusters[0], podio::ReturnTo);
REQUIRE_THAT(linkedClusters,
UnorderedEquals(WeightedObjVec{WeightedObject(clusters[1], 0.5f), WeightedObject{clusters[2], 0.25f}}));

linkedClusters = navigator.getLinked(clusters[2], podio::LookupFrom);
linkedClusters = navigator.getLinked(clusters[2], podio::ReturnFrom);
REQUIRE_THAT(linkedClusters,
UnorderedEquals(WeightedObjVec{WeightedObject{clusters[0], 0.25f}, WeightedObject{clusters[1], 0.66f}}));
}

0 comments on commit 9837182

Please sign in to comment.