Go to the documentation of this file.00001 #ifndef DataFormats_Common_ValueMap_h
00002 #define DataFormats_Common_ValueMap_h
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "DataFormats/Provenance/interface/ProductID.h"
00012 #include "FWCore/Utilities/interface/EDMException.h"
00013 #include <vector>
00014 #include <map>
00015 #include <iterator>
00016 #include <algorithm>
00017
00018 namespace edm {
00019 namespace helper {
00020 template<typename Map>
00021 class Filler {
00022 private:
00023 typedef std::vector<size_t> index_vector;
00024 typedef std::vector<typename Map::value_type> value_vector;
00025 typedef std::map<ProductID, value_vector> value_map;
00026 typedef typename Map::offset offset;
00027 typedef typename Map::id_offset_vector id_offset_vector;
00028 public:
00029 explicit Filler(Map & map) :
00030 map_(map) {
00031 add(map);
00032 }
00033 void add(const Map & map) {
00034 if (map.empty()) return;
00035 typename id_offset_vector::const_iterator j = map.ids_.begin();
00036 const typename id_offset_vector::const_iterator end = map.ids_.end();
00037 size_t i = 0;
00038 const size_t size = map.values_.size();
00039 std::pair<ProductID, offset> id = *j;
00040 do {
00041 ProductID id = j->first;
00042 ++j;
00043 size_t max = (j == end ? size : j->second);
00044 typename value_map::iterator f = values_.find(id);
00045 if(f!=values_.end()) throwAdd();
00046 value_vector & values = values_.insert(std::make_pair(id, value_vector())).first->second;
00047 while(i!=max)
00048 values.push_back( map.values_[i++] );
00049 } while(j != end);
00050 }
00051 template<typename H, typename I>
00052 void insert(const H & h, I begin, I end) {
00053 ProductID id = h.id();
00054 size_t size = h->size(), sizeIt = end - begin;
00055 if(sizeIt!=size) throwFillSize();
00056 typename value_map::const_iterator f = values_.find(id);
00057 if(f != values_.end()) throwFillID(id);
00058 value_vector & values = values_.insert(make_pair(id, value_vector(size))).first->second;
00059 std::copy(begin, end, values.begin());
00060 }
00061 void fill() {
00062 map_.clear();
00063 offset off = 0;
00064 for(typename value_map::const_iterator i = values_.begin(); i != values_.end(); ++i) {
00065 ProductID id = i->first;
00066 map_.ids_.push_back(std::make_pair(id, off));
00067 const value_vector & values = i->second;
00068 for(typename value_vector::const_iterator j = values.begin(); j != values.end(); ++j) {
00069 map_.values_.push_back( *j );
00070 ++off;
00071 }
00072 }
00073 }
00074
00075 protected:
00076 Map & map_;
00077
00078 private:
00079 value_map values_;
00080 void throwFillSize() const {
00081 Exception::throwThis(errors::InvalidReference,
00082 "ValueMap::Filler: handle and reference "
00083 "collections should the same size\n");
00084 }
00085 void throwFillID(ProductID id) const {
00086 Exception e(errors::InvalidReference);
00087 e << "index map has already been filled for id: " << id << "\n";
00088 e.raise();
00089 }
00090 void throwAdd() const {
00091 Exception::throwThis(errors::InvalidReference,
00092 "ValueMap: trying to add entries for an already existing product\n");
00093 }
00094 };
00095 }
00096
00097 template<typename T>
00098 class ValueMap {
00099 public:
00100 typedef T value_type;
00101 typedef std::vector<value_type> container;
00102 typedef unsigned int offset;
00103 typedef std::vector<std::pair<ProductID, offset> > id_offset_vector;
00104 typedef typename container::reference reference_type;
00105 typedef typename container::const_reference const_reference_type;
00106
00107 ValueMap() { }
00108
00109 void swap(ValueMap& other) {
00110 values_.swap(other.values_);
00111 ids_.swap(other.ids_);
00112 }
00113
00114 ValueMap& operator=(ValueMap const& rhs) {
00115 ValueMap temp(rhs);
00116 this->swap(temp);
00117 return *this;
00118 }
00119
00120 template<typename RefKey>
00121 const_reference_type operator[](const RefKey & r) const {
00122 return get(r.id(), r.key());
00123 }
00124
00125 size_t rawIndexOf(ProductID id, size_t idx) const {
00126 typename id_offset_vector::const_iterator f = getIdOffset(id);
00127 if(f==ids_.end()) throwNotExisting();
00128 offset off = f->second;
00129 size_t j = off+idx;
00130 if(j >= values_.size()) throwIndexBound();
00131 return j;
00132 }
00133 const_reference_type get(ProductID id, size_t idx) const {
00134 return values_[rawIndexOf(id,idx)];
00135 }
00136 template<typename RefKey>
00137 reference_type operator[](const RefKey & r) {
00138 return get(r.id(), r.key());
00139 }
00140 reference_type get(ProductID id, size_t idx) {
00141 return values_[rawIndexOf(id,idx)];
00142 }
00143
00144 ValueMap<T> & operator+=(const ValueMap<T> & o) {
00145 add(o);
00146 return *this;
00147 }
00148 bool contains(ProductID id) const {
00149 return getIdOffset(id) != ids_.end();
00150 }
00151 size_t size() const { return values_.size(); }
00152 size_t idSize() const { return ids_.size(); }
00153 bool empty() const { return values_.empty(); }
00154 void clear() { values_.clear(); ids_.clear(); }
00155
00156 typedef helper::Filler<ValueMap<T> > Filler;
00157
00158 struct const_iterator {
00159 typedef ptrdiff_t difference_type;
00160 const_iterator():values_(0) {}
00161 ProductID id() const { return i_->first; }
00162 typename container::const_iterator begin() const {
00163 return values_->begin() + i_->second;
00164 }
00165 typename container::const_iterator end() const {
00166 if(i_ == end_) return values_->end();
00167 id_offset_vector::const_iterator end = i_; ++end;
00168 if(end == end_) return values_->end();
00169 return values_->begin() + end->second;
00170 }
00171 size_t size() const { return end() - begin(); }
00172 const T & operator[](size_t i) { return *(begin()+i); }
00173 const_iterator& operator++() { ++i_; return *this; }
00174 const_iterator operator++(int) { const_iterator ci = *this; ++i_; return ci; }
00175 const_iterator& operator--() { --i_; return *this; }
00176 const_iterator operator--(int) { const_iterator ci = *this; --i_; return ci; }
00177 difference_type operator-(const const_iterator & o) const { return i_ - o.i_; }
00178 const_iterator operator+(difference_type n) const { return const_iterator(i_ + n, end_, values_); }
00179 const_iterator operator-(difference_type n) const { return const_iterator(i_ - n, end_, values_); }
00180 bool operator<(const const_iterator & o) const { return i_ < o.i_; }
00181 bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
00182 bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
00183 const_iterator & operator +=(difference_type d) { i_ += d; return *this; }
00184 const_iterator & operator -=(difference_type d) { i_ -= d; return *this; }
00185 private:
00186 const_iterator(const id_offset_vector::const_iterator & i_,
00187 const id_offset_vector::const_iterator & end,
00188 const container * values) :
00189 values_(values), i_(i_), end_(end) { }
00190 const container * values_;
00191 id_offset_vector::const_iterator i_, end_;
00192 friend class ValueMap<T>;
00193 };
00194
00195 const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
00196 const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
00197
00199 const id_offset_vector & ids() const { return ids_; }
00201 const_reference_type get(size_t idx) const { return values_[idx]; }
00202 protected:
00203 container values_;
00204 id_offset_vector ids_;
00205
00206 typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
00207 typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
00208 if(i==ids_.end()) return i;
00209 return i->first == id ? i : ids_.end();
00210 }
00211
00212 void throwIndexBound() const {
00213 Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
00214 }
00215
00216 private:
00217 struct IDComparator {
00218 bool operator()(const std::pair<ProductID, offset> & p, const ProductID & id) {
00219 return p.first < id;
00220 }
00221 };
00222 void throwNotExisting() const {
00223 Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
00224 }
00225
00226 void add( const ValueMap<T> & o ) {
00227 Filler filler(*this);
00228 filler.add(o);
00229 filler.fill();
00230 }
00231
00232 friend class helper::Filler<ValueMap<T> >;
00233 };
00234
00235 template<typename T>
00236 inline ValueMap<T> operator+( const ValueMap<T> & a1,
00237 const ValueMap<T> & a2 ) {
00238 ValueMap<T> a = a1;
00239 a += a2;
00240 return a;
00241 }
00242
00243
00244 template <typename T>
00245 inline
00246 void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
00247 lhs.swap(rhs);
00248 }
00249
00250 }
00251 #endif