CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AssociativeIterator.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AssociativeIterator_h
2 #define DataFormats_Common_AssociativeIterator_h
3 
43 
44 namespace edm {
45  class Event;
46  template <class T> class View;
47  template <class T> class Handle;
48  template <class T> class Association;
49  template <class T> class RefToBase;
50  template <class T> class Ptr;
51  template <class C, class T, class F> class Ref;
52 }
53 
54 namespace edm {
55 
56  // Helper classes to convert one ref type to another.
57  // Now it's able to convert anything to itself, and RefToBase to anything else
58  // This won't be needed if we used Ptr
59  namespace helper {
60  template<typename RefFrom, typename RefTo>
61  struct RefConverter {
62  static RefTo convert(const RefFrom &ref) { return RefTo(ref); }
63  };
64  template<typename T>
65  struct RefConverter<RefToBase<T>, Ptr<T> > {
66  static Ptr<T> convert(const RefToBase<T> &ref) { return Ptr<T>(ref.id(), ref.isAvailable() ? ref.get() : 0, ref.key()); }
67  };
68  template<typename T, typename C, typename V, typename F>
69  struct RefConverter<RefToBase<T>, Ref<C,V,F> > {
70  static Ref<C,V,F> convert(const RefToBase<T> &ref) { return ref.template castTo<Ref<C,V,F> >(); }
71  };
72  }
73 
75  // the implementation uses View, and works for RefType = Ref, RefToBase and Ptr
76  template<typename RefType>
78  public:
82 
83  RefType get(const ProductID &id, size_t idx) const {
84  typedef typename edm::RefToBase<element_type> BaseRefType; // could also use Ptr, but then I can't do Ptr->RefToBase
85  if (id_ != id) {
86  id_ = id;
87  iEvent_.get(id_, view_);
88  }
89  BaseRefType ref = view_->refAt(idx);
91  return conv::convert(ref);
92  }
93  private:
95  mutable ProductID id_;
97  };
98 
99  // unfortunately it's not possible to define value_type of an Association<C> correctly
100  // so we need yet another template trick
101  namespace helper {
102  template<typename AC>
104  typedef typename AC::value_type type;
105  };
106 
107  template<typename C>
110  };
111  }
112 
113 template<typename KeyRefType, typename AssociativeCollection,
114  typename ItemGetter = EdmEventItemGetter<KeyRefType> >
116  public:
117  typedef KeyRefType key_type;
120  typedef typename std::pair<key_type, val_type> value_type;
121 
123 
125  AssociativeIterator(const AssociativeCollection &map, const ItemGetter &getter) ;
126 
127  self_type & operator++() ;
128  self_type & operator--() ;
130  // self_type & skipTo(const ProductID &id, size_t offs = 0) ; // to be implemented one day
131 
132  const value_type & operator*() const { return *(this->get()); }
133  const value_type * operator->() const { return (this->get()); }
134  const value_type * get() const { chkPair(); return & pair_; }
135 
136  const key_type & key() const { chkPair(); return pair_.first; }
137  const val_type & val() const { return map_.get(idx_); }
138  const ProductID & id() const { return ioi_->first; }
139 
140  operator bool() const { return idx_ < map_.size(); }
141  self_type end() const ;
142 
143  bool operator==(const self_type &other) const { return other.idx_ == idx_; }
144  bool operator!=(const self_type &other) const { return other.idx_ != idx_; }
145  bool operator<( const self_type &other) const { return other.idx_ < idx_; }
146 
147  private:
148  typedef typename AssociativeCollection::id_offset_vector id_offset_vector;
149  typedef typename id_offset_vector::const_iterator id_offset_iterator;
150  const AssociativeCollection & map_;
152  size_t idx_;
153 
154  ItemGetter getter_;
155 
156  mutable bool pairOk_;
157  mutable value_type pair_;
158 
159  void chkPair() const ;
160 
161  };
162 
163  template<typename KeyRefType, typename AC, typename IG>
165  map_(map), ioi_(map_.ids().begin()), ioi2_(ioi_+1), idx_(0),
166  getter_(getter),
167  pairOk_(false)
168  {
169  }
170 
171  template<typename KeyRefType, typename AC, typename IG>
173  pairOk_ = false;
174  idx_++;
175  if (ioi2_ < map_.ids().end()) {
176  if (ioi2_->second == idx_) {
177  ++ioi_; ++ioi2_;
178  }
179  }
180  return *this;
181  }
182 
183  template<typename KeyRefType, typename AC, typename IG>
185  pairOk_ = false;
186  idx_--;
187  if (ioi_->second < idx_) {
188  --ioi_; --ioi2_;
189  }
190  return *this;
191 
192  }
193 
194  template<typename KeyRefType, typename AC, typename IG>
196  pairOk_ = false;
197  ioi_++; ioi2_++;
198  if (ioi_ == map_.ids().end()) {
199  idx_ = map_.size();
200  } else {
201  idx_ = ioi_->second;
202  }
203  }
204 
205  /*
206  template<typename KeyRefType, typename AC, typename IG>
207  AssociativeIterator<KeyRefType,AC,IG> & AssociativeIterator<KeyRefType,AC,IG>::skipTo(const ProductID &id, size_t offs) {
208  pairOk_ = false;
209  throw Exception(errors::UnimplementedFeature);
210  }
211  */
212 
213  template<typename KeyRefType, typename AC, typename IG>
215  self_type ret(map_, getter_);
216  ret.ioi_ = map_.ids().end();
217  ret.ioi2_ = ret.ioi_ + 1;
218  ret.idx_ = map_.size();
219  return ret;
220  }
221 
222  template<typename KeyRefType, typename AC, typename IG>
224  if (pairOk_) return;
225  pair_.first = getter_.get(id(), idx_ - ioi_->second);
226  pair_.second = map_.get(idx_);
227  pairOk_ = true;
228  }
229 
230 }
231 
232 #endif
233 
value_type const * get() const
Definition: RefToBase.h:212
AssociativeIterator(const AssociativeCollection &map, const ItemGetter &getter)
Create the associative iterator, pointing at the beginning of the collection.
KeyRefType::value_type key_val_type
AssociativeCollection::id_offset_vector id_offset_vector
static HepMC::IO_HEPEVT conv
bool isAvailable() const
Definition: RefToBase.h:112
const val_type & val() const
Handle< View< element_type > > view_
AssociativeIterator< KeyRefType, AssociativeCollection, ItemGetter > self_type
reco::GenParticleCollection::const_iterator IG
const edm::Event & iEvent_
static RefTo convert(const RefFrom &ref)
ProductID id() const
Definition: RefToBase.h:220
int iEvent
Definition: GenABIO.cc:230
EdmEventItemGetter(const edm::Event &iEvent)
size_t key() const
Definition: RefToBase.h:228
const value_type & operator*() const
Helper class that fetches some type of Ref given ProductID and index, using the edm::Event.
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:318
bool operator<(const self_type &other) const
Container::value_type value_type
RefType::value_type element_type
const value_type * operator->() const
id_offset_vector::const_iterator id_offset_iterator
std::pair< key_type, val_type > value_type
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
static Ptr< T > convert(const RefToBase< T > &ref)
const AssociativeCollection & map_
helper::AssociativeCollectionValueType< AssociativeCollection >::type val_type
bool operator!=(const self_type &other) const
#define begin
Definition: vmac.h:30
bool operator==(const self_type &other) const
static Ref< C, V, F > convert(const RefToBase< T > &ref)
volatile std::atomic< bool > shutdown_flag false
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
long double T
const ProductID & id() const
const key_type & key() const