Belle II Software light-2406-ragdoll
RelationVector.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
9#pragma once
10
11#include <framework/datastore/RelationEntry.h>
12#include <framework/utilities/ArrayIterator.h>
13
14#include <vector>
15#include <string>
16#include <functional>
17
18namespace Belle2 {
25 public:
28
30 RelationVectorBase(const std::string& name, int index, const std::vector<Belle2::RelationEntry>& relations,
31 const std::vector<std::string>& names): m_name(name), m_index(index), m_relations(relations), m_relationNames(names) {}
32
33 protected:
35 void add(const RelationVectorBase& other);
36
38 void apply(int index, const std::function<void(std::vector<unsigned int>&, std::vector<float>&, size_t)>& f);
39
40 std::string m_name;
41 int m_index{ -1};
42 std::vector<RelationEntry> m_relations;
43 std::vector<std::string> m_relationNames;
44 friend class DataStore;
45 };
46
67 template <class T> class RelationVector : protected RelationVectorBase {
68 public:
73
74
80
85 const std::vector<Belle2::RelationEntry>& relations() const {return m_relations;}
86
88 size_t size() const { return m_relations.size();}
89
90
96 T* object(int index) const {return static_cast<T*>(m_relations[index].object);}
97
103 T* operator[](int index) const {return object(index);}
104
110 float weight(int index) const {return m_relations[index].weight;}
111
119 void remove(int index)
120 {
121 apply(index, [](std::vector<unsigned int>& indices, std::vector<float>& weights,
122 size_t elidx) {
123 indices.erase(indices.begin() + elidx);
124 weights.erase(weights.begin() + elidx);
125 });
126
127 m_relations.erase(m_relations.begin() + index);
128 }
129
131 void setWeight(int index, float weight)
132 {
133 apply(index, [weight](std::vector<unsigned int>&, std::vector<float>& weights, size_t elidx) {
134 weights[elidx] = weight;
135 });
136
137 m_relations[index].weight = weight;
138 }
139
141 iterator begin() { return iterator(this, 0); }
143 iterator end() { return iterator(this, size()); }
144
146 const_iterator begin() const { return const_iterator(this, 0); }
148 const_iterator end() const { return const_iterator(this, size()); }
149 };
151}
Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc.
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
base class for RelationVector<T>
RelationVectorBase(const std::string &name, int index, const std::vector< Belle2::RelationEntry > &relations, const std::vector< std::string > &names)
Constructor.
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.
void add(const RelationVectorBase &other)
add another list of relations.
std::vector< RelationEntry > m_relations
The vector of relation entries.
RelationVectorBase()
Construct empty set.
int m_index
index of object these relations belong to.
std::string m_name
entry name of array containing object these relations belong to.
std::vector< std::string > m_relationNames
Names of associated relations.
Class for type safe access to objects that are referred to in relations.
void setWeight(int index, float weight)
Set a new weight for the given relation.
T * object(int index) const
Get object with index.
size_t size() const
Get number of relations.
const_iterator begin() const
Return const_iterator to first entry.
void remove(int index)
Remove relation at given index.
ArrayIterator< RelationVector< T >, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
RelationVector(const RelationVectorBase &b)
Constructor.
ArrayIterator< RelationVector< T >, T > iterator
STL-like iterator over the T objects (not T* ).
iterator end()
Return iterator to last entry +1.
const_iterator end() const
Return const_iterator to last entry +1.
T * operator[](int index) const
Get object with index.
iterator begin()
Return iterator to first entry.
float weight(int index) const
Get weight with index.
const std::vector< Belle2::RelationEntry > & relations() const
Accessor for the relations vector.
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24