CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AssociationMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AssociationMap_h
2 #define DataFormats_Common_AssociationMap_h
3 
39 
40 #include <utility>
41 #include "oneapi/tbb/concurrent_unordered_map.h"
42 
43 namespace edm {
44 
45  class EDProductGetter;
46 
47  template <typename Tag>
51  typedef typename Tag::val_type internal_val_type;
52 
53  public:
55  typedef AssociationMap<Tag> self;
57  typedef typename Tag::index_type index_type;
59  typedef typename Tag::key_type key_type;
61  typedef typename Tag::data_type data_type;
63  typedef typename Tag::ref_type ref_type;
65  typedef typename Tag::map_type map_type;
67  typedef typename map_type::size_type size_type;
73  typedef typename oneapi::tbb::concurrent_unordered_map<index_type, value_type> internal_transient_map_type;
74 
76  struct const_iterator {
77  typedef typename self::value_type value_type;
78  typedef ptrdiff_t difference_type;
79  typedef value_type* pointer;
81  typedef typename map_type::const_iterator::iterator_category iterator_category;
82  const_iterator() : map_(nullptr) {}
83  const_iterator(const self* map, typename map_type::const_iterator mi) : map_(map), i(mi) {}
85  ++i;
86  return *this;
87  }
89  const_iterator ci = *this;
90  ++i;
91  return ci;
92  }
94  --i;
95  return *this;
96  }
98  const_iterator ci = *this;
99  --i;
100  return ci;
101  }
102  bool operator==(const const_iterator& ci) const { return i == ci.i; }
103  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
104  const value_type& operator*() const { return map_->get(i->first); }
105  const value_type* operator->() const { return &operator*(); }
106 
107  private:
108  const self* map_;
109  typename map_type::const_iterator i;
110  };
111 
114 
115  // You will see better performance if you use other constructors.
116  // Use this when the arguments the other constructors require are
117  // not easily available.
118  explicit AssociationMap(EDProductGetter const* getter) : ref_(getter) {}
119 
120  // It is rare for this to be useful
121  explicit AssociationMap(const ref_type& ref) : ref_(ref) {}
122 
123  // In most cases this is the best constructor to use.
124  // This constructor should be passed 2 arguments, except in the
125  // case where the template parameter is OneToValue where it should
126  // be passed 1 argument. In most cases, the arguments will be valid
127  // Handle's to the containers. Internally, the AssociationMap holds
128  // a RefProd for each container. An argument passed here is forwarded
129  // to a RefProd constructor. Alternatively, you can pass in
130  // a RefProd or anything else a RefProd can be constructed from.
131  // The exceptional case is when the container template argument
132  // is a View (For your peace of mind, I suggest you stop reading
133  // this comment here if you are not dealing with the View case).
134  // Usually one would not pass a Handle argument in the View
135  // case. Then internally AssociationMap holds a RefToBaseProd
136  // for each container instead of a RefProd and one would pass a
137  // RefToBaseProd as an argument in that case. Also see the comments
138  // at the top of this file that are relevant to the View case.
139  // In the View case, the code would sometimes look similar to
140  // the following:
141  //
142  // typedef edm::AssociationMap<edm::OneToOne<edm::View<X>, edm::View<Y> > > AssocOneToOneView;
143  // edm::Handle<edm::View<X> > inputView1;
144  // event.getByToken(inputToken1V_, inputView1);
145  // edm::Handle<edm::View<Y> > inputView2;
146  // event.getByToken(inputToken2V_, inputView2);
147  // // If you are certain the Views are not empty!
148  // std::unique_ptr<AssocOneToOneView> assoc8(new AssocOneToOneView(
149  // edm::makeRefToBaseProdFrom(inputView1->refAt(0), event),
150  // edm::makeRefToBaseProdFrom(inputView2->refAt(0), event)
151  // ));
152 
153  template <typename... Args>
154  AssociationMap(Args... args) : ref_(std::forward<Args>(args)...) {}
155 
157  void clear() {
158  map_.clear();
159  transientMap_.clear();
160  }
162  size_type size() const { return map_.size(); }
164  bool empty() const { return map_.empty(); }
166  void insert(const key_type& k, const data_type& v) { Tag::insert(ref_, map_, k, v); }
167  void insert(const value_type& kv) { Tag::insert(ref_, map_, kv.key, kv.val); }
169  const_iterator begin() const { return const_iterator(this, map_.begin()); }
171  const_iterator end() const { return const_iterator(this, map_.end()); }
173  const_iterator find(const key_type& k) const {
174  if (ref_.key.id() != k.id())
175  return end();
176  return find(k.key());
177  }
180  index_type i = k.key();
181  transientMap_.unsafe_erase(i);
182  return map_.erase(i);
183  }
185  const result_type& operator[](const key_type& k) const {
186  helpers::checkRef(ref_.key, k);
187  return get(k.key()).val;
188  }
189 
190  template <typename K>
191  const result_type& operator[](const K& k) const {
192  helpers::checkRef(ref_.key, k);
193  return get(k.key()).val;
194  }
195 
198  if (ref_.key.id() != k.id())
199  return 0;
200  typename map_type::const_iterator f = map_.find(k.key());
201  if (f == map_.end())
202  return 0;
203  return Tag::size(f->second);
204  }
205 
206  template <typename K>
207  size_type numberOfAssociations(const K& k) const {
208  if (ref_.key.id() != k.id())
209  return 0;
210  typename map_type::const_iterator f = map_.find(k.key());
211  if (f == map_.end())
212  return 0;
213  return Tag::size(f->second);
214  }
215 
217  const ref_type& refProd() const { return ref_; }
218 
221  typename Tag::transient_map_type map() { return Tag::transientMap(ref_, map_); }
224  typename Tag::transient_key_vector keys() { return Tag::transientKeyVector(ref_, map_); }
227  typename Tag::transient_val_vector values() { return Tag::transientValVector(ref_, map_); }
229  void post_insert() { Tag::sort(map_); }
230 
231  // Find should be private! However, generated reflex dictionaries do not compile
232  // if Find is private.
234  struct Find {
235  using first_argument_type = const self&;
237  using result_type = const value_type*;
238 
240  };
241 
242  //Used by ROOT storage
244 
245  private:
254  typename map_type::const_iterator f = map_.find(i);
255  if (f == map_.end())
256  return end();
257  return const_iterator(this, f);
258  }
260  const value_type& get(size_type i) const {
261  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
262  if (tf == transientMap_.end()) {
263  typename map_type::const_iterator f = map_.find(i);
264  if (f == map_.end())
265  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
266  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
267  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
268  transientMap_.insert(std::make_pair(i, v));
269  return ins.first->second;
270  } else {
271  return tf->second;
272  }
273  }
274  friend struct const_iterator;
275  friend struct Find;
277  template <typename, typename, typename>
278  friend class OneToValue;
279  template <typename, typename, typename>
280  friend class OneToOne;
281  template <typename, typename, typename>
282  friend class OneToMany;
283  template <typename, typename, typename, typename>
284  friend class OneToManyWithQuality;
285  };
286 
287  namespace refhelper {
288  template <typename Tag>
289  struct FindTrait<AssociationMap<Tag>, typename AssociationMap<Tag>::value_type> {
291  };
292  } // namespace refhelper
293 
294 } // namespace edm
295 #endif
::ecal::reco::ComputationScalarType data_type
const edm::EventSetup & c
const result_type operator()(first_argument_type c, second_argument_type i)
Tag::index_type index_type
index type
friend struct const_iterator
const_iterator end() const
last iterator over the map (read only)
const result_type & operator[](const K &k) const
internal_transient_map_type transientMap_
transient reference map
bool empty() const
return true if empty
const_iterator find(const key_type &k) const
find element with specified reference key
Tag::data_type data_type
insert data type
const value_type * operator->() const
AssociationMap(const ref_type &ref)
void clear()
clear map
helpers::KeyVal< key_type, internal_val_type > value_type
type returned by dereferenced iterator, also can be inserted
Tag::transient_val_vector values()
Tag::key_type key_type
insert key type
ref_type ref_
reference set
#define CMS_CLASS_VERSION(_version_)
uint16_t size_type
value_type::value_type result_type
type return by operator[]
size_type numberOfAssociations(const key_type &k) const
number of associations to a key
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:84
size_type erase(const key_type &k)
erase the element whose key is k
bool operator!=(const const_iterator &ci) const
const_iterator(const self *map, typename map_type::const_iterator mi)
const ref_type & refProd() const
return ref-prod structure
void post_insert()
post insert action
tuple ins
Definition: cuy.py:313
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:50
Tag::transient_key_vector keys()
bool operator==(const const_iterator &ci) const
oneapi::tbb::concurrent_unordered_map< index_type, value_type > internal_transient_map_type
transient map type
Tag::val_type internal_val_type
size_type size() const
map size
map_type map_
index map
void insert(const key_type &k, const data_type &v)
insert an association
AssociationMap(Args...args)
AssociationMap(EDProductGetter const *getter)
map_type::const_iterator::iterator_category iterator_category
Tag::ref_type ref_type
Holds the RefProd or RefToBaseProd of 1 or 2 collections.
Tag::map_type map_type
map type
void insert(const value_type &kv)
AssociationMap()
default constructor
const_iterator begin() const
first iterator over the map (read only)
const result_type & operator[](const key_type &k) const
find element with specified reference key
void checkRef(const RP &rp, const R &r)
throw if r hasn&#39;t the same id as rp
friend struct Find
size_type numberOfAssociations(const K &k) const
tuple size
Write out results.
const value_type & get(size_type i) const
return value_typeelement with key i
Tag::transient_map_type map()
const value_type & operator*() const
map_type::size_type size_type
size type