Belle II Software development
WithWeightedItems< T, AItem > Class Template Reference

A mixin class to attach a set of weighted items to a class. More...

#include <WithWeightedItems.h>

Inheritance diagram for WithWeightedItems< T, AItem >:

Public Member Functions

 WithWeightedItems (const T &t)
 Also forward the copy constructor form the base class object.
 
TrackingUtilities::Weight getWeight () const
 Cumulated weight of the contained items.
 
template<class APredicate>
void eraseIf (const APredicate &predicate)
 Erase items from this node that satisfy the predicate.
 
void insert (const AItem &item, TrackingUtilities::Weight weight=1.0)
 Add an item with weight.
 
template<class AMeasure>
void insert (const This &items, AMeasure &measure)
 Add the items from another item range assigning a weight from the predicate.
 
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator begin ()
 Begin iterator of the contained items.
 
std::vector< TrackingUtilities::WithWeight< AItem > >::const_iterator begin () const
 Begin iterator of the contained items.
 
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator end ()
 End iterator of the contained items.
 
std::vector< TrackingUtilities::WithWeight< AItem > >::const_iterator end () const
 End iterator of the contained items.
 
size_t size () const
 Getter for the number of items.
 
void clear ()
 Clear the contained items.
 

Private Types

using Super = T
 Type of the base class.
 
using This = WithWeightedItems<T, AItem>
 Type of this class.
 

Private Attributes

std::vector< TrackingUtilities::WithWeight< AItem > > m_items
 Memory for the weighted items.
 
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator m_itEnd {m_items.end()}
 Memory for the end of the items that are not erased.
 
TrackingUtilities::Weight m_weightSum = 0.0
 Cumulated weight of the contained items, kept up to date on insertion and removal.
 

Detailed Description

template<class T, class AItem>
class Belle2::TrackFindingCDC::WithWeightedItems< T, AItem >

A mixin class to attach a set of weighted items to a class.

Definition at line 25 of file WithWeightedItems.h.

Member Typedef Documentation

◆ Super

template<class T, class AItem>
using Super = T
private

Type of the base class.

Definition at line 29 of file WithWeightedItems.h.

◆ This

template<class T, class AItem>
using This = WithWeightedItems<T, AItem>
private

Type of this class.

Definition at line 32 of file WithWeightedItems.h.

Constructor & Destructor Documentation

◆ WithWeightedItems()

template<class T, class AItem>
WithWeightedItems ( const T & t)
inlineexplicit

Also forward the copy constructor form the base class object.

Definition at line 42 of file WithWeightedItems.h.

43 : T(t)
44 {
45 }

Member Function Documentation

◆ begin() [1/2]

template<class T, class AItem>
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator begin ( )
inline

Begin iterator of the contained items.

Definition at line 96 of file WithWeightedItems.h.

97 {
98 return m_items.begin();
99 }

◆ begin() [2/2]

template<class T, class AItem>
std::vector< TrackingUtilities::WithWeight< AItem > >::const_iterator begin ( ) const
inline

Begin iterator of the contained items.

Definition at line 102 of file WithWeightedItems.h.

103 {
104 return m_items.begin();
105 }

◆ clear()

template<class T, class AItem>
void clear ( )
inline

Clear the contained items.

Definition at line 127 of file WithWeightedItems.h.

128 {
129 m_items.clear();
130 m_itEnd = m_items.end();
131 m_weightSum = 0.0;
132 Super& super = *this;
133 TrackingUtilities::clearIfApplicable(super);
134 }

◆ end() [1/2]

template<class T, class AItem>
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator end ( )
inline

End iterator of the contained items.

Definition at line 108 of file WithWeightedItems.h.

109 {
110 return m_itEnd;
111 }

◆ end() [2/2]

template<class T, class AItem>
std::vector< TrackingUtilities::WithWeight< AItem > >::const_iterator end ( ) const
inline

End iterator of the contained items.

Definition at line 114 of file WithWeightedItems.h.

115 {
116 return m_itEnd;
117 }

◆ eraseIf()

template<class T, class AItem>
template<class APredicate>
void eraseIf ( const APredicate & predicate)
inline

Erase items from this node that satisfy the predicate.

Definition at line 55 of file WithWeightedItems.h.

56 {
57 // Sneakily hide the items instead of erasing them.
58 // auto notPredicate = [&predicate](AItem & item) { return not predicate(item); };
59 // m_itEnd = std::partition(m_items.begin(), m_items.end(), notPredicate);
60 // Properly delete use the following.
61 m_itEnd = std::remove_if(m_items.begin(), m_items.end(), predicate);
62 if (m_itEnd != m_items.end()) {
63 m_items.erase(m_itEnd, m_items.end());
64 // Recompute the cumulated weight in the same order as a fresh accumulation
65 m_weightSum = std::accumulate(begin(),
66 end(),
67 static_cast<TrackingUtilities::Weight>(0.0),
68 [](TrackingUtilities::Weight accumulatedWeight, const TrackingUtilities::WithWeight<AItem>& weightedItem) {
69 return accumulatedWeight + weightedItem.getWeight();
70 });
71 }
72 }

◆ getWeight()

template<class T, class AItem>
TrackingUtilities::Weight getWeight ( ) const
inline

Cumulated weight of the contained items.

Definition at line 48 of file WithWeightedItems.h.

49 {
50 return m_weightSum;
51 }

◆ insert() [1/2]

template<class T, class AItem>
void insert ( const AItem & item,
TrackingUtilities::Weight weight = 1.0 )
inline

Add an item with weight.

Definition at line 75 of file WithWeightedItems.h.

76 {
77 // The end marker always coincides with the end of the item vector, hence append.
78 m_items.emplace_back(item, weight);
79 m_itEnd = m_items.end();
80 m_weightSum += weight;
81 }

◆ insert() [2/2]

template<class T, class AItem>
template<class AMeasure>
void insert ( const This & items,
AMeasure & measure )
inline

Add the items from another item range assigning a weight from the predicate.

Definition at line 85 of file WithWeightedItems.h.

86 {
87 for (AItem item : items.m_items) {
88 const TrackingUtilities::Weight weight = measure(item);
89 if (not std::isnan(weight)) {
90 insert(item, weight);
91 }
92 }
93 }

◆ size()

template<class T, class AItem>
size_t size ( ) const
inline

Getter for the number of items.

Definition at line 120 of file WithWeightedItems.h.

121 {
122 return std::distance(begin(), end());
123 }

Member Data Documentation

◆ m_items

template<class T, class AItem>
std::vector<TrackingUtilities::WithWeight<AItem> > m_items
private

Memory for the weighted items.

Definition at line 138 of file WithWeightedItems.h.

◆ m_itEnd

template<class T, class AItem>
std::vector<TrackingUtilities::WithWeight<AItem>>::iterator m_itEnd {m_items.end()}
private

Memory for the end of the items that are not erased.

Definition at line 141 of file WithWeightedItems.h.

141{m_items.end()};

◆ m_weightSum

template<class T, class AItem>
TrackingUtilities::Weight m_weightSum = 0.0
private

Cumulated weight of the contained items, kept up to date on insertion and removal.

Definition at line 144 of file WithWeightedItems.h.


The documentation for this class was generated from the following file: