CMS 3D CMS Logo

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 
22 
23 #include <algorithm>
24 #include <stdexcept>
25 #include <vector>
26 
27 namespace edm {
28 
29  template <typename C,
30  typename T = typename refhelper::ValueTrait<C>::value,
31  typename F = typename refhelper::FindTrait<C, T>::value>
32  class RefVector {
33  public:
34  typedef C collection_type;
35  typedef T member_type;
36  typedef F finder_type;
40  typedef value_type const const_reference; // better this than the default 'const value_type &'
41  typedef const_reference reference; // as operator[] returns 'const R' and not 'R &'
42 
43  // key_type is the type of the key into the collection
44  typedef typename value_type::key_type key_type;
45  typedef std::vector<key_type> KeyVec;
46 
47  // size_type is the type of the index into the RefVector
48  typedef typename KeyVec::size_type size_type;
50 
54  ~RefVector() = default;
55 
57  RefVector(RefVector&& rh) noexcept : refVector_(std::move(rh.refVector_)) {}
58 
59  RefVector& operator=(RefVector const& rhs);
60  RefVector& operator=(RefVector&& rhs) noexcept {
61  refVector_ = std::move(rhs.refVector_);
62  return *this;
63  }
64 
65  RefVector(ProductID const& iId) : refVector_(iId) {}
67  void push_back(value_type const& ref) { refVector_.pushBack(ref.refCore(), ref.key()); }
68 
71  RefCore const& core = refVector_.refCore();
72  key_type const& key = refVector_.keys()[idx];
73  void const* memberPointer = refVector_.cachedMemberPointer(idx);
74  if (memberPointer) {
75  RefCore newCore(core); // NOLINT
76  newCore.setProductPtr(memberPointer);
77  return value_type(newCore, key);
78  }
79  return value_type(core, key);
80  }
81 
83  value_type const at(size_type idx) const {
84  RefCore const& core = refVector_.refCore();
85  key_type const& key = refVector_.keys().at(idx);
86  void const* memberPointer = refVector_.cachedMemberPointer(idx);
87  if (memberPointer) {
88  RefCore newCore(core); // NOLINT
89  newCore.setProductPtr(memberPointer);
90  return value_type(newCore, key);
91  }
92  return value_type(core, key);
93  }
94 
96  contents_type const& refVector() const { return refVector_; }
97 
99  bool empty() const { return refVector_.empty(); }
100 
102  size_type size() const { return refVector_.size(); }
103 
105  size_type capacity() const { return refVector_.capacity(); }
106 
109 
111  const_iterator begin() const;
112 
114  const_iterator end() const;
115 
117  ProductID id() const { return refVector_.refCore().id(); }
118 
121 
123  bool isNull() const { return !id().isValid(); }
124 
126  bool isNonnull() const { return !isNull(); }
127 
129  bool operator!() const { return isNull(); }
130 
133  bool isAvailable() const;
134 
136  bool isTransient() const { return refVector_.refCore().isTransient(); }
137 
139  iterator erase(iterator const& pos);
140 
142  void clear() { refVector_.clear(); }
143 
145  void swap(RefVector<C, T, F>& other) noexcept;
146 
148  bool hasProductCache() const { return refVector_.refCore().productPtr() != 0; }
149 
150  void fillView(ProductID const& id, std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;
151 
152  //Needed for ROOT storage
154 
155  private:
157  };
158 
159  template <typename C, typename T, typename F>
160  inline void RefVector<C, T, F>::swap(RefVector<C, T, F>& other) noexcept {
161  refVector_.swap(other.refVector_);
162  }
163 
164  template <typename C, typename T, typename F>
167  this->swap(temp);
168  return *this;
169  }
170 
171  template <typename C, typename T, typename F>
172  inline void swap(RefVector<C, T, F>& a, RefVector<C, T, F>& b) noexcept {
173  a.swap(b);
174  }
175 
176  template <typename C, typename T, typename F>
178  std::vector<void const*>& pointers,
179  FillViewHelperVector& helpers) const {
180  pointers.reserve(this->size());
181  helpers.reserve(this->size());
182 
183  size_type key = 0;
184  for (const_iterator i = begin(), e = end(); i != e; ++i, ++key) {
185  member_type const* address = i->isNull() ? nullptr : &**i;
186  pointers.push_back(address);
187  helpers.emplace_back(i->id(), i->key());
188  }
189  }
190 
191  template <typename C, typename T, typename F>
192  inline void fillView(RefVector<C, T, F> const& obj,
193  ProductID const& id,
194  std::vector<void const*>& pointers,
196  obj.fillView(id, pointers, helpers);
197  }
198 
199  template <typename C, typename T, typename F>
200  struct has_fillView<RefVector<C, T, F> > {
201  static bool const value = true;
202  };
203 
204  template <typename C, typename T, typename F>
205  inline bool operator==(RefVector<C, T, F> const& lhs, RefVector<C, T, F> const& rhs) {
206  return lhs.refVector() == rhs.refVector();
207  }
208 
209  template <typename C, typename T, typename F>
210  inline bool operator!=(RefVector<C, T, F> const& lhs, RefVector<C, T, F> const& rhs) {
211  return !(lhs == rhs);
212  }
213 
214  template <typename C, typename T, typename F>
217  typename contents_type::keys_type::iterator newPos = refVector_.eraseAtIndex(index);
218  typename contents_type::keys_type::size_type newIndex = newPos - refVector_.keys().begin();
219  return iterator(this, newIndex);
220  }
221 
222  template <typename C, typename T, typename F>
224  return iterator(this, 0);
225  }
226 
227  template <typename C, typename T, typename F>
229  return iterator(this, size());
230  }
231 
232  template <typename C, typename T, typename F>
234  if (refVector_.refCore().isAvailable()) {
235  return true;
236  } else if (empty()) {
237  return false;
238  }
239 
240  // The following is the simplest implementation, but
241  // this is woefully inefficient and could be optimized
242  // to run much faster with some nontrivial effort. There
243  // is only 1 place in the code base where this function
244  // is used at all and I'm not sure whether it will ever
245  // be used with thinned collections, so for the moment I
246  // am not spending the time to optimize this.
247  for (size_type i = 0; i < size(); ++i) {
248  if (!(*this)[i].isAvailable()) {
249  return false;
250  }
251  }
252  return true;
253  }
254 
255  template <typename C, typename T, typename F>
256  std::ostream& operator<<(std::ostream& os, RefVector<C, T, F> const& r) {
257  for (typename RefVector<C, T, F>::const_iterator i = r.begin(), e = r.end(); i != e; ++i) {
258  os << *i << '\n';
259  }
260  return os;
261  }
262 } // namespace edm
263 
265 namespace edm {
266  namespace detail {
267 
268  template <typename C, typename T, typename F>
269  struct GetProduct<RefVector<C, T, F> > {
270  typedef T element_type;
272  static element_type const* address(iter const& i) { return &**i; }
273  };
274  } // namespace detail
275 } // namespace edm
276 #endif
constexpr bool operator==(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
RefCore const & refCore() const
Definition: Ref.h:267
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
bool isValid() const
Definition: ProductID.h:32
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:164
bool isTransient() const
Checks if product collection is tansient (i.e. non persistable)
Definition: RefVector.h:136
~RefVector()=default
value_type const const_reference
Definition: RefVector.h:40
value_type::key_type key_type
Definition: RefVector.h:44
bool hasProductCache() const
Checks if product is in memory.
Definition: RefVector.h:148
Definition: __init__.py:1
ProductID id() const
Definition: RefCore.h:48
void swap(RefVectorBase< KEY > &other) noexcept
swap two vectors
bool isAvailable() const
Definition: RefCore.cc:146
RefVector(RefVector &&rh) noexcept
Definition: RefVector.h:57
const_reference reference
Definition: RefVector.h:41
void clear()
clear the vector
value_type const at(size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:83
contents_type refVector_
Definition: RefVector.h:156
#define CMS_CLASS_VERSION(_version_)
bool empty() const
Is vector empty?
Definition: RefVectorBase.h:76
void const * productPtr() const
Definition: RefCore.h:51
uint16_t size_type
key_type key() const
Accessor for product key.
Definition: Ref.h:250
KeyVec::size_type size_type
Definition: RefVector.h:48
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefVector.h:120
value_type const operator[](size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:70
bool isNull() const
Checks for null.
Definition: RefVector.h:123
bool operator!() const
Checks for null.
Definition: RefVector.h:129
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:117
void swap(RefVector< C, T, F > &other) noexcept
Swap two vectors.
Definition: RefVector.h:160
contents_type const & refVector() const
Accessor for all data.
Definition: RefVector.h:96
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:57
RefVector(ProductID const &iId)
Definition: RefVector.h:65
std::vector< key_type > KeyVec
Definition: RefVector.h:45
bool isTransient() const
Definition: RefCore.h:105
refhelper::RefVectorTrait< C, T, F >::iterator_type iterator
Definition: RefVector.h:37
keys_type::size_type size_type
Definition: RefVectorBase.h:35
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
RefVector(RefVector const &rh)
Definition: RefVector.h:56
Definition: value.py:1
constexpr bool operator!=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
void reserve(size_type n)
Reserve space for RefVector.
Definition: RefVector.h:108
iterator const_iterator
Definition: RefVector.h:38
size_type capacity() const
Capacity of vector.
Definition: RefVectorBase.h:99
RefVectorBase< key_type > contents_type
Definition: RefVector.h:49
RefVector & operator=(RefVector &&rhs) noexcept
Definition: RefVector.h:60
void pushBack(RefCore const &product, KEY const &key)
Definition: RefVectorBase.h:81
size_type size() const
Size of the RefVector.
Definition: RefVector.h:102
void clear()
Clear the vector.
Definition: RefVector.h:142
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
RefVector< C, T, F >::const_iterator iter
Definition: RefVector.h:271
double b
Definition: hdecay.h:118
void const * cachedMemberPointer(size_type idx) const
Definition: RefVectorBase.h:68
size_type capacity() const
Capacity of the RefVector.
Definition: RefVector.h:105
HLT enums.
double a
Definition: hdecay.h:119
static element_type const * address(iter const &i)
Definition: RefVector.h:272
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:67
FindUsingAdvance< C, T > value
Definition: RefTraits.h:42
keys_type::iterator eraseAtIndex(size_type index)
erase an element from the vector
void reserve(size_type n)
Reserve space for vector.
refhelper::RefVectorTrait< C, T, F >::ref_type value_type
Definition: RefVector.h:39
RefVector & operator=(RefVector const &rhs)
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
void fillView(ProductID const &id, std::vector< void const *> &pointers, FillViewHelperVector &helpers) const
Definition: RefVector.h:177
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
long double T
RefCore const & refCore() const
Accessor for product ID and product getter.
Definition: RefVectorBase.h:66
edm::RefVector< Container > RefVector
bool isAvailable() const
Definition: RefVector.h:233
def move(src, dest)
Definition: eostools.py:511
keys_type const & keys() const
Accessor for vector of keys and pointers.
Definition: RefVectorBase.h:73
size_type size() const
Size of vector.
Definition: RefVectorBase.h:79
bool isNonnull() const
Checks for non-null.
Definition: RefVector.h:126
iterator erase(iterator const &pos)
Erase an element from the vector.
Definition: RefVector.h:215
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector