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 
197  //Used by ROOT storage
199 
200  private:
205  };
206 
207  template <typename ID, typename C, typename P>
208  inline void RangeMap<ID, C, P>::swap(RangeMap<ID, C, P>& other) {
209  collection_.swap(other.collection_);
210  map_.swap(other.map_);
211  }
212 
213  // free swap function
214  template <typename ID, typename C, typename P>
216  a.swap(b);
217  }
218 
219 } // namespace edm
220 
221 #endif
C collection_
stored collection
Definition: RangeMap.h:202
void post_insert()
perfor post insert action
Definition: RangeMap.h:165
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
id_iterator & operator++()
Definition: RangeMap.h:139
bool operator==(const id_iterator &ci) const
Definition: RangeMap.h:157
mapType map_
identifier map
Definition: RangeMap.h:204
void swap(RangeMap< ID, C, P > &other)
swap member function
Definition: RangeMap.h:208
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:126
uint32_t ID
Definition: Definitions.h:24
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:192
identifier iterator
Definition: RangeMap.h:130
std::vector< ID > ids() const
indentifier vector
Definition: RangeMap.h:186
#define CMS_CLASS_VERSION(_version_)
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:135
assert(be >=bs)
uint16_t size_type
size_t size() const
return number of contained object
Definition: RangeMap.h:124
id_iterator & operator--()
Definition: RangeMap.h:148
U second(std::pair< T, U > const &p)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
size_t id_size() const
number of contained identifiers
Definition: RangeMap.h:184
std::pair< unsigned int, unsigned int > pairType
index range
Definition: RangeMap.h:46
comparator helper class
Definition: RangeMap.h:55
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:182
id_iterator operator++(int)
Definition: RangeMap.h:143
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
bool operator()(ID id, const typename mapType::value_type &p)
Definition: RangeMap.h:57
void put(ID id, CI begin, CI end)
insert an object range with specified identifier
Definition: RangeMap.h:111
ii
Definition: cuy.py:589
comp(const CMP c)
Definition: RangeMap.h:56
C::reference reference
reference type
Definition: RangeMap.h:39
std::map< ID, pairType > mapType
map of identifier to index range
Definition: RangeMap.h:48
id_iterator(const_iterator o)
Definition: RangeMap.h:138
mapType::const_iterator const_iterator
Definition: RangeMap.h:136
double b
Definition: hdecay.h:120
std::pair< OmniClusterRef, TrackingParticleRef > P
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
const ID operator*() const
Definition: RangeMap.h:159
HLT enums.
double a
Definition: hdecay.h:121
RangeMap()
default constructor
Definition: RangeMap.h:66
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:180
C::value_type value_type
contained object type
Definition: RangeMap.h:35
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:58
bool operator!=(const id_iterator &ci) const
Definition: RangeMap.h:158
tmp
align.sh
Definition: createJobs.py:716
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:128
C::pointer pointer
pointer type
Definition: RangeMap.h:41
id_iterator operator--(int)
Definition: RangeMap.h:152
C::size_type size_type
collection size type
Definition: RangeMap.h:37