LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
PtrVector.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_PtrVector_h
2 #define canvas_Persistency_Common_PtrVector_h
3 
4 // ======================================================================
5 //
6 // PtrVector: a container which returns art::Ptr<>'s referring to items
7 // in one container in the art::Event
8 //
9 // ======================================================================
10 
13 #include "cetlib/compiler_macros.h"
14 
15 #include <initializer_list>
16 #include <iterator>
17 #include <vector>
18 
19 #if GCC_IS_AT_LEAST(4, 9, 0) || CLANG_IS_AT_LEAST(3, 5, 0)
20 #define PV_INSERT_POSITION_TYPE const_iterator
21 #else
22 #define PV_INSERT_POSITION_TYPE iterator
23 #endif
24 
25 namespace art {
26  template <typename T>
27  class PtrVector;
28 
29  template <typename T>
30  void swap(PtrVector<T>&, PtrVector<T>&);
31 
32  template <typename COMP>
33  class ComparePtrs {
34  public:
35  ComparePtrs(COMP comp) : comp_(comp) {}
36  template <typename T>
37  bool
38  operator()(Ptr<T> const& a, Ptr<T> const& b)
39  {
40  return comp_(*a, *b);
41  }
42 
43  private:
44  COMP comp_;
45  };
46 }
47 
48 template <typename T>
49 class art::PtrVector : public PtrVectorBase {
50 private:
51  typedef std::vector<Ptr<T>> data_t;
52 
53 public:
54  typedef typename data_t::value_type value_type;
55  typedef typename data_t::allocator_type allocator_type;
56  typedef typename data_t::reference reference;
57  typedef typename data_t::const_reference const_reference;
58  typedef typename data_t::pointer pointer;
59  typedef typename data_t::const_pointer const_pointer;
60  typedef typename data_t::iterator iterator;
62  typedef typename data_t::reverse_iterator reverse_iterator;
63  typedef typename data_t::const_reverse_iterator const_reverse_iterator;
64  typedef typename data_t::difference_type difference_type;
65  typedef typename data_t::size_type size_type;
66 
67  PtrVector();
68  template <typename U>
70 
71  template <typename U>
72  PtrVector(std::initializer_list<Ptr<U>> il);
73  template <typename U>
74  PtrVector<T>& operator=(std::initializer_list<Ptr<U>> il);
75 
76  template <typename U>
77  PtrVector<T>& operator=(PtrVector<U> const& other) &;
78 
79  // Iterators.
80  iterator begin();
81  const_iterator begin() const;
82  iterator end();
83  const_iterator end() const;
84  reverse_iterator rbegin();
85  const_reverse_iterator rbegin() const;
86  reverse_iterator rend();
87  const_reverse_iterator rend() const;
88 
89  const_iterator cbegin() const; // C+2011.
90  const_iterator cend() const; // C+2011.
91  const_reverse_iterator crbegin() const; // C+2011.
92  const_reverse_iterator crend() const; // C+2011.
93 
94  // Capacity.
95  size_type size() const;
96  size_type max_size() const;
97  void resize(size_type n);
98  size_type capacity() const;
99  bool empty() const;
100  void reserve(size_type n);
101  void shrink_to_fit(); // C+2011.
102 
103  // Element access.
104  Ptr<T> const& operator[](unsigned long const idx) const;
105  reference at(size_type n);
106  const_reference at(size_type n) const;
107  reference front();
108  const_reference front() const;
109  reference back();
110  const_reference back() const;
111  // No C++2011 data() functions by design.
112 
113  // Modifiers.
114  template <typename U>
115  void assign(size_type n, Ptr<U> const& p);
116  template <class InputIterator>
117  void assign(InputIterator first, InputIterator last);
118  template <typename U>
119  void assign(std::initializer_list<Ptr<U>> il);
120  template <typename U>
121  void push_back(Ptr<U> const& p);
122  void pop_back();
123  template <typename U>
124  iterator insert(iterator position, Ptr<U> const& p);
125  template <typename U>
126  void insert(iterator position, size_type n, Ptr<U> const& p);
127  template <typename InputIterator>
128  iterator insert(PV_INSERT_POSITION_TYPE position,
129  InputIterator first,
130  InputIterator last);
131  iterator erase(iterator position);
132  iterator erase(iterator first, iterator last);
133  void swap(PtrVector& other);
134  void swap(key_type k1, key_type k2);
135  void clear();
136  // No C++2011 emplace() due to problems associated with checking for
137  // compatible ProductID.
138 
139  bool operator==(PtrVector const& other) const;
140  void sort();
141  template <class COMP>
142  void sort(COMP comp);
143  static short
145  {
146  return 11;
147  }
148 
149 private:
150  void fill_offsets(indices_t& indices) override;
151  void fill_from_offsets(indices_t const& indices) const override;
152  void zeroTransients() override;
153 
154  // Need to explicitly zero this from custom streamer for base class.
155  mutable data_t ptrs_;
156 }; // PtrVector<T>
157 
158 #include <algorithm>
159 #include <functional>
160 #include <iterator>
161 #include <type_traits>
162 
163 // Constructors.
164 template <typename T>
166 {}
167 
168 template <typename T>
169 template <typename U>
171  : PtrVectorBase(other), ptrs_()
172 {
173  // Ensure that types are compatible.
175  "PtrVector: incompatible types");
176  ptrs_.reserve(other.size());
177  std::copy(other.begin(), other.end(), std::back_inserter(ptrs_));
178 }
179 
180 template <typename T>
181 template <typename U>
182 inline art::PtrVector<T>::PtrVector(std::initializer_list<Ptr<U>> il)
183  : PtrVectorBase(), ptrs_()
184 {
187  "PtrVector: incompatible types");
188  ptrs_.reserve(il.size());
189  for (auto&& p : il) {
190  updateCore(p.refCore());
191  ptrs_.push_back(std::move(p));
192  }
193 }
194 
195 template <typename T>
196 template <typename U>
197 inline art::PtrVector<T>&
198 art::PtrVector<T>::operator=(std::initializer_list<Ptr<U>> il)
199 {
202  "PtrVector: incompatible types");
203  assign(il);
204  return *this;
205 }
206 
207 template <typename T>
208 template <typename U>
209 inline art::PtrVector<T>&
211 {
213  "PtrVector: incompatible types");
214  this->PtrVectorBase::operator=(other);
215  ptrs_.clear();
216  std::copy(other.cbegin(), other.cend(), std::back_inserter(ptrs_));
217  return *this;
218 }
219 
220 // Iterators.
221 template <typename T>
222 inline auto
224 {
225  return ptrs_.begin();
226 }
227 
228 template <typename T>
229 inline auto
231 {
232  return ptrs_.begin();
233 }
234 
235 template <typename T>
236 inline auto
238 {
239  return ptrs_.end();
240 }
241 
242 template <typename T>
243 inline auto
245 {
246  return ptrs_.end();
247 }
248 
249 template <typename T>
250 inline auto
252 {
253  return ptrs_.rbegin();
254 }
255 
256 template <typename T>
257 inline auto
259 {
260  return ptrs_.rbegin();
261 }
262 
263 template <typename T>
264 inline auto
266 {
267  return ptrs_.rend();
268 }
269 
270 template <typename T>
271 inline auto
273 {
274  return ptrs_.rend();
275 }
276 
277 template <typename T>
278 inline auto
280 {
281  return ptrs_.cbegin();
282 }
283 
284 template <typename T>
285 inline auto
287 {
288  return ptrs_.cend();
289 }
290 
291 template <typename T>
292 inline auto
294 {
295  return ptrs_.crbegin();
296 }
297 
298 template <typename T>
299 inline auto
301 {
302  return ptrs_.crend();
303 }
304 
305 // Capacity.
306 template <typename T>
307 inline auto
309 {
310  return ptrs_.size();
311 }
312 
313 template <typename T>
314 inline auto
316 {
317  return ptrs_.max_size();
318 }
319 
320 template <typename T>
321 inline void
323 {
324  ptrs_.resize(n);
325 }
326 
327 template <typename T>
328 inline auto
330 {
331  return ptrs_.capacity();
332 }
333 
334 template <typename T>
335 inline bool
337 {
338  return ptrs_.empty();
339 }
340 
341 template <typename T>
342 inline void
344 {
345  ptrs_.reserve(n);
346 }
347 
348 template <typename T>
349 inline void
351 {
352  ptrs_.shrink_to_fit();
353 }
354 
355 // Element access.
356 template <typename T>
358  unsigned long const idx) const
359 {
360  return *(begin() + idx);
361 }
362 
363 template <typename T>
364 inline auto
366 {
367  return ptrs_.at(n);
368 }
369 
370 template <typename T>
371 inline auto
373 {
374  return ptrs_.at(n);
375 }
376 
377 template <typename T>
378 inline auto
380 {
381  return ptrs_.front();
382 }
383 
384 template <typename T>
385 inline auto
387 {
388  return ptrs_.front();
389 }
390 
391 template <typename T>
392 inline auto
394 {
395  return ptrs_.back();
396 }
397 
398 template <typename T>
399 inline auto
401 {
402  return ptrs_.back();
403 }
404 
405 // Modifiers.
406 template <typename T>
407 template <typename U>
408 inline void
410 {
413  "PtrVector: incompatible types");
415  updateCore(p.refCore());
416  ptrs_.assign(n, p);
417 }
418 
419 template <typename T>
420 template <typename InputIterator>
421 inline void
422 art::PtrVector<T>::assign(InputIterator first, InputIterator last)
423 {
425  std::for_each(
426  first, last, [this](Ptr<T> const& p) { updateCore(p.refCore()); });
427  ptrs_.assign(first, last);
428 }
429 
430 template <typename T>
431 template <typename U>
432 inline void
433 art::PtrVector<T>::assign(std::initializer_list<Ptr<U>> il)
434 {
435  assign(il.begin(), il.end());
436 }
437 
438 template <typename T>
439 template <typename U>
440 inline void
442 {
443  // Ensure that types are compatible.
446  "PtrVector: incompatible types");
447  updateCore(p.refCore());
448  ptrs_.push_back(p);
449 }
450 
451 template <typename T>
452 inline void
454 {
455  ptrs_.pop_back();
456 }
457 
458 template <typename T>
459 template <typename U>
460 inline typename art::PtrVector<T>::iterator
462 {
463  // Ensure that types are compatible.
466  "PtrVector: incompatible types");
467  updateCore(p.refCore());
468  return ptrs_.insert(position, p);
469 }
470 
471 template <typename T>
472 template <typename U>
473 inline void
475 {
476  // Ensure that types are compatible.
479  "PtrVector: incompatible types");
480  updateCore(p.refCore());
481  ptrs_.insert(position, n, p);
482 }
483 
484 template <typename T>
485 template <typename InputIterator>
486 inline auto
488  InputIterator first,
489  InputIterator last) -> iterator
490 {
491  std::for_each(
492  first, last, [this](Ptr<T> const& p) { updateCore(p.refCore()); });
493 #if GCC_IS_AT_LEAST(4, 9, 0) || CLANG_IS_AT_LEAST(3, 5, 0)
494  // C++2011.
495  return ptrs_.insert(position, first, last);
496 #else
497  // Inefficient with C++03 interface.
498  auto const orig_dist = std::distance(ptrs_.begin(), position);
499  ptrs_.insert(position, first, last);
500  iterator result = ptrs_.begin();
501  std::advance(result, orig_dist);
502  return result;
503 #endif
504 }
505 
506 template <typename T>
507 inline auto
509 {
510  return ptrs_.erase(position);
511 }
512 
513 template <typename T>
514 inline auto
516 {
517  return ptrs_.erase(first, last);
518 }
519 
520 template <typename T>
521 inline void
523 {
524  ptrs_.swap(other.ptrs_);
525  PtrVectorBase::swap(other);
526 }
527 
528 template <typename T>
529 inline void
531 {
532  std::swap(ptrs_[k1], ptrs_[k2]);
533 }
534 
535 template <typename T>
536 inline void
538 {
539  ptrs_.clear();
541 }
542 
543 template <typename T>
544 inline bool
546 {
547  return ptrs_ == other.ptrs_ && this->PtrVectorBase::operator==(other);
548 }
549 
550 template <typename T>
551 inline void
553 {
554  sort(std::less<T>());
555 }
556 
557 template <typename T>
558 template <class COMP>
559 inline void
561 {
562  std::sort(ptrs_.begin(), ptrs_.end(), ComparePtrs<COMP>(comp));
563 }
564 
565 template <typename T>
566 void
568 {
569  // Precondition: indices is expected to be empty.
570  assert(indices.empty());
571  indices.reserve(ptrs_.size());
572  for (auto const& i : ptrs_) {
573  indices.push_back(i.key());
574  }
575 }
576 
577 template <typename T>
578 void
580 {
581  // Precondition: ptrs_ is expected to be empty.
582  assert(ptrs_.empty());
583  ptrs_.reserve(indices.size());
584  for (auto i : indices) {
585  ptrs_.emplace_back(id(), i, productGetter());
586  }
587 }
588 
589 template <typename T>
590 inline void
592 {
593  data_t tmp;
594  ptrs_.swap(tmp);
595 }
596 
597 template <typename T>
598 inline void
600 {
601  lhs.swap(rhs);
602 }
603 
604 #undef PV_INSERT_POSITION_TYPE
605 #endif /* canvas_Persistency_Common_PtrVector_h */
606 
607 // Local Variables:
608 // mode: c++
609 // End:
void swap(PtrVectorBase &)
data_t::const_reference const_reference
Definition: PtrVector.h:57
void reserve(size_type n)
Definition: PtrVector.h:343
std::vector< Ptr< T > > data_t
Definition: PtrVector.h:51
void swap(PtrVector< T > &, PtrVector< T > &)
Definition: PtrVector.h:599
size_type max_size() const
Definition: PtrVector.h:315
bool operator()(Ptr< T > const &a, Ptr< T > const &b)
Definition: PtrVector.h:38
EDProductGetter const * productGetter() const
Definition: PtrVectorBase.h:90
void shrink_to_fit()
Definition: PtrVector.h:350
intermediate_table::iterator iterator
PtrVector< T > & operator=(std::initializer_list< Ptr< U >> il)
void swap(PtrVector &other)
Definition: PtrVector.h:522
iterator begin()
Definition: PtrVector.h:223
void fill_offsets(indices_t &indices) override
Definition: PtrVector.h:567
iterator erase(iterator position)
Definition: PtrVector.h:508
const_reverse_iterator crbegin() const
Definition: PtrVector.h:293
ComparePtrs(COMP comp)
Definition: PtrVector.h:35
Float_t tmp
Definition: plot.C:37
reverse_iterator rbegin()
Definition: PtrVector.h:251
size_type capacity() const
Definition: PtrVector.h:329
static short Class_Version()
Definition: PtrVector.h:144
const_reverse_iterator crend() const
Definition: PtrVector.h:300
data_t::size_type size_type
Definition: PtrVector.h:65
reference back()
Definition: PtrVector.h:393
data_t::value_type value_type
Definition: PtrVector.h:54
void fill_from_offsets(indices_t const &indices) const override
Definition: PtrVector.h:579
intermediate_table::const_iterator const_iterator
bool operator==(PtrVectorBase const &) const
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
void swap(Handle< T > &a, Handle< T > &b)
RefCore const & refCore() const
Definition: Ptr.h:370
data_t::difference_type difference_type
Definition: PtrVector.h:64
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
iterator end()
Definition: PtrVector.h:237
data_t::pointer pointer
Definition: PtrVector.h:58
void resize(size_type n)
Definition: PtrVector.h:322
reference at(size_type n)
Definition: PtrVector.h:365
data_t::const_pointer const_pointer
Definition: PtrVector.h:59
const_iterator cbegin() const
Definition: PtrVector.h:279
unsigned long key_type
Definition: PtrVectorBase.h:23
data_t::iterator iterator
Definition: PtrVector.h:60
bool empty() const
Definition: PtrVector.h:336
size_type size() const
Definition: PtrVector.h:308
data_t::const_iterator const_iterator
Definition: PtrVector.h:61
reference front()
Definition: PtrVector.h:379
iterator insert(iterator position, Ptr< U > const &p)
std::string value(boost::any const &)
const_iterator cend() const
Definition: PtrVector.h:286
void pop_back()
Definition: PtrVector.h:453
HLT enums.
reverse_iterator rend()
Definition: PtrVector.h:265
data_t ptrs_
Definition: PtrVector.h:155
Char_t n[5]
void assign(size_type n, Ptr< U > const &p)
Definition: PtrVector.h:409
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
std::vector< key_type > indices_t
Definition: PtrVectorBase.h:24
data_t::reference reference
Definition: PtrVector.h:56
#define PV_INSERT_POSITION_TYPE
Definition: PtrVector.h:22
void updateCore(RefCore const &core)
data_t::reverse_iterator reverse_iterator
Definition: PtrVector.h:62
bool operator==(Provenance const &a, Provenance const &b)
Definition: Provenance.h:168
Ptr< T > const & operator[](unsigned long const idx) const
Definition: PtrVector.h:357
bool operator==(PtrVector const &other) const
Definition: PtrVector.h:545
void clear()
Definition: PtrVector.h:537
Definition: fwd.h:25
data_t::const_reverse_iterator const_reverse_iterator
Definition: PtrVector.h:63
vec_iX clear()
void zeroTransients() override
Definition: PtrVector.h:591
data_t::allocator_type allocator_type
Definition: PtrVector.h:55