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
edm::FillViewHelperVector
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
Definition: FillViewHelperVector.h:30
edm::RefVector::reserve
void reserve(size_type n)
Reserve space for RefVector.
Definition: RefVector.h:108
edm::RefCore
Definition: RefCore.h:21
edm::RefVectorBase< key_type >
edm::RefVector::clear
void clear()
Clear the vector.
Definition: RefVector.h:142
edm::RefCore::setProductPtr
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:57
mps_fire.i
i
Definition: mps_fire.py:428
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
edm::RefVector::size_type
KeyVec::size_type size_type
Definition: RefVector.h:48
edm::RefVector::isAvailable
bool isAvailable() const
Definition: RefVector.h:233
edm::RefVector::id
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:117
edm::RefVector::value_type
refhelper::RefVectorTrait< C, T, F >::ref_type value_type
Definition: RefVector.h:39
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::RefVector::const_iterator
iterator const_iterator
Definition: RefVector.h:38
edm::RefVectorBase::refCore
RefCore const & refCore() const
Accessor for product ID and product getter.
Definition: RefVectorBase.h:66
RefVectorIterator.h
edm::RefVector::iterator
refhelper::RefVectorTrait< C, T, F >::iterator_type iterator
Definition: RefVector.h:37
edm::RefVector::erase
iterator erase(iterator const &pos)
Erase an element from the vector.
Definition: RefVector.h:215
pos
Definition: PixelAliasList.h:18
edm::RefVector::begin
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
edm::RefVector::operator=
RefVector & operator=(RefVector &&rhs) noexcept
Definition: RefVector.h:60
edm::detail::GetProduct
Definition: GetProduct.h:33
edm::RefVector::fillView
void fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
Definition: RefVector.h:177
edm::RefVector::isNull
bool isNull() const
Checks for null.
Definition: RefVector.h:123
RefTraits.h
edm::RefVector
Definition: EDProductfwd.h:27
detail
Definition: ConvertingESProducerWithDependenciesT.h:23
edm::RefVector::refVector
contents_type const & refVector() const
Accessor for all data.
Definition: RefVector.h:96
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
GetProduct.h
edm::RefVectorBase::swap
void swap(RefVectorBase< KEY > &other) noexcept
swap two vectors
Definition: RefVectorBase.h:118
edm::RefVectorBase::clear
void clear()
clear the vector
Definition: RefVectorBase.h:111
edm::Ref
Definition: AssociativeIterator.h:58
reco::JetExtendedAssociation::RefVector
edm::RefVector< Container > RefVector
Definition: JetExtendedAssociation.h:34
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
edm::RefVectorBase::empty
bool empty() const
Is vector empty?
Definition: RefVectorBase.h:76
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::RefCore::productGetter
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
edm::operator<<
std::ostream & operator<<(std::ostream &ost, const HLTGlobalStatus &hlt)
Formatted printout of trigger tbale.
Definition: HLTGlobalStatus.h:106
edm::EDProductGetter
Definition: EDProductGetter.h:41
edm::RefVector::end
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
edm::operator!=
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
Definition: debugging_allocator.h:75
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
edm::RefVector::empty
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
edm::operator==
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
Definition: debugging_allocator.h:72
ProductID.h
trackingPlots.other
other
Definition: trackingPlots.py:1460
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
edm::RefVector::operator[]
const value_type operator[](size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:70
b
double b
Definition: hdecay.h:118
edm::RefVector::capacity
size_type capacity() const
Capacity of the RefVector.
Definition: RefVector.h:105
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
edm::RefVectorBase< key_type >::size_type
keys_type::size_type size_type
Definition: RefVectorBase.h:35
edm::RefVector::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: RefVector.h:126
edm::RefVector::productGetter
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefVector.h:120
edm::RefCore::isTransient
bool isTransient() const
Definition: RefCore.h:105
edm::RefVector::RefVector
RefVector()
Definition: RefVector.h:53
core
Definition: __init__.py:1
a
double a
Definition: hdecay.h:119
helpers
Definition: makeCompositeCandidate.h:8
edm::RefVector::RefVector
RefVector(RefVector &&rh) noexcept
Definition: RefVector.h:57
RefHolderBase.h
edm::detail::GetProduct< RefVector< C, T, F > >::address
static element_type const * address(iter const &i)
Definition: RefVector.h:272
edm::RefVectorBase::cachedMemberPointer
void const * cachedMemberPointer(size_type idx) const
Definition: RefVectorBase.h:68
RefVectorBase.h
value
Definition: value.py:1
edm::detail::GetProduct< RefVector< C, T, F > >::iter
RefVector< C, T, F >::const_iterator iter
Definition: RefVector.h:271
edm::RefVector::operator=
RefVector & operator=(RefVector const &rhs)
edm::RefVectorBase::pushBack
void pushBack(RefCore const &product, KEY const &key)
Definition: RefVectorBase.h:81
edm::RefCore::id
ProductID id() const
Definition: RefCore.h:48
svgfig.template
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
edm::RefVectorBase::reserve
void reserve(size_type n)
Reserve space for vector.
Definition: RefVectorBase.h:102
FillView.h
edm::Ref::refCore
RefCore const & refCore() const
Definition: Ref.h:265
edm::RefVector::push_back
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:67
edm::RefVector::~RefVector
~RefVector()=default
edm::ProductID::isValid
bool isValid() const
Definition: ProductID.h:32
edm::Ref::key_type
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:164
edm::RefVector::reference
const_reference reference
Definition: RefVector.h:41
edm::RefVector::RefVector
RefVector(RefVector const &rh)
Definition: RefVector.h:56
edm::RefCore::productPtr
void const * productPtr() const
Definition: RefCore.h:51
alignCSCRings.r
r
Definition: alignCSCRings.py:93
edm::RefVector::hasProductCache
bool hasProductCache() const
Checks if product is in memory.
Definition: RefVector.h:148
edm::RefVectorBase::capacity
size_type capacity() const
Capacity of vector.
Definition: RefVectorBase.h:99
edm::RefVector::refVector_
contents_type refVector_
Definition: RefVector.h:156
edm::RefVector::operator!
bool operator!() const
Checks for null.
Definition: RefVector.h:129
eostools.move
def move(src, dest)
Definition: eostools.py:511
Ref.h
CMS_CLASS_VERSION.h
gen::C
C
Definition: PomwigHadronizer.cc:78
edm::RefVector::member_type
T member_type
Definition: RefVector.h:35
T
long double T
Definition: Basic3DVectorLD.h:48
edm::RefVectorIterator
Definition: EDProductfwd.h:33
edm::RefVector::RefVector
RefVector(ProductID const &iId)
Definition: RefVector.h:65
edm::RefVector::KeyVec
std::vector< key_type > KeyVec
Definition: RefVector.h:45
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
edm::Ref::key
key_type key() const
Accessor for product key.
Definition: Ref.h:250
edm::RefVector::key_type
value_type::key_type key_type
Definition: RefVector.h:44
traits.h
edm::RefVectorBase::size
size_type size() const
Size of vector.
Definition: RefVectorBase.h:79
edm::RefCore::isAvailable
bool isAvailable() const
Definition: RefCore.cc:146
edm::RefVector::at
const value_type at(size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:83
edm::RefVector::finder_type
F finder_type
Definition: RefVector.h:36
edm::RefVector::size
size_type size() const
Size of the RefVector.
Definition: RefVector.h:102
crabWrapper.key
key
Definition: crabWrapper.py:19
edm::refhelper::FindTrait::value
FindUsingAdvance< C, T > value
Definition: RefTraits.h:42
edm::RefVector::collection_type
C collection_type
Definition: RefVector.h:34
edm::RefVectorBase::keys
keys_type const & keys() const
Accessor for vector of keys and pointers.
Definition: RefVectorBase.h:73
edm::RefVector::isTransient
bool isTransient() const
Checks if product collection is tansient (i.e. non persistable)
Definition: RefVector.h:136
EDProductfwd.h
edm::RefVectorBase::eraseAtIndex
keys_type::iterator eraseAtIndex(size_type index)
erase an element from the vector
Definition: RefVectorBase.h:105
edm::RefVector::swap
void swap(RefVector< C, T, F > &other) noexcept
Swap two vectors.
Definition: RefVector.h:160
edm::has_fillView
Definition: traits.h:114
edm::ProductID
Definition: ProductID.h:27
edm::RefVector::const_reference
const value_type const_reference
Definition: RefVector.h:40
edm::detail::GetProduct< RefVector< C, T, F > >::element_type
T element_type
Definition: RefVector.h:270
edm::refhelper::ValueTrait::value
C::value_type value
Definition: RefTraits.h:57
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
edm::RefVector::contents_type
RefVectorBase< key_type > contents_type
Definition: RefVector.h:49
RefVectorTraits.h