1// Dear emacs, this is -*- c++ -*-
4 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
7 * @file AthContainers/DataVector.icc
8 * @author scott snyder, Paolo Calafiura, etc
9 * @date May 2005; rewritten from earlier version.
10 * @brief An STL vector of pointers that by default owns its pointed-to
12 * Implementation file.
16#include "AthContainers/tools/CompareAndPrint.h"
17#include "AthContainers/tools/ATHCONTAINERS_ASSERT.h"
18#include "CxxUtils/releasing_iterator.h"
19#include "CxxUtils/throw_out_of_range.h"
24//****************************************************************************
28namespace ROOT { namespace Meta { namespace Selection {
29 namespace DataVector_detail {
30 template <class B1, class B2, class B3> class VirtBases;
35namespace DataVector_detail {
37/* #define DO_REMOVE_DUPLICATES to activate for debugging purposes (Slow) */
38// Note: this name should be distinct from the corresponding one in DataList,
39// even though they're in different namespaces. This due to an apparent
40// Koenig lookup bug in gccxml 0.9.0.
42void optimizeMeAway_DV(FI, bool){}
44 * @brief Remove duplicates from a @c DataVector before deleting elements.
45 * @param b Start of range to scan.
46 * @param e One past end of range to scan.
47 * @param quiet If true, complain if duplicates are found.
48 * @return One past the last unique elements.
50 * The elements within the range are sorted, then duplicate elements
51 * are moved to the end. If duplicate elements are found and
52 * @a quiet is @c true, then a complaint will be printed.
54template <class ForwIter>
55ForwIter remove_duplicates(ForwIter b, ForwIter e, bool quiet=false)
57#ifdef DO_REMOVE_DUPLICATES
59 return std::unique(b, e, DataModel_detail::CompareAndPrint(quiet));
61 optimizeMeAway_DV(b, quiet);
67 * @brief VirtBases for one class.
69 * @c DataVector<T> derives from this for the case of
70 * @c T deriving virtually from a single class.
71 * It in turn derives from @c B1.
74struct VirtBases<B1, DataModel_detail::NoBase, DataModel_detail::NoBase>
75 : virtual public DataVector<B1>
77 // Make these types available to the derived @c DataVector.
78 typedef typename DataVector<B1>::PtrVector PtrVector;
79 typedef typename DataVector<B1>::size_type size_type;
80 typedef typename DataVector<B1>::difference_type difference_type;
81 typedef typename DataVector<B1>::allocator_type allocator_type;
82 typedef typename DataVector<B1>::Deleter Deleter;
83 // cppcheck-suppress duplInheritedMember; deliberate
84 static constexpr bool must_own = DataVector<B1>::must_own;
86 // We're using virtual derivation.
87 // cppcheck-suppress duplInheritedMember; deliberate
88 static constexpr bool has_virtual = true;
90 // We need to delete the move ctor; otherwise, we get warnings about
91 // a virtual base having a non-trivial move ctor. Then we also need
92 // to explicitly default the ordinary ctors so that they remain visible.
93 VirtBases () = default;
94 VirtBases (const VirtBases&) = default;
95 // default doesn't work here with gcc 4.7; it does with 4.9.
96 VirtBases& operator= (VirtBases&) { return *this; }
97 VirtBases& operator= (VirtBases&&) = delete;
101 // Pass this down to base classes.
102 void clearMostDerived()
104 DataVector<B1>::clearMostDerived();
107 // We need these here to prevent ambiguities,
108 // but they shouldn't actually be called.
109 virtual const std::type_info& dv_typeid() const override
111 return typeid(VirtBases);
113 virtual const DataModel_detail::DVLInfoBase& dvlinfo_v() const override
115 return DataVector<B1>::dvlinfo();
117 virtual void setMostDerived() override
124 int baseOffset1 (const char* p, const VirtBases& dv,
125 const std::type_info& ti)
127 return DataVector<B1>::baseOffset1 (p, dv, ti);
133 ROOT::Meta::Selection::
134 DataVector_detail::VirtBases<B1,
135 DataModel_detail::NoBase,
136 DataModel_detail::NoBase>::self self;
141 * @brief VirtBases for two classes.
143 * @c DataVector<T> derives from this for the case of
144 * @c T deriving from two classes.
145 * It in turn derives from @c B1 and @c B2.
147template <class B1, class B2>
148struct VirtBases<B1, B2, DataModel_detail::NoBase>
149 : virtual public DataVector<B1>,
150 virtual public DataVector<B2>
152 // Check to be sure that @c B1 and @c B2 have the same ultimate base type.
153 static_assert (std::is_same_v<typename DataVector<B1>::PtrVector,
154 typename DataVector<B2>::PtrVector>);
156 // Make these types available to the derived @c DataVector.
157 typedef typename DataVector<B1>::PtrVector PtrVector;
158 typedef typename DataVector<B1>::size_type size_type;
159 typedef typename DataVector<B1>::difference_type difference_type;
160 typedef typename DataVector<B1>::allocator_type allocator_type;
161 typedef typename DataVector<B1>::Deleter Deleter;
162 // cppcheck-suppress duplInheritedMember; deliberate
163 static constexpr bool must_own = DataVector<B1>::must_own;
165 // We're using virtual derivation.
166 // cppcheck-suppress duplInheritedMember; deliberate
167 static constexpr bool has_virtual = true;
170 // We need to delete the move ctor; otherwise, we get warnings about
171 // a virtual base having a non-trivial move ctor. Then we also need
172 // to explicitly default the ordinary ctors so that they remain visible.
173 VirtBases () = default;
174 VirtBases (const VirtBases&) = default;
175 // default doesn't work here with gcc 4.7; it does with 4.9.
176 VirtBases& operator= (VirtBases&) { return *this; }
177 VirtBases& operator= (VirtBases&&) = delete;
181 // Pass this down to base classes.
182 void clearMostDerived()
184 DataVector<B1>::clearMostDerived();
185 DataVector<B2>::clearMostDerived();
188 // We need these here to prevent ambiguities,
189 // but they shouldn't actually be called.
190 virtual const std::type_info& dv_typeid() const override
192 return typeid(VirtBases);
194 virtual const DataModel_detail::DVLInfoBase& dvlinfo_v() const override
196 return DataVector<B1>::dvlinfo();
198 virtual void setMostDerived() override
205 int baseOffset1 (const char* p, const VirtBases& dv,
206 const std::type_info& ti)
208 int ret = DataVector<B1>::baseOffset1 (p, dv, ti);
211 return DataVector<B2>::baseOffset1 (p, dv, ti);
217 ROOT::Meta::Selection::
218 DataVector_detail::VirtBases<B1, B2,
219 DataModel_detail::NoBase>::self self;
224 * @brief VirtBases for three classes.
226 * @c DataVector<T> derives from this for the case of
227 * @c T deriving from three classes.
228 * It in turn derives from @c B1, @c B2, and @c B3.
230template <class B1, class B2, class B3>
232 : virtual public DataVector<B1>,
233 virtual public DataVector<B2>,
234 virtual public DataVector<B3>
236 // Check to be sure that @c B1, @c B2, and @c B3 have the same
237 // ultimate base type.
238 static_assert (std::is_same_v<typename DataVector<B1>::PtrVector,
239 typename DataVector<B2>::PtrVector>);
240 static_assert (std::is_same_v<typename DataVector<B1>::PtrVector,
241 typename DataVector<B3>::PtrVector>);
244 // Make these types available to the derived @c DataVector.
245 typedef typename DataVector<B1>::PtrVector PtrVector;
246 typedef typename DataVector<B1>::size_type size_type;
247 typedef typename DataVector<B1>::difference_type difference_type;
248 typedef typename DataVector<B1>::allocator_type allocator_type;
249 typedef typename DataVector<B1>::Deleter Deleter;
250 // cppcheck-suppress duplInheritedMember; deliberate
251 static constexpr bool must_own = DataVector<B1>::must_own;
253 // We're using virtual derivation.
254 // cppcheck-suppress duplInheritedMember; deliberate
255 static constexpr bool has_virtual = true;
258 // We need to delete the move ctor; otherwise, we get warnings about
259 // a virtual base having a non-trivial move ctor. Then we also need
260 // to explicitly default the ordinary ctors so that they remain visible.
261 VirtBases () = default;
262 VirtBases (const VirtBases&) = default;
263 // default doesn't work here with gcc 4.7; it does with 4.9.
264 VirtBases& operator= (VirtBases&) { return *this; }
265 VirtBases& operator= (VirtBases&&) = delete;
269 // Pass this down to base classes.
270 void clearMostDerived()
272 DataVector<B1>::clearMostDerived();
273 DataVector<B2>::clearMostDerived();
274 DataVector<B3>::clearMostDerived();
277 // We need these here to prevent ambiguities,
278 // but they shouldn't actually be called.
279 virtual const std::type_info& dv_typeid() const override
281 return typeid(VirtBases);
283 virtual const DataModel_detail::DVLInfoBase& dvlinfo_v() const override
285 return DataVector<B1>::dvlinfo();
287 virtual void setMostDerived() override
294 int baseOffset1 (const char* p, const VirtBases& dv,
295 const std::type_info& ti)
297 int ret = DataVector<B1>::baseOffset1 (p, dv, ti);
300 ret = DataVector<B2>::baseOffset1 (p, dv, ti);
303 return DataVector<B3>::baseOffset1 (p, dv, ti);
309 ROOT::Meta::Selection::DataVector_detail::VirtBases<B1, B2, B3>::self
314} // namespace DataVector_detail
317namespace ROOT { namespace Meta { namespace Selection {
318namespace DataVector_detail {
320template< class B1, class B2, class B3 >
321class VirtBases : KeepFirstTemplateArguments< 1 >, SelectNoInstance
326 typedef DataVector_detail::VirtBases< B1, B2, B3 > self;
334//****************************************************************************
335// Generic (derived) DataVector implementation.
339//=== Constructors, destructors, assignment.
343 * @brief Default constructor.
344 * @param ownPolicy The ownership mode for the container.
345 * @param trackIndices The index tracking policy.
347 * By default, a @c DataVector will own its elements.
348 * To avoid this, pass @c SG::VIEW_ELEMENTS for @a ownPolicy.
350 * Note that we do the complete initialization here in the derived class,
351 * using the default constructors for the base classes. The reason
352 * for this is to be able to deal nicely with the virtual derivation
353 * case. We can arrange to call the proper base class from here to get
354 * things initialized in the virtual derivation case. But then anyone
355 * who derives from us must also know to explicitly reference that base
356 * class. Doing the initialization explicitly here means that other classes
357 * who derive from us need only know about the most derived DataVector
360template <class T, class BASE>
362DataVector<T, BASE>::DataVector
363 (SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/,
364 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/)
366 this->m_ownPolicy = ownPolicy;
367 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
368 this->m_isMostDerived = true;
369 BASE::clearMostDerived();
374 * @brief Sized constructor.
375 * @param n The size of the container.
376 * @param ownPolicy The ownership mode for the container.
377 * @param trackIndices The index tracking policy.
379 * Note that unlike the standard vector constructor, you can't specify
380 * an initial value here. The container will be initialized with 0's.
382 * By default, a @c DataVector will own its elements.
383 * To avoid this, pass @c SG::VIEW_ELEMENTS for @a ownPolicy.
385 * Note that we do the complete initialization here in the derived class,
386 * using the default constructors for the base classes. The reason
387 * for this is to be able to deal nicely with the virtual derivation
388 * case. We can arrange to call the proper base class from here to get
389 * things initialized in the virtual derivation case. But then anyone
390 * who derives from us must also know to explicitly reference that base
391 * class. Doing the initialization explicitly here means that other classes
392 * who derive from us need only know about the most derived DataVector
395template <class T, class BASE>
397DataVector<T, BASE>::DataVector
399 SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/,
400 SG::IndexTrackingPolicy trackIndices /*=SG::DEFAULT_TRACK_INDICES*/)
402 this->m_ownPolicy = ownPolicy;
403 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
404 this->m_isMostDerived = true;
405 BASE::clearMostDerived();
406 this->m_pCont.resize (n);
411 * @brief Move constructor.
412 * @param rhs The container from which to move.
414 * Any auxiliary data will be moved along with the container contents.
416template <class T, class BASE>
417DataVector<T, BASE>::DataVector (DataVector&& rhs) noexcept
419 SG::AuxVectorBase::operator= (std::move (rhs));
420 this->m_ownPolicy = rhs.m_ownPolicy;
421 this->m_pCont = std::move (rhs.m_pCont);
422 this->m_deleter = std::move(rhs.m_deleter);
423 rhs.m_deleter = nullptr;
425 // Need to reset the container pointer on elements.
426 this->setIndices (this->begin(), this->end());
427 this->m_isMostDerived = true;
432 * @brief Constructor from iterators.
433 * @param first The start of the range to put in the new container.
434 * @param last The end of the range to put in the new container.
435 * @param ownPolicy The ownership mode for the container.
436 * @param trackIndices The index tracking policy.
437 * @param store An associated auxiliary data store.
439 * By default, a view container is made, which does not own its elements.
440 * To have the container take ownership of the pointers passed
441 * to this constructor, pass @c SG::OWN_ELEMENTS for @a ownPolicy.
443 * Note that we do the complete initialization here in the derived class,
444 * using the default constructors for the base classes. The reason
445 * for this is to be able to deal nicely with the virtual derivation
446 * case. We can arrange to call the proper base class from here to get
447 * things initialized in the virtual derivation case. But then anyone
448 * who derives from us must also know to explicitly reference that base
449 * class. Doing the initialization explicitly here means that other classes
450 * who derive from us need only know about the most derived DataVector
453template <class T, class BASE>
454template <class InputIterator>
456DataVector<T, BASE>::DataVector
457 (InputIterator first,
459 SG::OwnershipPolicy ownPolicy /*= SG::VIEW_ELEMENTS*/,
460 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_INDEX_TRACKING*/,
461 SG::IAuxStore* store /*= 0*/)
463 // Make sure that the input iterator can actually be converted
464 // to a T*. Lets us give a compilation error for this:
465 // DATAVECTOR_BASE(D, B);
467 // DataVector<D> d (bb, bb+1);
468 // which would otherwise compile.
469 //FIXME: Rewrite using concepts once we're using c++20.
470 using ittype = typename std::iterator_traits<InputIterator>::value_type;
471 static_assert (std::is_convertible_v<ittype, const T*> ||
472 std::is_convertible_v<ittype, std::unique_ptr<T> >);
474 this->m_ownPolicy = ownPolicy;
475 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
476 this->m_isMostDerived = true;
477 BASE::clearMostDerived();
479 this->setStore (store);
480 insert (this->begin(), first, last);
485 * @brief Constructor from an initializer list.
486 * @param l An initializer list.
487 * @param ownPolicy The ownership mode for the container.
488 * @param trackIndices The index tracking policy.
489 * @param store An associated auxiliary data store.
492 * A @c DataVector constructed this way will *not* own its elements
493 * by default. To change this, pass @c SG::OWN_ELEMENTS for @a ownPolicy.
495 * Note that we do the complete initialization here in the derived class,
496 * using the default constructors for the base classes. The reason
497 * for this is to be able to deal nicely with the virtual derivation
498 * case. We can arrange to call the proper base class from here to get
499 * things initialized in the virtual derivation case. But then anyone
500 * who derives from us must also know to explicitly reference that base
501 * class. Doing the initialization explicitly here means that other classes
502 * who derive from us need only know about the most derived DataVector
505template <class T, class BASE>
507DataVector<T, BASE>::DataVector
508 (std::initializer_list<value_type> l,
509 SG::OwnershipPolicy ownPolicy /*= SG::VIEW_ELEMENTS*/,
510 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_INDEX_TRACKING*/,
511 SG::IAuxStore* store /*= 0*/)
512 : DataVector (l.begin(), l.end(), ownPolicy, trackIndices, store)
518 * @brief Assignment operator.
519 * @param rhs The DataVector from which to assign.
520 * @return This object.
522 * This is a `shallow' copy; after the completion of this, the DataVector
523 * will not own its elements. Any elements it owned prior to this call
526 * Note: this method may only be called using the most derived
527 * @c DataVector in the hierarchy.
529template <class T, class BASE>
531DataVector<T, BASE>& DataVector<T, BASE>::operator= (const DataVector& rhs)
534 // Ensure we're not being called via a base class.
535 testInsert ("assignment operator");
536 this->clear(); // Release any currently-owned elements.
537 this->m_ownPolicy = SG::VIEW_ELEMENTS;
538 this->setStore ((SG::IConstAuxStore*)0);
539 this->template initAuxVectorBase<DataVector>(this->m_ownPolicy,
540 SG::DEFAULT_TRACK_INDICES);
541 this->m_pCont = rhs.m_pCont;
548 * @brief Move assignment.
549 * @param rhs The container from which to move.
551 * Any auxiliary data will be moved along with the container contents.
553 * (Can't be noexcept because testInsert() can throw.)
555template <class T, class BASE>
557DataVector<T, BASE>::operator= (DataVector<T, BASE>&& rhs)
560 // Ensure we're not being called via a base class.
561 testInsert ("assignment operator");
563 this->clear(); // Release any currently-owned elements.
565 SG::AuxVectorBase::operator= (std::move (rhs));
566 this->m_ownPolicy = rhs.m_ownPolicy;
567 this->m_pCont = std::move (rhs.m_pCont);
569 delete this->m_deleter;
570 this->m_deleter = std::move(rhs.m_deleter);
571 rhs.m_deleter = nullptr;
573 // Need to reset the container pointer on elements.
574 this->setIndices (this->begin(), this->end());
581 * @brief Assignment operator, from an initializer list.
582 * @param l An initializer list.
583 * @return This object.
585 * This is equivalent to @c assign.
586 * Any existing owned elements will be released.
587 * The @c DataVector's ownership policy determines whether it will take
588 * ownership of the new elements.
590template <class T, class BASE>
593DataVector<T, BASE>::operator= (std::initializer_list<value_type> l)
595 this->assign (l.begin(), l.end());
601 * @brief Assign from iterators.
602 * @param first The start of the range to put in the container.
603 * @param last The end of the range to put in the container.
605 * Any existing owned elements will be released.
606 * The @c DataVector's ownership policy determines whether it will take
607 * ownership of the new elements.
609template <class T, class BASE>
610template <class InputIterator>
611void DataVector<T, BASE>::assign(InputIterator first, InputIterator last)
613 // Ensure we're not being called via a base class.
614 testInsert ("assign");
615 this->clear(); // Release any currently-owned elements.
616 insert(begin(), first, last);
621 * @brief Assign from an initializer list.
622 * @param l An initializer list.
624 * Any existing owned elements will be released.
625 * The @c DataVector's ownership policy determines whether it will take
626 * ownership of the new elements.
628template <class T, class BASE>
629void DataVector<T, BASE>::assign(std::initializer_list<value_type> l)
631 this->assign (l.begin(), l.end());
635//=== Size and capacity.
639 * @fn size_type size() const
640 * @brief Returns the number of elements in the collection.
642template <class T, class BASE>
644typename DataVector<T, BASE>::size_type
645DataVector<T, BASE>::size() const noexcept
647 return this->m_pCont.size();
652 * @brief Resizes the collection to the specified number of elements.
653 * @param sz The new size of the collection.
655 * Note that this function differs from the standard in that it does
656 * not allow specifying the value of any inserted elements.
657 * They will always be 0.
659 * If the container is shrunk, elements will be deleted as with @c erase().
661template <class T, class BASE>
662void DataVector<T, BASE>::resize(size_type sz)
664 if (sz < this->size()) {
665 this->erase (this->begin()+sz, this->end());
667 this->m_pCont.insert(this->m_pCont.end(), sz - this->m_pCont.size(), 0);
668 SG::AuxVectorBase::resize<DataVector> (sz);
674 * @brief Attempt to preallocate enough memory for a specified number
676 * @param n Number of elements required.
678template <class T, class BASE>
680void DataVector<T, BASE>::reserve (size_type n)
682 this->m_pCont.reserve (n);
683 SG::AuxVectorBase::reserve<DataVector> (n);
691 * @brief Access an element, as an rvalue.
692 * @param n Array index to access.
693 * @return The element at @a n.
695 * No bounds checking is done.
696 * Note that we return a @c const @c T* rather than a reference.
698template <class T, class BASE>
700const T* DataVector<T, BASE>::operator[] (size_type n) const
702 return do_cast(this->m_pCont[n]);
707 * @brief Access an element, as an rvalue.
708 * @param n Array index to access.
709 * @return The element at @a n.
711 * This is a synonym for operator[] const, to be used when calling from root
712 * (where we can't readily call just the const version of a method).
714template <class T, class BASE>
716const T* DataVector<T, BASE>::get (size_type n) const
718 return do_cast(this->m_pCont[n]);
723 * @brief Access an element, as an lvalue.
724 * @param n Array index to access.
725 * @return Proxy to the element at @a n.
727 * No bounds checking is done.
728 * Note that we return a proxy object rather than a reference;
729 * the proxy will handle deleting an owned element if it's assigned to.
731template <class T, class BASE>
733typename DataVector<T, BASE>::ElementProxy
734DataVector<T, BASE>::operator[] (size_type n)
736 return ElementProxy (this->m_pCont.begin() + n, this);
741 * @brief Access an element, as an rvalue.
742 * @param n Array index to access.
743 * @return The element at @a n.
745 * Will raise @c std::out_of_range if the index is out-of-bounds.
746 * Note that we return a @c const @c T* rather than a reference.
748template <class T, class BASE>
750const T* DataVector<T, BASE>::at (size_type n) const
752 return do_cast(this->m_pCont.at(n));
757 * @brief Access an element, as an lvalue.
758 * @param n Array index to access.
759 * @return Proxy to the element at @a n.
761 * Will raise @c std::out_of_range if the index is out-of-bounds.
762 * Note that we return a proxy object rather than a reference;
763 * the proxy will handle deleting an owned element if it's assigned to.
765template <class T, class BASE>
767typename DataVector<T, BASE>::ElementProxy
768DataVector<T, BASE>::at (size_type n)
770 // Can't use m_pCont's at here, because we need an iterator.
771 // So we have to do the bounds check ourselves.
772 if (n >= this->size())
773 CxxUtils::throw_out_of_range (__PRETTY_FUNCTION__, n, this->size(), this);
774 return ElementProxy (this->m_pCont.begin() + n, this);
779 * @brief Access the first element in the collection as an rvalue.
780 * @return The first element in the collection.
782 * No checking is done to ensure that the container is not empty.
783 * Note that we return a @c const @c T* rather than a reference.
785template <class T, class BASE>
787const T* DataVector<T, BASE>::front() const
789 return do_cast (this->m_pCont.front());
794 * @brief Access the last element in the collection as an rvalue.
795 * @return The last element in the collection.
797 * No checking is done to ensure that the container is not empty.
798 * Note that we return a @c const @c T* rather than a reference.
800template <class T, class BASE>
802const T* DataVector<T, BASE>::back() const
804 return do_cast (this->m_pCont.back());
809 * @brief Access the first element in the collection as an lvalue.
810 * @return Proxy to the first element in the collection.
812 * No checking is done to ensure that the container is not empty.
813 * Note that we return a proxy object rather than a reference;
814 * the proxy will handle deleting an owned element if it's assigned to.
816template <class T, class BASE>
818typename DataVector<T, BASE>::ElementProxy
819DataVector<T, BASE>::front ()
821 return ElementProxy (this->m_pCont.begin(), this);
826 * @brief Access the last element in the collection as an lvalue.
827 * @return Proxy to the last element in the collection.
829 * No checking is done to ensure that the container is not empty.
830 * Note that we return a proxy object rather than a reference;
831 * the proxy will handle deleting an owned element if it's assigned to.
833template <class T, class BASE>
835typename DataVector<T, BASE>::ElementProxy
836DataVector<T, BASE>::back ()
838 return ElementProxy (this->m_pCont.end()-1, this);
842//=== Iterator creation.
846 * @brief Return a @c const_iterator pointing at the beginning
848 * @return A @c const_iterator.
850 * Note that dereferencing the iterator will yield a @c const @c T* rather
853template <class T, class BASE>
855typename DataVector<T, BASE>::const_iterator
856DataVector<T, BASE>::begin() const noexcept
858 return const_iterator (this->m_pCont.begin());
863 * @brief Return a @c const_iterator pointing past the end
865 * @return A @c const_iterator.
867 * Note that dereferencing the iterator will yield a @c const @c T* rather
870template <class T, class BASE>
872typename DataVector<T, BASE>::const_iterator
873DataVector<T, BASE>::end() const noexcept
875 return const_iterator (this->m_pCont.end());
880 * @brief Return an @c iterator pointing at the beginning
882 * @return An @c iterator.
884 * Note that dereferencing the iterator will yield a proxy rather
885 * than a reference; the proxy will handle deleting an owned element
886 * if it's assigned to.
888template <class T, class BASE>
890typename DataVector<T, BASE>::iterator
891DataVector<T, BASE>::begin() noexcept
893 return iterator (this->m_pCont.begin(), this);
898 * @brief Return an @c iterator pointing past the end
900 * @return An @c iterator.
902 * Note that dereferencing the iterator will yield a proxy rather
903 * than a reference; the proxy will handle deleting an owned element
904 * if it's assigned to.
906template <class T, class BASE>
908typename DataVector<T, BASE>::iterator
909DataVector<T, BASE>::end() noexcept
911 return iterator (this->m_pCont.end(), this);
916 * @brief Return a @c const_reverse_iterator pointing past the end
918 * @return A @c const_reverse_iterator.
920 * Note that dereferencing the iterator will yield a @c const @c T* rather
923template <class T, class BASE>
925typename DataVector<T, BASE>::const_reverse_iterator
926DataVector<T, BASE>::rbegin() const noexcept
928 return const_reverse_iterator (end());
933 * @brief Return a @c const_reverse_iterator pointing at the beginning
935 * @return A @c const_reverse_iterator.
937 * Note that dereferencing the iterator will yield a @c const @c T* rather
940template <class T, class BASE>
942typename DataVector<T, BASE>::const_reverse_iterator
943DataVector<T, BASE>::rend() const noexcept
945 return const_reverse_iterator (begin());
950 * @brief Return a @c reverse_iterator pointing past the end
952 * @return A @c reverse_iterator.
954 * Note that dereferencing the iterator will yield a proxy rather
955 * than a reference; the proxy will handle deleting an owned element
956 * if it's assigned to.
958template <class T, class BASE>
960typename DataVector<T, BASE>::reverse_iterator
961DataVector<T, BASE>::rbegin() noexcept
963 return reverse_iterator (iterator (this->m_pCont.end(), this));
968 * @brief Return a @c reverse_iterator pointing at the beginning
970 * @return A @c reverse_iterator.
972 * Note that dereferencing the iterator will yield a proxy rather
973 * than a reference; the proxy will handle deleting an owned element
974 * if it's assigned to.
976template <class T, class BASE>
978typename DataVector<T, BASE>::reverse_iterator
979DataVector<T, BASE>::rend() noexcept
981 return reverse_iterator (iterator (this->m_pCont.begin(), this));
986 * @brief Return a @c const_iterator pointing at the beginning
988 * @return A @c const_iterator.
990 * Note that dereferencing the iterator will yield a @c const @c T* rather
993template <class T, class BASE>
995typename DataVector<T, BASE>::const_iterator
996DataVector<T, BASE>::cbegin() const noexcept
998 return const_iterator (this->m_pCont.begin());
1003 * @brief Return a @c const_iterator pointing past the end
1004 * of the collection.
1005 * @return A @c const_iterator.
1007 * Note that dereferencing the iterator will yield a @c const @c T* rather
1010template <class T, class BASE>
1012typename DataVector<T, BASE>::const_iterator
1013DataVector<T, BASE>::cend() const noexcept
1015 return const_iterator (this->m_pCont.end());
1020 * @brief Return a @c const_reverse_iterator pointing past the end
1021 * of the collection.
1022 * @return A @c const_reverse_iterator.
1024 * Note that dereferencing the iterator will yield a @c const @c T* rather
1027template <class T, class BASE>
1029typename DataVector<T, BASE>::const_reverse_iterator
1030DataVector<T, BASE>::crbegin() const noexcept
1032 return const_reverse_iterator (cend());
1037 * @brief Return a @c const_reverse_iterator pointing at the beginning
1038 * of the collection.
1039 * @return A @c const_reverse_iterator.
1041 * Note that dereferencing the iterator will yield a @c const @c T* rather
1044template <class T, class BASE>
1046typename DataVector<T, BASE>::const_reverse_iterator
1047DataVector<T, BASE>::crend() const noexcept
1049 return const_reverse_iterator (cbegin());
1053//=== Insertion operations.
1057 * @brief Add an element to the end of the collection.
1058 * @param pElem The element to add to the collection.
1060 * The container's ownership policy will determine if it takes ownership
1061 * of the new element.
1063 * Note: this method may only be called using the most derived
1064 * @c DataVector in the hierarchy.
1066 * Returns the pushed pointer.
1068template <class T, class BASE>
1070typename DataVector<T, BASE>::value_type
1071DataVector<T, BASE>::push_back(value_type pElem)
1073 // Ensure we're not being called via a base class.
1074 testInsert ("push_back");
1075 this->m_pCont.push_back(static_cast<typename PtrVector::value_type>(pElem));
1076 SG::AuxVectorBase::resize<DataVector> (this->size());
1078 this->moveAux (this->size()-1, pElem, false, true);
1084 * @brief Add an element to the end of the collection.
1085 * @param pElem The element to add to the collection.
1087 * The container must be an owning container.
1089 * Note: this method may only be called using the most derived
1090 * @c DataVector in the hierarchy.
1092 * Returns the pushed pointer.
1094template <class T, class BASE>
1096typename DataVector<T, BASE>::value_type
1097DataVector<T, BASE>::push_back(std::unique_ptr<base_value_type> pElem)
1099 // Container must own its elements.
1100 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1101 SG::throwExcNonowningContainer();
1103 // Ensure we're not being called via a base class.
1104 testInsert ("push_back");
1105 value_type ptr = pElem.release();
1106 this->m_pCont.push_back(ptr);
1107 SG::AuxVectorBase::resize<DataVector> (this->size());
1109 this->moveAux (this->size()-1, ptr, false, true);
1115 * @brief Create and add a number of new elements to the end of the container.
1116 * @param n The number of new elements to add.
1117 * @param alloc Functional to call to allocate a new element to push.
1118 * Should be callable like <code>T* = alloc();</code>
1121 * dv.push_new (n, [](){ return new Foo; });
1123 * It may also be useful to allocate from a @c DataPool.
1125 * Note: this method may only be called using the most derived
1126 * @c DataVector in the hierarchy.
1128 * Returns the original size of the vector.
1130template <class T, class BASE>
1131template <CxxUtils::detail::AllocationFunction<T> F>
1133DataVector<T, BASE>::push_new(size_type n, F alloc) -> size_type
1135 // Ensure we're not being called via a base class.
1136 testInsert ("push_new");
1138 size_t sz = this->m_pCont.size();
1139 this->m_pCont.resize (sz + n);
1140 SG::AuxVectorBase::resize<DataVector> (this->size());
1141 for (size_t i = 0; i < n; i++) {
1143 this->m_pCont[sz+i] = p;
1144 this->moveAux (sz+i, p, false, true);
1151 * @brief Add an element to the end of the collection.
1152 * @param pElem The element to add to the collection.
1154 * The container's ownership policy will determine if it takes ownership
1155 * of the new element.
1157 * Note: this method may only be called using the most derived
1158 * @c DataVector in the hierarchy.
1160 * For @c DataVector, this is like the same as @c push_back, and
1161 * it returns the pushed element.
1162 * It's included just for interface compatibility with `std::vector`.
1164template <class T, class BASE>
1166typename DataVector<T, BASE>::value_type
1167DataVector<T, BASE>::emplace_back(value_type pElem)
1169 return this->push_back (pElem);
1174 * @brief Add a new element to the collection.
1175 * @param position Iterator before which the element will be added.
1176 * @param pElem The element to add to the collection.
1177 * @return An iterator that points to the inserted data.
1179 * The container's ownership policy will determine if it takes ownership
1180 * of the new element.
1182 * Note: this method may only be called using the most derived
1183 * @c DataVector in the hierarchy.
1185template <class T, class BASE>
1186typename DataVector<T, BASE>::iterator
1187DataVector<T, BASE>::insert(iterator position, value_type pElem)
1189 // Ensure we're not being called via a base class.
1190 testInsert ("insert");
1191 iterator ret (this->m_pCont.insert(position.base(), pElem), this);
1192 this->shift (ret - this->begin(), 1);
1193 this->moveAux (ret-this->begin(), pElem, false, true);
1199 * @brief Add a new element to the collection.
1200 * @param position Iterator before which the element will be added.
1201 * @param pElem The element to add to the collection.
1202 * @return An iterator that points to the inserted data.
1204 * The container must be an owning container.
1206 * Note: this method may only be called using the most derived
1207 * @c DataVector in the hierarchy.
1209template <class T, class BASE>
1210typename DataVector<T, BASE>::iterator
1211DataVector<T, BASE>::insert(iterator position,
1212 std::unique_ptr<base_value_type> pElem)
1214 // Container must own its elements.
1215 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1216 SG::throwExcNonowningContainer();
1218 // Ensure we're not being called via a base class.
1219 testInsert ("insert");
1220 value_type ptr = pElem.release();
1221 iterator ret (this->m_pCont.insert(position.base(), ptr), this);
1222 this->shift (ret - this->begin(), 1);
1223 this->moveAux (ret-this->begin(), ptr, false, true);
1229 * @brief Add a new element to the collection.
1230 * @param position Iterator before which the element will be added.
1231 * @param pElem The element to add to the collection.
1232 * @return An iterator that points to the inserted data.
1234 * The container's ownership policy will determine if it takes ownership
1235 * of the new element.
1237 * Note: this method may only be called using the most derived
1238 * @c DataVector in the hierarchy.
1240 * For @c DataVector, this is just the same as @c insert.
1241 * It's included just for interface compatibility with `std::vector`.
1243template <class T, class BASE>
1244typename DataVector<T, BASE>::iterator
1245DataVector<T, BASE>::emplace(iterator position, value_type pElem)
1247 return this->insert (position, pElem);
1252 * @brief Add a group of new elements to the collection.
1253 * @param position Iterator before which the element will be added.
1254 * @param first The start of the range to put in the container.
1255 * @param last The end of the range to put in the container.
1257 * The container's ownership policy will determine if it takes ownership
1258 * of the new element.
1260 * Note: this method may only be called using the most derived
1261 * @c DataVector in the hierarchy.
1263 * This overload is for the case where the iterator value type
1264 * is convertible to T*.
1266template <class T, class BASE>
1267template <class InputIterator,
1268 DataVector_detail::enable_if_ptr_itr<InputIterator, T> >
1269void DataVector<T, BASE>::insert(iterator position,
1270 InputIterator first,
1273 // Make sure that the input iterator can actually be converted
1274 // to a T*. Lets us give a compilation error for this:
1275 // DATAVECTOR_BASE(D, B);
1278 // d.insert (d.begin(), bb, bb+1);
1279 // which would otherwise compile.
1280 using ittype = typename std::iterator_traits<InputIterator>::value_type;
1281 static_assert (std::is_convertible_v<ittype, const T*>);
1283 // Ensure we're not being called via a base class.
1284 testInsert ("insert");
1285 size_t idx = position - this->begin();
1286 size_t old_sz = this->m_pCont.size();
1287 this->m_pCont.insert(position.base(), first, last);
1288 size_t n = this->m_pCont.size() - old_sz;
1289 this->shift (idx, n);
1290 this->moveAux (idx, first, last, false, true);
1295 * @brief Add a group of new elements to the collection.
1296 * @param position Iterator before which the element will be added.
1297 * @param first The start of the range to put in the container.
1298 * @param last The end of the range to put in the container.
1300 * The container's ownership policy will determine if it takes ownership
1301 * of the new element.
1303 * Note: this method may only be called using the most derived
1304 * @c DataVector in the hierarchy.
1306 * This overload is for the case where the iterator value type
1307 * is convertible to unique_ptr<T>.
1309template <class T, class BASE>
1310template <class InputIterator,
1311 DataVector_detail::enable_if_up_itr<InputIterator, T> >
1312void DataVector<T, BASE>::insert(iterator position,
1313 InputIterator first,
1316 // Make sure that the input iterator can actually be converted
1317 // to a T*. Lets us give a compilation error for this:
1318 // DATAVECTOR_BASE(D, B);
1321 // d.insert (d.begin(), bb, bb+1);
1322 // which would otherwise compile.
1323 using ittype = typename std::iterator_traits<InputIterator>::value_type;
1324 static_assert (std::is_convertible_v<typename ittype::pointer, const T*>);
1326 // Ensure we're not being called via a base class.
1327 testInsert ("insert");
1328 size_t idx = position - this->begin();
1329 size_t old_sz = this->m_pCont.size();
1330 using CxxUtils::releasing_iterator;
1331 this->m_pCont.insert(position.base(), releasing_iterator(first), releasing_iterator(last));
1332 size_t n = this->m_pCont.size() - old_sz;
1333 this->shift (idx, n);
1334 this->moveAux (idx, this->begin()+idx, this->begin()+idx+n);
1339 * @brief Add a group of new elements to the collection.
1340 * @param position Iterator before which the element will be added.
1341 * @param l An initializer list.
1343 * The container's ownership policy will determine if it takes ownership
1344 * of the new element.
1346 * Note: this method may only be called using the most derived
1347 * @c DataVector in the hierarchy.
1349template <class T, class BASE>
1350void DataVector<T, BASE>::insert(iterator position,
1351 std::initializer_list<value_type> l)
1353 insert (position, l.begin(), l.end());
1358 * @brief Insert the contents of another @c DataVector,
1359 * with auxiliary data copied via move semantics.
1360 * @param position Iterator before which the new elements will be added.
1361 * @param other The vector to add.
1363 * The ownership mode of this vector must be the same as @c other;
1364 * otherwise, an exception will be thrown.
1366 * If both vectors are view vectors, then this is the same
1367 * as <code> insert (position, other.begin(), other.end()) </code>.
1369 * Otherwise, the elements from @c other will be inserted into this vector.
1370 * This vector will take ownership of the elements, and the ownership
1371 * mode of @c other will be changed to @c VIEW_ELEMENTS.
1372 * Auxiliary data for these elements will be transferred,
1373 * using move semantics if possible. (Thus, the auxiliary store
1374 * for @c other may be modified and must not be locked.)
1375 * Finally, the auxiliary store pointer for @c other will be cleared
1376 * (but the store itself will not be deleted since it's not owned
1379 * Note: this method may only be called using the most derived
1380 * @c DataVector in the hierarchy.
1382template <class T, class BASE>
1384DataVector<T, BASE>::insertMove (iterator position, DataVector& other)
1386 if (this->m_ownPolicy != other.ownPolicy())
1387 throw SG::ExcInsertMoveOwnershipMismatch();
1389 if (this->m_ownPolicy == SG::VIEW_ELEMENTS) {
1390 this->insert (position, other.begin(), other.end());
1394 testInsert ("insertMove");
1395 size_t pos = position.base() - this->m_pCont.begin();
1396 this->m_pCont.insert (position.base(), other.begin(), other.end());
1397 this->setIndices (this->begin()+pos, this->end(), pos);
1398 other.m_ownPolicy = SG::VIEW_ELEMENTS;
1400 SG::IAuxStore* otherStore = other.getStore();
1402 SG::IAuxStore* store = this->getStore();
1404 if (!store->insertMove (pos, *otherStore))
1407 else if (this->hasStore())
1408 throw SG::ExcConstAuxData ("insertMove");
1409 other.setStore (static_cast<SG::IAuxStore*>(nullptr));
1411 else if (other.hasStore())
1412 throw SG::ExcConstAuxData ("insertMove");
1416//=== Erasure operations.
1420 * @brief Remove element at a given position.
1421 * @param position Iterator pointing to the element to be removed.
1422 * @return An iterator pointing to the next element (or @c end()).
1424 * If the container owns its elements, then the pointed-to element
1427template <class T, class BASE>
1428typename DataVector<T, BASE>::iterator
1429DataVector<T, BASE>::erase(iterator position)
1431 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1432 this->clearIndex (position);
1433 iterator ret (this->erase_base (position.base()), this);
1434 this->shift (ret - this->begin() + 1, -1);
1440 * @brief Remove a range of elements.
1441 * @param first Iterator pointing to the first element to be removed.
1442 * @param last Iterator pointing one past the last element to be removed.
1443 * @return An iterator pointing to the element pointed to by @a last
1444 * prior to erasing (or @c end()).
1446 * If the container owns its elements, then the removed elements
1447 * will be deleted. Any duplicates will be removed in this process,
1448 * but don't rely on this.
1450template <class T, class BASE>
1451typename DataVector<T, BASE>::iterator
1452DataVector<T, BASE>::erase(iterator first, iterator last)
1454 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1455 this->clearIndices (first, last);
1456 iterator ret (this->erase_base (first.base(), last.base()), this);
1457 this->shift (ret - this->begin() + (last-first), -(last-first));
1463 * @brief Remove the last element from the collection.
1465 * If the container owns its elements, then the removed element
1468template <class T, class BASE>
1469void DataVector<T, BASE>::pop_back()
1471 if (!this->m_pCont.empty()) {
1472 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
1473 this->doDelete (this->m_pCont.back());
1475 this->clearIndex (iterator (this->m_pCont.end() - 1, this));
1476 this->m_pCont.pop_back();
1477 SG::AuxVectorBase::resize<DataVector> (this->m_pCont.size());
1483 * @brief Erase all the elements in the collection.
1485 * If the container owns its elements, then the removed elements
1486 * will be deleted. Any duplicates will be removed in this process,
1487 * but don't rely on this.
1489template <class T, class BASE>
1491void DataVector<T, BASE>::clear()
1493 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1494 this->clearIndices (begin(), end());
1495 this->erase_base (this->m_pCont.begin(), this->m_pCont.end());
1496 SG::AuxVectorBase::resize<DataVector> (0);
1504 * @brief Swap this collection with another.
1505 * @param rhs The collection with which to swap.
1507 * Ownership is swapped along with the collection content.
1509 * Note: this method may only be called using the most-derived
1510 * @c DataVector in the hierarchy. The @a rhs must also be
1511 * referenced using the most-derived @c DataVector.
1513 * Warning: If this container has auxiliary data, then this
1514 * is an O(N) operation, not O(1).
1516template <class T, class BASE>
1517void DataVector<T, BASE>::swap(DataVector& rhs)
1519 testInsert ("swap");
1520 rhs.testInsert ("swap");
1521 std::swap(this->m_ownPolicy, rhs.m_ownPolicy);
1522 SG::AuxVectorBase::swap (rhs);
1523 this->m_pCont.swap(rhs.m_pCont);
1524 std::swap (this->m_deleter, rhs.m_deleter);
1525 this->setIndices (this->begin(), this->end());
1526 rhs.setIndices (rhs.begin(), rhs.end());
1531 * @brief Swap the referents of two @c DataVector iterators.
1532 * @param a The first iterator for the swap.
1533 * @param b The second iterator for the swap.
1535template <class T, class BASE>
1536void DataVector<T, BASE>::iter_swap (iterator a, iterator b)
1538 if (a.ownPolicy() != b.ownPolicy()) {
1539 throw SG::ExcBadIterSwap();
1541 a.testInsert ("iter_swap");
1542 b.testInsert ("iter_swap");
1543 std::iter_swap (a.base(), b.base());
1544 DataVector* acont = a.container();
1545 DataVector* bcont = b.container();
1546 if (typename SG::AuxStore_traits<DataVector>::flag())
1547 acont->swapElementsAux (a.base() - acont->stdcont().begin(),
1548 b.base() - bcont->stdcont().begin(),
1549 DataModel_detail::DVLCast<DataVector>::cast(*a.base()),
1550 DataModel_detail::DVLCast<DataVector>::cast(*b.base()),
1556 * @brief Sort the container.
1558 * This just sorts by pointer value, so it's probably not very useful.
1560template <class T, class BASE>
1561void DataVector<T, BASE>::sort()
1563 typedef std::less<typename PtrVector::value_type> less;
1564 std::sort (this->m_pCont.begin(), this->m_pCont.end(),
1565 DataModel_detail::Compwrapper<DataVector, less> (less()));
1566 this->resortAux (this->begin(), this->end());
1571 * @brief Sort the container with a user-specified comparison operator.
1572 * @param comp Functional to compare two values.
1574template <class T, class BASE>
1575template <class COMPARE>
1576void DataVector<T, BASE>::sort(COMPARE comp)
1578 std::sort (this->m_pCont.begin(), this->m_pCont.end(),
1579 DataModel_detail::Compwrapper<DataVector, COMPARE> (comp));
1580 this->resortAux (this->begin(), this->end());
1584//=== Non-standard operations.
1588 * @brief Swap one element out of the container.
1589 * @param index Index of the element in the container to swap.
1590 * @param newElement New element to put in the container.
1592 * @param oldElem Reference to receive the element removed from the
1595 * Reference @a oldElem is initialized with element @a index of the
1596 * collection (no bounds checking). Then element @a index is set
1597 * to @newElem. If the collection owns its elements, then it will
1598 * take ownership of @a newElem and release (without deleting)
1599 * the element returned through @a oldElem.
1601 * Note: this method may only be called using the most derived
1602 * @c DataVector in the hierarchy.
1604template <class T, class BASE>
1605void DataVector<T, BASE>::swapElement(size_type index,
1609 testInsert ("swapElement");
1611 DataModel_detail::DVLCast<DataVector>::cast(this->m_pCont[index]);
1612 this->clearIndex (iterator (this->m_pCont.begin() + index, this));
1613 this->m_pCont[index] = newElem;
1614 this->moveAux (index, newElem);
1619 * @brief Swap one element out of the container.
1620 * @param pos The element in the container to swap.
1621 * @param newElem New element to put in the container.
1623 * @param oldElem Reference to receive the element removed from the
1626 * Reference @a oldElem is initialized with element @a pos of the
1627 * collection (no bounds checking). Then element @a index is set
1628 * to @c newElem. If the collection owns its elements, then it will
1629 * take ownership of @a newElem and release (without deleting)
1630 * the element returned through @a oldElem.
1632 * Note: this method may only be called using the most derived
1633 * @c DataVector in the hierarchy.
1635template <class T, class BASE>
1636void DataVector<T, BASE>::swapElement(iterator pos,
1640 testInsert ("swapElement");
1642 DataModel_detail::DVLCast<DataVector>::cast(*pos.base());
1643 this->clearIndex (pos);
1644 *pos.base() = newElem;
1645 this->moveAux (pos.base() - this->m_pCont.begin(), newElem);
1650 * @brief Swap one element out of the container.
1651 * @param index Index of the element in the container to swap.
1652 * @param newElement New element to put in the container.
1654 * @param oldElem Reference to receive the element removed from the
1657 * Reference @a oldElem is initialized with element @a index of the
1658 * collection (no bounds checking). Then element @a index is set
1661 * The collection must own its elements to use its interface.
1662 * The collection will take ownership of @c newElem and will return
1663 * ownership of @c oldElem.
1665 * Note: this method may only be called using the most derived
1666 * @c DataVector in the hierarchy.
1668template <class T, class BASE>
1669void DataVector<T, BASE>::swapElement(size_type index,
1670 std::unique_ptr<base_value_type> newElem,
1671 std::unique_ptr<base_value_type>& oldElem)
1673 // Container must own its elements.
1674 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1675 SG::throwExcNonowningContainer();
1677 testInsert ("swapElement");
1678 oldElem = std::unique_ptr<base_value_type>
1679 (DataModel_detail::DVLCast<DataVector>::cast(this->m_pCont[index]));
1680 this->clearIndex (iterator (this->m_pCont.begin() + index, this));
1681 value_type ptr = newElem.release();
1682 this->m_pCont[index] = ptr;
1683 this->moveAux (index, ptr);
1688 * @brief Swap one element out of the container.
1689 * @param pos The element in the container to swap.
1690 * @param newElem New element to put in the container.
1692 * @param oldElem Reference to receive the element removed from the
1695 * Reference @a oldElem is initialized with element @a pos of the
1696 * collection (no bounds checking). Then element @a index is set
1699 * The collection must own its elements to use its interface.
1700 * The collection will take ownership of @c newElem and will return
1701 * ownership of @c oldElem.
1703 * Note: this method may only be called using the most derived
1704 * @c DataVector in the hierarchy.
1706template <class T, class BASE>
1707void DataVector<T, BASE>::swapElement(iterator pos,
1708 std::unique_ptr<base_value_type> newElem,
1709 std::unique_ptr<base_value_type>& oldElem)
1711 // Container must own its elements.
1712 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1713 SG::throwExcNonowningContainer();
1715 testInsert ("swapElement");
1716 oldElem = std::unique_ptr<base_value_type>
1717 (DataModel_detail::DVLCast<DataVector>::cast(*pos.base()));
1718 this->clearIndex (pos);
1719 value_type ptr = newElem.release();
1721 this->moveAux (pos.base() - this->m_pCont.begin(), ptr);
1726 * @brief Erase all the elements in the collection, and reset
1727 * the ownership mode.
1728 * @param ownPolicy The new ownership policy of the container.
1730 * If the container owns its elements, then the removed elements
1731 * will be deleted. Any duplicates will be removed in this process,
1732 * but don't rely on this.
1734template <class T, class BASE>
1735void DataVector<T, BASE>::clear (SG::OwnershipPolicy ownPolicy)
1738 this->m_ownPolicy = ownPolicy;
1739 this->template initAuxVectorBase<DataVector> (ownPolicy,
1740 SG::DEFAULT_TRACK_INDICES);
1745 * @brief Erase all the elements in the collection, and reset
1746 * the ownership mode.
1747 * @param ownPolicy The new ownership policy of the container.
1748 * @param trackIndices The index tracking policy.
1750 * If the container owns its elements, then the removed elements
1751 * will be deleted. Any duplicates will be removed in this process,
1752 * but don't rely on this.
1754template <class T, class BASE>
1755void DataVector<T, BASE>::clear (SG::OwnershipPolicy ownPolicy,
1756 SG::IndexTrackingPolicy trackIndices)
1759 this->m_ownPolicy = ownPolicy;
1760 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
1765 * @brief Return the DV/DL info struct for this class.
1767 * This can be used to make sure that it's instantiated.
1769template <class T, class BASE>
1770const DataModel_detail::DVLInfoBase& DataVector<T, BASE>::dvlinfo()
1772 static const DataModel_detail::DVLInfo<DataVector<T, BASE> > info;
1778 * @brief Erase all the elements in the collection, and change
1779 * how elements are to be deleted.
1780 * @param deleter Object to be used to delete object.
1781 * Passing nullptr will change back to the default.
1783 * If the container owns its elements, then the removed elements
1784 * will be deleted. Any duplicates will be removed in this process,
1785 * but don't rely on this.
1786 * After the current elements are deleted, the Deleter object is changed.
1788template <class T, class BASE>
1789void DataVector<T, BASE>::clear (std::unique_ptr<Deleter> deleter)
1792 delete this->m_deleter;
1793 this->m_deleter = deleter.release();
1798 * @brief Return the DV/DL info struct for this class.
1800 * This can be used to make sure that it's instantiated.
1802template <class T, class BASE>
1803const DataModel_detail::DVLInfoBase& DataVector<T, BASE>::dvlinfo_v() const
1805 return DataVector<T, BASE>::dvlinfo();
1810 * @brief Return the offset of a base @c DataVector class.
1811 * @param ti @c std::type_info of the desired class.
1813 * If @c ti represents a @c DataVector base class of this one,
1814 * then return the offset of that base class. Otherwise, return -1.
1816 * This function is here due to limitations of root 6, which can't
1817 * calculate these offsets correctly from the dictionary if
1818 * virtual derivation is used.
1820template <class T, class BASE>
1821int DataVector<T, BASE>::baseOffset (const std::type_info& ti)
1824 return baseOffset1 (reinterpret_cast<const char*>(&dv), dv, ti);
1829 * @brief Convert to @c AuxVectorBase.
1831 * Needed to get @x AuxVectorBase from a @c ConstDataVector.
1832 * Present in @c DataVector as well for consistency.
1833 * We only really need it in the base class; however, root6 fails
1834 * constructing a @c TMethodCall for this if there is virtual
1835 * derivation. A workaround is to redeclare this in the derived
1838template <class T, class BASE>
1839const SG::AuxVectorBase& DataVector<T, BASE>::auxbase() const
1845//=== Internal operations.
1849 * @brief Helper for @c baseOffset.
1850 * @param p Pointer to the start of the top-level object.
1851 * @param dv Reference to the DataVector object.
1852 * @param ti @c std::type_info of the desired class.
1854 * If @c ti represents a @c DataVector base class of this one,
1855 * then return the offset of that base class. Otherwise, return -1.
1858template <class T, class BASE>
1859int DataVector<T, BASE>::baseOffset1 (const char* p, const DataVector& dv,
1860 const std::type_info& ti)
1862 if (typeid(DataVector) == ti)
1863 return reinterpret_cast<const char*>(&dv) - p;
1864 return BASE::baseOffset1 (p, dv, ti);
1869 * @brief Reset indices / reorder aux data after elements have been permuted.
1870 * @param beg Start of the range of elements to process.
1871 * @param end End of the range of elements to process.
1873 * Call this after some operation that has permuted the elements in the
1874 * container (such as sort). The index information in the elements
1875 * will be used to permute all auxiliary data in the same way.
1876 * Finally, all the indices will be reset in the correct order.
1878template <class T, class BASE>
1879void DataVector<T, BASE>::resortAux (iterator beg, iterator end)
1881 if (typename SG::AuxStore_traits<DataVector>::flag() &&
1882 beg >= this->begin() && end <= this->end())
1884 SG::AuxVectorBase::resortAux (beg-begin(), beg, end);
1890 * @brief Test if we can insert; raise an exception if not.
1891 * @param op Description of the attempted operation.
1893 * In order to maintain type-safety, we can only allow insertions
1894 * using the most-derived instance of @c DataVector. This checks
1895 * this by testing the @c m_isMostDerived, which is set by the constructors
1896 * to true only for the most-derived instance.
1897 * If the test fails, we call to potentially out-of-line code to continue.
1899template <class T, class BASE>
1901void DataVector<T, BASE>::testInsert (const char* op)
1903 if (ATHCONTAINERS_LIKELY (m_isMostDerived))
1905 this->testInsertOol (op);
1910 * @brief Test if we can insert; raise an exception if not.
1911 * @param op Description of the attempted operation.
1913 * This continues the test of @c testInsert. There is one case
1914 * where @c m_isMostDerived may not be set correctly. If this container
1915 * was made via copy construction, then all the @c m_isMostDerived flags
1916 * will be false. So we call @c setMostDerived to set the flags correctly
1917 * and test again. If the test fails again, then we raise an exception.
1919template <class T, class BASE>
1920void DataVector<T, BASE>::testInsertOol (const char* op)
1922 this->setMostDerived();
1923 if (!m_isMostDerived)
1924 throw SG::ExcInsertionInBaseClass (op, typeid(DataVector), dv_typeid());
1929 * @brief Handle element assignment.
1930 * @param pos Position in the container to assign.
1931 * @param newElem The new element to assign.
1933 * The old element is freed if this container owns elements.
1934 * Auxiliary data are copied if appropriate.
1936template <class T, class BASE>
1937void DataVector<T, BASE>::assignElement (typename BaseContainer::iterator pos,
1940 testInsert ("assignElement");
1941 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
1942 this->doDelete (*pos);
1944 this->clearIndex (iterator (pos, this));
1946 this->moveAux (pos - this->m_pCont.begin(), newElem);
1951 * @brief Handle element assignment.
1952 * @param pos Position in the container to assign.
1953 * @param newElem The new element to assign.
1955 * The container must own its elements.
1956 * Auxiliary data are copied if appropriate.
1958template <class T, class BASE>
1960DataVector<T, BASE>::assignElement (typename BaseContainer::iterator pos,
1961 std::unique_ptr<base_value_type> newElem)
1963 // Container must own its elements.
1964 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1965 SG::throwExcNonowningContainer();
1967 testInsert ("assignElement");
1968 this->doDelete (*pos);
1969 value_type ptr = newElem.release();
1971 this->moveAux (pos - this->m_pCont.begin(), ptr);
1976 * @brief Handle element assignment from a base pointer.
1977 * @param pos Position in the container to assign.
1978 * @param newElem The new element to assign.
1980 * The old element is freed if this container owns elements.
1981 * Auxiliary data are copied if appropriate.
1983template <class T, class BASE>
1985DataVector<T, BASE>::assignBaseElement (typename BaseContainer::iterator pos,
1986 typename BaseContainer::value_type newElem)
1988 testInsert ("assignBaseElement");
1989 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
1990 this->doDelete (*pos);
1992 this->clearIndex (iterator (pos, this));
1994 if (typename SG::AuxStore_traits<DataVector>::flag())
1995 this->moveAux (pos - this->m_pCont.begin(),
1996 DataModel_detail::DVLCast<DataVector>::cast(newElem));
2001 * @brief Shift the auxiliary elements of the container.
2002 * @param pos The starting index for the shift.
2003 * @param offs The (signed) amount of the shift.
2005 * The elements in the container should have already been shifted;
2006 * this operation will then adjust the element indices and also shift
2007 * the elements in the vectors for all aux data items.
2008 * @c offs may be either positive or negative.
2010 * If @c offs is positive, then the container is growing.
2011 * The container size should be increased by @c offs,
2012 * the element at @c pos moved to @c pos + @c offs,
2013 * and similarly for following elements.
2014 * The elements between @c pos and @c pos + @c offs should
2015 * be default-initialized.
2017 * If @c offs is negative, then the container is shrinking.
2018 * The element at @c pos should be moved to @c pos + @c offs,
2019 * and similarly for following elements.
2020 * The container should then be shrunk by @c -offs elements
2021 * (running destructors as appropriate).
2023template <class T, class BASE>
2024void DataVector<T, BASE>::shift (size_t pos, ptrdiff_t offs)
2026 SG::AuxVectorBase::shift (*this, pos, offs);
2031 * @brief Helper to shorten calls to @c DataVector_detail::DVLCast.
2032 * @param p The value to convert.
2033 * @return The value as a @c const @c T*.
2035 * The conversion will be done with @c static_cast if possible,
2036 * with @c dynamic_cast otherwise.
2038template <class T, class BASE>
2041DataVector<T, BASE>::do_cast (const typename PtrVector::value_type p)
2043 return DataModel_detail::DVLCast<DataVector>::cast (p);
2048 * @brief Helper to shorten calls to @c DataVector_detail::DVLCast.
2049 * @param p The value to convert.
2050 * @return The value as a @c T*.
2052 * The conversion will be done with @c static_cast if possible,
2053 * with @c dynamic_cast otherwise.
2055template <class T, class BASE>
2058DataVector<T, BASE>::do_cast_nc (typename PtrVector::value_type p)
2060 return DataModel_detail::DVLCast<DataVector>::cast (p);
2065 * @brief Find the most-derived @c DataVector class in the hierarchy.
2066 * @return The @c type_info for the class for which this method gets run.
2068 * This is used to generate a nice error message when the most-derived
2069 * check for insertions fails.
2070 * Every @c DataVector defines this virtual method, so when it's
2071 * called, the one corresponding to the most-derived @c DataVector
2074template <class T, class BASE>
2075const std::type_info& DataVector<T, BASE>::dv_typeid() const
2077 return typeid(DataVector);
2082 * @brief Clear @c m_isMostDerived for this instance and for all bases.
2084 * Called from the constructor after setting @c m_isMostDerived.
2086template <class T, class BASE>
2088void DataVector<T, BASE>::clearMostDerived()
2090 this->m_isMostDerived = false;
2091 BASE::clearMostDerived();
2096 * @brief Set @c m_isMostDerived for this instance and clear it for all bases.
2098 * Called from @c testInsert if the test fails. The flag may not have
2099 * been set if this container was made via copy construction, so set
2100 * it appropriately now so we can test again.
2102template <class T, class BASE>
2103void DataVector<T, BASE>::setMostDerived()
2105 m_isMostDerived = true;
2106 BASE::clearMostDerived();
2110//****************************************************************************
2111// Specialized (base) DataVector implementation.
2115// An abbreviation for the DataVector specialization to try to make
2116// things a little more readable.
2117#define DATAVECTOR DataVector<T, DataModel_detail::NoBase>
2120//=== Constructors, destructors, assignment.
2123 * @brief Default constructor.
2124 * @param ownPolicy The ownership mode for the container.
2125 * @param trackIndices The index tracking policy.
2127 * By default, a @c DataVector will own its elements.
2128 * To avoid this, pass @c SG::VIEW_ELEMENTS for @a ownPolicy.
2132DATAVECTOR::DataVector
2133 (SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/,
2134 SG::IndexTrackingPolicy trackIndices /*=SG::DEFAULT_TRACK_INDICES*/)
2135 : m_ownPolicy(ownPolicy)
2137 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
2138 this->m_isMostDerived = true;
2143 * @brief Sized constructor.
2144 * @param n The size of the container.
2145 * @param ownPolicy The ownership mode for the container.
2146 * @param trackIndices The index tracking policy.
2148 * Note that unlike the standard vector constructor, you can't specify
2149 * an initial value here. The container will be initialized with 0's.
2151 * By default, a @c DataVector will own its elements.
2152 * To avoid this, pass @c SG::VIEW_ELEMENTS for @a ownPolicy.
2156DATAVECTOR::DataVector
2158 SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/,
2159 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/)
2160 : m_ownPolicy(ownPolicy),
2163 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
2164 this->m_isMostDerived = true;
2169 * @brief Copy constructor.
2170 * @param rhs The container from which to copy.
2172 * This is a `shallow' copy; the new container will not own its elements.
2176DATAVECTOR::DataVector(const DataVector& rhs)
2178 m_ownPolicy(SG::VIEW_ELEMENTS),
2179 m_pCont(rhs.m_pCont)
2181 this->template initAuxVectorBase<DataVector> (m_ownPolicy,
2182 SG::DEFAULT_TRACK_INDICES);
2183 // Leave m_isMostDerived false here, because we may be being called
2184 // from a derived class implicit copy constructor. The flags will get
2185 // set correctly when @c testInsert gets called.
2190 * @brief Move constructor.
2191 * @param rhs The container from which to move.
2193 * Any auxiliary data will be moved along with the container contents.
2197DATAVECTOR::DataVector(DataVector&& rhs) noexcept
2198 : AuxVectorBase (std::move (rhs)),
2199 m_ownPolicy(rhs.m_ownPolicy),
2200 m_pCont(std::move (rhs.m_pCont))
2202 m_deleter = std::move(rhs.m_deleter);
2203 rhs.m_deleter = nullptr;
2205 // Need to reset the container pointer on elements.
2206 // Functions called from here can throw GaudiException.
2207 // However, logically that should not actually happen; so leave this
2208 // declared as noexcept.
2209 this->setIndices (this->begin(), this->end());
2211 // This doesn't get called from derived classes.
2212 // Go ahead and set this flag now.
2213 m_isMostDerived = true;
2218 * @brief Constructor from iterators.
2219 * @param first The start of the range to put in the new container.
2220 * @param last The end of the range to put in the new container.
2221 * @param ownPolicy The ownership mode for the container.
2222 * @param trackIndices The index tracking policy.
2223 * @param store An associated auxiliary data store.
2225 * By default, a view container is made, which does not own its elements.
2226 * To have the container take ownership of the pointers passed
2227 * to this constructor, pass @c SG::OWN_ELEMENTS for @a ownPolicy.
2230template <class InputIterator>
2232DATAVECTOR::DataVector
2233 (InputIterator first,
2235 SG::OwnershipPolicy ownPolicy /*= SG::VIEW_ELEMENTS*/,
2236 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/,
2237 SG::IAuxStore* store /*= 0*/)
2238 : m_ownPolicy(ownPolicy)
2240 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
2241 this->m_isMostDerived = true;
2243 this->setStore (store);
2244 insert (this->begin(), first, last);
2249 * @brief Constructor from an initializer list.
2250 * @param l An initializer list.
2251 * @param last The end of the range to put in the new container.
2252 * @param ownPolicy The ownership mode for the container.
2253 * @param trackIndices The index tracking policy.
2254 * @param store An associated auxiliary data store.
2257 * A @c DataVector constructed this way will *not* own its elements
2258 * by default. To change this, pass @c SG::OWN_ELEMENTS for @a ownPolicy.
2262DATAVECTOR::DataVector
2263 (std::initializer_list<value_type> l,
2264 SG::OwnershipPolicy ownPolicy /*= SG::VIEW_ELEMENTS*/,
2265 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/,
2266 SG::IAuxStore* store /*= 0*/)
2267 : DataVector (l.begin(), l.end(), ownPolicy, trackIndices, store)
2273 * @brief Assignment operator.
2274 * @param rhs The DataVector from which to assign.
2275 * @return This object.
2277 * This is a `shallow' copy; after the completion of this, the @c DataVector
2278 * will not own its elements. Any elements it owned prior to this call
2281 * Note: this method may only be called using the most derived
2282 * @c DataVector in the hierarchy.
2286DATAVECTOR& DATAVECTOR::operator= (const DataVector& rhs)
2289 // Ensure we're not being called via a base class.
2290 // This can throw an exception, so this method should not be noexcept.
2291 testInsert ("assignment operator");
2292 clear(); // Release any currently-owned elements.
2293 m_ownPolicy = SG::VIEW_ELEMENTS;
2294 this->setStore ((SG::IConstAuxStore*)0);
2295 this->template initAuxVectorBase<DataVector> (m_ownPolicy,
2296 SG::DEFAULT_TRACK_INDICES);
2297 m_pCont = rhs.m_pCont;
2304 * @brief Move assignment.
2305 * @param rhs The container from which to move.
2307 * Any auxiliary data will be moved along with the container contents.
2310DATAVECTOR& DATAVECTOR::operator= (DATAVECTOR&& rhs)
2313 // Ensure we're not being called via a base class.
2314 // This can throw an exception, so this method should not be noexcept.
2315 testInsert ("assignment operator");
2317 this->clear(); // Release any currently-owned elements.
2319 SG::AuxVectorBase::operator= (std::move (rhs));
2320 this->m_ownPolicy = rhs.m_ownPolicy;
2321 this->m_pCont = std::move (rhs.m_pCont);
2323 delete this->m_deleter;
2324 this->m_deleter = std::move(rhs.m_deleter);
2325 rhs.m_deleter = nullptr;
2327 // Need to reset the container pointer on elements.
2328 this->setIndices (this->begin(), this->end());
2335 * @brief Assignment operator, from an initializer list.
2336 * @param l An initializer list.
2337 * @return This object.
2339 * This is equivalent to @c assign.
2340 * Any existing owned elements will be released.
2341 * The @c DataVector's ownership policy determines whether it will take
2342 * ownership of the new elements.
2346DATAVECTOR& DATAVECTOR::operator= (std::initializer_list<value_type> l)
2348 this->assign (l.begin(), l.end());
2354 * @brief Assign from iterators.
2355 * @param first The start of the range to put in the container.
2356 * @param last The end of the range to put in the container.
2358 * Any existing owned elements will be released.
2359 * The @c DataVector's ownership policy determines whether it will take
2360 * ownership of the new elements.
2363template <class InputIterator>
2364void DATAVECTOR::assign (InputIterator first,
2367 // Ensure we're not being called via a base class.
2368 testInsert ("assign");
2369 clear(); // Release any currently-owned elements.
2370 insert(begin(), first, last);
2375 * @brief Assign from an initializer list.
2376 * @param l An initializer list.
2378 * Any existing owned elements will be released.
2379 * The @c DataVector's ownership policy determines whether it will take
2380 * ownership of the new elements.
2383void DATAVECTOR::assign (std::initializer_list<value_type> l)
2385 this->assign (l.begin(), l.end());
2390 * @brief Destructor.
2392 * If this container owns its elements, the contained elements will
2393 * be deleted as well. Before doing this, the destructor will scan
2394 * for duplicate pointers (takes @f$n \log n@f$ time); duplicates are only
2395 * destroyed once. Duplicates should, however, be considered an error;
2396 * don't rely on this behavior.
2399DATAVECTOR::~DataVector()
2401 if (m_ownPolicy == SG::OWN_ELEMENTS) {
2402 typename PtrVector::iterator new_end =
2403 DataVector_detail::remove_duplicates(m_pCont.begin(), m_pCont.end());
2404 this->doDelete (m_pCont.begin(), new_end);
2410//=== Size and capacity.
2414 * @brief Returns the number of elements in the collection.
2418typename DATAVECTOR::size_type DATAVECTOR::size() const noexcept
2420 return m_pCont.size();
2425 * @brief Returns the number of elements in the collection.
2427 * This version is virtual, to be callable from the AuxData
2432typename DATAVECTOR::size_type DATAVECTOR::size_v() const
2434 return this->size();
2439 * @brief Returns the @c size() of the largest possible collection.
2443typename DATAVECTOR::size_type DATAVECTOR::max_size() const noexcept
2445 return m_pCont.max_size();
2450 * @brief Resizes the collection to the specified number of elements.
2451 * @param sz The new size of the collection.
2453 * Note that this function differs from the standard in that it does
2454 * not allow specifying the value of any inserted elements.
2455 * They will always be 0.
2457 * If the container is shrunk, elements will be deleted as with @c erase().
2460void DATAVECTOR::resize(size_type sz)
2462 if (sz < this->size()) {
2463 this->erase (this->begin()+sz, this->end());
2465 this->m_pCont.insert(this->m_pCont.end(), sz - this->m_pCont.size(), 0);
2466 SG::AuxVectorBase::resize<DataVector> (sz);
2472 * @brief Returns the total number of elements that the collection can hold
2473 * before needing to allocate more memory.
2477typename DATAVECTOR::size_type DATAVECTOR::capacity() const noexcept
2479 return m_pCont.capacity();
2484 * @brief Returns the total number of elements that the collection can hold
2485 * before needing to allocate more memory.
2487 * This version is virtual, to be callable from the AuxData
2492typename DATAVECTOR::size_type DATAVECTOR::capacity_v() const
2499 * @brief Returns @c true if the collection is empty.
2503bool DATAVECTOR::empty() const noexcept
2505 return m_pCont.empty();
2510 * @brief Attempt to preallocate enough memory for a specified number
2512 * @param n Number of elements required.
2516void DATAVECTOR::reserve (size_type n)
2518 m_pCont.reserve (n);
2519 SG::AuxVectorBase::reserve<DataVector> (n);
2524 * @brief Change the vector capacity to match the current size.
2526 * Note: this does not affect auxiliary data.
2530void DATAVECTOR::shrink_to_fit()
2532 m_pCont.shrink_to_fit();
2536//=== Element access.
2540 * @brief Access an element, as an rvalue.
2541 * @param n Array index to access.
2542 * @return The element at @a n.
2544 * No bounds checking is done.
2545 * Note that we return a @c const @c T* rather than a reference.
2549const T* DATAVECTOR::operator[] (size_type n) const
2556 * @brief Access an element, as an rvalue.
2557 * @param n Array index to access.
2558 * @return The element at @a n.
2560 * This is a synonym for operator[] const, to be used when calling from root
2561 * (where we can't readily call just the const version of a method).
2565const T* DATAVECTOR::get (size_type n) const
2567 return do_cast(this->m_pCont[n]);
2572 * @brief Access an element, as an lvalue.
2573 * @param n Array index to access.
2574 * @return Proxy to the element at @a n.
2576 * No bounds checking is done.
2577 * Note that we return a proxy object rather than a reference;
2578 * the proxy will handle deleting an owned element if it's assigned to.
2582typename DATAVECTOR::ElementProxy DATAVECTOR::operator[] (size_type n)
2584 return ElementProxy (m_pCont.begin() + n, this);
2589 * @brief Access an element, as an rvalue.
2590 * @param n Array index to access.
2591 * @return The element at @a n.
2593 * Will raise @c std::out_of_range if the index is out-of-bounds.
2594 * Note that we return a @c const @c T* rather than a reference.
2598const T* DATAVECTOR::at (size_type n) const
2600 return m_pCont.at(n);
2605 * @brief Access an element, as an lvalue.
2606 * @param n Array index to access.
2607 * @return Proxy to the element at @a n.
2609 * Will raise @c std::out_of_range if the index is out-of-bounds.
2610 * Note that we return a proxy object rather than a reference;
2611 * the proxy will handle deleting an owned element if it's assigned to.
2615typename DATAVECTOR::ElementProxy DATAVECTOR::at (size_type n)
2617 // Can't use m_pCont's at here, because we need an iterator.
2618 // So we have to do the bounds check ourselves.
2620 CxxUtils::throw_out_of_range (__PRETTY_FUNCTION__, n, this->size(), this);
2621 return ElementProxy (m_pCont.begin() + n, this);
2626 * @brief Access the first element in the collection as an rvalue.
2627 * @return The first element in the collection.
2629 * No checking is done to ensure that the container is not empty.
2630 * Note that we return a @c const @c T* rather than a reference.
2634const T* DATAVECTOR::front() const
2636 return m_pCont.front();
2641 * @brief Access the last element in the collection as an rvalue.
2642 * @return The last element in the collection.
2644 * No checking is done to ensure that the container is not empty.
2645 * Note that we return a @c const @c T* rather than a reference.
2649const T* DATAVECTOR::back() const
2651 return m_pCont.back();
2656 * @brief Access the first element in the collection as an lvalue.
2657 * @return Proxy to the first element in the collection.
2659 * No checking is done to ensure that the container is not empty.
2660 * Note that we return a proxy object rather than a reference;
2661 * the proxy will handle deleting an owned element if it's assigned to.
2665typename DATAVECTOR::ElementProxy DATAVECTOR::front ()
2667 return ElementProxy (m_pCont.begin(), this);
2672 * @brief Access the last element in the collection as an lvalue.
2673 * @return Proxy to the last element in the collection.
2675 * No checking is done to ensure that the container is not empty.
2676 * Note that we return a proxy object rather than a reference;
2677 * the proxy will handle deleting an owned element if it's assigned to.
2681typename DATAVECTOR::ElementProxy DATAVECTOR::back ()
2683 return ElementProxy (m_pCont.end()-1, this);
2687//=== Iterator creation.
2691 * @brief Return a @c const_iterator pointing at the beginning
2692 * of the collection.
2693 * @return A @c const_iterator.
2695 * Note that dereferencing the iterator will yield a @c const @c T* rather
2700typename DATAVECTOR::const_iterator DATAVECTOR::begin() const noexcept
2702 return m_pCont.begin();
2707 * @brief Return a @c const_iterator pointing past the end
2708 * of the collection.
2709 * @return A @c const_iterator.
2711 * Note that dereferencing the iterator will yield a @c const @c T* rather
2716typename DATAVECTOR::const_iterator DATAVECTOR::end() const noexcept
2718 return m_pCont.end();
2723 * @brief Return an @c iterator pointing at the beginning
2724 * of the collection.
2725 * @return An @c iterator.
2727 * Note that dereferencing the iterator will yield a proxy rather
2728 * than a reference; the proxy will handle deleting an owned element
2729 * if it's assigned to.
2733typename DATAVECTOR::iterator DATAVECTOR::begin() noexcept
2735 return iterator (m_pCont.begin(), this);
2740 * @brief Return an @c iterator pointing past the end
2741 * of the collection.
2742 * @return An @c iterator.
2744 * Note that dereferencing the iterator will yield a proxy rather
2745 * than a reference; the proxy will handle deleting an owned element
2746 * if it's assigned to.
2750typename DATAVECTOR::iterator DATAVECTOR::end() noexcept
2752 return iterator (m_pCont.end(), this);
2757 * @brief Return a @c const_reverse_iterator pointing past the end
2758 * of the collection.
2759 * @return A @c const_reverse_iterator.
2761 * Note that dereferencing the iterator will yield a @c const @c T* rather
2766typename DATAVECTOR::const_reverse_iterator DATAVECTOR::rbegin() const noexcept
2768 return const_reverse_iterator (m_pCont.end());
2773 * @brief Return a @c const_reverse_iterator pointing at the beginning
2774 * of the collection.
2775 * @return A @c const_reverse_iterator.
2777 * Note that dereferencing the iterator will yield a @c const @c T* rather
2782typename DATAVECTOR::const_reverse_iterator DATAVECTOR::rend() const noexcept
2784 return const_reverse_iterator (const_iterator (m_pCont.begin()));
2789 * @brief Return a @c reverse_iterator pointing past the end
2790 * of the collection.
2791 * @return A @c reverse_iterator.
2793 * Note that dereferencing the iterator will yield a proxy rather
2794 * than a reference; the proxy will handle deleting an owned element
2795 * if it's assigned to.
2799typename DATAVECTOR::reverse_iterator DATAVECTOR::rbegin() noexcept
2801 return reverse_iterator(iterator (m_pCont.end(), this));
2806 * @brief Return a @c reverse_iterator pointing at the beginning
2807 * of the collection.
2808 * @return A @c reverse_iterator.
2810 * Note that dereferencing the iterator will yield a proxy rather
2811 * than a reference; the proxy will handle deleting an owned element
2812 * if it's assigned to.
2816typename DATAVECTOR::reverse_iterator DATAVECTOR::rend() noexcept
2818 return reverse_iterator(iterator (m_pCont.begin(), this));
2823 * @brief Return a @c const_iterator pointing at the beginning
2824 * of the collection.
2825 * @return A @c const_iterator.
2827 * Note that dereferencing the iterator will yield a @c const @c T* rather
2832typename DATAVECTOR::const_iterator DATAVECTOR::cbegin() const noexcept
2834 return m_pCont.begin();
2839 * @brief Return a @c const_iterator pointing past the end
2840 * of the collection.
2841 * @return A @c const_iterator.
2843 * Note that dereferencing the iterator will yield a @c const @c T* rather
2848typename DATAVECTOR::const_iterator DATAVECTOR::cend() const noexcept
2850 return m_pCont.end();
2855 * @brief Return a @c const_reverse_iterator pointing past the end
2856 * of the collection.
2857 * @return A @c const_reverse_iterator.
2859 * Note that dereferencing the iterator will yield a @c const @c T* rather
2864typename DATAVECTOR::const_reverse_iterator DATAVECTOR::crbegin() const noexcept
2866 return const_reverse_iterator (m_pCont.end());
2871 * @brief Return a @c const_reverse_iterator pointing at the beginning
2872 * of the collection.
2873 * @return A @c const_reverse_iterator.
2875 * Note that dereferencing the iterator will yield a @c const @c T* rather
2880typename DATAVECTOR::const_reverse_iterator DATAVECTOR::crend() const noexcept
2882 return const_reverse_iterator (const_iterator (m_pCont.begin()));
2886//=== Insertion operations.
2890 * @brief Add an element to the end of the collection.
2891 * @param pElem The element to add to the collection.
2893 * The container's ownership policy will determine if it takes ownership
2894 * of the new element.
2896 * Note: this method may only be called using the most derived
2897 * @c DataVector in the hierarchy.
2899 * Returns the pushed pointer.
2903typename DATAVECTOR::value_type
2904DATAVECTOR::push_back(value_type pElem)
2906 // Ensure we're not being called via a base class.
2907 testInsert ("push_back");
2908 m_pCont.push_back(pElem);
2909 SG::AuxVectorBase::resize<DataVector> (this->size());
2911 this->moveAux (this->size()-1, pElem, false, true);
2917 * @brief Add an element to the end of the collection.
2918 * @param pElem The element to add to the collection.
2920 * The container must be an owning container.
2922 * Note: this method may only be called using the most derived
2923 * @c DataVector in the hierarchy.
2925 * Returns the pushed pointer.
2929typename DATAVECTOR::value_type
2930DATAVECTOR::push_back(std::unique_ptr<base_value_type> pElem)
2932 // Container must own its elements.
2933 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
2934 SG::throwExcNonowningContainer();
2936 // Ensure we're not being called via a base class.
2937 testInsert ("push_back");
2938 value_type ptr = pElem.release();
2939 m_pCont.push_back(ptr);
2940 SG::AuxVectorBase::resize<DataVector> (this->size());
2942 this->moveAux (this->size()-1, ptr, false, true);
2948 * @brief Create and add a number of new elements to the end of the container.
2949 * @param n The number of new elements to add.
2950 * @param alloc Functional to call to allocate a new element to push.
2951 * Should be callable like <code>T* = alloc();</code>
2954 * dv.push_new (n, [](){ return new Foo; });
2956 * It may also be useful to allocate from a @c DataPool.
2958 * Note: this method may only be called using the most derived
2959 * @c DataVector in the hierarchy.
2961 * Returns the original size of the vector.
2964template <CxxUtils::detail::AllocationFunction<T> F>
2966DATAVECTOR::push_new(size_type n, F alloc) -> size_type
2968 // Ensure we're not being called via a base class.
2969 testInsert ("push_new");
2971 size_t sz = this->m_pCont.size();
2972 this->m_pCont.resize (sz + n);
2973 SG::AuxVectorBase::resize<DataVector> (this->size());
2974 for (size_t i = 0; i < n; i++) {
2976 this->m_pCont[sz+i] = p;
2977 this->moveAux (sz+i, p, false, true);
2984 * @brief Add an element to the end of the collection.
2985 * @param pElem The element to add to the collection.
2987 * The container's ownership policy will determine if it takes ownership
2988 * of the new element.
2990 * Note: this method may only be called using the most derived
2991 * @c DataVector in the hierarchy.
2993 * For @c DataVector, this is like the same as @c push_back, and
2994 * it returns the pushed element.
2995 * It's included just for interface compatibility with `std::vector`.
2999typename DATAVECTOR::value_type
3000DATAVECTOR::emplace_back(value_type pElem)
3002 return this->push_back (pElem);
3007 * @brief Add a new element to the collection.
3008 * @param position Iterator before which the element will be added.
3009 * @param pElem The element to add to the collection.
3010 * @return An iterator that points to the inserted data.
3012 * The container's ownership policy will determine if it takes ownership
3013 * of the new element.
3015 * Note: this method may only be called using the most derived
3016 * @c DataVector in the hierarchy.
3020typename DATAVECTOR::iterator
3021DATAVECTOR::insert(iterator position, value_type pElem)
3023 // Ensure we're not being called via a base class.
3024 testInsert ("insert");
3025 iterator ret (m_pCont.insert(position.base(), pElem), this);
3026 this->shift (ret - this->begin(), 1);
3027 this->moveAux (ret-this->begin(), pElem, false, true);
3033 * @brief Add a new element to the collection.
3034 * @param position Iterator before which the element will be added.
3035 * @param pElem The element to add to the collection.
3036 * @return An iterator that points to the inserted data.
3038 * The container must be an owning container.
3040 * Note: this method may only be called using the most derived
3041 * @c DataVector in the hierarchy.
3045typename DATAVECTOR::iterator
3046DATAVECTOR::insert(iterator position, std::unique_ptr<base_value_type> pElem)
3048 // Container must own its elements.
3049 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3050 SG::throwExcNonowningContainer();
3052 // Ensure we're not being called via a base class.
3053 testInsert ("insert");
3054 value_type ptr = pElem.release();
3055 iterator ret (m_pCont.insert(position.base(), ptr), this);
3056 this->shift (ret - this->begin(), 1);
3057 this->moveAux (ret-this->begin(), ptr, false, true);
3063 * @brief Add a new element to the collection.
3064 * @param position Iterator before which the element will be added.
3065 * @param pElem The element to add to the collection.
3066 * @return An iterator that points to the inserted data.
3068 * The container's ownership policy will determine if it takes ownership
3069 * of the new element.
3071 * Note: this method may only be called using the most derived
3072 * @c DataVector in the hierarchy.
3074 * For @c DataVector, this is just the same as @c insert.
3075 * It's included just for interface compatibility with `std::vector`.
3079typename DATAVECTOR::iterator
3080DATAVECTOR::emplace(iterator position, value_type pElem)
3082 return this->insert (position, pElem);
3087 * @brief Add a group of new elements to the collection.
3088 * @param position Iterator before which the element will be added.
3089 * @param first The start of the range to put in the container.
3090 * @param last The end of the range to put in the container.
3092 * The container's ownership policy will determine if it takes ownership
3093 * of the new element.
3095 * Note: this method may only be called using the most derived
3096 * @c DataVector in the hierarchy.
3098 * This overload is for the case where the iterator value type
3099 * is convertible to T*.
3102template <class InputIterator,
3103 DataVector_detail::enable_if_ptr_itr<InputIterator, T> >
3106DATAVECTOR::insert (iterator position, InputIterator first, InputIterator last)
3108 static_assert (std::is_same_v<T*, typename DATAVECTOR::value_type>);
3109 // Ensure we're not being called via a base class.
3110 testInsert ("insert");
3111 size_t idx = position - this->begin();
3112 size_t old_sz = this->m_pCont.size();
3113 m_pCont.insert(position.base(), first, last);
3114 size_t n = this->m_pCont.size() - old_sz;
3115 this->shift (idx, n);
3116 this->moveAux (idx, first, last, false, true);
3121 * @brief Add a group of new elements to the collection.
3122 * @param position Iterator before which the element will be added.
3123 * @param first The start of the range to put in the container.
3124 * @param last The end of the range to put in the container.
3126 * The container's ownership policy will determine if it takes ownership
3127 * of the new element.
3129 * Note: this method may only be called using the most derived
3130 * @c DataVector in the hierarchy.
3132 * This overload is for the case where the iterator value type
3133 * is convertible to unique_ptr<T>.
3136template <class InputIterator,
3137 DataVector_detail::enable_if_up_itr<InputIterator, T> >
3140DATAVECTOR::insert (iterator position, InputIterator first, InputIterator last)
3142 // Ensure we're not being called via a base class.
3143 testInsert ("insert");
3144 size_t idx = position - this->begin();
3145 size_t old_sz = this->m_pCont.size();
3146 using CxxUtils::releasing_iterator;
3147 m_pCont.insert(position.base(), releasing_iterator(first), releasing_iterator(last));
3148 size_t n = this->m_pCont.size() - old_sz;
3149 this->shift (idx, n);
3150 this->moveAux (idx, this->begin()+idx, this->begin()+idx+n);
3155 * @brief Add a group of new elements to the collection.
3156 * @param position Iterator before which the element will be added.
3157 * @param l An initializer list.
3159 * The container's ownership policy will determine if it takes ownership
3160 * of the new element.
3162 * Note: this method may only be called using the most derived
3163 * @c DataVector in the hierarchy.
3168DATAVECTOR::insert (iterator position, std::initializer_list<value_type> l)
3170 this->insert (position, l.begin(), l.end());
3175 * @brief Insert the contents of another @c DataVector,
3176 * with auxiliary data copied via move semantics.
3177 * @param position Iterator before which the new elements will be added.
3178 * @param other The vector to add.
3180 * The ownership mode of this vector must be the same as @c other;
3181 * otherwise, an exception will be thrown.
3183 * If both vectors are view vectors, then this is the same
3184 * as <code> insert (position, other.begin(), other.end()) </code>.
3186 * Otherwise, the elements from @c other will be inserted into this vector.
3187 * This vector will take ownership of the elements, and the ownership
3188 * mode of @c other will be changed to @c VIEW_ELEMENTS.
3189 * Auxiliary data for these elements will be transferred,
3190 * using move semantics if possible. (Thus, the auxiliary store
3191 * for @c other may be modified and must not be locked.)
3192 * Finally, the auxiliary store pointer for @c other will be cleared
3193 * (but the store itself will not be deleted since it's not owned
3196 * Note: this method may only be called using the most derived
3197 * @c DataVector in the hierarchy.
3201DATAVECTOR::insertMove (iterator position, DataVector& other)
3203 if (this->m_ownPolicy != other.ownPolicy())
3204 throw SG::ExcInsertMoveOwnershipMismatch();
3206 if (this->m_ownPolicy == SG::VIEW_ELEMENTS) {
3207 this->insert (position, other.begin(), other.end());
3211 testInsert ("insertMove");
3212 size_t pos = position.base() - this->m_pCont.begin();
3213 this->m_pCont.insert (position.base(), other.begin(), other.end());
3214 this->setIndices (this->begin()+pos, this->end(), pos);
3215 other.m_ownPolicy = SG::VIEW_ELEMENTS;
3217 SG::IAuxStore* otherStore = other.getStore();
3219 SG::IAuxStore* store = this->getStore();
3221 if (!store->insertMove (pos, *otherStore))
3224 else if (this->hasStore())
3225 throw SG::ExcConstAuxData ("insertMove");
3226 other.setStore (static_cast<SG::IAuxStore*>(nullptr));
3228 else if (other.hasStore())
3229 throw SG::ExcConstAuxData ("insertMove");
3233//=== Erasure operations.
3237 * @brief Remove element at a given position.
3238 * @param position Iterator pointing to the element to be removed.
3239 * @return An iterator pointing to the next element (or @c end()).
3241 * If the container owns its elements, then the pointed-to element
3246typename DATAVECTOR::iterator DATAVECTOR::erase(iterator position)
3248 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3249 this->clearIndex (position);
3250 iterator ret (this->erase_base (position.base()), this);
3251 this->shift (ret - this->begin() + 1, -1);
3257 * @brief Remove a range of elements.
3258 * @param first Iterator pointing to the first element to be removed.
3259 * @param last Iterator pointing one past the last element to be removed.
3260 * @return An iterator pointing to the element pointed to by @a last
3261 * prior to erasing (or @c end()).
3263 * If the container owns its elements, then the removed elements
3264 * will be deleted. Any duplicates will be removed in this process,
3265 * but don't rely on this.
3269typename DATAVECTOR::iterator DATAVECTOR::erase(iterator first, iterator last)
3271 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3272 this->clearIndices (first, last);
3273 iterator ret (this->erase_base (first.base(), last.base()), this);
3274 this->shift (ret - this->begin() + (last-first), -(last-first));
3280 * @brief Remove the last element from the collection.
3282 * If the container owns its elements, then the removed element
3286void DATAVECTOR::pop_back()
3288 if (!m_pCont.empty()) {
3289 if (m_ownPolicy == SG::OWN_ELEMENTS)
3290 this->doDelete (m_pCont.back());
3292 this->clearIndex (m_pCont.end() - 1);
3294 SG::AuxVectorBase::resize<DataVector> (this->m_pCont.size());
3300 * @brief Erase all the elements in the collection.
3302 * If the container owns its elements, then the removed elements
3303 * will be deleted. Any duplicates will be removed in this process,
3304 * but don't rely on this.
3308void DATAVECTOR::clear()
3310 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3311 this->clearIndices (begin(), end());
3312 this->erase_base (m_pCont.begin(), m_pCont.end());
3313 SG::AuxVectorBase::resize<DataVector> (0);
3321 * @brief Swap this collection with another.
3322 * @param rhs The collection with which to swap.
3324 * Ownership is swapped along with the collection content.
3326 * Note: this method may only be called using the most-derived
3327 * @c DataVector in the hierarchy. The @a rhs must also be
3328 * referenced using the most-derived @c DataVector.
3330 * Warning: If this container has auxiliary data, then this
3331 * is an O(N) operation, not O(1).
3334void DATAVECTOR::swap(DataVector& rhs)
3336 testInsert ("swap");
3337 rhs.testInsert ("swap");
3338 std::swap(m_ownPolicy, rhs.m_ownPolicy);
3339 SG::AuxVectorBase::swap (rhs);
3340 m_pCont.swap(rhs.m_pCont);
3341 std::swap (this->m_deleter, rhs.m_deleter);
3342 this->setIndices (this->begin(), this->end());
3343 rhs.setIndices (rhs.begin(), rhs.end());
3348 * @brief Swap the referents of two @c DataVector iterators.
3349 * @param a The first iterator for the swap.
3350 * @param b The second iterator for the swap.
3353void DATAVECTOR::iter_swap (iterator a, iterator b)
3355 if (a.ownPolicy() != b.ownPolicy()) {
3356 throw SG::ExcBadIterSwap();
3358 a.testInsert ("iter_swap");
3359 b.testInsert ("iter_swap");
3360 std::iter_swap (a.base(), b.base());
3361 DataVector* acont = a.container();
3362 DataVector* bcont = b.container();
3363 if (typename SG::AuxStore_traits<DataVector>::flag())
3364 acont->swapElementsAux (a.base() - acont->stdcont().begin(),
3365 b.base() - bcont->stdcont().begin(),
3373 * @brief Sort the container.
3375 * This just sorts by pointer value, so it's probably not very useful.
3378void DATAVECTOR::sort()
3380 std::sort(m_pCont.begin(), m_pCont.end());
3381 this->resortAux (this->begin(), this->end());
3386 * @brief Sort the container with a user-specified comparison operator.
3387 * @param comp Functional to compare two values.
3390template <class COMPARE>
3391void DATAVECTOR::sort(COMPARE comp)
3393 std::sort(m_pCont.begin(), m_pCont.end(), comp);
3394 this->resortAux (this->begin(), this->end());
3398//=== Non-standard operations.
3402 * @brief Swap one element out of the container.
3403 * @param index Index of the element in the container to swap.
3404 * @param newElement New element to put in the container.
3406 * @param oldElem Reference to receive the element removed from the
3409 * Reference @a oldElem is initialized with element @a index of the
3410 * collection (no bounds checking). Then element @a index is set
3411 * to @newElem. If the collection owns its elements, then it will
3412 * take ownership of @a newElem and release (without deleting)
3413 * the element returned through @a oldElem.
3415 * Note: this method may only be called using the most derived
3416 * @c DataVector in the hierarchy.
3420DATAVECTOR::swapElement (size_type index,
3424 testInsert ("swapElement");
3425 oldElem = m_pCont[index];
3426 this->clearIndex (iterator (m_pCont.begin() + index, this));
3427 m_pCont[index] = newElem;
3428 this->moveAux (index, newElem);
3433 * @brief Swap one element out of the container.
3434 * @param pos The element in the container to swap.
3435 * @param newElem New element to put in the container.
3437 * @param oldElem Reference to receive the element removed from the
3440 * Reference @a oldElem is initialized with element @a pos of the
3441 * collection (no bounds checking). Then element @a index is set
3442 * to @c newElem. If the collection owns its elements, then it will
3443 * take ownership of @a newElem and release (without deleting)
3444 * the element returned through @a oldElem.
3446 * Note: this method may only be called using the most derived
3447 * @c DataVector in the hierarchy.
3451DATAVECTOR::swapElement (iterator pos,
3455 testInsert ("swapElement");
3456 oldElem = *pos.base();
3457 this->clearIndex (pos);
3458 *pos.base() = newElem;
3459 this->moveAux (pos.base() - this->m_pCont.begin(), newElem);
3464 * @brief Swap one element out of the container.
3465 * @param index Index of the element in the container to swap.
3466 * @param newElement New element to put in the container.
3468 * @param oldElem Reference to receive the element removed from the
3471 * Reference @a oldElem is initialized with element @a index of the
3472 * collection (no bounds checking). Then element @a index is set
3475 * The collection must own its elements to use its interface.
3476 * The collection will take ownership of @c newElem and will return
3477 * ownership of @c oldElem.
3479 * Note: this method may only be called using the most derived
3480 * @c DataVector in the hierarchy.
3484DATAVECTOR::swapElement (size_type index,
3485 std::unique_ptr<base_value_type> newElem,
3486 std::unique_ptr<base_value_type>& oldElem)
3488 // Container must own its elements.
3489 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3490 SG::throwExcNonowningContainer();
3492 testInsert ("swapElement");
3493 oldElem = std::unique_ptr<base_value_type> (m_pCont[index]);
3494 this->clearIndex (iterator (m_pCont.begin() + index, this));
3495 value_type ptr = newElem.release();
3496 m_pCont[index] = ptr;
3497 this->moveAux (index, ptr);
3502 * @brief Swap one element out of the container.
3503 * @param pos The element in the container to swap.
3504 * @param newElem New element to put in the container.
3506 * @param oldElem Reference to receive the element removed from the
3509 * Reference @a oldElem is initialized with element @a pos of the
3510 * collection (no bounds checking). Then element @a index is set
3513 * The collection must own its elements to use its interface.
3514 * The collection will take ownership of @c newElem and will return
3515 * ownership of @c oldElem.
3517 * Note: this method may only be called using the most derived
3518 * @c DataVector in the hierarchy.
3522DATAVECTOR::swapElement (iterator pos,
3523 std::unique_ptr<base_value_type> newElem,
3524 std::unique_ptr<base_value_type>& oldElem)
3526 // Container must own its elements.
3527 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3528 SG::throwExcNonowningContainer();
3530 testInsert ("swapElement");
3531 oldElem = std::unique_ptr<base_value_type> (*pos.base());
3532 this->clearIndex (pos);
3533 value_type ptr = newElem.release();
3535 this->moveAux (pos.base() - this->m_pCont.begin(), ptr);
3540 * @brief Return the underlying @c std::vector of the container.
3541 * @return Reference to the @c std::vector actually holding the collection.
3543 * Note that @c DataVector<T>::stdcont does not necessarily return
3544 * a @c std::vector<T*> if @c DataVector inheritance is being used.
3548const typename DATAVECTOR::PtrVector& DATAVECTOR::stdcont() const
3555 * @brief Return the ownership policy setting for this container.
3559SG::OwnershipPolicy DATAVECTOR::ownPolicy() const
3566 * @brief Erase all the elements in the collection, and reset
3567 * the ownership mode.
3568 * @param ownPolicy The new ownership policy of the container.
3569 * @param trackIndices The index tracking policy.
3571 * If the container owns its elements, then the removed elements
3572 * will be deleted. Any duplicates will be removed in this process,
3573 * but don't rely on this.
3576void DATAVECTOR::clear (SG::OwnershipPolicy ownPolicy)
3579 m_ownPolicy = ownPolicy;
3580 this->template initAuxVectorBase<DataVector> (ownPolicy,
3581 SG::DEFAULT_TRACK_INDICES);
3586 * @brief Erase all the elements in the collection, and reset
3587 * the ownership mode.
3588 * @param ownPolicy The new ownership policy of the container.
3589 * @param trackIndices The index tracking policy.
3591 * If the container owns its elements, then the removed elements
3592 * will be deleted. Any duplicates will be removed in this process,
3593 * but don't rely on this.
3596void DATAVECTOR::clear (SG::OwnershipPolicy ownPolicy,
3597 SG::IndexTrackingPolicy trackIndices)
3600 m_ownPolicy = ownPolicy;
3601 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
3606 * @brief Return the DV/DL info struct for this class.
3608 * This can be used to make sure that it's instantiated.
3611const DataModel_detail::DVLInfoBase& DATAVECTOR::dvlinfo()
3613 static const DataModel_detail::DVLInfo<DataVector<T, DataModel_detail::NoBase> > info;
3619 * @brief Erase all the elements in the collection, and change
3620 * how elements are to be deleted.
3621 * @param deleter Object to be used to delete object.
3622 * (The DataVector does not take ownership.)
3623 * Passing nullptr will change back to the default.
3625 * If the container owns its elements, then the removed elements
3626 * will be deleted. Any duplicates will be removed in this process,
3627 * but don't rely on this.
3628 * After the current elements are deleted, the Deleter object is changed.
3631void DATAVECTOR::clear (std::unique_ptr<Deleter> deleter)
3634 delete this->m_deleter;
3635 this->m_deleter = deleter.release();
3640 * @brief Return the DV/DL info struct for this class.
3642 * This can be used to make sure that it's instantiated.
3645const DataModel_detail::DVLInfoBase& DATAVECTOR::dvlinfo_v() const
3647 return DATAVECTOR::dvlinfo();
3652 * @brief Return the offset of a base @c DataVector class.
3653 * @param ti @c std::type_info of the desired class.
3655 * If @c ti represents a @c DataVector base class of this one,
3656 * then return the offset of that base class. Otherwise, return -1.
3658 * This function is here due to limitations of root 6, which can't
3659 * calculate these offsets correctly from the dictionary if
3660 * virtual derivation is used.
3663int DATAVECTOR::baseOffset (const std::type_info& ti)
3665 if (typeid(DataVector) == ti)
3672 * @brief Convert to @c AuxVectorBase.
3674 * Needed to get @x AuxVectorBase from a @c ConstDataVector.
3675 * Present in @c DataVector as well for consistency.
3679const SG::AuxVectorBase& DATAVECTOR::auxbase() const
3685//=== Internal operations.
3689 * @brief Helper for @c baseOffset.
3690 * @param p Pointer to the start of the top-level object.
3691 * @param dv Reference to the DataVector object.
3692 * @param ti @c std::type_info of the desired class.
3694 * If @c ti represents a @c DataVector base class of this one,
3695 * then return the offset of that base class. Otherwise, return -1.
3699int DATAVECTOR::baseOffset1 (const char* p, const DataVector& dv,
3700 const std::type_info& ti)
3702 if (typeid(DataVector) == ti)
3703 return reinterpret_cast<const char*>(&dv) - p;
3709 * @brief Reset indices / reorder aux data after elements have been permuted.
3710 * @param beg Start of the range of elements to process.
3711 * @param end End of the range of elements to process.
3713 * Call this after some operation that has permuted the elements in the
3714 * container (such as sort). The index information in the elements
3715 * will be used to permute all auxiliary data in the same way.
3716 * Finally, all the indices will be reset in the correct order.
3719void DATAVECTOR::resortAux (iterator beg, iterator end)
3721 if (typename SG::AuxStore_traits<DataVector>::flag() &&
3722 beg >= this->begin() && end <= this->end())
3724 SG::AuxVectorBase::resortAux (beg-begin(), beg, end);
3730 * @brief Handle element assignment.
3731 * @param pos Position in the container to assign.
3732 * @param newElem The new element to assign.
3734 * The old element is freed if this container owns elements.
3735 * Auxiliary data are copied if appropriate.
3738void DATAVECTOR::assignElement (typename BaseContainer::iterator pos,
3741 testInsert ("assignElement");
3742 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
3743 this->doDelete (*pos);
3745 this->clearIndex (iterator (pos, this));
3747 this->moveAux (pos - this->m_pCont.begin(), newElem);
3752 * @brief Handle element assignment.
3753 * @param pos Position in the container to assign.
3754 * @param newElem The new element to assign.
3756 * The container must own its elements.
3757 * Auxiliary data are copied if appropriate.
3761DATAVECTOR::assignElement (typename BaseContainer::iterator pos,
3762 std::unique_ptr<base_value_type> newElem)
3764 // Container must own its elements.
3765 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3766 SG::throwExcNonowningContainer();
3768 testInsert ("assignElement");
3769 this->doDelete (*pos);
3770 value_type ptr = newElem.release();
3772 this->moveAux (pos - this->m_pCont.begin(), ptr);
3777 * @brief Handle element assignment from a base pointer.
3778 * @param pos Position in the container to assign.
3779 * @param newElem The new element to assign.
3781 * The old element is freed if this container owns elements.
3782 * Auxiliary data are copied if appropriate.
3785void DATAVECTOR::assignBaseElement (typename BaseContainer::iterator pos,
3786 typename BaseContainer::value_type newElem)
3788 testInsert ("assignBaseElement");
3789 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
3790 this->doDelete (*pos);
3792 this->clearIndex (iterator (pos, this));
3794 if (typename SG::AuxStore_traits<DataVector>::flag())
3795 this->moveAux (pos - this->m_pCont.begin(), newElem);
3800 * @brief Shift the auxiliary elements of the container.
3801 * @param pos The starting index for the shift.
3802 * @param offs The (signed) amount of the shift.
3804 * The elements in the container should have already been shifted;
3805 * this operation will then adjust the element indices and also shift
3806 * the elements in the vectors for all aux data items.
3807 * @c offs may be either positive or negative.
3809 * If @c offs is positive, then the container is growing.
3810 * The container size should be increased by @c offs,
3811 * the element at @c pos moved to @c pos + @c offs,
3812 * and similarly for following elements.
3813 * The elements between @c pos and @c pos + @c offs should
3814 * be default-initialized.
3816 * If @c offs is negative, then the container is shrinking.
3817 * The element at @c pos should be moved to @c pos + @c offs,
3818 * and similarly for following elements.
3819 * The container should then be shrunk by @c -offs elements
3820 * (running destructors as appropriate).
3823void DATAVECTOR::shift (size_t pos, ptrdiff_t offs)
3825 SG::AuxVectorBase::shift (*this, pos, offs);
3830 * @brief Helper to shorten calls to @c DataModel_detail::DVLCast.
3831 * @param p The value to convert.
3832 * @return The value as a @c const @c T*.
3834 * This is a no-op for the base class.
3838const T* DATAVECTOR::do_cast (const typename PtrVector::value_type p)
3845 * @brief Helper to shorten calls to @c DataModel_detail::DVLCast.
3846 * @param p The value to convert.
3847 * @return The value as a @c T*.
3849 * This is a no-op for the base class.
3853T* DATAVECTOR::do_cast_nc (typename PtrVector::value_type p)
3860 * @brief Find the most-derived @c DataVector class in the hierarchy.
3861 * @return The @c type_info for the class for which this method gets run.
3863 * This is used to generate a nice error message when the most-derived
3864 * check for insertions fails.
3865 * Every @c DataVector defines this virtual method, so when it's
3866 * called, the one corresponding to the most-derived @c DataVector
3870const std::type_info& DATAVECTOR::dv_typeid() const
3872 return typeid(DataVector);
3877 * @brief Helper for @c erase(). Remove element at a given position.
3878 * @param position Iterator pointing to the element to be removed.
3879 * @return An iterator pointing to the next element (or @c end()).
3881 * This function factors out common code between @c erase() in the
3882 * base and derived @c DataVector classes. It deals with the
3883 * @c std::vector iterators directly.
3886typename DATAVECTOR::PtrVector::iterator
3887DATAVECTOR::erase_base(typename PtrVector::iterator position)
3889 if (m_ownPolicy == SG::OWN_ELEMENTS && position != m_pCont.end())
3890 this->doDelete (*position);
3891 return m_pCont.erase(position);
3896 * @brief Helper for @c erase(). Remove a range of elements.
3897 * @param first Iterator pointing to the first element to be removed.
3898 * @param last Iterator pointing one past the last element to be removed.
3899 * @return An iterator pointing to the element pointed to by @a last
3900 * prior to erasing (or @c end()).
3902 * This function factors out common code between @c erase() in the
3903 * base and derived @c DataVector classes. It deals with the
3904 * @c std::vector iterators directly.
3907typename DATAVECTOR::PtrVector::iterator
3908DATAVECTOR::erase_base(typename PtrVector::iterator first,
3909 typename PtrVector::iterator last)
3911 if (first == last) return first;
3912 if (m_ownPolicy == SG::OWN_ELEMENTS) {
3913 typename PtrVector::iterator new_end =
3914 DataVector_detail::remove_duplicates(first, last);
3915 this->doDelete (first, new_end);
3917 return m_pCont.erase(first, last);
3922 * @brief Delete an element
3923 * @param p The element to delete.
3927void DATAVECTOR::doDelete (value_type p)
3930 m_deleter->doDelete (p);
3939 * @brief Delete a range of elements
3940 * @param first Start of range to delete.
3941 * @param last End of range to delete.
3945void DATAVECTOR::doDelete (typename PtrVector::iterator first,
3946 typename PtrVector::iterator last)
3949 m_deleter->doDelete (first, last);
3952 for (; first != last; ++first) {
3960 * @brief Test if we can insert; raise an exception if not.
3961 * @param op Description of the attempted operation.
3963 * In order to maintain type-safety, we can only allow insertions
3964 * using the most-derived instance of @c DataVector. This checks
3965 * this by testing the @c m_isMostDerived, which is set by the constructors
3966 * to true only for the most-derived instance.
3967 * If the test fails, we call to potentially out-of-line code to continue.
3971void DATAVECTOR::testInsert (const char* op)
3973 if (ATHCONTAINERS_LIKELY (m_isMostDerived))
3975 this->testInsertOol (op);
3980 * @brief Test if we can insert; raise an exception if not.
3981 * @param op Description of the attempted operation.
3983 * This continues the test of @c testInsert. There is one case
3984 * where @c m_isMostDerived may not be set correctly. If this container
3985 * was made via copy construction, then all the @c m_isMostDerived flags
3986 * will be false. So we call @c setMostDerived to set the flags correctly
3987 * and test again. If the test fails again, then we raise an exception.
3990void DATAVECTOR::testInsertOol (const char* op)
3992 this->setMostDerived();
3993 if (!m_isMostDerived)
3994 throw SG::ExcInsertionInBaseClass (op, typeid(DataVector), dv_typeid());
3999 * @brief Clear @c m_isMostDerived for this instance and for all bases.
4001 * Called from the constructor after setting @c m_isMostDerived.
4005void DATAVECTOR::clearMostDerived()
4007 this->m_isMostDerived = false;
4012 * @brief Set @c m_isMostDerived for this instance and clear it for all bases.
4014 * Called from @c testInsert if the test fails. The flag may not have
4015 * been set if this container was made via copy construction, so set
4016 * it appropriately now so we can test again.
4019void DATAVECTOR::setMostDerived()
4021 m_isMostDerived = true;
4028//****************************************************************************
4029// Free function implementations.
4034 * @brief Vector equality comparison.
4035 * @param a A @c DataVector.
4036 * @param b A @c DataVector of the same type as @a b.
4037 * @return True iff the size and elements of the vectors are equal.
4039 * This is an equivalence relation. It is linear in the size of the
4040 * vectors. Vectors are considered equivalent if their sizes are equal,
4041 * and if corresponding elements compare equal.
4044bool operator== (const DataVector<T>& a, const DataVector<T>& b)
4046 return a.stdcont() == b.stdcont();
4050/// Based on operator==
4052bool operator!= (const DataVector<T>& a, const DataVector<T>& b)
4054 return a.stdcont() != b.stdcont();
4059 * @brief Vector ordering relation.
4060 * @param a A @c DataVector.
4061 * @param b A @c DataVector of the same type as @a x.
4062 * @return True iff @a a is lexicographically less than @a b.
4064 * This is a total ordering relation. It is linear in the size of the
4065 * vectors. Comparisons are done on the pointer values of the elements.
4067 * See @c std::lexicographical_compare() for how the determination is made.
4070bool operator< (const DataVector<T>& a, const DataVector<T>& b)
4072 return a.stdcont() < b.stdcont();
4076/// Based on operator<
4078bool operator> (const DataVector<T>& a, const DataVector<T>& b)
4080 return a.stdcont() > b.stdcont();
4084/// Based on operator<
4086bool operator<= (const DataVector<T>& a, const DataVector<T>& b)
4088 return a.stdcont() <= b.stdcont();
4092/// Based on operator<
4094bool operator>= (const DataVector<T>& a, const DataVector<T>& b)
4096 return a.stdcont() >= b.stdcont();
4100/// See @c DataVector<T, BASE>::swap().
4102void swap (DataVector<T>& a, DataVector<T>& b)
4109 * @brief Specialization of @c ClassName for @c DataVector.
4111 * This overrides the default implementation of @c ClassName
4112 * to hide @c DataVector's second template parameter.
4115std::string ClassName<DataVector<T> >::name()
4117 std::string out = "DataVector<";
4118 out += ClassName<T>::name();
4119 if (out[out.size()-1] == '>')
4126#ifndef XAOD_STANDALONE
4129// Set up initialization of element type BaseInfo
4130namespace DataVector_detail {
4131#define DVLTYPE DataVector
4132#include "AthContainers/tools/DVLEltBaseInfo.icc"
4134} // namespace DataVector_detail
4140namespace DataVector_detail {
4141/// Dummy implementation for the DVLEltBaseInit structure
4143struct DVLEltBaseInit {};
4144} // namespace DataVector_detail
4147#endif // not XAOD_STANDALONE
4151// We need to specialize the function that DVLInfo uses to create the container
4154 * @brief Construct a new container.
4155 * @param nreserve Number of elements for which to reserve space.
4156 * (Ignored if not appropriate.)
4157 * @param cont[out] Pointer to the constructed container.
4158 * (Returned via an argument to allow for template
4159 * argument deduction.)
4161 * Specialization for DataVector.
4164void dvl_makecontainer (size_t nreserve, DataVector<T>*& cont)
4166 cont = new DataVector<T> (SG::VIEW_ELEMENTS);
4167 cont->reserve (nreserve);