Belle II Software development
WeightedRelation.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <tracking/trackFindingCDC/numerics/Weight.h>
11
12#include <algorithm>
13#include <utility>
14#include <type_traits>
15#include <cassert>
16
17namespace Belle2 {
22 namespace TrackFindingCDC {
23
25 template <class AFrom, class ATo = AFrom>
26 class WeightedRelation : public std::pair<std::pair<AFrom*, Weight>, ATo*> {
27
29 using Super = std::pair<std::pair<AFrom*, Weight>, ATo*>;
30
31 public:
33 using From = AFrom;
34
36 using To = ATo;
37
38 public:
40 WeightedRelation() = default;
41
43 WeightedRelation(From* from, Weight weight, To* to)
44 : Super({from, weight}, to)
45 {}
46
49 {
50 return (getFrom() < rhs.getFrom() or
51 (not(rhs.getFrom() < getFrom()) and
52 // highest weight first
53 (getWeight() > rhs.getWeight() or
54 (not(rhs.getWeight() > getWeight()) and
55 (getTo() < rhs.getTo())))));
56 }
57
59 friend bool operator<(const std::pair<From*, Weight>& weightedPtr,
60 const WeightedRelation<From, To>& weightedRelation)
61 {
62 return (weightedPtr.first < weightedRelation.getFrom() or
63 (not(weightedRelation.getFrom() < weightedPtr.first) and
64 // highest weight first
65 (weightedPtr.second > weightedRelation.getWeight())));
66 }
67
69 friend bool operator<(const WeightedRelation<From, To>& weightedRelation,
70 const std::pair<From*, Weight>& weightedPtr)
71 {
72 return (weightedRelation.getFrom() < weightedPtr.first or
73 (not(weightedPtr.first < weightedRelation.getFrom()) and
74 // highest weight first
75 (weightedRelation.getWeight() > weightedPtr.second)));
76 }
77
79 friend bool operator<(const From* ptrFrom, const WeightedRelation<From, To>& weightedRelation)
80 {
81 return ptrFrom < weightedRelation.getFrom();
82 }
83
85 friend bool operator<(const WeightedRelation<From, To>& weightedRelation, const From* ptrFrom)
86 {
87 return weightedRelation.getFrom() < ptrFrom;
88 }
89
91 From* getFrom() const
92 {
93 return getWeightedFrom().first;
94 }
95
97 Weight getWeight() const
98 {
99 return getWeightedFrom().second;
100 }
101
103 void setWeight(Weight weight)
104 {
105 this->first.second = weight;
106 }
107
109 const std::pair<From*, Weight>& getWeightedFrom() const
110 {
111 return this->first;
112 }
113
115 To* getTo() const
116 {
117 return this->second;
118 }
119
122 {
123 //return WeightedRelation<To, From>(getTo(), getWeight(), getFrom());
124 return {getTo(), getWeight(), getFrom()};
125 }
126 };
127
129 template <class AFrom, class ATo = AFrom>
136 template <class AWeightedRelations>
137 static bool areSymmetric(const AWeightedRelations& weightedRelations)
138 {
139 static_assert(std::is_same<AFrom, ATo>::value, "Symmetric check requires some types in From and To");
140 assert(std::is_sorted(std::begin(weightedRelations), std::end(weightedRelations)));
141 auto reversedRelationExists =
142 [&weightedRelations](const WeightedRelation<AFrom, ATo>& weightedRelation) -> bool {
143 auto reversedRelations = std::equal_range(std::begin(weightedRelations),
144 std::end(weightedRelations),
145 weightedRelation.reversed());
146 return reversedRelations.first != reversedRelations.second;
147 };
148 return std::all_of(std::begin(weightedRelations),
149 std::end(weightedRelations),
150 reversedRelationExists);
151 }
152 };
153 }
155}
Type for two related objects with a weight.
Weight getWeight() const
Getter for the weight.
friend bool operator<(const WeightedRelation< From, To > &weightedRelation, const From *ptrFrom)
Operator to compare key type item to the relations for associative lookups.
From * getFrom() const
Getter for the pointer to the from side object.
friend bool operator<(const WeightedRelation< From, To > &weightedRelation, const std::pair< From *, Weight > &weightedPtr)
Operator to compare key type weighted item to the relations for associative lookups.
WeightedRelation(From *from, Weight weight, To *to)
Creating a relation with one object on the from side, one on the to side and a weight.
AFrom From
Type of from which the relation originates.
const std::pair< From *, Weight > & getWeightedFrom() const
Getter for the pointer to the weighted from side object.
friend bool operator<(const std::pair< From *, Weight > &weightedPtr, const WeightedRelation< From, To > &weightedRelation)
Operator to compare key type weighted item to the relations for associative lookups.
void setWeight(Weight weight)
Setter for the weight.
ATo To
Type of to which the relation points.
To * getTo() const
Getter for the pointer to the to side object.
friend bool operator<(const From *ptrFrom, const WeightedRelation< From, To > &weightedRelation)
Operator to compare key type item to the relations for associative lookups.
bool operator<(const WeightedRelation< From, To > &rhs) const
Operator for ordering of relations.
WeightedRelation< To, From > reversed() const
Make a relation in the opposite direction with the same weight.
WeightedRelation()=default
Default constructor.
std::pair< std::pair< AFrom *, Weight >, ATo * > Super
Type of the base class.
Abstract base class for different kinds of events.
Utility structure with functions related to weighted relations.
static bool areSymmetric(const AWeightedRelations &weightedRelations)
Checks for the symmetry of a range of weighted relations Explicitly checks for each weighted relation...