Belle II Software  release-05-02-19
RelationVector.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2012 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/datastore/RelationEntry.h>
14 #include <framework/utilities/ArrayIterator.h>
15 
16 #include <vector>
17 #include <string>
18 #include <functional>
19 
20 namespace Belle2 {
26  class RelationVectorBase {
27  public:
29  RelationVectorBase() { }
30 
32  RelationVectorBase(const std::string& name, int index, const std::vector<Belle2::RelationEntry>& relations,
33  const std::vector<std::string>& names): m_name(name), m_index(index), m_relations(relations), m_relationNames(names) {}
34 
35  protected:
37  void add(const RelationVectorBase& other);
38 
40  void apply(int index, const std::function<void(std::vector<unsigned int>&, std::vector<float>&, size_t)>& f);
41 
42  std::string m_name;
43  int m_index{ -1};
44  std::vector<RelationEntry> m_relations;
45  std::vector<std::string> m_relationNames;
46  friend class DataStore;
47  };
48 
69  template <class T> class RelationVector : protected RelationVectorBase {
70  public:
72  typedef ArrayIterator<RelationVector<T>, T> iterator;
74  typedef ArrayIterator<RelationVector<T>, const T> const_iterator;
75 
76 
81  explicit RelationVector(const RelationVectorBase& b): RelationVectorBase(b) {}
82 
87  const std::vector<Belle2::RelationEntry>& relations() const {return m_relations;}
88 
90  size_t size() const { return m_relations.size();}
91 
92 
98  T* object(int index) const {return static_cast<T*>(m_relations[index].object);}
99 
105  T* operator[](int index) const {return object(index);}
106 
112  float weight(int index) const {return m_relations[index].weight;}
113 
121  void remove(int index)
122  {
123  apply(index, [](std::vector<unsigned int>& indices, std::vector<float>& weights,
124  size_t elidx) {
125  indices.erase(indices.begin() + elidx);
126  weights.erase(weights.begin() + elidx);
127  });
128 
129  m_relations.erase(m_relations.begin() + index);
130  }
131 
133  void setWeight(int index, float weight)
134  {
135  apply(index, [weight](std::vector<unsigned int>&, std::vector<float>& weights, size_t elidx) {
136  weights[elidx] = weight;
137  });
138 
139  m_relations[index].weight = weight;
140  }
141 
143  iterator begin() { return iterator(this, 0); }
145  iterator end() { return iterator(this, size()); }
146 
148  const_iterator begin() const { return const_iterator(this, 0); }
150  const_iterator end() const { return const_iterator(this, size()); }
151  };
153 }
Belle2::RelationVector::size
size_t size() const
Get number of relations.
Definition: RelationVector.h:98
Belle2::RelationVectorBase::RelationVectorBase
RelationVectorBase()
Construct empty set.
Definition: RelationVector.h:37
Belle2::RelationVector::iterator
ArrayIterator< RelationVector< T >, T > iterator
STL-like iterator over the T objects (not T* ).
Definition: RelationVector.h:80
Belle2::RelationVectorBase::add
void add(const RelationVectorBase &other)
add another list of relations.
Definition: RelationVector.cc:52
Belle2::RelationVectorBase::m_index
int m_index
index of object these relations belong to.
Definition: RelationVector.h:51
Belle2::RelationVector::operator[]
T * operator[](int index) const
Get object with index.
Definition: RelationVector.h:113
Belle2::ArrayIterator
Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc.
Definition: ArrayIterator.h:88
Belle2::RelationVectorBase::apply
void apply(int index, const std::function< void(std::vector< unsigned int > &, std::vector< float > &, size_t)> &f)
apply function to the relation associated with the RelationEntry at given index.
Definition: RelationVector.cc:6
Belle2::RelationVectorBase::m_relationNames
std::vector< std::string > m_relationNames
Names of associated relations.
Definition: RelationVector.h:53
Belle2::RelationVectorBase
base class for RelationVector<T>
Definition: RelationVector.h:34
Belle2::RelationVector::begin
iterator begin()
Return iterator to first entry.
Definition: RelationVector.h:151
Belle2::RelationVector::remove
void remove(int index)
Remove relation at given index.
Definition: RelationVector.h:129
Belle2::RelationVector::setWeight
void setWeight(int index, float weight)
Set a new weight for the given relation.
Definition: RelationVector.h:141
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationVector::object
T * object(int index) const
Get object with index.
Definition: RelationVector.h:106
Belle2::RelationVector::const_iterator
ArrayIterator< RelationVector< T >, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition: RelationVector.h:82
Belle2::RelationVectorBase::m_name
std::string m_name
entry name of array containing object these relations belong to.
Definition: RelationVector.h:50
Belle2::RelationVector::RelationVector
RelationVector(const RelationVectorBase &b)
Constructor.
Definition: RelationVector.h:89
Belle2::RelationVector::weight
float weight(int index) const
Get weight with index.
Definition: RelationVector.h:120
Belle2::RelationVector::end
iterator end()
Return iterator to last entry +1.
Definition: RelationVector.h:153
Belle2::RelationVector::relations
const std::vector< Belle2::RelationEntry > & relations() const
Accessor for the relations vector.
Definition: RelationVector.h:95
Belle2::RelationVectorBase::m_relations
std::vector< RelationEntry > m_relations
The vector of relation entries.
Definition: RelationVector.h:52
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53