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 
17 
18 namespace edm {
19  template<typename Tag>
22  typedef typename Tag::val_type internal_val_type;
23  public:
25  typedef AssociationMap<Tag> self;
27  typedef typename Tag::index_type index_type;
29  typedef typename Tag::key_type key_type;
31  typedef typename Tag::data_type data_type;
33  typedef typename Tag::ref_type ref_type;
35  typedef typename Tag::map_type map_type;
37  typedef typename map_type::size_type size_type;
43  typedef typename std::map<index_type, value_type> internal_transient_map_type;
44 
46  struct const_iterator {
47  typedef typename self::value_type value_type;
48  typedef ptrdiff_t difference_type;
49  typedef value_type * pointer;
50  typedef value_type & reference;
51  typedef typename map_type::const_iterator::iterator_category iterator_category;
52  const_iterator(): map_(0) { }
53  const_iterator(const self * map, typename map_type::const_iterator mi) :
54  map_(map), i(mi) { }
55  const_iterator& operator++() { ++i; return *this; }
56  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
57  const_iterator& operator--() { --i; return *this; }
58  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
59  bool operator==(const const_iterator& ci) const { return i == ci.i; }
60  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
61  const value_type & operator *() const { return (*map_)[ i->first ]; }
62  const value_type * operator->() const { return &operator *(); }
63  private:
64  const self * map_;
65  typename map_type::const_iterator i;
66  };
67 
71  explicit
72  AssociationMap(const ref_type & ref) : ref_(ref) { }
74  void clear() { map_.clear(); transientMap_.clear(); }
76  size_type size() const { return map_.size(); }
78  bool empty() const { return map_.empty(); }
80  void insert(const key_type & k, const data_type & v) {
81  Tag::insert(ref_, map_, k, v);
82  }
83  void insert(const value_type & kv) {
84  Tag::insert(ref_, map_, kv.key, kv.val);
85  }
87  const_iterator begin() const { return const_iterator(this, map_.begin()); }
89  const_iterator end() const { return const_iterator(this, map_.end()); }
91  const_iterator find(const key_type & k) const {
92  if (ref_.key.id() != k.id()) return end();
93  return find(k.key());
94  }
97  index_type i = k.key();
98  transientMap_.erase(i);
99  return map_.erase(i);
100  }
102  const result_type & operator[](const key_type & k) const {
103  helpers::checkRef(ref_.key, k);
104  return operator[](k.key()).val;
105  }
108  if (ref_.key.id() != k.id()) return 0;
109  typename map_type::const_iterator f = map_.find(k.key());
110  if (f == map_.end()) return 0;
111  return Tag::size(f->second);
112  }
114  const ref_type & refProd() const { return ref_; }
115 
118  typename Tag::transient_map_type map() {
119  return Tag::transientMap( ref_, map_ );
120  }
123  typename Tag::transient_key_vector keys() {
124  return Tag::transientKeyVector( ref_, map_ );
125  }
128  typename Tag::transient_val_vector values() {
129  return Tag::transientValVector( ref_, map_ );
130  }
133 
134  // Find should be private! However, generated reflex dictionaries do not compile
135  // if Find is private.
137  struct Find :
138  public std::binary_function<const self&, size_type, const value_type *> {
139  typedef Find self;
140  const value_type * operator()(typename self::first_argument_type c,
141  typename self::second_argument_type i) {
142  return &(*c.find(i));
143  }
144  };
145 
146  //Used by ROOT storage
148 
149  private:
158  typename map_type::const_iterator f = map_.find(i);
159  if (f == map_.end()) return end();
160  return const_iterator(this, f);
161  }
163  const value_type & operator[](size_type i) const {
164  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
165  if (tf == transientMap_.end()) {
166  typename map_type::const_iterator f = map_.find(i);
167  if (f == map_.end())
168  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
169  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
170  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
171  transientMap_.insert(std::make_pair(i, v));
172  return ins.first->second;
173  } else {
174  return tf->second;
175  }
176  }
177  friend struct const_iterator;
178  friend struct Find;
180  template<typename, typename, typename> friend class OneToValue;
181  template<typename, typename, typename> friend class OneToOne;
182  template<typename, typename, typename> friend class OneToMany;
183  template<typename, typename, typename, typename> friend class OneToManyWithQuality;
184  };
185 
186  namespace refhelper {
187  template<typename Tag>
189  typename AssociationMap<Tag>::value_type> {
191  };
192  }
193 
194 }
195 
196 #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)
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)
default constructor
void clear()
clear map
helpers::KeyVal< key_type, internal_val_type > value_type
value type
Tag::transient_val_vector values()
Tag::key_type key_type
insert key type
key_type key() const
Accessor for product key.
Definition: Ref.h:266
ref_type ref_
reference set
#define CMS_CLASS_VERSION(_version_)
uint16_t size_type
value_type::value_type result_type
result type
ProductID id() const
Accessor for product ID.
Definition: Ref.h:256
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
int k[5][pyjets_maxn]
Tag::val_type internal_val_type
insert key 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
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
reference set type
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
tuple size
Write out results.
const value_type & operator[](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