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.37 $
20  *
21  * $Id: RangeMap.h,v 1.37 2009/11/04 15:30:20 wmtan Exp $
22  *
23  */
24 #include <map>
25 #include <vector>
26 #include <ext/functional>
31 
32 namespace edm {
33 
35  class RangeMap {
36  public:
38  typedef typename C::value_type value_type;
40  typedef typename C::size_type size_type;
42  typedef typename C::reference reference;
44  typedef typename C::pointer pointer;
46  typedef typename C::const_iterator const_iterator;
48  //use unsigned int rather than C::size_type in order to avoid porting problems
49  typedef std::pair<unsigned int, unsigned int> pairType;
51  typedef std::map<ID, pairType> mapType;
53  typedef std::pair<const_iterator, const_iterator> range;
54 
55  private:
57  template<typename CMP>
58  struct comp {
59  comp(const CMP c) : cmp(c) { }
60  bool operator()(ID id, const typename mapType::value_type & p) {
61  return cmp(id, p.first);
62  }
63  bool operator()(const typename mapType::value_type & p, ID id) {
64  return cmp(p.first, id);
65  }
66  private:
67  CMP cmp;
68 
69  };
70 
71  public:
73  RangeMap() { }
81  template<typename CMP>
82  range get(ID id, CMP comparator) const {
83  using namespace __gnu_cxx;
84  std::pair<typename mapType::const_iterator,
85  typename mapType::const_iterator> r =
86  std::equal_range(map_.begin(), map_.end(), id, comp<CMP>(comparator));
88  if ((r.first) == map_.end()){
89  begin = end = collection_.end();
90  return std::make_pair(begin,end);
91  } else {
92  begin = collection_.begin() + (r.first)->second.first;
93  }
94  if ((r.second) == map_.end()){
95  end = collection_.end();
96  }else{
97  end = collection_.begin() + (r.second)->second.first;
98  }
99  return std::make_pair(begin,end);
100  }
102  template<typename CMP>
103  range get(std::pair<ID, CMP> p) const {
104  return get(p.first, p.second);
105  }
107  range get(ID id) const {
109  typename mapType::const_iterator i = map_.find(id);
110  if (i != map_.end()) {
111  begin = collection_.begin() + i->second.first;
112  end = collection_.begin() + i->second.second;
113  } else {
114  begin = end = collection_.end();
115  }
116  return std::make_pair(begin, end);
117  }
119  template<typename CI>
120  void put(ID id, CI begin, CI end) {
121  typename mapType::const_iterator i = map_.find(id);
122  if(i != map_.end()) {
123  throw Exception(errors::LogicError, "trying to insert duplicate entry");
124  }
125  assert(i == map_.end());
126  pairType & p = map_[ id ];
127  p.first = collection_.size();
128  for(CI i = begin; i != end; ++i)
129  collection_.push_back(P::clone(*i));
130  p.second = collection_.size();
131  }
133  size_t size() const { return collection_.size(); }
135  typename C::const_iterator begin() const { return collection_.begin(); }
137  typename C::const_iterator end() const { return collection_.end(); }
139  struct id_iterator {
140  typedef ID value_type;
141  typedef ID * pointer;
142  typedef ID & reference;
143  typedef ptrdiff_t difference_type;
144  typedef typename mapType::const_iterator::iterator_category iterator_category;
145  typedef typename mapType::const_iterator const_iterator;
148  id_iterator & operator=(const id_iterator & it) { i = it.i; return *this; }
149  id_iterator& operator++() { ++i; return *this; }
150  id_iterator operator++(int) { id_iterator ci = *this; ++i; return ci; }
151  id_iterator& operator--() { --i; return *this; }
152  id_iterator operator--(int) { id_iterator ci = *this; --i; return ci; }
153  bool operator==(const id_iterator& ci) const { return i == ci.i; }
154  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
155  const ID operator * () const { return i->first; }
156  private:
158  };
160  void post_insert() {
161  // sorts the container via ID
162  C tmp;
163  for (typename mapType::iterator it = map_.begin(), itEnd = map_.end(); it != itEnd; it ++) {
164  range r = get((*it).first);
165  //do cast to acknowledge that we may be going from a larger type to a smaller type but we are OK
166  unsigned int begIt = static_cast<unsigned int>(tmp.size());
167  for(const_iterator i = r.first; i != r.second; ++i)
168  tmp.push_back(P::clone(*i));
169  unsigned int endIt = static_cast<unsigned int>(tmp.size());
170  it->second = pairType(begIt, endIt);
171  }
172  collection_ = tmp;
173  }
175  id_iterator id_begin() const { return id_iterator(map_.begin()); }
177  id_iterator id_end() const { return id_iterator(map_.end()); }
179  size_t id_size() const { return map_.size(); }
181  std::vector<ID> ids() const {
182  std::vector<ID> temp(id_size());
183  std::copy(id_begin(), id_end(), temp.begin());
184  return temp;
185  }
188 
190  void swap(RangeMap<ID, C, P> & other);
191 
193  RangeMap& operator=(RangeMap const& rhs);
194 
195  //Used by ROOT storage
197 
198  private:
203  };
204 
205  template <typename ID, typename C, typename P>
206  inline
207  void
208  RangeMap<ID, C, P>::swap(RangeMap<ID, C, P> & other) {
209  collection_.swap(other.collection_);
210  map_.swap(other.map_);
211  }
212 
213  template <typename ID, typename C, typename P>
214  inline
218  this->swap(temp);
219  return *this;
220  }
221 
222  // free swap function
223  template <typename ID, typename C, typename P>
224  inline
225  void
227  a.swap(b);
228  }
229 
230 }
231 
232 #endif
type
Definition: HCALResponse.h:22
C collection_
stored collection
Definition: RangeMap.h:200
int i
Definition: DBlmapReader.cc:9
void post_insert()
perfor post insert action
Definition: RangeMap.h:160
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:53
id_iterator & operator++()
Definition: RangeMap.h:149
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:175
mapType map_
identifier map
Definition: RangeMap.h:202
void swap(RangeMap< ID, C, P > &other)
swap member function
Definition: RangeMap.h:208
size_t size() const
return number of contained object
Definition: RangeMap.h:133
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:135
uint32_t ID
Definition: Definitions.h:26
bool operator==(const id_iterator &ci) const
Definition: RangeMap.h:153
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:187
identifier iterator
Definition: RangeMap.h:139
#define P
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:177
#define CMS_CLASS_VERSION(_version_)
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:144
uint16_t size_type
id_iterator & operator--()
Definition: RangeMap.h:151
const ID operator*() const
Definition: RangeMap.h:155
U second(std::pair< T, U > const &p)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:46
std::pair< unsigned int, unsigned int > pairType
index range
Definition: RangeMap.h:49
comparator helper class
Definition: RangeMap.h:58
id_iterator operator++(int)
Definition: RangeMap.h:150
bool operator()(ID id, const typename mapType::value_type &p)
Definition: RangeMap.h:60
id_iterator & operator=(const id_iterator &it)
Definition: RangeMap.h:148
Container::value_type value_type
void put(ID id, CI begin, CI end)
insert an object range with specified identifier
Definition: RangeMap.h:120
comp(const CMP c)
Definition: RangeMap.h:59
C::reference reference
reference type
Definition: RangeMap.h:42
std::map< ID, pairType > mapType
map of identifier to index range
Definition: RangeMap.h:51
T * clone(const T *tp)
Definition: Ptr.h:42
id_iterator(const_iterator o)
Definition: RangeMap.h:147
mapType::const_iterator const_iterator
Definition: RangeMap.h:145
double b
Definition: hdecay.h:120
size_t id_size() const
number of contained identifiers
Definition: RangeMap.h:179
#define private
Definition: FWFileEntry.h:18
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
bool operator!=(const id_iterator &ci) const
Definition: RangeMap.h:154
double a
Definition: hdecay.h:121
RangeMap()
default constructor
Definition: RangeMap.h:73
C::value_type value_type
contained object type
Definition: RangeMap.h:38
std::vector< ID > ids() const
indentifier vector
Definition: RangeMap.h:181
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:63
RangeMap & operator=(RangeMap const &rhs)
copy assignment
Definition: RangeMap.h:216
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:137
C::pointer pointer
pointer type
Definition: RangeMap.h:44
id_iterator operator--(int)
Definition: RangeMap.h:152
def template
Definition: svgfig.py:520
C::size_type size_type
collection size type
Definition: RangeMap.h:40