00001 #ifndef DataFormats_Common_DetSetVector_h
00002 #define DataFormats_Common_DetSetVector_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <algorithm>
00039 #include <iterator>
00040 #include <vector>
00041
00042 #include "boost/concept_check.hpp"
00043 #include "boost/mpl/if.hpp"
00044 #include "boost/bind.hpp"
00045
00046
00047 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
00048 #include "DataFormats/Common/interface/DetSet.h"
00049 #include "DataFormats/Common/interface/FillView.h"
00050 #include "DataFormats/Common/interface/Ref.h"
00051 #include "DataFormats/Common/interface/traits.h"
00052
00053 #include "FWCore/Utilities/interface/EDMException.h"
00054
00055 #include "DataFormats/Common/interface/BoolCache.h"
00056
00057 namespace edm {
00058 class ProductID;
00059
00060
00061
00062 template <class T> class DetSetVector;
00063
00064
00065
00066
00067
00068 namespace detail {
00069
00070 inline
00071 void _throw_range(det_id_type i) {
00072 Exception::throwThis(errors::InvalidReference,
00073 "DetSetVector::operator[] called with index not in collection;\nindex value: ", i);
00074 }
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 template <class T>
00088 class DetSetVector :
00089 public boost::mpl::if_c<boost::is_base_of<edm::DoNotSortUponInsertion, T>::value,
00090 edm::DoNotSortUponInsertion,
00091 Other>::type
00092 {
00095 BOOST_CLASS_REQUIRE(T, boost, LessThanComparableConcept);
00096 public:
00097
00098 typedef DetSet<T> detset;
00099 typedef detset value_type;
00100 typedef std::vector<detset> collection_type;
00101
00102 typedef detset& reference;
00103 typedef detset const& const_reference;
00104
00105 typedef typename collection_type::iterator iterator;
00106 typedef typename collection_type::const_iterator const_iterator;
00107 typedef typename collection_type::size_type size_type;
00108
00111
00113 DetSetVector();
00114
00126 explicit DetSetVector(std::vector<DetSet<T> > & input, bool alreadySorted=false);
00127
00128
00129 void swap(DetSetVector& other);
00130
00131 DetSetVector& operator= (DetSetVector const& other);
00132
00134
00135
00136
00137
00138 void insert(detset const& s);
00139
00143 reference find_or_insert(det_id_type id);
00144
00146 bool empty() const;
00147
00149 size_type size() const;
00150
00151
00152
00153
00154
00157 iterator find(det_id_type id);
00158 const_iterator find(det_id_type id) const;
00159
00163 reference operator[](det_id_type i);
00164 const_reference operator[](det_id_type i) const;
00165
00167 iterator begin();
00168 const_iterator begin() const;
00169
00171 iterator end();
00172 const_iterator end() const;
00173
00176 void getIds(std::vector<det_id_type> & result) const;
00177
00180 void post_insert();
00181
00182 void fillView(ProductID const& id,
00183 std::vector<void const*>& pointers,
00184 helper_vector& helpers) const;
00185
00186
00187 CMS_CLASS_VERSION(10)
00188
00189 private:
00190 collection_type _sets;
00191 edm::BoolCache _alreadySorted;
00192
00194 void _sort();
00195
00196 };
00197
00198 template <class T>
00199 inline
00200 DetSetVector<T>::DetSetVector() :
00201 _sets()
00202 { }
00203
00204 template <class T>
00205 inline
00206 DetSetVector<T>::DetSetVector(std::vector<DetSet<T> > & input, bool alreadySorted) :
00207 _sets(), _alreadySorted(alreadySorted)
00208 {
00209 _sets.swap(input);
00210 if (!alreadySorted) _sort();
00211 }
00212
00213 template <class T>
00214 inline
00215 void
00216 DetSetVector<T>::swap(DetSetVector<T>& other) {
00217 _sets.swap(other._sets);
00218 bool tmp = _alreadySorted; _alreadySorted = other._alreadySorted; other._alreadySorted = tmp;
00219 }
00220
00221 template <class T>
00222 inline
00223 DetSetVector<T>&
00224 DetSetVector<T>::operator= (DetSetVector<T> const& other)
00225 {
00226 DetSetVector<T> temp(other);
00227 swap(temp);
00228 return *this;
00229 }
00230
00231 template <class T>
00232 inline
00233 void
00234 DetSetVector<T>::insert(detset const& t) {
00235 _alreadySorted = false;
00236
00237 _sets.insert(std::lower_bound(_sets.begin(),
00238 _sets.end(),
00239 t),
00240 t);
00241 #if 0
00242
00243
00244 _sets.push_back(t);
00245
00246 _sort();
00247 #endif
00248 }
00249
00250 template <class T>
00251 inline
00252 typename DetSetVector<T>::reference
00253 DetSetVector<T>::find_or_insert(det_id_type id) {
00254
00255
00256 std::pair<iterator,iterator> p =
00257 std::equal_range(_sets.begin(), _sets.end(), id);
00258
00259
00260
00261 if (p.first != p.second) return *p.first;
00262
00263
00264
00265 return *(_sets.insert(p.first, detset(id)));
00266 }
00267
00268 template <class T>
00269 inline
00270 bool
00271 DetSetVector<T>::empty() const {
00272 return _sets.empty();
00273 }
00274
00275 template <class T>
00276 inline
00277 typename DetSetVector<T>::size_type
00278 DetSetVector<T>::size() const {
00279 return _sets.size();
00280 }
00281
00282 template <class T>
00283 inline
00284 typename DetSetVector<T>::iterator
00285 DetSetVector<T>::find(det_id_type id) {
00286 _alreadySorted = false;
00287 std::pair<iterator,iterator> p =
00288 std::equal_range(_sets.begin(), _sets.end(), id);
00289 if (p.first == p.second) return _sets.end();
00290
00291
00292
00293
00294
00295 #if 0
00296 assert(std::distance(p.first, p.second) == 1);
00297 #endif
00298
00299 return p.first;
00300 }
00301
00302 template <class T>
00303 inline
00304 typename DetSetVector<T>::const_iterator
00305 DetSetVector<T>::find(det_id_type id) const {
00306 std::pair<const_iterator,const_iterator> p =
00307 std::equal_range(_sets.begin(), _sets.end(), id);
00308 if (p.first == p.second) return _sets.end();
00309
00310
00311 assert(std::distance(p.first, p.second) == 1);
00312 return p.first;
00313 }
00314
00315 template <class T>
00316 inline
00317 typename DetSetVector<T>::reference
00318 DetSetVector<T>::operator[](det_id_type i) {
00319 _alreadySorted = false;
00320
00321
00322 iterator it = this->find(i);
00323 if (it == this->end()) detail::_throw_range(i);
00324 return *it;
00325 }
00326
00327 template <class T>
00328 inline
00329 typename DetSetVector<T>::const_reference
00330 DetSetVector<T>::operator[](det_id_type i) const {
00331
00332
00333 const_iterator it = this->find(i);
00334 if (it == this->end()) detail::_throw_range(i);
00335 return *it;
00336 }
00337
00338 template <class T>
00339 inline
00340 typename DetSetVector<T>::iterator
00341 DetSetVector<T>::begin() {
00342 _alreadySorted = false;
00343 return _sets.begin();
00344 }
00345
00346 template <class T>
00347 inline
00348 typename DetSetVector<T>::const_iterator
00349 DetSetVector<T>::begin() const {
00350 return _sets.begin();
00351 }
00352
00353 template <class T>
00354 inline
00355 typename DetSetVector<T>::iterator
00356 DetSetVector<T>::end() {
00357 _alreadySorted = false;
00358 return _sets.end();
00359 }
00360
00361 template <class T>
00362 inline
00363 typename DetSetVector<T>::const_iterator
00364 DetSetVector<T>::end() const {
00365 return _sets.end();
00366 }
00367
00368
00369 template <class T>
00370 inline
00371 void
00372 DetSetVector<T>::getIds(std::vector<det_id_type> & result) const
00373 {
00374 std::transform(this->begin(), this->end(),
00375 std::back_inserter(result),
00376 boost::bind(&DetSet<T>::id,_1));
00377 }
00378
00379 template <class T>
00380 inline
00381 void
00382 DetSetVector<T>::post_insert() {
00383 if (_alreadySorted) return;
00384 typename collection_type::iterator i = _sets.begin();
00385 typename collection_type::iterator e = _sets.end();
00386
00387 for (; i != e; ++i) {
00388
00389 std::sort(i->data.begin(), i->data.end());
00390 }
00391 }
00392
00393 template <class T>
00394 inline
00395 void
00396 DetSetVector<T>::_sort() {
00397 std::sort(_sets.begin(), _sets.end());
00398 }
00399
00400 template<class T>
00401 void DetSetVector<T>::fillView(ProductID const& id,
00402 std::vector<void const*>& pointers,
00403 helper_vector& helpers) const
00404 {
00405 detail::reallyFillView(*this, id, pointers, helpers);
00406 }
00407
00408
00409
00410
00411
00412 template <class T>
00413 inline
00414 void
00415 fillView(DetSetVector<T> const& obj,
00416 ProductID const& id,
00417 std::vector<void const*>& pointers,
00418 helper_vector& helpers)
00419 {
00420 obj.fillView(id, pointers, helpers);
00421 }
00422
00423 template <class T>
00424 struct has_fillView<edm::DetSetVector<T> >
00425 {
00426 static bool const value = true;
00427 };
00428
00429
00430
00431 template <class T>
00432 inline
00433 void
00434 swap(DetSetVector<T>& a, DetSetVector<T>& b)
00435 {
00436 a.swap(b);
00437 }
00438
00439 }
00440
00441
00442
00443 namespace edm {
00444
00445 namespace refhelper {
00446 template<typename T>
00447 class FindForDetSetVector : public std::binary_function<const DetSetVector<T>&, std::pair<det_id_type, typename DetSet<T>::collection_type::size_type>, const T*> {
00448 public:
00449 typedef FindForDetSetVector<T> self;
00450 typename self::result_type operator()(typename self::first_argument_type iContainer, typename self::second_argument_type iIndex) {
00451 return &(*(iContainer.find(iIndex.first)->data.begin()+iIndex.second));
00452 }
00453 };
00454
00455 template<typename T>
00456 struct FindTrait<DetSetVector<T>,T> {
00457 typedef FindForDetSetVector<T> value;
00458 };
00459 }
00460
00461
00462
00463 template<class HandleT>
00464 Ref<typename HandleT::element_type, typename HandleT::element_type::value_type::value_type>
00465 makeRefTo(const HandleT& iHandle,
00466 det_id_type iDetID,
00467 typename HandleT::element_type::value_type::const_iterator itIter) {
00468 typedef typename HandleT::element_type Vec;
00469 typename Vec::value_type::collection_type::size_type index=0;
00470 typename Vec::const_iterator itFound = iHandle->find(iDetID);
00471 if(itFound == iHandle->end()) {
00472 Exception::throwThis(errors::InvalidReference,
00473 "an edm::Ref to an edm::DetSetVector was given a DetId, ", iDetID, ", that is not in the DetSetVector");
00474 }
00475 index += (itIter- itFound->data.begin());
00476 if(index >= itFound->data.size()) {
00477 Exception::throwThis(errors::InvalidReference,
00478 "an edm::Ref to a edm::DetSetVector is being made with an interator that is not part of the edm::DetSet itself");
00479 }
00480 return Ref<typename HandleT::element_type,
00481 typename HandleT::element_type::value_type::value_type>
00482 (iHandle,std::make_pair(iDetID,index));
00483 }
00484
00485 template<class HandleT>
00486 Ref<typename HandleT::element_type, typename HandleT::element_type::value_type::value_type>
00487 makeRefToDetSetVector(const HandleT& iHandle,
00488 det_id_type iDetID,
00489 typename HandleT::element_type::value_type::iterator itIter) {
00490 typedef typename HandleT::element_type Vec;
00491 typename Vec::detset::const_iterator itIter2 = itIter;
00492 return makeRefTo(iHandle,iDetID,itIter2);
00493 }
00494 }
00495 #endif