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) {
58  return cmp(id, p.first);
59  }
60  bool operator()(const typename mapType::value_type & p, ID id) {
61  return cmp(p.first, id);
62  }
63  private:
64  CMP cmp;
65 
66  };
67 
68  public:
70  RangeMap() { }
78  template<typename CMP>
79  range get(ID id, CMP comparator) const {
80  std::pair<typename mapType::const_iterator,
81  typename mapType::const_iterator> r =
82  std::equal_range(map_.begin(), map_.end(), id, comp<CMP>(comparator));
83  const_iterator begin, end;
84  if ((r.first) == map_.end()){
85  begin = end = collection_.end();
86  return std::make_pair(begin,end);
87  } else {
88  begin = collection_.begin() + (r.first)->second.first;
89  }
90  if ((r.second) == map_.end()){
91  end = collection_.end();
92  }else{
93  end = collection_.begin() + (r.second)->second.first;
94  }
95  return std::make_pair(begin,end);
96  }
98  template<typename CMP>
99  range get(std::pair<ID, CMP> p) const {
100  return get(p.first, p.second);
101  }
103  range get(ID id) const {
104  const_iterator begin, end;
105  typename mapType::const_iterator i = map_.find(id);
106  if (i != map_.end()) {
107  begin = collection_.begin() + i->second.first;
108  end = collection_.begin() + i->second.second;
109  } else {
110  begin = end = collection_.end();
111  }
112  return std::make_pair(begin, end);
113  }
115  template<typename CI>
116  void put(ID id, CI begin, CI end) {
117  typename mapType::const_iterator i = map_.find(id);
118  if(i != map_.end()) {
119  throw Exception(errors::LogicError, "trying to insert duplicate entry");
120  }
121  assert(i == map_.end());
122  pairType & p = map_[ id ];
123  p.first = collection_.size();
124  for(CI i = begin; i != end; ++i)
125  collection_.push_back(P::clone(*i));
126  p.second = collection_.size();
127  }
129  size_t size() const { return collection_.size(); }
131  typename C::const_iterator begin() const { return collection_.begin(); }
133  typename C::const_iterator end() const { return collection_.end(); }
135  struct id_iterator {
136  typedef ID value_type;
137  typedef ID * pointer;
138  typedef ID & reference;
139  typedef ptrdiff_t difference_type;
140  typedef typename mapType::const_iterator::iterator_category iterator_category;
141  typedef typename mapType::const_iterator const_iterator;
143  id_iterator(const_iterator o) : i(o) { }
144  id_iterator& operator++() { ++i; return *this; }
145  id_iterator operator++(int) { id_iterator ci = *this; ++i; return ci; }
146  id_iterator& operator--() { --i; return *this; }
147  id_iterator operator--(int) { id_iterator ci = *this; --i; return ci; }
148  bool operator==(const id_iterator& ci) const { return i == ci.i; }
149  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
150  const ID operator * () const { return i->first; }
151  private:
152  const_iterator i;
153  };
155  void post_insert() {
156  // sorts the container via ID
157  C tmp;
158  for (typename mapType::iterator it = map_.begin(), itEnd = map_.end(); it != itEnd; it ++) {
159  range r = get((*it).first);
160  //do cast to acknowledge that we may be going from a larger type to a smaller type but we are OK
161  unsigned int begIt = static_cast<unsigned int>(tmp.size());
162  for(const_iterator i = r.first; i != r.second; ++i)
163  tmp.push_back(P::clone(*i));
164  unsigned int endIt = static_cast<unsigned int>(tmp.size());
165  it->second = pairType(begIt, endIt);
166  }
167  collection_ = tmp;
168  }
170  id_iterator id_begin() const { return id_iterator(map_.begin()); }
172  id_iterator id_end() const { return id_iterator(map_.end()); }
174  size_t id_size() const { return map_.size(); }
176  std::vector<ID> ids() const {
177  std::vector<ID> temp(id_size());
178  std::copy(id_begin(), id_end(), temp.begin());
179  return temp;
180  }
182  reference operator[](size_type i) { return collection_[ i ]; }
183 
185  void swap(RangeMap<ID, C, P> & other);
186 
188  RangeMap& operator=(RangeMap const& rhs);
189 
190  //Used by ROOT storage
192 
193  private:
197  mapType map_;
198  };
199 
200  template <typename ID, typename C, typename P>
201  inline
202  void
203  RangeMap<ID, C, P>::swap(RangeMap<ID, C, P> & other) {
204  collection_.swap(other.collection_);
205  map_.swap(other.map_);
206  }
207 
208  template <typename ID, typename C, typename P>
209  inline
213  this->swap(temp);
214  return *this;
215  }
216 
217  // free swap function
218  template <typename ID, typename C, typename P>
219  inline
220  void
222  a.swap(b);
223  }
224 
225 }
226 
227 #endif
type
Definition: HCALResponse.h:21
C collection_
stored collection
Definition: RangeMap.h:195
void post_insert()
perfor post insert action
Definition: RangeMap.h:155
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
id_iterator & operator++()
Definition: RangeMap.h:144
def copy(args, dbName)
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:170
mapType map_
identifier map
Definition: RangeMap.h:197
void swap(RangeMap< ID, C, P > &other)
swap member function
Definition: RangeMap.h:203
size_t size() const
return number of contained object
Definition: RangeMap.h:129
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:131
uint32_t ID
Definition: Definitions.h:26
bool operator==(const id_iterator &ci) const
Definition: RangeMap.h:148
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:182
identifier iterator
Definition: RangeMap.h:135
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:172
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:140
uint16_t size_type
id_iterator & operator--()
Definition: RangeMap.h:146
U second(std::pair< T, U > const &p)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
std::pair< unsigned int, unsigned int > pairType
index range
Definition: RangeMap.h:46
comparator helper class
Definition: RangeMap.h:55
id_iterator operator++(int)
Definition: RangeMap.h:145
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:520
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:116
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:143
mapType::const_iterator const_iterator
Definition: RangeMap.h:141
double b
Definition: hdecay.h:120
std::pair< OmniClusterRef, TrackingParticleRef > P
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
size_t id_size() const
number of contained identifiers
Definition: RangeMap.h:174
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
HLT enums.
bool operator!=(const id_iterator &ci) const
Definition: RangeMap.h:149
double a
Definition: hdecay.h:121
RangeMap()
default constructor
Definition: RangeMap.h:70
C::value_type value_type
contained object type
Definition: RangeMap.h:35
MatrixMeschach operator*(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
std::vector< ID > ids() const
indentifier vector
Definition: RangeMap.h:176
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:60
RangeMap & operator=(RangeMap const &rhs)
copy assignment
Definition: RangeMap.h:211
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:133
C::pointer pointer
pointer type
Definition: RangeMap.h:41
id_iterator operator--(int)
Definition: RangeMap.h:147
C::size_type size_type
collection size type
Definition: RangeMap.h:37