CMS 3D CMS Logo

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