CMS 3D CMS Logo

MapOfVectors.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_MapOfVectors_h
2 #define DataFormats_Common_MapOfVectors_h
3 
4 #include <vector>
5 #include <map>
6 
7 #include <boost/range/iterator_range.hpp>
8 #include <boost/iterator/iterator_facade.hpp>
9 
10 class TestMapOfVectors;
11 
12 namespace edm {
13 
14  /* a linearized read-only map-of vectors
15 
16  */
17  template<typename K, typename T>
18  class MapOfVectors {
19  public:
20 
21  typedef MapOfVectors<K,T> self;
22  typedef std::map<K,std::vector<T> > TheMap;
23 
24  typedef unsigned int size_type;
25 
26  typedef std::vector<K> Keys;
27  typedef std::vector<size_type> Offsets;
28  typedef std::vector<T> Data;
29 
30  typedef typename Keys::const_iterator key_iterator;
31  typedef Offsets::const_iterator offset_iterator;
32  typedef typename Data::const_iterator data_iterator;
33 
34  typedef boost::iterator_range<data_iterator> range;
35 
36  typedef std::pair<K , range> Pair;
37 
38  class Iter
39  : public boost::iterator_facade<Iter,
40  Pair const,
41  boost::forward_traversal_tag >
42  {
43 
44  public:
45  typedef Iter self;
46  Iter() {}
47 
48  explicit Iter(key_iterator k, offset_iterator o, std::vector<T> const & d )
49  : key(k),
50  off(o),
51  data(d.begin())
52  {}
53 
54  private:
56 
57  void increment() {
58  ++key; ++off;
59  }
60 
61  bool equal(self const& other) const {
62  return this->key == other.key;
63  }
64 
65  Pair const & dereference() const {
66  // FIXME can be optimized...
67  cache.first = *key;
68  cache.second = range(data+(*off),data+(*(off+1)));
69  return cache;
70  }
71 
72 
73  key_iterator key;
74  offset_iterator off;
75  data_iterator data;
76  mutable Pair cache;
77  };
78 
80 
81 
82  range emptyRange() const { return range(m_data.end(),m_data.end());}
83 
84  MapOfVectors() : m_offsets(1,0) {}
85 
86  MapOfVectors(TheMap const & it) {
87  m_keys.reserve(it.size());
88  m_offsets.reserve(it.size()+1);
89  m_offsets.push_back(0);
90  size_type tot=0;
91  for(typename TheMap::const_iterator p=it.begin(); p!=it.end();++p)
92  tot += (*p).second.size();
93  m_data.reserve(tot);
94  for(typename TheMap::const_iterator p=it.begin(); p!=it.end();++p)
95  loadNext((*p).first,(*p).second);
96 
97  }
98 
99  void loadNext(K const & k, std::vector<T> const & v) {
100  m_keys.push_back(k);
101  m_data.resize(m_offsets.back()+v.size());
102  std::copy(v.begin(),v.end(),m_data.begin()+m_offsets.back());
103  m_offsets.push_back(m_data.size());
104  }
105 
106  size_type size() const { return m_keys.size();}
107 
108  bool empty() const { return m_keys.empty();}
109 
110  key_iterator findKey(K const & k) const {
111  std::pair<key_iterator,key_iterator> p =
112  std::equal_range(m_keys.begin(), m_keys.end(), k);
113  return (p.first!=p.second) ? p.first : m_keys.end();
114  }
115 
116  size_type offset(K const & k) const {
117  key_iterator p = findKey(k);
118  if (p==m_keys.end()) return m_data.size();
119  return m_offsets[p-m_keys.begin()];
120  }
121 
122  range find(K const & k) const {
123  key_iterator p = findKey(k);
124  if (p==m_keys.end()) return emptyRange();
125  size_type loc = p-m_keys.begin();
126  data_iterator b = m_data.begin()+m_offsets[loc];
127  data_iterator e = m_data.begin()+m_offsets[loc+1];
128  return range(b,e);
129  }
130 
131  const_iterator begin() const {
132  return const_iterator(m_keys.begin(),m_offsets.begin(),m_data);
133  }
134 
135  const_iterator end() const {
136  return const_iterator(m_keys.end(),m_offsets.begin()+m_keys.size(),m_data);
137  }
138 
140  m_keys.swap(other.m_keys);
141  m_offsets.swap(other.m_offsets);
142  m_data.swap(other.m_data);
143  }
144 
146  MapOfVectors temp(rhs);
147  this->swap(temp);
148  return *this;
149  }
150 
151  private:
152  //for testing
153  friend class ::TestMapOfVectors;
154 
155  std::vector<K> m_keys;
156  std::vector<size_type> m_offsets;
157  std::vector<T> m_data;
158 
159  };
160 
161  // Free swap function
162  template <typename K, typename T>
163  inline
164  void
166  lhs.swap(rhs);
167  }
168 
169 }
170 
171 #endif // DatFormats_Common_MapOfVectors_h
Iter(key_iterator k, offset_iterator o, std::vector< T > const &d)
Definition: MapOfVectors.h:48
size_type offset(K const &k) const
Definition: MapOfVectors.h:116
Keys::const_iterator key_iterator
Definition: MapOfVectors.h:30
unsigned int size_type
Definition: MapOfVectors.h:24
MapOfVectors(TheMap const &it)
Definition: MapOfVectors.h:86
std::vector< T > m_data
Definition: MapOfVectors.h:157
void loadNext(K const &k, std::vector< T > const &v)
Definition: MapOfVectors.h:99
Offsets::const_iterator offset_iterator
Definition: MapOfVectors.h:31
void swap(MapOfVectors &other)
Definition: MapOfVectors.h:139
std::map< K, std::vector< T > > TheMap
Definition: MapOfVectors.h:22
key_iterator findKey(K const &k) const
Definition: MapOfVectors.h:110
bool equal(self const &other) const
Definition: MapOfVectors.h:61
std::vector< size_type > m_offsets
Definition: MapOfVectors.h:156
std::vector< K > m_keys
Definition: MapOfVectors.h:155
const_iterator begin() const
Definition: MapOfVectors.h:131
std::pair< K, range > Pair
Definition: MapOfVectors.h:36
friend class boost::iterator_core_access
Definition: MapOfVectors.h:55
size_type size() const
Definition: MapOfVectors.h:106
MapOfVectors & operator=(MapOfVectors const &rhs)
Definition: MapOfVectors.h:145
range find(K const &k) const
Definition: MapOfVectors.h:122
int k[5][pyjets_maxn]
std::vector< size_type > Offsets
Definition: MapOfVectors.h:27
range emptyRange() const
Definition: MapOfVectors.h:82
Data::const_iterator data_iterator
Definition: MapOfVectors.h:32
std::vector< T > Data
Definition: MapOfVectors.h:28
double b
Definition: hdecay.h:120
offset_iterator off
Definition: MapOfVectors.h:74
Pair const & dereference() const
Definition: MapOfVectors.h:65
bool empty() const
Definition: MapOfVectors.h:108
HLT enums.
const_iterator end() const
Definition: MapOfVectors.h:135
boost::iterator_range< data_iterator > range
Definition: MapOfVectors.h:34
std::vector< K > Keys
Definition: MapOfVectors.h:26