Belle II Software development
WithWeightedItems.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/WithWeight.h>
11
12#include <vector>
13#include <algorithm>
14#include <numeric>
15#include <cmath>
16
17namespace Belle2 {
22 namespace TrackFindingCDC {
24 template <class T, class AItem>
25 class WithWeightedItems : public T {
26
27 private:
29 using Super = T;
30
33
34 public:
36 using T::T;
37
39 using T::operator=;
40
42 explicit WithWeightedItems(const T& t)
43 : T(t)
44 {
45 }
46
48 Weight getWeight() const
49 {
50 return std::accumulate(begin(),
51 end(),
52 static_cast<Weight>(0.0),
53 [](Weight accumulatedWeight, const WithWeight<AItem>& weightedItem) {
54 return accumulatedWeight + weightedItem.getWeight();
55 });
56 }
57
59 template <class APredicate>
60 void eraseIf(const APredicate& predicate)
61 {
62 // Sneakily hide the items instead of erasing them.
63 // auto notPredicate = [&predicate](AItem & item) { return not predicate(item); };
64 // m_itEnd = std::partition(m_items.begin(), m_items.end(), notPredicate);
65 // Properly delete use the following.
66 m_itEnd = std::remove_if(m_items.begin(), m_items.end(), predicate);
67 m_items.erase(m_itEnd, m_items.end());
68 }
69
71 void insert(const AItem& item, Weight weight = 1.0)
72 {
73 m_itEnd = m_items.insert(end(), WithWeight<AItem>(item));
74 m_itEnd->setWeight(weight);
75 ++m_itEnd;
76 }
77
79 template <class AMeasure>
80 void insert(const This& items, AMeasure& measure)
81 {
82 for (AItem item : items.m_items) {
83 const Weight weight = measure(item);
84 if (not std::isnan(weight)) {
85 insert(item, weight);
86 }
87 }
88 }
89
91 typename std::vector<WithWeight<AItem>>::iterator begin()
92 {
93 return m_items.begin();
94 }
95
97 typename std::vector<WithWeight<AItem>>::const_iterator begin() const
98 {
99 return m_items.begin();
100 }
101
103 typename std::vector<WithWeight<AItem>>::iterator end()
104 {
105 return m_itEnd;
106 }
107
109 typename std::vector<WithWeight<AItem>>::const_iterator end() const
110 {
111 return m_itEnd;
112 }
113
115 size_t size() const
116 {
117 return std::distance(begin(), end());
118 }
119
120 public:
122 void clear()
123 {
124 m_items.clear();
125 m_itEnd = m_items.end();
126 Super& super = *this;
127 clearIfApplicable(super);
128 }
129
130 private:
132 std::vector<WithWeight<AItem>> m_items;
133
135 typename std::vector<WithWeight<AItem>>::iterator m_itEnd{m_items.end()};
136 };
137 }
139}
A mixin class to attach a weight to an object.
Definition: WithWeight.h:24
Weight getWeight() const
Getter for the weight.
Definition: WithWeight.h:58
A mixin class to attach a set of weighted items to a class.
size_t size() const
Getter for the number of items.
Weight getWeight() const
Cumulated weight of the contained items.
std::vector< WithWeight< AItem > > m_items
Memory for the weighted items.
void eraseIf(const APredicate &predicate)
Erase items from this node that satisfy the predicate.
WithWeightedItems(const T &t)
Also forward the copy constructor form the base class object.
std::vector< WithWeight< AItem > >::iterator begin()
Begin iterator of the contained items.
std::vector< WithWeight< AItem > >::const_iterator end() const
End iterator of the contained items.
std::vector< WithWeight< AItem > >::iterator end()
End iterator of the contained items.
void insert(const This &items, AMeasure &measure)
Add the items from another item range assigning a weight from the predicate.
void clear()
Clear the contained items.
std::vector< WithWeight< AItem > >::iterator m_itEnd
Memory for the end of the items that are not erased.
std::vector< WithWeight< AItem > >::const_iterator begin() const
Begin iterator of the contained items.
void insert(const AItem &item, Weight weight=1.0)
Add an item with weight.
Abstract base class for different kinds of events.