CMS 3D CMS Logo

RangeMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RangeMap_h
2 #define DataFormats_Common_RangeMap_h
3 /* \class edm::RangeMap
4  *
5  * Generic container storing objects arranged according
6  * to a specified identifier.
7  *
8  * The data content can be fetched either via
9  * an iterator, or specifying user-defined identifier
10  * match criteria.
11  *
12  * The template parameters are:
13  * - ID: identifier type
14  * - C : underlying collection used to
15  * - P : policy to perform object cloning
16  *
17  * \author Tommaso Boccali, Luca Lista INFN
18  *
19  *
20  */
21 #include <map>
22 #include <vector>
23 #include <functional>
28 
29 namespace edm {
30 
32  class RangeMap {
33  public:
35  typedef typename C::value_type value_type;
37  typedef typename C::size_type size_type;
39  typedef typename C::reference reference;
41  typedef typename C::pointer pointer;
43  typedef typename C::const_iterator const_iterator;
45  //use unsigned int rather than C::size_type in order to avoid porting problems
46  typedef std::pair<unsigned int, unsigned int> pairType;
48  typedef std::map<ID, pairType> mapType;
50  typedef std::pair<const_iterator, const_iterator> range;
51 
52  private:
54  template <typename CMP>
55  struct comp {
56  comp(const CMP c) : cmp(c) {}
57  bool operator()(ID id, const typename mapType::value_type& p) { return cmp(id, p.first); }
58  bool operator()(const typename mapType::value_type& p, ID id) { return cmp(p.first, id); }
59 
60  private:
61  CMP cmp;
62  };
63 
64  public:
66  RangeMap() {}
74  template <typename CMP>
75  range get(ID id, CMP comparator) const {
76  std::pair<typename mapType::const_iterator, typename mapType::const_iterator> r =
77  std::equal_range(map_.begin(), map_.end(), id, comp<CMP>(comparator));
79  if ((r.first) == map_.end()) {
80  begin = end = collection_.end();
81  return std::make_pair(begin, end);
82  } else {
83  begin = collection_.begin() + (r.first)->second.first;
84  }
85  if ((r.second) == map_.end()) {
86  end = collection_.end();
87  } else {
88  end = collection_.begin() + (r.second)->second.first;
89  }
90  return std::make_pair(begin, end);
91  }
93  template <typename CMP>
94  range get(std::pair<ID, CMP> p) const {
95  return get(p.first, p.second);
96  }
98  range get(ID id) const {
100  typename mapType::const_iterator i = map_.find(id);
101  if (i != map_.end()) {
102  begin = collection_.begin() + i->second.first;
103  end = collection_.begin() + i->second.second;
104  } else {
105  begin = end = collection_.end();
106  }
107  return std::make_pair(begin, end);
108  }
110  template <typename CI>
111  void put(ID id, CI begin, CI end) {
112  typename mapType::const_iterator i = map_.find(id);
113  if (i != map_.end()) {
114  throw Exception(errors::LogicError, "trying to insert duplicate entry");
115  }
116  assert(i == map_.end());
117  pairType& p = map_[id];
118  p.first = collection_.size();
119  for (CI ii = begin; ii != end; ++ii)
120  collection_.push_back(P::clone(*ii));
121  p.second = collection_.size();
122  }
124  size_t size() const { return collection_.size(); }
126  typename C::const_iterator begin() const { return collection_.begin(); }
128  typename C::const_iterator end() const { return collection_.end(); }
130  struct id_iterator {
131  typedef ID value_type;
132  typedef ID* pointer;
133  typedef ID& reference;
134  typedef ptrdiff_t difference_type;
135  typedef typename mapType::const_iterator::iterator_category iterator_category;
136  typedef typename mapType::const_iterator const_iterator;
140  ++i;
141  return *this;
142  }
144  id_iterator ci = *this;
145  ++i;
146  return ci;
147  }
149  --i;
150  return *this;
151  }
153  id_iterator ci = *this;
154  --i;
155  return ci;
156  }
157  bool operator==(const id_iterator& ci) const { return i == ci.i; }
158  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
159  const ID operator*() const { return i->first; }
160 
161  private:
163  };
165  void post_insert() {
166  // sorts the container via ID
167  C tmp;
168  for (typename mapType::iterator it = map_.begin(), itEnd = map_.end(); it != itEnd; it++) {
169  range r = get((*it).first);
170  //do cast to acknowledge that we may be going from a larger type to a smaller type but we are OK
171  unsigned int begIt = static_cast<unsigned int>(tmp.size());
172  for (const_iterator i = r.first; i != r.second; ++i)
173  tmp.push_back(P::clone(*i));
174  unsigned int endIt = static_cast<unsigned int>(tmp.size());
175  it->second = pairType(begIt, endIt);
176  }
177  collection_ = tmp;
178  }
180  id_iterator id_begin() const { return id_iterator(map_.begin()); }
182  id_iterator id_end() const { return id_iterator(map_.end()); }
184  size_t id_size() const { return map_.size(); }
186  std::vector<ID> ids() const {
187  std::vector<ID> temp(id_size());
188  std::copy(id_begin(), id_end(), temp.begin());
189  return temp;
190  }
193 
196 
198  RangeMap& operator=(RangeMap const& rhs);
199 
200  //Used by ROOT storage
202 
203  private:
208  };
209 
210  template <typename ID, typename C, typename P>
211  inline void RangeMap<ID, C, P>::swap(RangeMap<ID, C, P>& other) {
212  collection_.swap(other.collection_);
213  map_.swap(other.map_);
214  }
215 
216  template <typename ID, typename C, typename P>
219  this->swap(temp);
220  return *this;
221  }
222 
223  // free swap function
224  template <typename ID, typename C, typename P>
226  a.swap(b);
227  }
228 
229 } // namespace edm
230 
231 #endif
edm::RangeMap::id_iterator::iterator_category
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:135
mps_fire.i
i
Definition: mps_fire.py:428
edm::RangeMap::comp::operator()
bool operator()(ID id, const typename mapType::value_type &p)
Definition: RangeMap.h:57
edm::RangeMap::id_end
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:182
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
edm::RangeMap::post_insert
void post_insert()
perfor post insert action
Definition: RangeMap.h:165
edm::RangeMap::comp::comp
comp(const CMP c)
Definition: RangeMap.h:56
edm::errors::LogicError
Definition: EDMException.h:37
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::RangeMap::collection_
C collection_
stored collection
Definition: RangeMap.h:205
edm::RangeMap::id_size
size_t id_size() const
number of contained identifiers
Definition: RangeMap.h:184
edm::RangeMap::get
range get(ID id) const
get a range of objects with specified identifier
Definition: RangeMap.h:98
edm::RangeMap::id_iterator::operator!=
bool operator!=(const id_iterator &ci) const
Definition: RangeMap.h:158
edm::RangeMap::RangeMap
RangeMap()
default constructor
Definition: RangeMap.h:66
cms::cuda::assert
assert(be >=bs)
edm::RangeMap::begin
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:126
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
edm::RangeMap::comp
comparator helper class
Definition: RangeMap.h:55
edm::RangeMap::put
void put(ID id, CI begin, CI end)
insert an object range with specified identifier
Definition: RangeMap.h:111
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::RangeMap::id_iterator::operator--
id_iterator operator--(int)
Definition: RangeMap.h:152
edm::RangeMap::id_iterator
identifier iterator
Definition: RangeMap.h:130
edm::RangeMap::size
size_t size() const
return number of contained object
Definition: RangeMap.h:124
edm::RangeMap::id_iterator::pointer
ID * pointer
Definition: RangeMap.h:132
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:42
edm::RangeMap::id_iterator::operator==
bool operator==(const id_iterator &ci) const
Definition: RangeMap.h:157
edm::RangeMap::id_iterator::operator++
id_iterator & operator++()
Definition: RangeMap.h:139
edm::RangeMap::get
range get(std::pair< ID, CMP > p) const
get range of objects matching a specified identifier with a specified comparator.
Definition: RangeMap.h:94
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
CloneTrait.h
clone
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
edm::RangeMap::ids
std::vector< ID > ids() const
indentifier vector
Definition: RangeMap.h:186
edm::RangeMap::comp::operator()
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:58
edm::RangeMap::get
range get(ID id, CMP comparator) const
Definition: RangeMap.h:75
trackingPlots.other
other
Definition: trackingPlots.py:1464
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
edm::RangeMap::swap
void swap(RangeMap< ID, C, P > &other)
swap member function
Definition: RangeMap.h:211
b
double b
Definition: hdecay.h:118
edm::RangeMap::pointer
C::pointer pointer
pointer type
Definition: RangeMap.h:41
edm::RangeMap
Definition: RangeMap.h:32
edm::RangeMap::mapType
std::map< ID, pairType > mapType
map of identifier to index range
Definition: RangeMap.h:48
edm::RangeMap::id_iterator::difference_type
ptrdiff_t difference_type
Definition: RangeMap.h:134
a
double a
Definition: hdecay.h:119
align::ID
uint32_t ID
Definition: Definitions.h:24
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
edm::RangeMap::map_
mapType map_
identifier map
Definition: RangeMap.h:207
edm::RangeMap::end
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:128
gainCalibHelper::gainCalibPI::type
type
Definition: SiPixelGainCalibHelper.h:40
edm::RangeMap::const_iterator
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
edm::RangeMap::id_iterator::id_iterator
id_iterator(const_iterator o)
Definition: RangeMap.h:138
RecoTauValidation_cfi.reference
reference
Definition: RecoTauValidation_cfi.py:233
edm::RangeMap::id_iterator::reference
ID & reference
Definition: RangeMap.h:133
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
svgfig.template
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
edm::RangeMap::comp::cmp
CMP cmp
Definition: RangeMap.h:61
edm::RangeMap::id_iterator::operator--
id_iterator & operator--()
Definition: RangeMap.h:148
alignCSCRings.r
r
Definition: alignCSCRings.py:93
edm::RangeMap::value_type
C::value_type value_type
contained object type
Definition: RangeMap.h:35
edm::RangeMap::id_iterator::const_iterator
mapType::const_iterator const_iterator
Definition: RangeMap.h:136
edm::RangeMap::range
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
edm::RangeMap::reference
C::reference reference
reference type
Definition: RangeMap.h:39
CMS_CLASS_VERSION.h
edm::RangeMap::id_iterator::value_type
ID value_type
Definition: RangeMap.h:131
edm::RangeMap::id_begin
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:180
gen::C
C
Definition: PomwigHadronizer.cc:78
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
edm::RangeMap::id_iterator::id_iterator
id_iterator()
Definition: RangeMap.h:137
Exception
Definition: hltDiff.cc:245
edm::RangeMap::pairType
std::pair< unsigned int, unsigned int > pairType
index range
Definition: RangeMap.h:46
Exception.h
edm::RangeMap::id_iterator::operator*
const ID operator*() const
Definition: RangeMap.h:159
traits.h
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:56
edm::RangeMap::id_iterator::operator++
id_iterator operator++(int)
Definition: RangeMap.h:143
P
std::pair< OmniClusterRef, TrackingParticleRef > P
Definition: BDHadronTrackMonitoringAnalyzer.cc:203
edm::RangeMap::operator=
RangeMap & operator=(RangeMap const &rhs)
copy assignment
Definition: RangeMap.h:217
edm::RangeMap::id_iterator::i
const_iterator i
Definition: RangeMap.h:162
edm::RangeMap::size_type
C::size_type size_type
collection size type
Definition: RangeMap.h:37
cuy.ii
ii
Definition: cuy.py:589
edm::RangeMap::operator[]
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:192