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.
 
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, 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< WithWeight< AItem > >::iterator begin ()
 Begin iterator of the contained items.
 
std::vector< WithWeight< AItem > >::const_iterator begin () const
 Begin iterator of the contained items.
 
std::vector< WithWeight< AItem > >::iterator end ()
 End iterator of the contained items.
 
std::vector< 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< WithWeight< AItem > > m_items
 Memory for the weighted items.
 
std::vector< WithWeight< AItem > >::iterator m_itEnd {m_items.end()}
 Memory for the end of the items that are not erased.
 

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

using Super = T
private

Type of the base class.

Definition at line 29 of file WithWeightedItems.h.

◆ This

using This = WithWeightedItems<T, AItem>
private

Type of this class.

Definition at line 32 of file WithWeightedItems.h.

Constructor & Destructor Documentation

◆ WithWeightedItems()

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]

std::vector< WithWeight< AItem > >::iterator begin ( )
inline

Begin iterator of the contained items.

Definition at line 91 of file WithWeightedItems.h.

92 {
93 return m_items.begin();
94 }
std::vector< WithWeight< AItem > > m_items
Memory for the weighted items.

◆ begin() [2/2]

std::vector< WithWeight< AItem > >::const_iterator begin ( ) const
inline

Begin iterator of the contained items.

Definition at line 97 of file WithWeightedItems.h.

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

◆ clear()

void clear ( )
inline

Clear the contained items.

Definition at line 122 of file WithWeightedItems.h.

123 {
124 m_items.clear();
125 m_itEnd = m_items.end();
126 Super& super = *this;
127 clearIfApplicable(super);
128 }
std::vector< WithWeight< AItem > >::iterator m_itEnd
Memory for the end of the items that are not erased.

◆ end() [1/2]

std::vector< WithWeight< AItem > >::iterator end ( )
inline

End iterator of the contained items.

Definition at line 103 of file WithWeightedItems.h.

104 {
105 return m_itEnd;
106 }

◆ end() [2/2]

std::vector< WithWeight< AItem > >::const_iterator end ( ) const
inline

End iterator of the contained items.

Definition at line 109 of file WithWeightedItems.h.

110 {
111 return m_itEnd;
112 }

◆ eraseIf()

void eraseIf ( const APredicate &  predicate)
inline

Erase items from this node that satisfy the predicate.

Definition at line 60 of file WithWeightedItems.h.

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 }

◆ getWeight()

Weight getWeight ( ) const
inline

Cumulated weight of the contained items.

Definition at line 48 of file WithWeightedItems.h.

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 }
std::vector< WithWeight< AItem > >::iterator begin()
Begin iterator of the contained items.
std::vector< WithWeight< AItem > >::iterator end()
End iterator of the contained items.

◆ insert() [1/2]

void insert ( const AItem &  item,
Weight  weight = 1.0 
)
inline

Add an item with weight.

Definition at line 71 of file WithWeightedItems.h.

72 {
73 m_itEnd = m_items.insert(end(), WithWeight<AItem>(item));
74 m_itEnd->setWeight(weight);
75 ++m_itEnd;
76 }

◆ insert() [2/2]

void insert ( const This items,
AMeasure &  measure 
)
inline

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

Definition at line 80 of file WithWeightedItems.h.

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 }
void insert(const AItem &item, Weight weight=1.0)
Add an item with weight.

◆ size()

size_t size ( ) const
inline

Getter for the number of items.

Definition at line 115 of file WithWeightedItems.h.

116 {
117 return std::distance(begin(), end());
118 }

Member Data Documentation

◆ m_items

std::vector<WithWeight<AItem> > m_items
private

Memory for the weighted items.

Definition at line 132 of file WithWeightedItems.h.

◆ m_itEnd

std::vector<WithWeight<AItem>>::iterator m_itEnd {m_items.end()}
private

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

Definition at line 135 of file WithWeightedItems.h.


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