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 
18 
19 namespace edm {
20  template<typename Tag>
23  typedef typename Tag::val_type internal_val_type;
24  public:
26  typedef AssociationMap<Tag> self;
28  typedef typename Tag::index_type index_type;
30  typedef typename Tag::key_type key_type;
32  typedef typename Tag::data_type data_type;
34  typedef typename Tag::ref_type ref_type;
36  typedef typename Tag::map_type map_type;
38  typedef typename map_type::size_type size_type;
44  typedef typename std::map<index_type, value_type> internal_transient_map_type;
45 
47  struct const_iterator {
48  typedef typename self::value_type value_type;
49  typedef ptrdiff_t difference_type;
50  typedef value_type * pointer;
51  typedef value_type & reference;
52  typedef typename map_type::const_iterator::iterator_category iterator_category;
53  const_iterator(): map_(0) { }
54  const_iterator(const self * map, typename map_type::const_iterator mi) :
55  map_(map), i(mi) { }
56  const_iterator& operator++() { ++i; return *this; }
57  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
58  const_iterator& operator--() { --i; return *this; }
59  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
60  bool operator==(const const_iterator& ci) const { return i == ci.i; }
61  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
62  const value_type & operator *() const { return (*map_)[ i->first ]; }
63  const value_type * operator->() const { return &operator *(); }
64  private:
65  const self * map_;
66  typename map_type::const_iterator i;
67  };
68 
72  explicit
73  AssociationMap(const ref_type & ref) : ref_(ref) { }
75  void clear() { map_.clear(); transientMap_.clear(); }
77  size_type size() const { return map_.size(); }
79  bool empty() const { return map_.empty(); }
81  void insert(const key_type & k, const data_type & v) {
82  Tag::insert(ref_, map_, k, v);
83  }
84  void insert(const value_type & kv) {
85  Tag::insert(ref_, map_, kv.key, kv.val);
86  }
88  const_iterator begin() const { return const_iterator(this, map_.begin()); }
90  const_iterator end() const { return const_iterator(this, map_.end()); }
92  const_iterator find(const key_type & k) const {
93  if (ref_.key.id() != k.id()) return end();
94  return find(k.key());
95  }
98  index_type i = k.key();
99  transientMap_.erase(i);
100  return map_.erase(i);
101  }
103  const result_type & operator[](const key_type & k) const {
104  helpers::checkRef(ref_.key, k);
105  return operator[](k.key()).val;
106  }
109  if (ref_.key.id() != k.id()) return 0;
110  typename map_type::const_iterator f = map_.find(k.key());
111  if (f == map_.end()) return 0;
112  return Tag::size(f->second);
113  }
115  const ref_type & refProd() const { return ref_; }
116 
119  typename Tag::transient_map_type map() {
120  return Tag::transientMap( ref_, map_ );
121  }
124  typename Tag::transient_key_vector keys() {
125  return Tag::transientKeyVector( ref_, map_ );
126  }
129  typename Tag::transient_val_vector values() {
130  return Tag::transientValVector( ref_, map_ );
131  }
134 
135  // Find should be private! However, generated reflex dictionaries do not compile
136  // if Find is private.
138  struct Find :
139  public std::binary_function<const self&, size_type, const value_type *> {
140  typedef Find self;
141  const value_type * operator()(typename self::first_argument_type c,
142  typename self::second_argument_type i) {
143  return &(*c.find(i));
144  }
145  };
146 
147  //Used by ROOT storage
149 
150  private:
159  typename map_type::const_iterator f = map_.find(i);
160  if (f == map_.end()) return end();
161  return const_iterator(this, f);
162  }
164  const value_type & operator[](size_type i) const {
165  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
166  if (tf == transientMap_.end()) {
167  typename map_type::const_iterator f = map_.find(i);
168  if (f == map_.end())
169  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
170  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
171  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
172  transientMap_.insert(std::make_pair(i, v));
173  return ins.first->second;
174  } else {
175  return tf->second;
176  }
177  }
178  friend struct const_iterator;
179  friend struct Find;
181  template<typename, typename, typename> friend class OneToValue;
182  template<typename, typename, typename> friend class OneToOne;
183  template<typename, typename, typename> friend class OneToMany;
184  template<typename, typename, typename, typename> friend class OneToManyWithQuality;
185  };
186 
187  namespace refhelper {
188  template<typename Tag>
190  typename AssociationMap<Tag>::value_type> {
192  };
193  }
194 
195 }
196 
197 #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
ref_type ref_
reference set
#define CMS_CLASS_VERSION(_version_)
uint16_t size_type
value_type::value_type result_type
result type
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:18
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