Belle II Software  release-08-01-10
RelationIndex.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/RelationIndexManager.h>
12 
13 #include <boost/range/iterator_range.hpp>
14 
15 namespace Belle2 {
20  template <class T> class StoreArray;
21 
76  template<class FROM, class TO> class RelationIndex {
77  public:
80 
83 
85  typedef typename ElementIndex::template nth_index<0>::type index_from;
86 
88  typedef typename ElementIndex::template nth_index<1>::type index_to;
89 
95  typedef typename index_from::const_iterator iterator_from;
96 
98  typedef typename index_to::const_iterator iterator_to;
99 
101  typedef boost::iterator_range<iterator_from> range_from;
102 
104  typedef boost::iterator_range<iterator_to> range_to;
105 
113  explicit RelationIndex(const std::string& name = (DataStore::defaultRelationName<FROM, TO>()),
115  m_index(RelationIndexManager::Instance().get<FROM, TO>(RelationArray(name, durability))),
116  m_from(m_index->index().template get<0>()),
117  m_to(m_index->index().template get<1>()) {}
118 
131  RelationIndex(const StoreArray<FROM>& from, const StoreArray<TO>& to, const std::string& name = "",
133  m_index(RelationIndexManager::Instance().get<FROM, TO>(RelationArray(from, to, name, durability))),
134  m_from(m_index->index().template get<0>()),
135  m_to(m_index->index().template get<1>()) {}
136 
138  operator bool() const { return *(m_index.get()); }
139 
149  range_from getElementsFrom(const FROM* from) const { return m_from.equal_range(from); }
150 
160  range_from getElementsFrom(const FROM& from) const { return m_from.equal_range(&from); }
161 
171  range_to getElementsTo(const TO* to) const { return m_to.equal_range(to); }
172 
182  range_to getElementsTo(const TO& to) const { return m_to.equal_range(&to); }
183 
191  const Element* getFirstElementFrom(const FROM& from) const { return getFirstElementFrom(&from); }
192 
200  const Element* getFirstElementFrom(const FROM* from) const
201  {
202  iterator_from it = m_from.find(from);
203  if (it == m_from.end()) return 0;
204  return &(*it);
205  }
206 
214  const Element* getFirstElementTo(const TO& to) const { return getFirstElementTo(&to); }
215 
223  const Element* getFirstElementTo(const TO* to) const
224  {
225  iterator_to it = m_to.find(to);
226  if (it == m_to.end()) return 0;
227  return &(*it);
228  }
229 
231  AccessorParams getAccessorParams() const { return m_index->getAccessorParams(); }
232 
234  const AccessorParams& getFromAccessorParams() const { return m_index->getFromAccessorParams(); }
235 
237  const AccessorParams& getToAccessorParams() const { return m_index->getToAccessorParams(); }
238 
240  size_t size() const { return m_index->index().size(); }
241  protected:
243  const std::shared_ptr<RelationIndexContainer<FROM, TO>> m_index;
244 
247 
249  const index_to& m_to;
250  };
251 
253 } // end namespace Belle2
EDurability
Durability types.
Definition: DataStore.h:58
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
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.
Manager to keep a cache of existing RelationIndexContainers.
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
Definition: RelationIndex.h:76
RelationIndex(const std::string &name=(DataStore::defaultRelationName< FROM, TO >()), DataStore::EDurability durability=DataStore::c_Event)
Constructor.
ElementIndex::template nth_index< 0 >::type index_from
Typedef for easy access to the from side of the index.
Definition: RelationIndex.h:85
ElementIndex::template nth_index< 1 >::type index_to
Typedef for easy access to the to side of the index.
Definition: RelationIndex.h:88
const std::shared_ptr< RelationIndexContainer< FROM, TO > > m_index
Reference to the IndexContainer.
const Element * getFirstElementFrom(const FROM *from) const
Return a pointer to the first relation Element of the given object.
size_t size() const
Get the size of the index.
const Element * getFirstElementFrom(const FROM &from) const
Return a pointer to the first relation Element of the given object.
const AccessorParams & getFromAccessorParams() const
Get the AccessorParams of the StoreArray the relation points from.
RelationIndexContainer< FROM, TO >::Element Element
Struct representing a single element in the index.
Definition: RelationIndex.h:79
index_from::const_iterator iterator_from
Element iterator of the from side index.
Definition: RelationIndex.h:95
range_from getElementsFrom(const FROM &from) const
Return a range of all elements pointing from the given object.
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.
const Element * getFirstElementTo(const TO *to) const
Return a pointer to the first relation Element of the given object.
const AccessorParams & getToAccessorParams() const
Get the AccessorParams of the StoreArray the relation points to.
RelationIndex(const StoreArray< FROM > &from, const StoreArray< TO > &to, const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor with checks.
RelationIndexContainer< FROM, TO >::ElementIndex ElementIndex
Class representing a the index on the relation.
Definition: RelationIndex.h:82
boost::iterator_range< iterator_from > range_from
Iterator range [first,second) of the from side.
range_to getElementsTo(const TO &to) const
Return a range of all elements pointing to the given object.
AccessorParams getAccessorParams() const
Get the AccessorParams of the underlying relation.
boost::iterator_range< iterator_to > range_to
Iterator range [first,second) of the to side.
index_to::const_iterator iterator_to
Element iterator of the to side index.
Definition: RelationIndex.h:98
const index_to & m_to
Reference to the to index.
const Element * getFirstElementTo(const TO &to) const
Return a pointer to the first relation Element of the given object.
range_to getElementsTo(const TO *to) const
Return a range of all elements pointing to the given object.
const index_from & m_from
Reference to the from index.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
std::pair< std::string, DataStore::EDurability > AccessorParams
Pair of parameters needed to find an object in the DataStore.
Abstract base class for different kinds of events.