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  * \version $Revision: 1.36 $
20  *
21  * $Id: RangeMap.h,v 1.36 2008/07/23 22:50:16 wmtan Exp $
22  *
23  */
24 #include <map>
25 #include <vector>
26 #include <ext/functional>
30 
31 namespace edm {
32 
34  class RangeMap {
35  public:
37  typedef typename C::value_type value_type;
39  typedef typename C::size_type size_type;
41  typedef typename C::reference reference;
43  typedef typename C::pointer pointer;
45  typedef typename C::const_iterator const_iterator;
47  //use unsigned int rather than C::size_type in order to avoid porting problems
48  typedef std::pair<unsigned int, unsigned int> pairType;
50  typedef std::map<ID, pairType> mapType;
52  typedef std::pair<const_iterator, const_iterator> range;
53 
54  private:
56  template<typename CMP>
57  struct comp {
58  comp(const CMP c) : cmp(c) { }
59  bool operator()(ID id, const typename mapType::value_type & p) {
60  return cmp(id, p.first);
61  }
62  bool operator()(const typename mapType::value_type & p, ID id) {
63  return cmp(p.first, id);
64  }
65  private:
66  CMP cmp;
67 
68  };
69 
70  public:
72  RangeMap() { }
80  template<typename CMP>
81  range get(ID id, CMP comparator) const {
82  using namespace __gnu_cxx;
83  std::pair<typename mapType::const_iterator,
84  typename mapType::const_iterator> r =
85  std::equal_range(map_.begin(), map_.end(), id, comp<CMP>(comparator));
87  if ((r.first) == map_.end()){
88  begin = end = collection_.end();
89  return std::make_pair(begin,end);
90  } else {
91  begin = collection_.begin() + (r.first)->second.first;
92  }
93  if ((r.second) == map_.end()){
94  end = collection_.end();
95  }else{
96  end = collection_.begin() + (r.second)->second.first;
97  }
98  return std::make_pair(begin,end);
99  }
101  template<typename CMP>
102  range get(std::pair<ID, CMP> p) const {
103  return get(p.first, p.second);
104  }
106  range get(ID id) const {
108  typename mapType::const_iterator i = map_.find(id);
109  if (i != map_.end()) {
110  begin = collection_.begin() + i->second.first;
111  end = collection_.begin() + i->second.second;
112  } else {
113  begin = end = collection_.end();
114  }
115  return std::make_pair(begin, end);
116  }
118  template<typename CI>
119  void put(ID id, CI begin, CI end) {
120  typename mapType::const_iterator i = map_.find(id);
121  if(i != map_.end()) {
122  throw Exception(errors::LogicError, "trying to insert duplicate entry");
123  }
124  assert(i == map_.end());
125  pairType & p = map_[ id ];
126  p.first = collection_.size();
127  for(CI i = begin; i != end; ++i)
128  collection_.push_back(P::clone(*i));
129  p.second = collection_.size();
130  }
132  size_t size() const { return collection_.size(); }
134  typename C::const_iterator begin() const { return collection_.begin(); }
136  typename C::const_iterator end() const { return collection_.end(); }
138  struct id_iterator {
139  typedef ID value_type;
140  typedef ID * pointer;
141  typedef ID & reference;
142  typedef ptrdiff_t difference_type;
143  typedef typename mapType::const_iterator::iterator_category iterator_category;
144  typedef typename mapType::const_iterator const_iterator;
147  id_iterator & operator=(const id_iterator & it) { i = it.i; return *this; }
148  id_iterator& operator++() { ++i; return *this; }
149  id_iterator operator++(int) { id_iterator ci = *this; ++i; return ci; }
150  id_iterator& operator--() { --i; return *this; }
151  id_iterator operator--(int) { id_iterator ci = *this; --i; return ci; }
152  bool operator==(const id_iterator& ci) const { return i == ci.i; }
153  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
154  const ID operator * () const { return i->first; }
155  private:
157  };
159  void post_insert() {
160  // sorts the container via ID
161  C tmp;
162  for (typename mapType::iterator it = map_.begin(), itEnd = map_.end(); it != itEnd; it ++) {
163  range r = get((*it).first);
164  //do cast to acknowledge that we may be going from a larger type to a smaller type but we are OK
165  unsigned int begIt = static_cast<unsigned int>(tmp.size());
166  for(const_iterator i = r.first; i != r.second; ++i)
167  tmp.push_back(P::clone(*i));
168  unsigned int endIt = static_cast<unsigned int>(tmp.size());
169  it->second = pairType(begIt, endIt);
170  }
171  collection_ = tmp;
172  }
174  id_iterator id_begin() const { return id_iterator(map_.begin()); }
176  id_iterator id_end() const { return id_iterator(map_.end()); }
178  size_t id_size() const { return map_.size(); }
180  std::vector<ID> ids() const {
181  std::vector<ID> temp(id_size());
182  std::copy(id_begin(), id_end(), temp.begin());
183  return temp;
184  }
187 
189  void swap(RangeMap<ID, C, P> & other);
190 
192  RangeMap& operator=(RangeMap const& rhs);
193 
194  private:
199  };
200 
201  template <typename ID, typename C, typename P>
202  inline
203  void
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:22
C collection_
stored collection
Definition: RangeMap.h:196
int i
Definition: DBlmapReader.cc:9
void post_insert()
perfor post insert action
Definition: RangeMap.h:159
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:52
id_iterator & operator++()
Definition: RangeMap.h:148
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:174
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:132
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:134
uint32_t ID
Definition: Definitions.h:26
bool operator==(const id_iterator &ci) const
Definition: RangeMap.h:152
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:186
identifier iterator
Definition: RangeMap.h:138
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:176
Container::value_type value_type
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:143
uint16_t size_type
id_iterator & operator--()
Definition: RangeMap.h:150
const ID operator*() const
Definition: RangeMap.h:154
U second(std::pair< T, U > const &p)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:45
std::pair< unsigned int, unsigned int > pairType
index range
Definition: RangeMap.h:48
comparator helper class
Definition: RangeMap.h:57
id_iterator operator++(int)
Definition: RangeMap.h:149
bool operator()(ID id, const typename mapType::value_type &p)
Definition: RangeMap.h:59
id_iterator & operator=(const id_iterator &it)
Definition: RangeMap.h:147
void put(ID id, CI begin, CI end)
insert an object range with specified identifier
Definition: RangeMap.h:119
comp(const CMP c)
Definition: RangeMap.h:58
C::reference reference
reference type
Definition: RangeMap.h:41
std::map< ID, pairType > mapType
map of identifier to index range
Definition: RangeMap.h:50
T * clone(const T *tp)
Definition: Ptr.h:42
id_iterator(const_iterator o)
Definition: RangeMap.h:146
mapType::const_iterator const_iterator
Definition: RangeMap.h:144
double b
Definition: hdecay.h:120
size_t id_size() const
number of contained identifiers
Definition: RangeMap.h:178
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
bool operator!=(const id_iterator &ci) const
Definition: RangeMap.h:153
double a
Definition: hdecay.h:121
RangeMap()
default constructor
Definition: RangeMap.h:72
C::value_type value_type
contained object type
Definition: RangeMap.h:37
std::vector< ID > ids() const
indentifier vector
Definition: RangeMap.h:180
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:62
RangeMap & operator=(RangeMap const &rhs)
copy assignment
Definition: RangeMap.h:212
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:136
C::pointer pointer
pointer type
Definition: RangeMap.h:43
id_iterator operator--(int)
Definition: RangeMap.h:151
C::size_type size_type
collection size type
Definition: RangeMap.h:39