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.39 $
20  *
21  *
22  */
23 #include <map>
24 #include <vector>
25 #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++() { ++i; return *this; }
148  id_iterator operator++(int) { id_iterator ci = *this; ++i; return ci; }
149  id_iterator& operator--() { --i; return *this; }
150  id_iterator operator--(int) { id_iterator ci = *this; --i; return ci; }
151  bool operator==(const id_iterator& ci) const { return i == ci.i; }
152  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
153  const ID operator * () const { return i->first; }
154  private:
156  };
158  void post_insert() {
159  // sorts the container via ID
160  C tmp;
161  for (typename mapType::iterator it = map_.begin(), itEnd = map_.end(); it != itEnd; it ++) {
162  range r = get((*it).first);
163  //do cast to acknowledge that we may be going from a larger type to a smaller type but we are OK
164  unsigned int begIt = static_cast<unsigned int>(tmp.size());
165  for(const_iterator i = r.first; i != r.second; ++i)
166  tmp.push_back(P::clone(*i));
167  unsigned int endIt = static_cast<unsigned int>(tmp.size());
168  it->second = pairType(begIt, endIt);
169  }
170  collection_ = tmp;
171  }
173  id_iterator id_begin() const { return id_iterator(map_.begin()); }
175  id_iterator id_end() const { return id_iterator(map_.end()); }
177  size_t id_size() const { return map_.size(); }
179  std::vector<ID> ids() const {
180  std::vector<ID> temp(id_size());
181  std::copy(id_begin(), id_end(), temp.begin());
182  return temp;
183  }
186 
188  void swap(RangeMap<ID, C, P> & other);
189 
191  RangeMap& operator=(RangeMap const& rhs);
192 
193  //Used by ROOT storage
195 
196  private:
201  };
202 
203  template <typename ID, typename C, typename P>
204  inline
205  void
206  RangeMap<ID, C, P>::swap(RangeMap<ID, C, P> & other) {
207  collection_.swap(other.collection_);
208  map_.swap(other.map_);
209  }
210 
211  template <typename ID, typename C, typename P>
212  inline
216  this->swap(temp);
217  return *this;
218  }
219 
220  // free swap function
221  template <typename ID, typename C, typename P>
222  inline
223  void
225  a.swap(b);
226  }
227 
228 }
229 
230 #endif
type
Definition: HCALResponse.h:21
C collection_
stored collection
Definition: RangeMap.h:198
int i
Definition: DBlmapReader.cc:9
void post_insert()
perfor post insert action
Definition: RangeMap.h:158
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:52
id_iterator & operator++()
Definition: RangeMap.h:147
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:173
mapType map_
identifier map
Definition: RangeMap.h:200
void swap(RangeMap< ID, C, P > &other)
swap member function
Definition: RangeMap.h:206
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:151
reference operator[](size_type i)
direct access to an object in the collection
Definition: RangeMap.h:185
identifier iterator
Definition: RangeMap.h:138
#define P
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:175
#define CMS_CLASS_VERSION(_version_)
mapType::const_iterator::iterator_category iterator_category
Definition: RangeMap.h:143
uint16_t size_type
id_iterator & operator--()
Definition: RangeMap.h:149
const ID operator*() const
Definition: RangeMap.h:153
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:148
bool operator()(ID id, const typename mapType::value_type &p)
Definition: RangeMap.h:59
Container::value_type value_type
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
id_iterator(const_iterator o)
Definition: RangeMap.h:146
mapType::const_iterator const_iterator
Definition: RangeMap.h:144
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:177
#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:152
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:179
bool operator()(const typename mapType::value_type &p, ID id)
Definition: RangeMap.h:62
RangeMap & operator=(RangeMap const &rhs)
copy assignment
Definition: RangeMap.h:214
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:150
def template
Definition: svgfig.py:520
C::size_type size_type
collection size type
Definition: RangeMap.h:39