CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RefVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefVector_h
2 #define DataFormats_Common_RefVector_h
3 
4 /*----------------------------------------------------------------------
5 
6 RefVector: A template for a vector of interproduct references.
7  Each vector element is a reference to a member of the same product.
8 
9 ----------------------------------------------------------------------*/
10 
23 
24 #include <algorithm>
25 #include <stdexcept>
26 #include <vector>
27 
28 namespace edm {
29 
30  template<typename C,
31  typename T = typename refhelper::ValueTrait<C>::value,
32  typename F = typename refhelper::FindTrait<C, T>::value>
33  class RefVector {
34  public:
35  typedef C collection_type;
36  typedef T member_type;
37  typedef F finder_type;
41  typedef value_type const const_reference; // better this than the default 'const value_type &'
42  typedef const_reference reference; // as operator[] returns 'const R' and not 'R &'
43 
44  // key_type is the type of the key into the collection
45  typedef typename value_type::key_type key_type;
46  typedef std::vector<key_type> KeyVec;
47 
48  // size_type is the type of the index into the RefVector
49  typedef typename KeyVec::size_type size_type;
51 
56 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
58 #endif
59 
60  RefVector(ProductID const& iId) : refVector_(iId) {}
62  void push_back(value_type const& ref) {
63  refVector_.pushBack(ref.refCore(), ref.key());
64  }
65 
68  RefCore const& core = refVector_.refCore();
69  key_type const& key = refVector_.keys()[idx];
70  void const* memberPointer = refVector_.cachedMemberPointer(idx);
71  if(memberPointer) {
72  RefCore newCore(core);
73  newCore.setProductPtr(memberPointer);
74  return value_type(newCore, key);
75  }
76  return value_type(core, key);
77  }
78 
80  value_type const at(size_type idx) const {
81  RefCore const& core = refVector_.refCore();
82  key_type const& key = refVector_.keys().at(idx);
83  void const* memberPointer = refVector_.cachedMemberPointer(idx);
84  if(memberPointer) {
85  RefCore newCore(core);
86  newCore.setProductPtr(memberPointer);
87  return value_type(newCore, key);
88  }
89  return value_type(core, key);
90  }
91 
93  contents_type const& refVector() const {return refVector_;}
94 
96  bool empty() const {return refVector_.empty();}
97 
99  size_type size() const {return refVector_.size();}
100 
102  size_type capacity() const {return refVector_.capacity();}
103 
106 
108  const_iterator begin() const;
109 
111  const_iterator end() const;
112 
114  ProductID id() const {return refVector_.refCore().id();}
115 
118 
120  bool isNull() const {return !id().isValid();}
121 
123  bool isNonnull() const {return !isNull();}
124 
126  bool operator!() const {return isNull();}
127 
130  bool isAvailable() const;
131 
133  bool isTransient() const {return refVector_.refCore().isTransient();}
134 
136  iterator erase(iterator const& pos);
137 
139  void clear() {refVector_.clear();}
140 
142  void swap(RefVector<C, T, F> & other) noexcept;
143 
145  RefVector& operator=(RefVector const& rhs);
146 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
148  refVector_ = std::move(rhs.refVector_);
149  return *this;
150  }
151 #endif
152  bool hasProductCache() const {return refVector_.refCore().productPtr() != 0;}
154 
155  void fillView(ProductID const& id,
156  std::vector<void const*>& pointers,
157  FillViewHelperVector& helpers) const;
158 
159  //Needed for ROOT storage
161 
162  private:
163 
165  };
166 
167  template<typename C, typename T, typename F>
168  inline
169  void
170  RefVector<C, T, F>::swap(RefVector<C, T, F> & other) noexcept {
171  refVector_.swap(other.refVector_);
172  }
173 
174  template<typename C, typename T, typename F>
175  inline
179  this->swap(temp);
180  return *this;
181  }
182 
183  template<typename C, typename T, typename F>
184  inline
185  void
187  a.swap(b);
188  }
189 
190 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
191  template<typename C, typename T, typename F>
192  void
193  RefVector<C,T,F>::fillView(ProductID const&,
194  std::vector<void const*>& pointers,
195  FillViewHelperVector& helpers) const {
196 
197  pointers.reserve(this->size());
198  helpers.reserve(this->size());
199 
200  size_type key = 0;
201  for(const_iterator i=begin(), e=end(); i!=e; ++i, ++key) {
202  member_type const* address = i->isNull() ? 0 : &**i;
203  pointers.push_back(address);
204  helpers.emplace_back(i->id(),i->key());
205  }
206  }
207 #endif
208 
209  template<typename C, typename T, typename F>
210  inline
211  void
213  ProductID const& id,
214  std::vector<void const*>& pointers,
215  FillViewHelperVector& helpers) {
216  obj.fillView(id, pointers, helpers);
217  }
218 
219  template<typename C, typename T, typename F>
220  struct has_fillView<RefVector<C,T,F> > {
221  static bool const value = true;
222  };
223 
224  template<typename C, typename T, typename F>
225  inline
226  bool
228  return lhs.refVector() == rhs.refVector();
229  }
230 
231  template<typename C, typename T, typename F>
232  inline
233  bool
235  return !(lhs == rhs);
236  }
237 
238  template<typename C, typename T, typename F>
239  inline
240  typename RefVector<C, T, F>::iterator
243  typename contents_type::keys_type::iterator newPos =
244  refVector_.eraseAtIndex(index);
245  typename contents_type::keys_type::size_type newIndex = newPos - refVector_.keys().begin();
246  return iterator(this, newIndex);
247  }
248 
249  template<typename C, typename T, typename F>
251  return iterator(this, 0);
252  }
253 
254  template<typename C, typename T, typename F>
256  return iterator(this, size());
257  }
258 
259  template<typename C, typename T, typename F>
260  bool
262  if(refVector_.refCore().isAvailable()) {
263  return true;
264  } else if(empty()) {
265  return false;
266  }
267 
268  // The following is the simplest implementation, but
269  // this is woefully inefficient and could be optimized
270  // to run much faster with some nontrivial effort. There
271  // is only 1 place in the code base where this function
272  // is used at all and I'm not sure whether it will ever
273  // be used with thinned collections, so for the moment I
274  // am not spending the time to optimize this.
275  for(size_type i = 0; i < size(); ++i) {
276  if(!(*this)[i].isAvailable()) {
277  return false;
278  }
279  }
280  return true;
281  }
282 
283  template<typename C, typename T, typename F>
284  std::ostream&
285  operator<<(std::ostream& os, RefVector<C,T,F> const& r) {
287  i = r.begin(),
288  e = r.end();
289  i != e;
290  ++i) {
291  os << *i << '\n';
292  }
293  return os;
294  }
295 }
296 
298 namespace edm {
299  namespace detail {
300 
301  template<typename C, typename T, typename F>
302  struct GetProduct<RefVector<C, T, F> > {
303  typedef T element_type;
305  static element_type const* address(iter const& i) {
306  return &**i;
307  }
308  };
309  }
310 }
311 #endif
void const * cachedMemberPointer(size_type idx) const
Definition: RefVectorBase.h:55
int i
Definition: DBlmapReader.cc:9
bool isNonnull() const
Checks for non-null.
Definition: RefVector.h:123
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:47
value_type const const_reference
Definition: RefVector.h:41
value_type::key_type key_type
Definition: RefVector.h:45
size_type capacity() const
Capacity of the RefVector.
Definition: RefVector.h:102
bool empty() const
Is vector empty?
Definition: RefVectorBase.h:63
#define noexcept
RefCore const & refCore() const
Definition: Ref.h:279
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
const_reference reference
Definition: RefVector.h:42
key_type key() const
Accessor for product key.
Definition: Ref.h:264
contents_type refVector_
Definition: RefVector.h:164
keys_type::iterator eraseAtIndex(size_type index)
erase an element from the vector
Definition: RefVectorBase.h:92
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:255
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:96
uint16_t size_type
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:250
void pushBack(RefCore const &product, KEY const &key)
Definition: RefVectorBase.h:68
KeyVec::size_type size_type
Definition: RefVector.h:49
bool isNull() const
Checks for null.
Definition: RefVector.h:120
void swap(RefVectorBase< KEY > &other)
swap two vectors
void fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
size_type capacity() const
Capacity of vector.
Definition: RefVectorBase.h:86
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:114
RefVector(ProductID const &iId)
Definition: RefVector.h:60
std::vector< key_type > KeyVec
Definition: RefVector.h:46
RefCore const & refCore() const
Accessor for product ID and product getter.
Definition: RefVectorBase.h:53
def move
Definition: eostools.py:510
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool isTransient() const
Checks if product collection is tansient (i.e. non persistable)
Definition: RefVector.h:133
void const * productPtr() const
Definition: RefCore.h:41
bool isAvailable() const
Definition: RefCore.cc:168
refhelper::RefVectorTrait< C, T, F >::iterator_type iterator
Definition: RefVector.h:38
void clear()
clear the vector
Definition: RefVectorBase.h:98
void swap(RefVector< C, T, F > &other)
Swap two vectors.
Definition: RefVector.h:170
keys_type const & keys() const
Accessor for vector of keys and pointers.
Definition: RefVectorBase.h:60
RefVector(RefVector const &rh)
Definition: RefVector.h:55
void reserve(size_type n)
Reserve space for RefVector.
Definition: RefVector.h:105
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefVector.h:117
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
iterator const_iterator
Definition: RefVector.h:39
RefVectorBase< key_type > contents_type
Definition: RefVector.h:50
void clear()
Clear the vector.
Definition: RefVector.h:139
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
bool hasProductCache() const
Checks if product is in memory.
Definition: RefVector.h:153
RefVector< C, T, F >::const_iterator iter
Definition: RefVector.h:304
double b
Definition: hdecay.h:120
RefVector & operator=(RefVector const &rhs)
Copy assignment.
Definition: RefVector.h:177
EDProductGetter const * productGetter() const
Definition: RefCore.h:73
ProductID id() const
Definition: RefCore.h:38
value_type const operator[](size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:67
bool isTransient() const
Definition: RefCore.h:95
value_type const at(size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:80
bool isAvailable() const
Definition: RefVector.h:261
double a
Definition: hdecay.h:121
static element_type const * address(iter const &i)
Definition: RefVector.h:305
void push_back(value_type const &ref)
Add a Ref&lt;C, T&gt; to the RefVector.
Definition: RefVector.h:62
keys_type::size_type size_type
Definition: RefVectorBase.h:35
size_type size() const
Size of the RefVector.
Definition: RefVector.h:99
refhelper::RefVectorTrait< C, T, F >::ref_type value_type
Definition: RefVector.h:40
bool isValid() const
Definition: ProductID.h:35
contents_type const & refVector() const
Accessor for all data.
Definition: RefVector.h:93
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
size_type size() const
Size of vector.
Definition: RefVectorBase.h:66
long double T
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
bool operator!() const
Checks for null.
Definition: RefVector.h:126
edm::RefVector< Container > RefVector
def template
Definition: svgfig.py:520
void reserve(size_type n)
Reserve space for vector.
Definition: RefVectorBase.h:89
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:168
iterator erase(iterator const &pos)
Erase an element from the vector.
Definition: RefVector.h:241