CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 <ext/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  using namespace __gnu_cxx;
81  std::pair<typename mapType::const_iterator,
82  typename mapType::const_iterator> r =
83  std::equal_range(map_.begin(), map_.end(), id, comp<CMP>(comparator));
85  if ((r.first) == map_.end()){
86  begin = end = collection_.end();
87  return std::make_pair(begin,end);
88  } else {
89  begin = collection_.begin() + (r.first)->second.first;
90  }
91  if ((r.second) == map_.end()){
92  end = collection_.end();
93  }else{
94  end = collection_.begin() + (r.second)->second.first;
95  }
96  return std::make_pair(begin,end);
97  }
99  template<typename CMP>
100  range get(std::pair<ID, CMP> p) const {
101  return get(p.first, p.second);
102  }
104  range get(ID id) const {
106  typename mapType::const_iterator i = map_.find(id);
107  if (i != map_.end()) {
108  begin = collection_.begin() + i->second.first;
109  end = collection_.begin() + i->second.second;
110  } else {
111  begin = end = collection_.end();
112  }
113  return std::make_pair(begin, end);
114  }
116  template<typename CI>
117  void put(ID id, CI begin, CI end) {
118  typename mapType::const_iterator i = map_.find(id);
119  if(i != map_.end()) {
120  throw Exception(errors::LogicError, "trying to insert duplicate entry");
121  }
122  assert(i == map_.end());
123  pairType & p = map_[ id ];
124  p.first = collection_.size();
125  for(CI i = begin; i != end; ++i)
126  collection_.push_back(P::clone(*i));
127  p.second = collection_.size();
128  }
130  size_t size() const { return collection_.size(); }
132  typename C::const_iterator begin() const { return collection_.begin(); }
134  typename C::const_iterator end() const { return collection_.end(); }
136  struct id_iterator {
137  typedef ID value_type;
138  typedef ID * pointer;
139  typedef ID & reference;
140  typedef ptrdiff_t difference_type;
141  typedef typename mapType::const_iterator::iterator_category iterator_category;
142  typedef typename mapType::const_iterator const_iterator;
145  id_iterator& operator++() { ++i; return *this; }
146  id_iterator operator++(int) { id_iterator ci = *this; ++i; return ci; }
147  id_iterator& operator--() { --i; return *this; }
148  id_iterator operator--(int) { id_iterator ci = *this; --i; return ci; }
149  bool operator==(const id_iterator& ci) const { return i == ci.i; }
150  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
151  const ID operator * () const { return i->first; }
152  private:
154  };
156  void post_insert() {
157  // sorts the container via ID
158  C tmp;
159  for (typename mapType::iterator it = map_.begin(), itEnd = map_.end(); it != itEnd; it ++) {
160  range r = get((*it).first);
161  //do cast to acknowledge that we may be going from a larger type to a smaller type but we are OK
162  unsigned int begIt = static_cast<unsigned int>(tmp.size());
163  for(const_iterator i = r.first; i != r.second; ++i)
164  tmp.push_back(P::clone(*i));
165  unsigned int endIt = static_cast<unsigned int>(tmp.size());
166  it->second = pairType(begIt, endIt);
167  }
168  collection_ = tmp;
169  }
171  id_iterator id_begin() const { return id_iterator(map_.begin()); }
173  id_iterator id_end() const { return id_iterator(map_.end()); }
175  size_t id_size() const { return map_.size(); }
177  std::vector<ID> ids() const {
178  std::vector<ID> temp(id_size());
179  std::copy(id_begin(), id_end(), temp.begin());
180  return temp;
181  }
184 
186  void swap(RangeMap<ID, C, P> & other);
187 
189  RangeMap& operator=(RangeMap const& rhs);
190 
191  //Used by ROOT storage
193 
194  private:
199  };
200 
201  template <typename ID, typename C, typename P>
202  inline
203  void
204  RangeMap<ID, C, P>::swap(RangeMap<ID, C, P> & other) {
205  collection_.swap(other.collection_);
206  map_.swap(other.map_);
207  }
208 
209  template <typename ID, typename C, typename P>
210  inline
214  this->swap(temp);
215  return *this;
216  }
217 
218  // free swap function
219  template <typename ID, typename C, typename P>
220  inline
221  void
223  a.swap(b);
224  }
225 
226 }
227 
228 #endif
type
Definition: HCALResponse.h:21
C collection_
stored collection
Definition: RangeMap.h:196
int i
Definition: DBlmapReader.cc:9
void post_insert()
perfor post insert action
Definition: RangeMap.h:156
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
id_iterator & operator++()
Definition: RangeMap.h:145
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:171
mapType map_
identifier map
Definition: RangeMap.h:198
void swap(RangeMap< ID, C, P > &other)
swap member function
Definition: RangeMap.h:204
size_t size() const
return number of contained object
Definition: RangeMap.h:130
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:132
uint32_t ID
Definition: Definitions.h:26
assert(m_qm.get())
bool operator==(const id_iterator &ci) const
Definition: RangeMap.h:149
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:183
identifier iterator
Definition: RangeMap.h:136
#define P
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:173
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:141
uint16_t size_type
id_iterator & operator--()
Definition: RangeMap.h:147
const ID operator*() const
Definition: RangeMap.h:151
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:146
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:117
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:144
mapType::const_iterator const_iterator
Definition: RangeMap.h:142
double b
Definition: hdecay.h:120
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
size_t id_size() const
number of contained identifiers
Definition: RangeMap.h:175
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
bool operator!=(const id_iterator &ci) const
Definition: RangeMap.h:150
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
std::vector< ID > ids() const
indentifier vector
Definition: RangeMap.h:177
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:60
RangeMap & operator=(RangeMap const &rhs)
copy assignment
Definition: RangeMap.h:212
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:134
C::pointer pointer
pointer type
Definition: RangeMap.h:41
id_iterator operator--(int)
Definition: RangeMap.h:148
def template
Definition: svgfig.py:520
C::size_type size_type
collection size type
Definition: RangeMap.h:37