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) { }
57  map_ = it.map_; i = it.i; return *this;
58  }
59  const_iterator& operator++() { ++i; return *this; }
60  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
61  const_iterator& operator--() { --i; return *this; }
62  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
63  bool operator==(const const_iterator& ci) const { return i == ci.i; }
64  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
65  const value_type & operator *() const { return (*map_)[ i->first ]; }
66  const value_type * operator->() const { return &operator *(); }
67  private:
68  const self * map_;
69  typename map_type::const_iterator i;
70  };
71 
75  explicit
76  AssociationMap(const ref_type & ref) : ref_(ref) { }
78  void clear() { map_.clear(); transientMap_.clear(); }
80  size_type size() const { return map_.size(); }
82  bool empty() const { return map_.empty(); }
84  void insert(const key_type & k, const data_type & v) {
85  Tag::insert(ref_, map_, k, v);
86  }
87  void insert(const value_type & kv) {
88  Tag::insert(ref_, map_, kv.key, kv.val);
89  }
91  const_iterator begin() const { return const_iterator(this, map_.begin()); }
93  const_iterator end() const { return const_iterator(this, map_.end()); }
95  const_iterator find(const key_type & k) const {
96  if (ref_.key.id() != k.id()) return end();
97  return find(k.key());
98  }
101  index_type i = k.key();
102  transientMap_.erase(i);
103  return map_.erase(i);
104  }
106  const result_type & operator[](const key_type & k) const {
107  helpers::checkRef(ref_.key, k);
108  return operator[](k.key()).val;
109  }
112  if (ref_.key.id() != k.id()) return 0;
113  typename map_type::const_iterator f = map_.find(k.key());
114  if (f == map_.end()) return 0;
115  return Tag::size(f->second);
116  }
118  const ref_type & refProd() const { return ref_; }
119 
122  typename Tag::transient_map_type map() {
123  return Tag::transientMap( ref_, map_ );
124  }
127  typename Tag::transient_key_vector keys() {
128  return Tag::transientKeyVector( ref_, map_ );
129  }
132  typename Tag::transient_val_vector values() {
133  return Tag::transientValVector( ref_, map_ );
134  }
137 
138  // Find should be private! However, generated reflex dictionaries do not compile
139  // with gcc 3.4.5 if Find is private. Reflex should fix this!!
141  struct Find :
142  public std::binary_function<const self&, size_type, const value_type *> {
143  typedef Find self;
144  const value_type * operator()(typename self::first_argument_type c,
145  typename self::second_argument_type i) {
146  return &(*c.find(i));
147  }
148  };
149 
150  //Used by ROOT storage
152 
153  private:
162  typename map_type::const_iterator f = map_.find(i);
163  if (f == map_.end()) return end();
164  return const_iterator(this, f);
165  }
167  const value_type & operator[](size_type i) const {
168  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
169  if (tf == transientMap_.end()) {
170  typename map_type::const_iterator f = map_.find(i);
171  if (f == map_.end())
172  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
173  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
174  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
175  transientMap_.insert(std::make_pair(i, v));
176  return ins.first->second;
177  } else {
178  return tf->second;
179  }
180  }
181  friend struct const_iterator;
182  friend struct Find;
184  template<typename, typename, typename> friend class OneToValue;
185  template<typename, typename, typename> friend class OneToOne;
186  template<typename, typename, typename> friend class OneToMany;
187  template<typename, typename, typename, typename> friend class OneToManyWithQuality;
188  };
189 
190  namespace refhelper {
191  template<typename Tag>
193  typename AssociationMap<Tag>::value_type> {
195  };
196  }
197 
198 }
199 
200 #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="")
Definition: EDMException.cc:81
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
const_iterator & operator=(const const_iterator &it)
double f[11][100]
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
bool insert(Storage &, ItemType *, const IdTag &)
tuple size
Write out results.
mathSSE::Vec4< T > v
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