CMS 3D CMS Logo

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 "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  public:
54  typedef AssociationMap<Tag> self;
56  typedef typename Tag::index_type index_type;
58  typedef typename Tag::key_type key_type;
60  typedef typename Tag::data_type data_type;
62  typedef typename Tag::ref_type ref_type;
64  typedef typename Tag::map_type map_type;
66  typedef typename map_type::size_type size_type;
72  typedef typename tbb::concurrent_unordered_map<index_type, value_type> internal_transient_map_type;
73 
75  struct const_iterator {
76  typedef typename self::value_type value_type;
77  typedef ptrdiff_t difference_type;
78  typedef value_type * pointer;
79  typedef value_type & reference;
80  typedef typename map_type::const_iterator::iterator_category iterator_category;
82  const_iterator(const self * map, typename map_type::const_iterator mi) :
83  map_(map), i(mi) { }
84  const_iterator& operator++() { ++i; return *this; }
85  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
86  const_iterator& operator--() { --i; return *this; }
87  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
88  bool operator==(const const_iterator& ci) const { return i == ci.i; }
89  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
90  const value_type & operator *() const { return map_->get( i->first ); }
91  const value_type * operator->() const { return &operator *(); }
92  private:
93  const self * map_;
94  typename map_type::const_iterator i;
95  };
96 
99 
100  // You will see better performance if you use other constructors.
101  // Use this when the arguments the other constructors require are
102  // not easily available.
103  explicit
105  ref_(getter) { }
106 
107  // It is rare for this to be useful
108  explicit
109  AssociationMap(const ref_type & ref) : ref_(ref) { }
110 
111  // In most cases this is the best constructor to use.
112  // This constructor should be passed 2 arguments, except in the
113  // case where the template parameter is OneToValue where it should
114  // be passed 1 argument. In most cases, the arguments will be valid
115  // Handle's to the containers. Internally, the AssociationMap holds
116  // a RefProd for each container. An argument passed here is forwarded
117  // to a RefProd constructor. Alternatively, you can pass in
118  // a RefProd or anything else a RefProd can be constructed from.
119  // The exceptional case is when the container template argument
120  // is a View (For your peace of mind, I suggest you stop reading
121  // this comment here if you are not dealing with the View case).
122  // Usually one would not pass a Handle argument in the View
123  // case. Then internally AssociationMap holds a RefToBaseProd
124  // for each container instead of a RefProd and one would pass a
125  // RefToBaseProd as an argument in that case. Also see the comments
126  // at the top of this file that are relevant to the View case.
127  // In the View case, the code would sometimes look similar to
128  // the following:
129  //
130  // typedef edm::AssociationMap<edm::OneToOne<edm::View<X>, edm::View<Y> > > AssocOneToOneView;
131  // edm::Handle<edm::View<X> > inputView1;
132  // event.getByToken(inputToken1V_, inputView1);
133  // edm::Handle<edm::View<Y> > inputView2;
134  // event.getByToken(inputToken2V_, inputView2);
135  // // If you are certain the Views are not empty!
136  // std::unique_ptr<AssocOneToOneView> assoc8(new AssocOneToOneView(
137  // edm::makeRefToBaseProdFrom(inputView1->refAt(0), event),
138  // edm::makeRefToBaseProdFrom(inputView2->refAt(0), event)
139  // ));
140 
141  template<typename... Args>
142  AssociationMap(Args... args) : ref_(std::forward<Args>(args)...) {}
143 
145  void clear() { map_.clear(); transientMap_.clear(); }
147  size_type size() const { return map_.size(); }
149  bool empty() const { return map_.empty(); }
151  void insert(const key_type & k, const data_type & v) {
152  Tag::insert(ref_, map_, k, v);
153  }
154  void insert(const value_type & kv) {
155  Tag::insert(ref_, map_, kv.key, kv.val);
156  }
158  const_iterator begin() const { return const_iterator(this, map_.begin()); }
160  const_iterator end() const { return const_iterator(this, map_.end()); }
162  const_iterator find(const key_type & k) const {
163  if (ref_.key.id() != k.id()) return end();
164  return find(k.key());
165  }
167  size_type erase(const key_type& k) {
168  index_type i = k.key();
169  transientMap_.unsafe_erase(i);
170  return map_.erase(i);
171  }
173  const result_type & operator[](const key_type & k) const {
174  helpers::checkRef(ref_.key, k);
175  return get(k.key()).val;
176  }
177 
178  template<typename K>
179  const result_type& operator[](const K& k) const {
180  helpers::checkRef(ref_.key,k);
181  return get(k.key()).val;
182  }
183 
185  size_type numberOfAssociations(const key_type & k) const {
186  if (ref_.key.id() != k.id()) return 0;
187  typename map_type::const_iterator f = map_.find(k.key());
188  if (f == map_.end()) return 0;
189  return Tag::size(f->second);
190  }
191 
192  template<typename K>
193  size_type numberOfAssociations(const K & k) const {
194  if (ref_.key.id() != k.id()) return 0;
195  typename map_type::const_iterator f = map_.find(k.key());
196  if (f == map_.end()) return 0;
197  return Tag::size(f->second);
198  }
199 
201  const ref_type & refProd() const { return ref_; }
202 
205  typename Tag::transient_map_type map() {
206  return Tag::transientMap( ref_, map_ );
207  }
210  typename Tag::transient_key_vector keys() {
211  return Tag::transientKeyVector( ref_, map_ );
212  }
215  typename Tag::transient_val_vector values() {
216  return Tag::transientValVector( ref_, map_ );
217  }
220 
221  // Find should be private! However, generated reflex dictionaries do not compile
222  // if Find is private.
224  struct Find {
225 
226  using first_argument_type = const self&;
228  using result_type = const value_type *;
229 
231  return &(*c.find(i));
232  }
233  };
234 
235  //Used by ROOT storage
237 
238  private:
240  ref_type ref_;
242  map_type map_;
244  mutable internal_transient_map_type transientMap_;
246  const_iterator find(size_type i) const {
247  typename map_type::const_iterator f = map_.find(i);
248  if (f == map_.end()) return end();
249  return const_iterator(this, f);
250  }
252  const value_type & get(size_type i) const {
253  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
254  if (tf == transientMap_.end()) {
255  typename map_type::const_iterator f = map_.find(i);
256  if (f == map_.end())
257  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
258  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
259  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
260  transientMap_.insert(std::make_pair(i, v));
261  return ins.first->second;
262  } else {
263  return tf->second;
264  }
265  }
266  friend struct const_iterator;
267  friend struct Find;
268  friend struct refhelper::FindTrait<self,value_type>;
269  template<typename, typename, typename> friend class OneToValue;
270  template<typename, typename, typename> friend class OneToOne;
271  template<typename, typename, typename> friend class OneToMany;
272  template<typename, typename, typename, typename> friend class OneToManyWithQuality;
273  };
274 
275  namespace refhelper {
276  template<typename Tag>
278  typename AssociationMap<Tag>::value_type> {
280  };
281  }
282 
283 }
284 #endif
size
Write out results.
const result_type operator()(first_argument_type c, second_argument_type i)
Tag::index_type index_type
index type
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
#define nullptr
tbb::concurrent_unordered_map< index_type, value_type > internal_transient_map_type
transient map type
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[]
ins
Definition: cuy.py:314
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:83
size_type erase(const key_type &k)
erase the element whose key is k
bool operator!=(const const_iterator &ci) const
map_type::const_iterator i
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
double f[11][100]
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
int k[5][pyjets_maxn]
Tag::val_type internal_val_type
size_type size() const
map size
void insert(const key_type &k, const data_type &v)
insert an association
AssociationMap(Args...args)
AssociationMap(EDProductGetter const *getter)
HLT enums.
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
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