CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 "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;
81  const_iterator(): map_(0) { }
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::auto_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  }
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 
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  }
219  void post_insert() { Tag::sort(map_); }
220 
221  // Find should be private! However, generated reflex dictionaries do not compile
222  // if Find is private.
224  struct Find :
225  public std::binary_function<const self&, size_type, const value_type *> {
226  typedef Find self;
227  const value_type * operator()(typename self::first_argument_type c,
228  typename self::second_argument_type i) {
229  return &(*c.find(i));
230  }
231  };
232 
233  //Used by ROOT storage
235 
236  private:
245  typename map_type::const_iterator f = map_.find(i);
246  if (f == map_.end()) return end();
247  return const_iterator(this, f);
248  }
250  const value_type & get(size_type i) const {
251  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
252  if (tf == transientMap_.end()) {
253  typename map_type::const_iterator f = map_.find(i);
254  if (f == map_.end())
255  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
256  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
257  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
258  transientMap_.insert(std::make_pair(i, v));
259  return ins.first->second;
260  } else {
261  return tf->second;
262  }
263  }
264  friend struct const_iterator;
265  friend struct Find;
267  template<typename, typename, typename> friend class OneToValue;
268  template<typename, typename, typename> friend class OneToOne;
269  template<typename, typename, typename> friend class OneToMany;
270  template<typename, typename, typename, typename> friend class OneToManyWithQuality;
271  };
272 
273  namespace refhelper {
274  template<typename Tag>
276  typename AssociationMap<Tag>::value_type> {
278  };
279  }
280 
281 }
282 #endif
int i
Definition: DBlmapReader.cc:9
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
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
key_type key() const
Accessor for product key.
Definition: Ref.h:264
ref_type ref_
reference set
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
uint16_t size_type
value_type::value_type result_type
type return by operator[]
ProductID id() const
Accessor for product ID.
Definition: Ref.h:258
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="")
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
tuple ins
Definition: cuy.py:312
double f[11][100]
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
Tag::transient_key_vector keys()
bool operator==(const const_iterator &ci) const
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)
const value_type * operator()(typename self::first_argument_type c, typename self::second_argument_type i)
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