Belle II Software  release-05-02-19
RelationIndex.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2011 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  **************************************************************************/
9 
10 #pragma once
11 
12 #include <framework/datastore/RelationIndexManager.h>
13 
14 #include <boost/range/iterator_range.hpp>
15 
16 namespace Belle2 {
21  template <class T> class StoreArray;
22 
77  template<class FROM, class TO> class RelationIndex {
78  public:
80  typedef typename RelationIndexContainer<FROM, TO>::Element Element;
81 
84 
86  typedef typename ElementIndex::template nth_index<0>::type index_from;
87 
89  typedef typename ElementIndex::template nth_index<1>::type index_to;
90 
96  typedef typename index_from::const_iterator iterator_from;
97 
99  typedef typename index_to::const_iterator iterator_to;
100 
102  typedef boost::iterator_range<iterator_from> range_from;
103 
105  typedef boost::iterator_range<iterator_to> range_to;
106 
114  explicit RelationIndex(const std::string& name = (DataStore::defaultRelationName<FROM, TO>()),
116  m_index(RelationIndexManager::Instance().get<FROM, TO>(RelationArray(name, durability))),
117  m_from(m_index->index().template get<0>()),
118  m_to(m_index->index().template get<1>()) {}
119 
132  RelationIndex(const StoreArray<FROM>& from, const StoreArray<TO>& to, const std::string& name = "",
134  m_index(RelationIndexManager::Instance().get<FROM, TO>(RelationArray(from, to, name, durability))),
135  m_from(m_index->index().template get<0>()),
136  m_to(m_index->index().template get<1>()) {}
137 
139  operator bool() const { return *(m_index.get()); }
140 
150  range_from getElementsFrom(const FROM* from) const { return m_from.equal_range(from); }
151 
161  range_from getElementsFrom(const FROM& from) const { return m_from.equal_range(&from); }
162 
172  range_to getElementsTo(const TO* to) const { return m_to.equal_range(to); }
173 
183  range_to getElementsTo(const TO& to) const { return m_to.equal_range(&to); }
184 
192  const Element* getFirstElementFrom(const FROM& from) const { return getFirstElementFrom(&from); }
193 
201  const Element* getFirstElementFrom(const FROM* from) const
202  {
203  iterator_from it = m_from.find(from);
204  if (it == m_from.end()) return 0;
205  return &(*it);
206  }
207 
215  const Element* getFirstElementTo(const TO& to) const { return getFirstElementTo(&to); }
216 
224  const Element* getFirstElementTo(const TO* to) const
225  {
226  iterator_to it = m_to.find(to);
227  if (it == m_to.end()) return 0;
228  return &(*it);
229  }
230 
232  const AccessorParams& getAccessorParams() const { return m_index->getAccessorParams(); }
233 
235  const AccessorParams& getFromAccessorParams() const { return m_index->getFromAccessorParams(); }
236 
238  const AccessorParams& getToAccessorParams() const { return m_index->getToAccessorParams(); }
239 
241  size_t size() const { return m_index->index().size(); }
242  protected:
244  const std::shared_ptr<RelationIndexContainer<FROM, TO>> m_index;
245 
247  const index_from& m_from;
248 
250  const index_to& m_to;
251  };
252 
254 } // end namespace Belle2
Belle2::RelationArray
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:72
Belle2::RelationIndex::index_from
ElementIndex::template nth_index< 0 >::type index_from
Typedef for easy access to the from side of the index.
Definition: RelationIndex.h:93
Belle2::RelationIndex::getFirstElementTo
const Element * getFirstElementTo(const TO &to) const
Return a pointer to the first relation Element of the given object.
Definition: RelationIndex.h:222
Belle2::RelationIndex::iterator_from
index_from::const_iterator iterator_from
Element iterator of the from side index.
Definition: RelationIndex.h:103
Belle2::RelationIndex::ElementIndex
RelationIndexContainer< FROM, TO >::ElementIndex ElementIndex
Class representing a the index on the relation.
Definition: RelationIndex.h:90
Belle2::RelationIndex::RelationIndex
RelationIndex(const std::string &name=(DataStore::defaultRelationName< FROM, TO >()), DataStore::EDurability durability=DataStore::c_Event)
Constructor.
Definition: RelationIndex.h:121
Belle2::RelationIndex::Element
RelationIndexContainer< FROM, TO >::Element Element
Struct representing a single element in the index.
Definition: RelationIndex.h:87
Belle2::RelationIndex::getFirstElementFrom
const Element * getFirstElementFrom(const FROM &from) const
Return a pointer to the first relation Element of the given object.
Definition: RelationIndex.h:199
Belle2::RelationIndexManager
Manager to keep a cache of existing RelationIndexContainers.
Definition: RelationIndexManager.h:38
Belle2::RelationIndex::getElementsTo
range_to getElementsTo(const TO *to) const
Return a range of all elements pointing to the given object.
Definition: RelationIndex.h:179
Belle2::RelationIndex::size
size_t size() const
Get the size of the index.
Definition: RelationIndex.h:248
Belle2::RelationIndex::m_to
const index_to & m_to
Reference to the to index.
Definition: RelationIndex.h:257
Belle2::RelationIndex::getToAccessorParams
const AccessorParams & getToAccessorParams() const
Get the AccessorParams of the StoreArray the relation points to.
Definition: RelationIndex.h:245
Belle2::AccessorParams
std::pair< std::string, DataStore::EDurability > AccessorParams
Pair of parameters needed to find an object in the DataStore.
Definition: StoreAccessorBase.h:26
Belle2::RelationIndex::getElementsFrom
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.
Definition: RelationIndex.h:157
Belle2::RelationIndex::m_index
const std::shared_ptr< RelationIndexContainer< FROM, TO > > m_index
Reference to the IndexContainer.
Definition: RelationIndex.h:251
Belle2::RelationIndex::getAccessorParams
const AccessorParams & getAccessorParams() const
Get the AccessorParams of the underlying relation.
Definition: RelationIndex.h:239
Belle2::RelationIndex::range_from
boost::iterator_range< iterator_from > range_from
Iterator range [first,second) of the from side.
Definition: RelationIndex.h:109
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationIndex::getFromAccessorParams
const AccessorParams & getFromAccessorParams() const
Get the AccessorParams of the StoreArray the relation points from.
Definition: RelationIndex.h:242
Belle2::RelationIndex::m_from
const index_from & m_from
Reference to the from index.
Definition: RelationIndex.h:254
Belle2::RelationIndex::iterator_to
index_to::const_iterator iterator_to
Element iterator of the to side index.
Definition: RelationIndex.h:106
Belle2::RelationIndexContainer::ElementIndex
boost::multi_index::multi_index_container< Element, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::member< Element, const FROM *, &Element::from > >, boost::multi_index::ordered_non_unique< boost::multi_index::member< Element, const TO *, &Element::to > > > > ElementIndex
Boost MultiIndex container to keep the bidirectional index.
Definition: RelationIndexContainer.h:99
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::RelationIndex::range_to
boost::iterator_range< iterator_to > range_to
Iterator range [first,second) of the to side.
Definition: RelationIndex.h:112
Belle2::DataStore::EDurability
EDurability
Durability types.
Definition: DataStore.h:60
Belle2::RelationIndex::index_to
ElementIndex::template nth_index< 1 >::type index_to
Typedef for easy access to the to side of the index.
Definition: RelationIndex.h:96