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 
42 namespace edm {
43 
44  class EDProductGetter;
45 
46  template<typename Tag>
50  typedef typename Tag::val_type internal_val_type;
51  public:
53  typedef AssociationMap<Tag> self;
55  typedef typename Tag::index_type index_type;
57  typedef typename Tag::key_type key_type;
59  typedef typename Tag::data_type data_type;
61  typedef typename Tag::ref_type ref_type;
63  typedef typename Tag::map_type map_type;
65  typedef typename map_type::size_type size_type;
71  typedef typename std::map<index_type, value_type> internal_transient_map_type;
72 
74  struct const_iterator {
75  typedef typename self::value_type value_type;
76  typedef ptrdiff_t difference_type;
77  typedef value_type * pointer;
78  typedef value_type & reference;
79  typedef typename map_type::const_iterator::iterator_category iterator_category;
80  const_iterator(): map_(0) { }
81  const_iterator(const self * map, typename map_type::const_iterator mi) :
82  map_(map), i(mi) { }
83  const_iterator& operator++() { ++i; return *this; }
84  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
85  const_iterator& operator--() { --i; return *this; }
86  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
87  bool operator==(const const_iterator& ci) const { return i == ci.i; }
88  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
89  const value_type & operator *() const { return map_->get( i->first ); }
90  const value_type * operator->() const { return &operator *(); }
91  private:
92  const self * map_;
93  typename map_type::const_iterator i;
94  };
95 
98 
99 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
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 #endif
107 
108  // It is rare for this to be useful
109  explicit
110  AssociationMap(const ref_type & ref) : ref_(ref) { }
111 
112 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
113  // In most cases this is the best constructor to use.
114  // This constructor should be passed 2 arguments, except in the
115  // case where the template parameter is OneToValue where it should
116  // be passed 1 argument. In most cases, the arguments will be valid
117  // Handle's to the containers. Internally, the AssociationMap holds
118  // a RefProd for each container. An argument passed here is forwarded
119  // to a RefProd constructor. Alternatively, you can pass in
120  // a RefProd or anything else a RefProd can be constructed from.
121  // The exceptional case is when the container template argument
122  // is a View (For your peace of mind, I suggest you stop reading
123  // this comment here if you are not dealing with the View case).
124  // Usually one would not pass a Handle argument in the View
125  // case. Then internally AssociationMap holds a RefToBaseProd
126  // for each container instead of a RefProd and one would pass a
127  // RefToBaseProd as an argument in that case. Also see the comments
128  // at the top of this file that are relevant to the View case.
129  // In the View case, the code would sometimes look similar to
130  // the following:
131  //
132  // typedef edm::AssociationMap<edm::OneToOne<edm::View<X>, edm::View<Y> > > AssocOneToOneView;
133  // edm::Handle<edm::View<X> > inputView1;
134  // event.getByToken(inputToken1V_, inputView1);
135  // edm::Handle<edm::View<Y> > inputView2;
136  // event.getByToken(inputToken2V_, inputView2);
137  // // If you are certain the Views are not empty!
138  // std::auto_ptr<AssocOneToOneView> assoc8(new AssocOneToOneView(
139  // edm::makeRefToBaseProdFrom(inputView1->refAt(0), event),
140  // edm::makeRefToBaseProdFrom(inputView2->refAt(0), event)
141  // ));
142 
143  template<typename... Args>
144  AssociationMap(Args... args) : ref_(std::forward<Args>(args)...) {}
145 #endif
146 
148  void clear() { map_.clear(); transientMap_.clear(); }
150  size_type size() const { return map_.size(); }
152  bool empty() const { return map_.empty(); }
154  void insert(const key_type & k, const data_type & v) {
155  Tag::insert(ref_, map_, k, v);
156  }
157  void insert(const value_type & kv) {
158  Tag::insert(ref_, map_, kv.key, kv.val);
159  }
161  const_iterator begin() const { return const_iterator(this, map_.begin()); }
163  const_iterator end() const { return const_iterator(this, map_.end()); }
165  const_iterator find(const key_type & k) const {
166  if (ref_.key.id() != k.id()) return end();
167  return find(k.key());
168  }
171  index_type i = k.key();
172  transientMap_.erase(i);
173  return map_.erase(i);
174  }
176  const result_type & operator[](const key_type & k) const {
177  helpers::checkRef(ref_.key, k);
178  return get(k.key()).val;
179  }
180 
181  template<typename K>
182  const result_type& operator[](const K& k) const {
183  helpers::checkRef(ref_.key,k);
184  return get(k.key()).val;
185  }
186 
189  if (ref_.key.id() != k.id()) return 0;
190  typename map_type::const_iterator f = map_.find(k.key());
191  if (f == map_.end()) return 0;
192  return Tag::size(f->second);
193  }
194 
195  template<typename K>
196  size_type numberOfAssociations(const K & k) const {
197  if (ref_.key.id() != k.id()) return 0;
198  typename map_type::const_iterator f = map_.find(k.key());
199  if (f == map_.end()) return 0;
200  return Tag::size(f->second);
201  }
202 
204  const ref_type & refProd() const { return ref_; }
205 
208  typename Tag::transient_map_type map() {
209  return Tag::transientMap( ref_, map_ );
210  }
213  typename Tag::transient_key_vector keys() {
214  return Tag::transientKeyVector( ref_, map_ );
215  }
218  typename Tag::transient_val_vector values() {
219  return Tag::transientValVector( ref_, map_ );
220  }
223 
224  // Find should be private! However, generated reflex dictionaries do not compile
225  // if Find is private.
227  struct Find :
228  public std::binary_function<const self&, size_type, const value_type *> {
229  typedef Find self;
230  const value_type * operator()(typename self::first_argument_type c,
231  typename self::second_argument_type i) {
232  return &(*c.find(i));
233  }
234  };
235 
236  //Used by ROOT storage
238 
239  private:
248  typename map_type::const_iterator f = map_.find(i);
249  if (f == map_.end()) return end();
250  return const_iterator(this, f);
251  }
253  const value_type & get(size_type i) const {
254  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
255  if (tf == transientMap_.end()) {
256  typename map_type::const_iterator f = map_.find(i);
257  if (f == map_.end())
258  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
259  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
260  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
261  transientMap_.insert(std::make_pair(i, v));
262  return ins.first->second;
263  } else {
264  return tf->second;
265  }
266  }
267  friend struct const_iterator;
268  friend struct Find;
270  template<typename, typename, typename> friend class OneToValue;
271  template<typename, typename, typename> friend class OneToOne;
272  template<typename, typename, typename> friend class OneToMany;
273  template<typename, typename, typename, typename> friend class OneToManyWithQuality;
274  };
275 
276  namespace refhelper {
277  template<typename Tag>
279  typename AssociationMap<Tag>::value_type> {
281  };
282  }
283 
284 }
285 #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
std::map< index_type, value_type > internal_transient_map_type
transient map type
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
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
Container::value_type value_type
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)
#define private
Definition: FWFileEntry.h:17
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