CMS 3D CMS Logo

ValueMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_ValueMap_h
2 #define DataFormats_Common_ValueMap_h
3 /* \class ValueMap<T>
4  *
5  * \author Luca Lista, INFN
6  *
7  *
8  */
9 
13 #include <vector>
14 #include <map>
15 #include <iterator>
16 #include <algorithm>
17 #include <cstddef>
18 
19 namespace edm {
20  namespace helper {
21  template <typename Map>
22  class Filler {
23  private:
24  typedef std::vector<size_t> index_vector;
25  typedef std::vector<typename Map::value_type> value_vector;
26  typedef std::map<ProductID, value_vector> value_map;
27  typedef typename Map::offset offset;
28  typedef typename Map::id_offset_vector id_offset_vector;
29 
30  public:
31  explicit Filler(Map& map) : map_(map), totSize_(0) { add(map); }
32  void add(const Map& map) {
33  if (map.empty())
34  return;
35  typename id_offset_vector::const_iterator j = map.ids_.begin();
36  const typename id_offset_vector::const_iterator end = map.ids_.end();
37  size_t i = 0;
38  const size_t size = map.values_.size();
39  // std::pair<ProductID, offset> id = *j;
40  do {
41  ProductID id = j->first;
42  ++j;
43  size_t max = (j == end ? size : j->second);
44  typename value_map::iterator f = values_.find(id);
45  if (f != values_.end())
46  throwAdd();
47  value_vector& values = values_.insert(std::make_pair(id, value_vector())).first->second;
48  while (i != max)
49  values.push_back(map.values_[i++]);
50  } while (j != end);
51  }
52  template <typename H, typename I>
53  void insert(const H& h, I begin, I end) {
54  ProductID id = h.id();
55  size_t size = h->size(), sizeIt = end - begin;
56  if (sizeIt != size)
57  throwFillSize();
58  typename value_map::const_iterator f = values_.find(id);
59  if (f != values_.end())
60  throwFillID(id);
61  value_vector& values = values_.insert(make_pair(id, value_vector(size))).first->second;
62  std::copy(begin, end, values.begin());
63  totSize_ += size;
64  }
65  void fill() {
66  map_.clear();
67  offset off = 0;
68  map_.ids_.reserve(values_.size());
69  map_.values_.reserve(totSize_);
70  for (typename value_map::const_iterator i = values_.begin(); i != values_.end(); ++i) {
71  ProductID id = i->first;
72  map_.ids_.push_back(std::make_pair(id, off));
73  const value_vector& values = i->second;
74  for (typename value_vector::const_iterator j = values.begin(); j != values.end(); ++j) {
75  map_.values_.push_back(*j);
76  ++off;
77  }
78  }
79  map_.shrink_to_fit();
80  }
81 
82  protected:
83  Map& map_;
84 
85  private:
87  size_t totSize_;
88 
89  void throwFillSize() const {
91  "ValueMap::Filler: handle and reference "
92  "collections should the same size\n");
93  }
94  void throwFillID(ProductID id) const {
96  e << "index map has already been filled for id: " << id << "\n";
97  e.raise();
98  }
99  void throwAdd() const {
101  "ValueMap: trying to add entries for an already existing product\n");
102  }
103  };
104  } // namespace helper
105 
106  template <typename T>
107  class ValueMap {
108  public:
109  typedef T value_type;
110  typedef std::vector<value_type> container;
111  typedef unsigned int offset;
112  typedef std::vector<std::pair<ProductID, offset> > id_offset_vector;
114  typedef typename container::const_reference const_reference_type;
115 
116  ValueMap() {}
117 
118  void swap(ValueMap& other) {
119  values_.swap(other.values_);
120  ids_.swap(other.ids_);
121  }
122 
123  ValueMap& operator=(ValueMap const& rhs) {
124  ValueMap temp(rhs);
125  this->swap(temp);
126  return *this;
127  }
128 
129  template <typename RefKey>
130  const_reference_type operator[](const RefKey& r) const {
131  return get(r.id(), r.key());
132  }
133  // raw index of a given (id,key) pair
134  size_t rawIndexOf(ProductID id, size_t idx) const {
135  typename id_offset_vector::const_iterator f = getIdOffset(id);
136  if (f == ids_.end())
138  offset off = f->second;
139  size_t j = off + idx;
140  if (j >= values_.size())
141  throwIndexBound();
142  return j;
143  }
144  const_reference_type get(ProductID id, size_t idx) const { return values_[rawIndexOf(id, idx)]; }
145  template <typename RefKey>
146  reference_type operator[](const RefKey& r) {
147  return get(r.id(), r.key());
148  }
149  reference_type get(ProductID id, size_t idx) { return values_[rawIndexOf(id, idx)]; }
150 
152  add(o);
153  return *this;
154  }
155  bool contains(ProductID id) const { return getIdOffset(id) != ids_.end(); }
156  size_t size() const { return values_.size(); }
157  size_t idSize() const { return ids_.size(); }
158  bool empty() const { return values_.empty(); }
159  void clear() {
160  values_.clear();
161  ids_.clear();
162  }
163  void shrink_to_fit() {
164  ids_.shrink_to_fit();
165  values_.shrink_to_fit();
166  }
167 
169 
170  struct const_iterator {
171  typedef ptrdiff_t difference_type;
172  const_iterator() : values_(nullptr) {}
173  ProductID id() const { return i_->first; }
174  typename container::const_iterator begin() const { return values_->begin() + i_->second; }
175  typename container::const_iterator end() const {
176  if (i_ == end_)
177  return values_->end();
178  id_offset_vector::const_iterator end = i_;
179  ++end;
180  if (end == end_)
181  return values_->end();
182  return values_->begin() + end->second;
183  }
184  size_t size() const { return end() - begin(); }
185  const T& operator[](size_t i) { return *(begin() + i); }
187  ++i_;
188  return *this;
189  }
191  const_iterator ci = *this;
192  ++i_;
193  return ci;
194  }
196  --i_;
197  return *this;
198  }
200  const_iterator ci = *this;
201  --i_;
202  return ci;
203  }
204  difference_type operator-(const const_iterator& o) const { return i_ - o.i_; }
207  bool operator<(const const_iterator& o) const { return i_ < o.i_; }
208  bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
209  bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
211  i_ += d;
212  return *this;
213  }
215  i_ -= d;
216  return *this;
217  }
218 
219  private:
220  const_iterator(const id_offset_vector::const_iterator& i_,
221  const id_offset_vector::const_iterator& end,
222  const container* values)
223  : values_(values), i_(i_), end_(end) {}
225  id_offset_vector::const_iterator i_, end_;
226  friend class ValueMap<T>;
227  };
228 
229  const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
230  const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
231 
233  const id_offset_vector& ids() const { return ids_; }
235  const_reference_type get(size_t idx) const { return values_[idx]; }
236 
237  //Used by ROOT storage
239 
240  protected:
243 
244  typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
245  typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
246  if (i == ids_.end())
247  return i;
248  return i->first == id ? i : ids_.end();
249  }
250 
251  void throwIndexBound() const {
252  Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
253  }
254 
255  private:
256  struct IDComparator {
257  bool operator()(const std::pair<ProductID, offset>& p, const ProductID& id) { return p.first < id; }
258  };
259  void throwNotExisting() const {
260  Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
261  }
262 
263  void add(const ValueMap<T>& o) {
264  Filler filler(*this);
265  filler.add(o);
266  filler.fill();
267  }
268 
269  friend class helper::Filler<ValueMap<T> >;
270  };
271 
272  template <typename T>
273  inline ValueMap<T> operator+(const ValueMap<T>& a1, const ValueMap<T>& a2) {
274  ValueMap<T> a = a1;
275  a += a2;
276  return a;
277  }
278 
279  // Free swap function
280  template <typename T>
281  inline void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
282  lhs.swap(rhs);
283  }
284 
285 } // namespace edm
286 #endif
edm::ValueMap::offset
unsigned int offset
Definition: ValueMap.h:111
class-composition.H
H
Definition: class-composition.py:31
edm::helper::Filler::id_offset_vector
Map::id_offset_vector id_offset_vector
Definition: ValueMap.h:28
edm::helper::Filler::insert
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
mps_fire.i
i
Definition: mps_fire.py:428
edm::ValueMap::clear
void clear()
Definition: ValueMap.h:159
edm::ValueMap::ValueMap
ValueMap()
Definition: ValueMap.h:116
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
edm::errors::InvalidReference
Definition: EDMException.h:39
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
edm::ValueMap::const_iterator::size
size_t size() const
Definition: ValueMap.h:184
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
edm::ValueMap::throwNotExisting
void throwNotExisting() const
Definition: ValueMap.h:259
edm::ValueMap::operator=
ValueMap & operator=(ValueMap const &rhs)
Definition: ValueMap.h:123
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ValueMap::get
reference_type get(ProductID id, size_t idx)
Definition: ValueMap.h:149
edm::max
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:118
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
edm::swap
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
edm::ValueMap::const_iterator::operator+=
const_iterator & operator+=(difference_type d)
Definition: ValueMap.h:210
edm::helper::Filler::fill
void fill()
Definition: ValueMap.h:65
edm::ValueMap::const_iterator::operator-
difference_type operator-(const const_iterator &o) const
Definition: ValueMap.h:204
edm::ValueMap::const_iterator::operator-=
const_iterator & operator-=(difference_type d)
Definition: ValueMap.h:214
edm::helper::Filler::throwFillID
void throwFillID(ProductID id) const
Definition: ValueMap.h:94
edm::ValueMap::const_iterator::i_
id_offset_vector::const_iterator i_
Definition: ValueMap.h:225
edm::helper::Filler::Filler
Filler(Map &map)
Definition: ValueMap.h:31
edm::ValueMap::ids_
id_offset_vector ids_
Definition: ValueMap.h:242
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:36
edm::helper::Filler::offset
Map::offset offset
Definition: ValueMap.h:27
edm::ValueMap::const_iterator::operator++
const_iterator & operator++()
Definition: ValueMap.h:186
testProducerWithPsetDescEmpty_cfi.a2
a2
Definition: testProducerWithPsetDescEmpty_cfi.py:35
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
edm::Exception
Definition: EDMException.h:77
edm::ValueMap::container
std::vector< value_type > container
Definition: ValueMap.h:110
EDMException.h
edm::ValueMap::get
const_reference_type get(size_t idx) const
meant to be used in AssociativeIterator, not by the ordinary user
Definition: ValueMap.h:235
edm::ValueMap::const_iterator
Definition: ValueMap.h:170
edm::ValueMap::operator+=
ValueMap< T > & operator+=(const ValueMap< T > &o)
Definition: ValueMap.h:151
edm::ValueMap::empty
bool empty() const
Definition: ValueMap.h:158
edm::ValueMap::ids
const id_offset_vector & ids() const
meant to be used in AssociativeIterator, not by the ordinary user
Definition: ValueMap.h:233
Exhume::I
const std::complex< double > I
Definition: I.h:8
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
edm::ValueMap::const_iterator::difference_type
ptrdiff_t difference_type
Definition: ValueMap.h:171
mps_fire.end
end
Definition: mps_fire.py:242
ProductID.h
trackingPlots.other
other
Definition: trackingPlots.py:1467
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
edm::ValueMap::const_iterator::operator==
bool operator==(const const_iterator &ci) const
Definition: ValueMap.h:208
edm::helper::Filler::totSize_
size_t totSize_
Definition: ValueMap.h:87
h
edm::ValueMap::const_iterator::operator--
const_iterator & operator--()
Definition: ValueMap.h:195
edm::helper::Filler::throwAdd
void throwAdd() const
Definition: ValueMap.h:99
edm::ValueMap::values_
container values_
Definition: ValueMap.h:241
edm::ValueMap::end
const_iterator end() const
Definition: ValueMap.h:230
pfDeepBoostedJetPreprocessParams_cfi.lower_bound
lower_bound
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:15
edm::ValueMap::const_iterator::const_iterator
const_iterator()
Definition: ValueMap.h:172
a
double a
Definition: hdecay.h:119
edm::ValueMap::shrink_to_fit
void shrink_to_fit()
Definition: ValueMap.h:163
edm::ValueMap::const_iterator::operator--
const_iterator operator--(int)
Definition: ValueMap.h:199
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
edm::ValueMap::operator[]
reference_type operator[](const RefKey &r)
Definition: ValueMap.h:146
edm::ValueMap::reference_type
container::reference reference_type
Definition: ValueMap.h:113
edm::ValueMap::const_iterator::id
ProductID id() const
Definition: ValueMap.h:173
helper
Definition: helper.py:1
edm::ValueMap::begin
const_iterator begin() const
Definition: ValueMap.h:229
edm::ValueMap::swap
void swap(ValueMap &other)
Definition: ValueMap.h:118
RecoTauValidation_cfi.reference
reference
Definition: RecoTauValidation_cfi.py:234
edm::helper::Filler::throwFillSize
void throwFillSize() const
Definition: ValueMap.h:89
edm::ValueMap::const_iterator::operator+
const_iterator operator+(difference_type n) const
Definition: ValueMap.h:205
edm::ValueMap::IDComparator
Definition: ValueMap.h:256
edm::ValueMap::const_iterator::end
container::const_iterator end() const
Definition: ValueMap.h:175
edm::ValueMap::const_reference_type
container::const_reference const_reference_type
Definition: ValueMap.h:114
edm::ValueMap::Filler
helper::Filler< ValueMap< T > > Filler
Definition: ValueMap.h:168
edm::helper::Filler::value_map
std::map< ProductID, value_vector > value_map
Definition: ValueMap.h:26
edm::ValueMap::const_iterator::operator-
const_iterator operator-(difference_type n) const
Definition: ValueMap.h:206
edm::ValueMap::throwIndexBound
void throwIndexBound() const
Definition: ValueMap.h:251
edm::helper::Filler::values_
value_map values_
Definition: ValueMap.h:86
edm::ValueMap::const_iterator::values_
const container * values_
Definition: ValueMap.h:224
edm::ValueMap::const_iterator::end_
id_offset_vector::const_iterator end_
Definition: ValueMap.h:225
alignCSCRings.r
r
Definition: alignCSCRings.py:93
edm::ValueMap::get
const_reference_type get(ProductID id, size_t idx) const
Definition: ValueMap.h:144
edm::ValueMap::const_iterator::begin
container::const_iterator begin() const
Definition: ValueMap.h:174
CMS_CLASS_VERSION.h
edm::ValueMap::id_offset_vector
std::vector< std::pair< ProductID, offset > > id_offset_vector
Definition: ValueMap.h:112
edm::ValueMap::idSize
size_t idSize() const
Definition: ValueMap.h:157
edm::ValueMap::const_iterator::operator[]
const T & operator[](size_t i)
Definition: ValueMap.h:185
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:31
T
long double T
Definition: Basic3DVectorLD.h:48
edm::ValueMap
Definition: ValueMap.h:107
edm::Exception::throwThis
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:83
edm::ValueMap::IDComparator::operator()
bool operator()(const std::pair< ProductID, offset > &p, const ProductID &id)
Definition: ValueMap.h:257
edm::ValueMap::const_iterator::operator++
const_iterator operator++(int)
Definition: ValueMap.h:190
edm::helper::Filler::index_vector
std::vector< size_t > index_vector
Definition: ValueMap.h:24
edm::ValueMap::const_iterator::operator<
bool operator<(const const_iterator &o) const
Definition: ValueMap.h:207
edm::ValueMap::rawIndexOf
size_t rawIndexOf(ProductID id, size_t idx) const
Definition: ValueMap.h:134
edm::helper::Filler::add
void add(const Map &map)
Definition: ValueMap.h:32
ztail.d
d
Definition: ztail.py:151
edm::ValueMap::add
void add(const ValueMap< T > &o)
Definition: ValueMap.h:263
edm::helper::Filler::value_vector
std::vector< typename Map::value_type > value_vector
Definition: ValueMap.h:25
edm::ValueMap::contains
bool contains(ProductID id) const
Definition: ValueMap.h:155
edm::operator+
Association< C > operator+(const Association< C > &a1, const Association< C > &a2)
Definition: Association.h:122
edm::helper::Filler
Definition: ValueMap.h:22
genParticles_cff.map
map
Definition: genParticles_cff.py:11
edm::ValueMap::operator[]
const_reference_type operator[](const RefKey &r) const
Definition: ValueMap.h:130
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
edm::helper::Filler::map_
Map & map_
Definition: ValueMap.h:83
edm::ValueMap::const_iterator::const_iterator
const_iterator(const id_offset_vector::const_iterator &i_, const id_offset_vector::const_iterator &end, const container *values)
Definition: ValueMap.h:220
edm::ValueMap::const_iterator::operator!=
bool operator!=(const const_iterator &ci) const
Definition: ValueMap.h:209
edm::ValueMap::getIdOffset
id_offset_vector::const_iterator getIdOffset(ProductID id) const
Definition: ValueMap.h:244
edm::ProductID
Definition: ProductID.h:27
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
edm::ValueMap::size
size_t size() const
Definition: ValueMap.h:156
edm::ValueMap::value_type
T value_type
Definition: ValueMap.h:109