1// Dear emacs, this is -*- c++ -*-
4 Copyright (C) 2002-2026 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());
1077 if (pElem && this->trackIndices())
1078 this->moveAuxNoClear (this->size()-1, pElem);
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());
1108 if (ptr && this->trackIndices())
1109 this->moveAuxNoClear (this->size()-1, ptr);
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");
1137 bool trackIndices = this->trackIndices();
1139 size_t sz = this->m_pCont.size();
1140 this->m_pCont.resize (sz + n);
1141 SG::AuxVectorBase::resize<DataVector> (this->size());
1142 for (size_t i = 0; i < n; i++) {
1144 this->m_pCont[sz+i] = p;
1146 this->moveAuxNoClear (sz+i, p);
1154 * @brief Add an element to the end of the collection.
1155 * @param pElem The element to add to the collection.
1157 * The container's ownership policy will determine if it takes ownership
1158 * of the new element.
1160 * Note: this method may only be called using the most derived
1161 * @c DataVector in the hierarchy.
1163 * For @c DataVector, this is like the same as @c push_back, and
1164 * it returns the pushed element.
1165 * It's included just for interface compatibility with `std::vector`.
1167template <class T, class BASE>
1169typename DataVector<T, BASE>::value_type
1170DataVector<T, BASE>::emplace_back(value_type pElem)
1172 return this->push_back (pElem);
1177 * @brief Add a new element to the collection.
1178 * @param position Iterator before which the element will be added.
1179 * @param pElem The element to add to the collection.
1180 * @return An iterator that points to the inserted data.
1182 * The container's ownership policy will determine if it takes ownership
1183 * of the new element.
1185 * Note: this method may only be called using the most derived
1186 * @c DataVector in the hierarchy.
1188template <class T, class BASE>
1189typename DataVector<T, BASE>::iterator
1190DataVector<T, BASE>::insert(iterator position, value_type pElem)
1192 // Ensure we're not being called via a base class.
1193 testInsert ("insert");
1194 iterator ret (this->m_pCont.insert(position.base(), pElem), this);
1195 this->shift (ret - this->begin(), 1);
1196 this->moveAux (ret-this->begin(), pElem, false, true);
1202 * @brief Add a new element to the collection.
1203 * @param position Iterator before which the element will be added.
1204 * @param pElem The element to add to the collection.
1205 * @return An iterator that points to the inserted data.
1207 * The container must be an owning container.
1209 * Note: this method may only be called using the most derived
1210 * @c DataVector in the hierarchy.
1212template <class T, class BASE>
1213typename DataVector<T, BASE>::iterator
1214DataVector<T, BASE>::insert(iterator position,
1215 std::unique_ptr<base_value_type> pElem)
1217 // Container must own its elements.
1218 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1219 SG::throwExcNonowningContainer();
1221 // Ensure we're not being called via a base class.
1222 testInsert ("insert");
1223 value_type ptr = pElem.release();
1224 iterator ret (this->m_pCont.insert(position.base(), ptr), this);
1225 this->shift (ret - this->begin(), 1);
1226 if (this->trackIndices())
1227 this->moveAuxNoClear (ret-this->begin(), ptr);
1233 * @brief Add a new element to the collection.
1234 * @param position Iterator before which the element will be added.
1235 * @param pElem The element to add to the collection.
1236 * @return An iterator that points to the inserted data.
1238 * The container's ownership policy will determine if it takes ownership
1239 * of the new element.
1241 * Note: this method may only be called using the most derived
1242 * @c DataVector in the hierarchy.
1244 * For @c DataVector, this is just the same as @c insert.
1245 * It's included just for interface compatibility with `std::vector`.
1247template <class T, class BASE>
1248typename DataVector<T, BASE>::iterator
1249DataVector<T, BASE>::emplace(iterator position, value_type pElem)
1251 return this->insert (position, pElem);
1256 * @brief Add a group of new elements to the collection.
1257 * @param position Iterator before which the element will be added.
1258 * @param first The start of the range to put in the container.
1259 * @param last The end of the range to put in the container.
1261 * The container's ownership policy will determine if it takes ownership
1262 * of the new element.
1264 * Note: this method may only be called using the most derived
1265 * @c DataVector in the hierarchy.
1267 * This overload is for the case where the iterator value type
1268 * is convertible to T*.
1270template <class T, class BASE>
1271template <class InputIterator,
1272 DataVector_detail::enable_if_ptr_itr<InputIterator, T> >
1273void DataVector<T, BASE>::insert(iterator position,
1274 InputIterator first,
1277 // Make sure that the input iterator can actually be converted
1278 // to a T*. Lets us give a compilation error for this:
1279 // DATAVECTOR_BASE(D, B);
1282 // d.insert (d.begin(), bb, bb+1);
1283 // which would otherwise compile.
1284 using ittype = typename std::iterator_traits<InputIterator>::value_type;
1285 static_assert (std::is_convertible_v<ittype, const T*>);
1287 // Ensure we're not being called via a base class.
1288 testInsert ("insert");
1289 size_t idx = position - this->begin();
1290 size_t old_sz = this->m_pCont.size();
1291 this->m_pCont.insert(position.base(), first, last);
1292 size_t n = this->m_pCont.size() - old_sz;
1293 this->shift (idx, n);
1294 this->moveAux (idx, first, last, false, true);
1299 * @brief Add a group of new elements to the collection.
1300 * @param position Iterator before which the element will be added.
1301 * @param first The start of the range to put in the container.
1302 * @param last The end of the range to put in the container.
1304 * The container's ownership policy will determine if it takes ownership
1305 * of the new element.
1307 * Note: this method may only be called using the most derived
1308 * @c DataVector in the hierarchy.
1310 * This overload is for the case where the iterator value type
1311 * is convertible to unique_ptr<T>.
1313template <class T, class BASE>
1314template <class InputIterator,
1315 DataVector_detail::enable_if_up_itr<InputIterator, T> >
1316void DataVector<T, BASE>::insert(iterator position,
1317 InputIterator first,
1320 // Make sure that the input iterator can actually be converted
1321 // to a T*. Lets us give a compilation error for this:
1322 // DATAVECTOR_BASE(D, B);
1325 // d.insert (d.begin(), bb, bb+1);
1326 // which would otherwise compile.
1327 using ittype = typename std::iterator_traits<InputIterator>::value_type;
1328 static_assert (std::is_convertible_v<typename ittype::pointer, const T*>);
1330 // Ensure we're not being called via a base class.
1331 testInsert ("insert");
1332 size_t idx = position - this->begin();
1333 size_t old_sz = this->m_pCont.size();
1334 using CxxUtils::releasing_iterator;
1335 this->m_pCont.insert(position.base(), releasing_iterator(first), releasing_iterator(last));
1336 size_t n = this->m_pCont.size() - old_sz;
1337 this->shift (idx, n);
1338 this->moveAux (idx, this->begin()+idx, this->begin()+idx+n);
1343 * @brief Add a group of new elements to the collection.
1344 * @param position Iterator before which the element will be added.
1345 * @param l An initializer list.
1347 * The container's ownership policy will determine if it takes ownership
1348 * of the new element.
1350 * Note: this method may only be called using the most derived
1351 * @c DataVector in the hierarchy.
1353template <class T, class BASE>
1354void DataVector<T, BASE>::insert(iterator position,
1355 std::initializer_list<value_type> l)
1357 insert (position, l.begin(), l.end());
1362 * @brief Insert the contents of another @c DataVector,
1363 * with auxiliary data copied via move semantics.
1364 * @param position Iterator before which the new elements will be added.
1365 * @param other The vector to add.
1367 * The ownership mode of this vector must be the same as @c other;
1368 * otherwise, an exception will be thrown.
1370 * If both vectors are view vectors, then this is the same
1371 * as <code> insert (position, other.begin(), other.end()) </code>.
1373 * Otherwise, the elements from @c other will be inserted into this vector.
1374 * This vector will take ownership of the elements, and the ownership
1375 * mode of @c other will be changed to @c VIEW_ELEMENTS.
1376 * Auxiliary data for these elements will be transferred,
1377 * using move semantics if possible. (Thus, the auxiliary store
1378 * for @c other may be modified and must not be locked.)
1379 * Finally, the auxiliary store pointer for @c other will be cleared
1380 * (but the store itself will not be deleted since it's not owned
1383 * Note: this method may only be called using the most derived
1384 * @c DataVector in the hierarchy.
1386template <class T, class BASE>
1388DataVector<T, BASE>::insertMove (iterator position, DataVector& other)
1390 if (this->m_ownPolicy != other.ownPolicy())
1391 throw SG::ExcInsertMoveOwnershipMismatch();
1393 if (this->m_ownPolicy == SG::VIEW_ELEMENTS) {
1394 this->insert (position, other.begin(), other.end());
1398 testInsert ("insertMove");
1399 size_t pos = position.base() - this->m_pCont.begin();
1400 this->m_pCont.insert (position.base(), other.begin(), other.end());
1401 this->setIndices (this->begin()+pos, this->end(), pos);
1402 other.m_ownPolicy = SG::VIEW_ELEMENTS;
1404 SG::IAuxStore* otherStore = other.getStore();
1406 SG::IAuxStore* store = this->getStore();
1408 if (!store->insertMove (pos, *otherStore))
1411 else if (this->hasStore())
1412 throw SG::ExcConstAuxData ("insertMove");
1413 other.setStore (static_cast<SG::IAuxStore*>(nullptr));
1415 else if (other.hasStore())
1416 throw SG::ExcConstAuxData ("insertMove");
1420//=== Erasure operations.
1424 * @brief Remove element at a given position.
1425 * @param position Iterator pointing to the element to be removed.
1426 * @return An iterator pointing to the next element (or @c end()).
1428 * If the container owns its elements, then the pointed-to element
1431template <class T, class BASE>
1432typename DataVector<T, BASE>::iterator
1433DataVector<T, BASE>::erase(iterator position)
1435 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1436 this->clearIndex (position);
1437 iterator ret (this->erase_base (position.base()), this);
1438 this->shift (ret - this->begin() + 1, -1);
1444 * @brief Remove a range of elements.
1445 * @param first Iterator pointing to the first element to be removed.
1446 * @param last Iterator pointing one past the last element to be removed.
1447 * @return An iterator pointing to the element pointed to by @a last
1448 * prior to erasing (or @c end()).
1450 * If the container owns its elements, then the removed elements
1451 * will be deleted. Any duplicates will be removed in this process,
1452 * but don't rely on this.
1454template <class T, class BASE>
1455typename DataVector<T, BASE>::iterator
1456DataVector<T, BASE>::erase(iterator first, iterator last)
1458 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1459 this->clearIndices (first, last);
1460 iterator ret (this->erase_base (first.base(), last.base()), this);
1461 this->shift (ret - this->begin() + (last-first), -(last-first));
1467 * @brief Remove the last element from the collection.
1469 * If the container owns its elements, then the removed element
1472template <class T, class BASE>
1473void DataVector<T, BASE>::pop_back()
1475 if (!this->m_pCont.empty()) {
1476 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
1477 this->doDelete (this->m_pCont.back());
1479 this->clearIndex (iterator (this->m_pCont.end() - 1, this));
1480 this->m_pCont.pop_back();
1481 SG::AuxVectorBase::resize<DataVector> (this->m_pCont.size());
1487 * @brief Erase all the elements in the collection.
1489 * If the container owns its elements, then the removed elements
1490 * will be deleted. Any duplicates will be removed in this process,
1491 * but don't rely on this.
1493template <class T, class BASE>
1495void DataVector<T, BASE>::clear()
1497 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1498 this->clearIndices (begin(), end());
1499 this->erase_base (this->m_pCont.begin(), this->m_pCont.end());
1500 SG::AuxVectorBase::resize<DataVector> (0);
1508 * @brief Swap this collection with another.
1509 * @param rhs The collection with which to swap.
1511 * Ownership is swapped along with the collection content.
1513 * Note: this method may only be called using the most-derived
1514 * @c DataVector in the hierarchy. The @a rhs must also be
1515 * referenced using the most-derived @c DataVector.
1517 * Warning: If this container has auxiliary data, then this
1518 * is an O(N) operation, not O(1).
1520template <class T, class BASE>
1521void DataVector<T, BASE>::swap(DataVector& rhs)
1523 testInsert ("swap");
1524 rhs.testInsert ("swap");
1525 std::swap(this->m_ownPolicy, rhs.m_ownPolicy);
1526 SG::AuxVectorBase::swap (rhs);
1527 this->m_pCont.swap(rhs.m_pCont);
1528 std::swap (this->m_deleter, rhs.m_deleter);
1529 this->setIndices (this->begin(), this->end());
1530 rhs.setIndices (rhs.begin(), rhs.end());
1535 * @brief Swap the referents of two @c DataVector iterators.
1536 * @param a The first iterator for the swap.
1537 * @param b The second iterator for the swap.
1539template <class T, class BASE>
1540void DataVector<T, BASE>::iter_swap (iterator a, iterator b)
1542 if (a.ownPolicy() != b.ownPolicy()) {
1543 throw SG::ExcBadIterSwap();
1545 a.testInsert ("iter_swap");
1546 b.testInsert ("iter_swap");
1547 std::iter_swap (a.base(), b.base());
1548 DataVector* acont = a.container();
1549 DataVector* bcont = b.container();
1550 if (typename SG::AuxStore_traits<DataVector>::flag())
1551 acont->swapElementsAux (a.base() - acont->stdcont().begin(),
1552 b.base() - bcont->stdcont().begin(),
1553 DataModel_detail::DVLCast<DataVector>::cast(*a.base()),
1554 DataModel_detail::DVLCast<DataVector>::cast(*b.base()),
1560 * @brief Sort the container.
1562 * This just sorts by pointer value, so it's probably not very useful.
1564template <class T, class BASE>
1565void DataVector<T, BASE>::sort()
1567 typedef std::less<typename PtrVector::value_type> less;
1568 std::sort (this->m_pCont.begin(), this->m_pCont.end(),
1569 DataModel_detail::Compwrapper<DataVector, less> (less()));
1570 this->resortAux (this->begin(), this->end());
1575 * @brief Sort the container with a user-specified comparison operator.
1576 * @param comp Functional to compare two values.
1578template <class T, class BASE>
1579template <class COMPARE>
1580void DataVector<T, BASE>::sort(COMPARE comp)
1582 std::sort (this->m_pCont.begin(), this->m_pCont.end(),
1583 DataModel_detail::Compwrapper<DataVector, COMPARE> (comp));
1584 this->resortAux (this->begin(), this->end());
1588//=== Non-standard operations.
1592 * @brief Swap one element out of the container.
1593 * @param index Index of the element in the container to swap.
1594 * @param newElement New element to put in the container.
1596 * @param oldElem Reference to receive the element removed from the
1599 * Reference @a oldElem is initialized with element @a index of the
1600 * collection (no bounds checking). Then element @a index is set
1601 * to @newElem. If the collection owns its elements, then it will
1602 * take ownership of @a newElem and release (without deleting)
1603 * the element returned through @a oldElem.
1605 * Note: this method may only be called using the most derived
1606 * @c DataVector in the hierarchy.
1608template <class T, class BASE>
1609void DataVector<T, BASE>::swapElement(size_type index,
1613 testInsert ("swapElement");
1615 DataModel_detail::DVLCast<DataVector>::cast(this->m_pCont[index]);
1616 this->clearIndex (iterator (this->m_pCont.begin() + index, this));
1617 this->m_pCont[index] = newElem;
1618 this->moveAux (index, newElem);
1623 * @brief Swap one element out of the container.
1624 * @param pos The element in the container to swap.
1625 * @param newElem New element to put in the container.
1627 * @param oldElem Reference to receive the element removed from the
1630 * Reference @a oldElem is initialized with element @a pos of the
1631 * collection (no bounds checking). Then element @a index is set
1632 * to @c newElem. If the collection owns its elements, then it will
1633 * take ownership of @a newElem and release (without deleting)
1634 * the element returned through @a oldElem.
1636 * Note: this method may only be called using the most derived
1637 * @c DataVector in the hierarchy.
1639template <class T, class BASE>
1640void DataVector<T, BASE>::swapElement(iterator pos,
1644 testInsert ("swapElement");
1646 DataModel_detail::DVLCast<DataVector>::cast(*pos.base());
1647 this->clearIndex (pos);
1648 *pos.base() = newElem;
1649 this->moveAux (pos.base() - this->m_pCont.begin(), newElem);
1654 * @brief Swap one element out of the container.
1655 * @param index Index of the element in the container to swap.
1656 * @param newElement New element to put in the container.
1658 * @param oldElem Reference to receive the element removed from the
1661 * Reference @a oldElem is initialized with element @a index of the
1662 * collection (no bounds checking). Then element @a index is set
1665 * The collection must own its elements to use its interface.
1666 * The collection will take ownership of @c newElem and will return
1667 * ownership of @c oldElem.
1669 * Note: this method may only be called using the most derived
1670 * @c DataVector in the hierarchy.
1672template <class T, class BASE>
1673void DataVector<T, BASE>::swapElement(size_type index,
1674 std::unique_ptr<base_value_type> newElem,
1675 std::unique_ptr<base_value_type>& oldElem)
1677 // Container must own its elements.
1678 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1679 SG::throwExcNonowningContainer();
1681 testInsert ("swapElement");
1682 oldElem = std::unique_ptr<base_value_type>
1683 (DataModel_detail::DVLCast<DataVector>::cast(this->m_pCont[index]));
1684 this->clearIndex (iterator (this->m_pCont.begin() + index, this));
1685 value_type ptr = newElem.release();
1686 this->m_pCont[index] = ptr;
1687 this->moveAux (index, ptr);
1692 * @brief Swap one element out of the container.
1693 * @param pos The element in the container to swap.
1694 * @param newElem New element to put in the container.
1696 * @param oldElem Reference to receive the element removed from the
1699 * Reference @a oldElem is initialized with element @a pos of the
1700 * collection (no bounds checking). Then element @a index is set
1703 * The collection must own its elements to use its interface.
1704 * The collection will take ownership of @c newElem and will return
1705 * ownership of @c oldElem.
1707 * Note: this method may only be called using the most derived
1708 * @c DataVector in the hierarchy.
1710template <class T, class BASE>
1711void DataVector<T, BASE>::swapElement(iterator pos,
1712 std::unique_ptr<base_value_type> newElem,
1713 std::unique_ptr<base_value_type>& oldElem)
1715 // Container must own its elements.
1716 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1717 SG::throwExcNonowningContainer();
1719 testInsert ("swapElement");
1720 oldElem = std::unique_ptr<base_value_type>
1721 (DataModel_detail::DVLCast<DataVector>::cast(*pos.base()));
1722 this->clearIndex (pos);
1723 value_type ptr = newElem.release();
1725 this->moveAux (pos.base() - this->m_pCont.begin(), ptr);
1730 * @brief Erase all the elements in the collection, and reset
1731 * the ownership mode.
1732 * @param ownPolicy The new ownership policy of the container.
1734 * If the container owns its elements, then the removed elements
1735 * will be deleted. Any duplicates will be removed in this process,
1736 * but don't rely on this.
1738template <class T, class BASE>
1739void DataVector<T, BASE>::clear (SG::OwnershipPolicy ownPolicy)
1742 this->m_ownPolicy = ownPolicy;
1743 this->template initAuxVectorBase<DataVector> (ownPolicy,
1744 SG::DEFAULT_TRACK_INDICES);
1749 * @brief Erase all the elements in the collection, and reset
1750 * the ownership mode.
1751 * @param ownPolicy The new ownership policy of the container.
1752 * @param trackIndices The index tracking policy.
1754 * If the container owns its elements, then the removed elements
1755 * will be deleted. Any duplicates will be removed in this process,
1756 * but don't rely on this.
1758template <class T, class BASE>
1759void DataVector<T, BASE>::clear (SG::OwnershipPolicy ownPolicy,
1760 SG::IndexTrackingPolicy trackIndices)
1763 this->m_ownPolicy = ownPolicy;
1764 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
1769 * @brief Return the DV/DL info struct for this class.
1771 * This can be used to make sure that it's instantiated.
1773template <class T, class BASE>
1774const DataModel_detail::DVLInfoBase& DataVector<T, BASE>::dvlinfo()
1776 static const DataModel_detail::DVLInfo<DataVector<T, BASE> > info;
1782 * @brief Erase all the elements in the collection, and change
1783 * how elements are to be deleted.
1784 * @param deleter Object to be used to delete object.
1785 * Passing nullptr will change back to the default.
1787 * If the container owns its elements, then the removed elements
1788 * will be deleted. Any duplicates will be removed in this process,
1789 * but don't rely on this.
1790 * After the current elements are deleted, the Deleter object is changed.
1792template <class T, class BASE>
1793void DataVector<T, BASE>::clear (std::unique_ptr<Deleter> deleter)
1796 delete this->m_deleter;
1797 this->m_deleter = deleter.release();
1802 * @brief Return the DV/DL info struct for this class.
1804 * This can be used to make sure that it's instantiated.
1806template <class T, class BASE>
1807const DataModel_detail::DVLInfoBase& DataVector<T, BASE>::dvlinfo_v() const
1809 return DataVector<T, BASE>::dvlinfo();
1814 * @brief Return the offset of a base @c DataVector class.
1815 * @param ti @c std::type_info of the desired class.
1817 * If @c ti represents a @c DataVector base class of this one,
1818 * then return the offset of that base class. Otherwise, return -1.
1820 * This function is here due to limitations of root 6, which can't
1821 * calculate these offsets correctly from the dictionary if
1822 * virtual derivation is used.
1824template <class T, class BASE>
1825int DataVector<T, BASE>::baseOffset (const std::type_info& ti)
1828 return baseOffset1 (reinterpret_cast<const char*>(&dv), dv, ti);
1833 * @brief Convert to @c AuxVectorBase.
1835 * Needed to get @x AuxVectorBase from a @c ConstDataVector.
1836 * Present in @c DataVector as well for consistency.
1837 * We only really need it in the base class; however, root6 fails
1838 * constructing a @c TMethodCall for this if there is virtual
1839 * derivation. A workaround is to redeclare this in the derived
1842template <class T, class BASE>
1843const SG::AuxVectorBase& DataVector<T, BASE>::auxbase() const
1849//=== Internal operations.
1853 * @brief Helper for @c baseOffset.
1854 * @param p Pointer to the start of the top-level object.
1855 * @param dv Reference to the DataVector object.
1856 * @param ti @c std::type_info of the desired class.
1858 * If @c ti represents a @c DataVector base class of this one,
1859 * then return the offset of that base class. Otherwise, return -1.
1862template <class T, class BASE>
1863int DataVector<T, BASE>::baseOffset1 (const char* p, const DataVector& dv,
1864 const std::type_info& ti)
1866 if (typeid(DataVector) == ti)
1867 return reinterpret_cast<const char*>(&dv) - p;
1868 return BASE::baseOffset1 (p, dv, ti);
1873 * @brief Reset indices / reorder aux data after elements have been permuted.
1874 * @param beg Start of the range of elements to process.
1875 * @param end End of the range of elements to process.
1877 * Call this after some operation that has permuted the elements in the
1878 * container (such as sort). The index information in the elements
1879 * will be used to permute all auxiliary data in the same way.
1880 * Finally, all the indices will be reset in the correct order.
1882template <class T, class BASE>
1883void DataVector<T, BASE>::resortAux (iterator beg, iterator end)
1885 if (typename SG::AuxStore_traits<DataVector>::flag() &&
1886 beg >= this->begin() && end <= this->end())
1888 SG::AuxVectorBase::resortAux (beg-begin(), beg, end);
1894 * @brief Test if we can insert; raise an exception if not.
1895 * @param op Description of the attempted operation.
1897 * In order to maintain type-safety, we can only allow insertions
1898 * using the most-derived instance of @c DataVector. This checks
1899 * this by testing the @c m_isMostDerived, which is set by the constructors
1900 * to true only for the most-derived instance.
1901 * If the test fails, we call to potentially out-of-line code to continue.
1903template <class T, class BASE>
1905void DataVector<T, BASE>::testInsert (const char* op)
1907 if (ATHCONTAINERS_LIKELY (m_isMostDerived))
1909 this->testInsertOol (op);
1914 * @brief Test if we can insert; raise an exception if not.
1915 * @param op Description of the attempted operation.
1917 * This continues the test of @c testInsert. There is one case
1918 * where @c m_isMostDerived may not be set correctly. If this container
1919 * was made via copy construction, then all the @c m_isMostDerived flags
1920 * will be false. So we call @c setMostDerived to set the flags correctly
1921 * and test again. If the test fails again, then we raise an exception.
1923template <class T, class BASE>
1924void DataVector<T, BASE>::testInsertOol (const char* op)
1926 this->setMostDerived();
1927 if (!m_isMostDerived)
1928 throw SG::ExcInsertionInBaseClass (op, typeid(DataVector), dv_typeid());
1933 * @brief Handle element assignment.
1934 * @param pos Position in the container to assign.
1935 * @param newElem The new element to assign.
1937 * The old element is freed if this container owns elements.
1938 * Auxiliary data are copied if appropriate.
1940template <class T, class BASE>
1941void DataVector<T, BASE>::assignElement (typename BaseContainer::iterator pos,
1944 testInsert ("assignElement");
1945 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
1946 this->doDelete (*pos);
1948 this->clearIndex (iterator (pos, this));
1950 this->moveAux (pos - this->m_pCont.begin(), newElem);
1955 * @brief Handle element assignment.
1956 * @param pos Position in the container to assign.
1957 * @param newElem The new element to assign.
1959 * The container must own its elements.
1960 * Auxiliary data are copied if appropriate.
1962template <class T, class BASE>
1964DataVector<T, BASE>::assignElement (typename BaseContainer::iterator pos,
1965 std::unique_ptr<base_value_type> newElem)
1967 // Container must own its elements.
1968 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
1969 SG::throwExcNonowningContainer();
1971 testInsert ("assignElement");
1972 this->doDelete (*pos);
1973 value_type ptr = newElem.release();
1975 this->moveAux (pos - this->m_pCont.begin(), ptr);
1980 * @brief Handle element assignment from a base pointer.
1981 * @param pos Position in the container to assign.
1982 * @param newElem The new element to assign.
1984 * The old element is freed if this container owns elements.
1985 * Auxiliary data are copied if appropriate.
1987template <class T, class BASE>
1989DataVector<T, BASE>::assignBaseElement (typename BaseContainer::iterator pos,
1990 typename BaseContainer::value_type newElem)
1992 testInsert ("assignBaseElement");
1993 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
1994 this->doDelete (*pos);
1996 this->clearIndex (iterator (pos, this));
1998 if (typename SG::AuxStore_traits<DataVector>::flag())
1999 this->moveAux (pos - this->m_pCont.begin(),
2000 DataModel_detail::DVLCast<DataVector>::cast(newElem));
2005 * @brief Shift the auxiliary elements of the container.
2006 * @param pos The starting index for the shift.
2007 * @param offs The (signed) amount of the shift.
2009 * The elements in the container should have already been shifted;
2010 * this operation will then adjust the element indices and also shift
2011 * the elements in the vectors for all aux data items.
2012 * @c offs may be either positive or negative.
2014 * If @c offs is positive, then the container is growing.
2015 * The container size should be increased by @c offs,
2016 * the element at @c pos moved to @c pos + @c offs,
2017 * and similarly for following elements.
2018 * The elements between @c pos and @c pos + @c offs should
2019 * be default-initialized.
2021 * If @c offs is negative, then the container is shrinking.
2022 * The element at @c pos should be moved to @c pos + @c offs,
2023 * and similarly for following elements.
2024 * The container should then be shrunk by @c -offs elements
2025 * (running destructors as appropriate).
2027template <class T, class BASE>
2028void DataVector<T, BASE>::shift (size_t pos, ptrdiff_t offs)
2030 SG::AuxVectorBase::shift (*this, pos, offs);
2035 * @brief Helper to shorten calls to @c DataVector_detail::DVLCast.
2036 * @param p The value to convert.
2037 * @return The value as a @c const @c T*.
2039 * The conversion will be done with @c static_cast if possible,
2040 * with @c dynamic_cast otherwise.
2042template <class T, class BASE>
2045DataVector<T, BASE>::do_cast (const typename PtrVector::value_type p)
2047 return DataModel_detail::DVLCast<DataVector>::cast (p);
2052 * @brief Helper to shorten calls to @c DataVector_detail::DVLCast.
2053 * @param p The value to convert.
2054 * @return The value as a @c T*.
2056 * The conversion will be done with @c static_cast if possible,
2057 * with @c dynamic_cast otherwise.
2059template <class T, class BASE>
2062DataVector<T, BASE>::do_cast_nc (typename PtrVector::value_type p)
2064 return DataModel_detail::DVLCast<DataVector>::cast (p);
2069 * @brief Find the most-derived @c DataVector class in the hierarchy.
2070 * @return The @c type_info for the class for which this method gets run.
2072 * This is used to generate a nice error message when the most-derived
2073 * check for insertions fails.
2074 * Every @c DataVector defines this virtual method, so when it's
2075 * called, the one corresponding to the most-derived @c DataVector
2078template <class T, class BASE>
2079const std::type_info& DataVector<T, BASE>::dv_typeid() const
2081 return typeid(DataVector);
2086 * @brief Clear @c m_isMostDerived for this instance and for all bases.
2088 * Called from the constructor after setting @c m_isMostDerived.
2090template <class T, class BASE>
2092void DataVector<T, BASE>::clearMostDerived()
2094 this->m_isMostDerived = false;
2095 BASE::clearMostDerived();
2100 * @brief Set @c m_isMostDerived for this instance and clear it for all bases.
2102 * Called from @c testInsert if the test fails. The flag may not have
2103 * been set if this container was made via copy construction, so set
2104 * it appropriately now so we can test again.
2106template <class T, class BASE>
2107void DataVector<T, BASE>::setMostDerived()
2109 m_isMostDerived = true;
2110 BASE::clearMostDerived();
2114//****************************************************************************
2115// Specialized (base) DataVector implementation.
2119// An abbreviation for the DataVector specialization to try to make
2120// things a little more readable.
2121#define DATAVECTOR DataVector<T, DataModel_detail::NoBase>
2124//=== Constructors, destructors, assignment.
2127 * @brief Default constructor.
2128 * @param ownPolicy The ownership mode for the container.
2129 * @param trackIndices The index tracking policy.
2131 * By default, a @c DataVector will own its elements.
2132 * To avoid this, pass @c SG::VIEW_ELEMENTS for @a ownPolicy.
2136DATAVECTOR::DataVector
2137 (SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/,
2138 SG::IndexTrackingPolicy trackIndices /*=SG::DEFAULT_TRACK_INDICES*/)
2139 : m_ownPolicy(ownPolicy)
2141 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
2142 this->m_isMostDerived = true;
2147 * @brief Sized constructor.
2148 * @param n The size of the container.
2149 * @param ownPolicy The ownership mode for the container.
2150 * @param trackIndices The index tracking policy.
2152 * Note that unlike the standard vector constructor, you can't specify
2153 * an initial value here. The container will be initialized with 0's.
2155 * By default, a @c DataVector will own its elements.
2156 * To avoid this, pass @c SG::VIEW_ELEMENTS for @a ownPolicy.
2160DATAVECTOR::DataVector
2162 SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/,
2163 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/)
2164 : m_ownPolicy(ownPolicy),
2167 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
2168 this->m_isMostDerived = true;
2173 * @brief Copy constructor.
2174 * @param rhs The container from which to copy.
2176 * This is a `shallow' copy; the new container will not own its elements.
2180DATAVECTOR::DataVector(const DataVector& rhs)
2182 m_ownPolicy(SG::VIEW_ELEMENTS),
2183 m_pCont(rhs.m_pCont)
2185 this->template initAuxVectorBase<DataVector> (m_ownPolicy,
2186 SG::DEFAULT_TRACK_INDICES);
2187 // Leave m_isMostDerived false here, because we may be being called
2188 // from a derived class implicit copy constructor. The flags will get
2189 // set correctly when @c testInsert gets called.
2194 * @brief Move constructor.
2195 * @param rhs The container from which to move.
2197 * Any auxiliary data will be moved along with the container contents.
2201DATAVECTOR::DataVector(DataVector&& rhs) noexcept
2202 : AuxVectorBase (std::move (rhs)),
2203 m_ownPolicy(rhs.m_ownPolicy),
2204 m_pCont(std::move (rhs.m_pCont))
2206 m_deleter = std::move(rhs.m_deleter);
2207 rhs.m_deleter = nullptr;
2209 // Need to reset the container pointer on elements.
2210 // Functions called from here can throw GaudiException.
2211 // However, logically that should not actually happen; so leave this
2212 // declared as noexcept.
2213 this->setIndices (this->begin(), this->end());
2215 // This doesn't get called from derived classes.
2216 // Go ahead and set this flag now.
2217 m_isMostDerived = true;
2222 * @brief Constructor from iterators.
2223 * @param first The start of the range to put in the new container.
2224 * @param last The end of the range to put in the new container.
2225 * @param ownPolicy The ownership mode for the container.
2226 * @param trackIndices The index tracking policy.
2227 * @param store An associated auxiliary data store.
2229 * By default, a view container is made, which does not own its elements.
2230 * To have the container take ownership of the pointers passed
2231 * to this constructor, pass @c SG::OWN_ELEMENTS for @a ownPolicy.
2234template <class InputIterator>
2236DATAVECTOR::DataVector
2237 (InputIterator first,
2239 SG::OwnershipPolicy ownPolicy /*= SG::VIEW_ELEMENTS*/,
2240 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/,
2241 SG::IAuxStore* store /*= 0*/)
2242 : m_ownPolicy(ownPolicy)
2244 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
2245 this->m_isMostDerived = true;
2247 this->setStore (store);
2248 insert (this->begin(), first, last);
2253 * @brief Constructor from an initializer list.
2254 * @param l An initializer list.
2255 * @param last The end of the range to put in the new container.
2256 * @param ownPolicy The ownership mode for the container.
2257 * @param trackIndices The index tracking policy.
2258 * @param store An associated auxiliary data store.
2261 * A @c DataVector constructed this way will *not* own its elements
2262 * by default. To change this, pass @c SG::OWN_ELEMENTS for @a ownPolicy.
2266DATAVECTOR::DataVector
2267 (std::initializer_list<value_type> l,
2268 SG::OwnershipPolicy ownPolicy /*= SG::VIEW_ELEMENTS*/,
2269 SG::IndexTrackingPolicy trackIndices /*= SG::DEFAULT_TRACK_INDICES*/,
2270 SG::IAuxStore* store /*= 0*/)
2271 : DataVector (l.begin(), l.end(), ownPolicy, trackIndices, store)
2277 * @brief Assignment operator.
2278 * @param rhs The DataVector from which to assign.
2279 * @return This object.
2281 * This is a `shallow' copy; after the completion of this, the @c DataVector
2282 * will not own its elements. Any elements it owned prior to this call
2285 * Note: this method may only be called using the most derived
2286 * @c DataVector in the hierarchy.
2290DATAVECTOR& DATAVECTOR::operator= (const DataVector& rhs)
2293 // Ensure we're not being called via a base class.
2294 // This can throw an exception, so this method should not be noexcept.
2295 testInsert ("assignment operator");
2296 clear(); // Release any currently-owned elements.
2297 m_ownPolicy = SG::VIEW_ELEMENTS;
2298 this->setStore ((SG::IConstAuxStore*)0);
2299 this->template initAuxVectorBase<DataVector> (m_ownPolicy,
2300 SG::DEFAULT_TRACK_INDICES);
2301 m_pCont = rhs.m_pCont;
2308 * @brief Move assignment.
2309 * @param rhs The container from which to move.
2311 * Any auxiliary data will be moved along with the container contents.
2314DATAVECTOR& DATAVECTOR::operator= (DATAVECTOR&& rhs)
2317 // Ensure we're not being called via a base class.
2318 // This can throw an exception, so this method should not be noexcept.
2319 testInsert ("assignment operator");
2321 this->clear(); // Release any currently-owned elements.
2323 SG::AuxVectorBase::operator= (std::move (rhs));
2324 this->m_ownPolicy = rhs.m_ownPolicy;
2325 this->m_pCont = std::move (rhs.m_pCont);
2327 delete this->m_deleter;
2328 this->m_deleter = std::move(rhs.m_deleter);
2329 rhs.m_deleter = nullptr;
2331 // Need to reset the container pointer on elements.
2332 this->setIndices (this->begin(), this->end());
2339 * @brief Assignment operator, from an initializer list.
2340 * @param l An initializer list.
2341 * @return This object.
2343 * This is equivalent to @c assign.
2344 * Any existing owned elements will be released.
2345 * The @c DataVector's ownership policy determines whether it will take
2346 * ownership of the new elements.
2350DATAVECTOR& DATAVECTOR::operator= (std::initializer_list<value_type> l)
2352 this->assign (l.begin(), l.end());
2358 * @brief Assign from iterators.
2359 * @param first The start of the range to put in the container.
2360 * @param last The end of the range to put in the container.
2362 * Any existing owned elements will be released.
2363 * The @c DataVector's ownership policy determines whether it will take
2364 * ownership of the new elements.
2367template <class InputIterator>
2368void DATAVECTOR::assign (InputIterator first,
2371 // Ensure we're not being called via a base class.
2372 testInsert ("assign");
2373 clear(); // Release any currently-owned elements.
2374 insert(begin(), first, last);
2379 * @brief Assign from an initializer list.
2380 * @param l An initializer list.
2382 * Any existing owned elements will be released.
2383 * The @c DataVector's ownership policy determines whether it will take
2384 * ownership of the new elements.
2387void DATAVECTOR::assign (std::initializer_list<value_type> l)
2389 this->assign (l.begin(), l.end());
2394 * @brief Destructor.
2396 * If this container owns its elements, the contained elements will
2397 * be deleted as well. Before doing this, the destructor will scan
2398 * for duplicate pointers (takes @f$n \log n@f$ time); duplicates are only
2399 * destroyed once. Duplicates should, however, be considered an error;
2400 * don't rely on this behavior.
2403DATAVECTOR::~DataVector()
2405 if (m_ownPolicy == SG::OWN_ELEMENTS) {
2406 typename PtrVector::iterator new_end =
2407 DataVector_detail::remove_duplicates(m_pCont.begin(), m_pCont.end());
2408 this->doDelete (m_pCont.begin(), new_end);
2414//=== Size and capacity.
2418 * @brief Returns the number of elements in the collection.
2422typename DATAVECTOR::size_type DATAVECTOR::size() const noexcept
2424 return m_pCont.size();
2429 * @brief Returns the number of elements in the collection.
2431 * This version is virtual, to be callable from the AuxData
2436typename DATAVECTOR::size_type DATAVECTOR::size_v() const
2438 return this->size();
2443 * @brief Returns the @c size() of the largest possible collection.
2447typename DATAVECTOR::size_type DATAVECTOR::max_size() const noexcept
2449 return m_pCont.max_size();
2454 * @brief Resizes the collection to the specified number of elements.
2455 * @param sz The new size of the collection.
2457 * Note that this function differs from the standard in that it does
2458 * not allow specifying the value of any inserted elements.
2459 * They will always be 0.
2461 * If the container is shrunk, elements will be deleted as with @c erase().
2464void DATAVECTOR::resize(size_type sz)
2466 if (sz < this->size()) {
2467 this->erase (this->begin()+sz, this->end());
2469 this->m_pCont.insert(this->m_pCont.end(), sz - this->m_pCont.size(), 0);
2470 SG::AuxVectorBase::resize<DataVector> (sz);
2476 * @brief Returns the total number of elements that the collection can hold
2477 * before needing to allocate more memory.
2481typename DATAVECTOR::size_type DATAVECTOR::capacity() const noexcept
2483 return m_pCont.capacity();
2488 * @brief Returns the total number of elements that the collection can hold
2489 * before needing to allocate more memory.
2491 * This version is virtual, to be callable from the AuxData
2496typename DATAVECTOR::size_type DATAVECTOR::capacity_v() const
2503 * @brief Returns @c true if the collection is empty.
2507bool DATAVECTOR::empty() const noexcept
2509 return m_pCont.empty();
2514 * @brief Attempt to preallocate enough memory for a specified number
2516 * @param n Number of elements required.
2520void DATAVECTOR::reserve (size_type n)
2522 m_pCont.reserve (n);
2523 SG::AuxVectorBase::reserve<DataVector> (n);
2528 * @brief Change the vector capacity to match the current size.
2530 * Note: this does not affect auxiliary data.
2534void DATAVECTOR::shrink_to_fit()
2536 m_pCont.shrink_to_fit();
2540//=== Element access.
2544 * @brief Access an element, as an rvalue.
2545 * @param n Array index to access.
2546 * @return The element at @a n.
2548 * No bounds checking is done.
2549 * Note that we return a @c const @c T* rather than a reference.
2553const T* DATAVECTOR::operator[] (size_type n) const
2560 * @brief Access an element, as an rvalue.
2561 * @param n Array index to access.
2562 * @return The element at @a n.
2564 * This is a synonym for operator[] const, to be used when calling from root
2565 * (where we can't readily call just the const version of a method).
2569const T* DATAVECTOR::get (size_type n) const
2571 return do_cast(this->m_pCont[n]);
2576 * @brief Access an element, as an lvalue.
2577 * @param n Array index to access.
2578 * @return Proxy to the element at @a n.
2580 * No bounds checking is done.
2581 * Note that we return a proxy object rather than a reference;
2582 * the proxy will handle deleting an owned element if it's assigned to.
2586typename DATAVECTOR::ElementProxy DATAVECTOR::operator[] (size_type n)
2588 return ElementProxy (m_pCont.begin() + n, this);
2593 * @brief Access an element, as an rvalue.
2594 * @param n Array index to access.
2595 * @return The element at @a n.
2597 * Will raise @c std::out_of_range if the index is out-of-bounds.
2598 * Note that we return a @c const @c T* rather than a reference.
2602const T* DATAVECTOR::at (size_type n) const
2604 return m_pCont.at(n);
2609 * @brief Access an element, as an lvalue.
2610 * @param n Array index to access.
2611 * @return Proxy to the element at @a n.
2613 * Will raise @c std::out_of_range if the index is out-of-bounds.
2614 * Note that we return a proxy object rather than a reference;
2615 * the proxy will handle deleting an owned element if it's assigned to.
2619typename DATAVECTOR::ElementProxy DATAVECTOR::at (size_type n)
2621 // Can't use m_pCont's at here, because we need an iterator.
2622 // So we have to do the bounds check ourselves.
2624 CxxUtils::throw_out_of_range (__PRETTY_FUNCTION__, n, this->size(), this);
2625 return ElementProxy (m_pCont.begin() + n, this);
2630 * @brief Access the first element in the collection as an rvalue.
2631 * @return The first element in the collection.
2633 * No checking is done to ensure that the container is not empty.
2634 * Note that we return a @c const @c T* rather than a reference.
2638const T* DATAVECTOR::front() const
2640 return m_pCont.front();
2645 * @brief Access the last element in the collection as an rvalue.
2646 * @return The last element in the collection.
2648 * No checking is done to ensure that the container is not empty.
2649 * Note that we return a @c const @c T* rather than a reference.
2653const T* DATAVECTOR::back() const
2655 return m_pCont.back();
2660 * @brief Access the first element in the collection as an lvalue.
2661 * @return Proxy to the first element in the collection.
2663 * No checking is done to ensure that the container is not empty.
2664 * Note that we return a proxy object rather than a reference;
2665 * the proxy will handle deleting an owned element if it's assigned to.
2669typename DATAVECTOR::ElementProxy DATAVECTOR::front ()
2671 return ElementProxy (m_pCont.begin(), this);
2676 * @brief Access the last element in the collection as an lvalue.
2677 * @return Proxy to the last element in the collection.
2679 * No checking is done to ensure that the container is not empty.
2680 * Note that we return a proxy object rather than a reference;
2681 * the proxy will handle deleting an owned element if it's assigned to.
2685typename DATAVECTOR::ElementProxy DATAVECTOR::back ()
2687 return ElementProxy (m_pCont.end()-1, this);
2691//=== Iterator creation.
2695 * @brief Return a @c const_iterator pointing at the beginning
2696 * of the collection.
2697 * @return A @c const_iterator.
2699 * Note that dereferencing the iterator will yield a @c const @c T* rather
2704typename DATAVECTOR::const_iterator DATAVECTOR::begin() const noexcept
2706 return m_pCont.begin();
2711 * @brief Return a @c const_iterator pointing past the end
2712 * of the collection.
2713 * @return A @c const_iterator.
2715 * Note that dereferencing the iterator will yield a @c const @c T* rather
2720typename DATAVECTOR::const_iterator DATAVECTOR::end() const noexcept
2722 return m_pCont.end();
2727 * @brief Return an @c iterator pointing at the beginning
2728 * of the collection.
2729 * @return An @c iterator.
2731 * Note that dereferencing the iterator will yield a proxy rather
2732 * than a reference; the proxy will handle deleting an owned element
2733 * if it's assigned to.
2737typename DATAVECTOR::iterator DATAVECTOR::begin() noexcept
2739 return iterator (m_pCont.begin(), this);
2744 * @brief Return an @c iterator pointing past the end
2745 * of the collection.
2746 * @return An @c iterator.
2748 * Note that dereferencing the iterator will yield a proxy rather
2749 * than a reference; the proxy will handle deleting an owned element
2750 * if it's assigned to.
2754typename DATAVECTOR::iterator DATAVECTOR::end() noexcept
2756 return iterator (m_pCont.end(), this);
2761 * @brief Return a @c const_reverse_iterator pointing past the end
2762 * of the collection.
2763 * @return A @c const_reverse_iterator.
2765 * Note that dereferencing the iterator will yield a @c const @c T* rather
2770typename DATAVECTOR::const_reverse_iterator DATAVECTOR::rbegin() const noexcept
2772 return const_reverse_iterator (m_pCont.end());
2777 * @brief Return a @c const_reverse_iterator pointing at the beginning
2778 * of the collection.
2779 * @return A @c const_reverse_iterator.
2781 * Note that dereferencing the iterator will yield a @c const @c T* rather
2786typename DATAVECTOR::const_reverse_iterator DATAVECTOR::rend() const noexcept
2788 return const_reverse_iterator (const_iterator (m_pCont.begin()));
2793 * @brief Return a @c reverse_iterator pointing past the end
2794 * of the collection.
2795 * @return A @c reverse_iterator.
2797 * Note that dereferencing the iterator will yield a proxy rather
2798 * than a reference; the proxy will handle deleting an owned element
2799 * if it's assigned to.
2803typename DATAVECTOR::reverse_iterator DATAVECTOR::rbegin() noexcept
2805 return reverse_iterator(iterator (m_pCont.end(), this));
2810 * @brief Return a @c reverse_iterator pointing at the beginning
2811 * of the collection.
2812 * @return A @c reverse_iterator.
2814 * Note that dereferencing the iterator will yield a proxy rather
2815 * than a reference; the proxy will handle deleting an owned element
2816 * if it's assigned to.
2820typename DATAVECTOR::reverse_iterator DATAVECTOR::rend() noexcept
2822 return reverse_iterator(iterator (m_pCont.begin(), this));
2827 * @brief Return a @c const_iterator pointing at the beginning
2828 * of the collection.
2829 * @return A @c const_iterator.
2831 * Note that dereferencing the iterator will yield a @c const @c T* rather
2836typename DATAVECTOR::const_iterator DATAVECTOR::cbegin() const noexcept
2838 return m_pCont.begin();
2843 * @brief Return a @c const_iterator pointing past the end
2844 * of the collection.
2845 * @return A @c const_iterator.
2847 * Note that dereferencing the iterator will yield a @c const @c T* rather
2852typename DATAVECTOR::const_iterator DATAVECTOR::cend() const noexcept
2854 return m_pCont.end();
2859 * @brief Return a @c const_reverse_iterator pointing past the end
2860 * of the collection.
2861 * @return A @c const_reverse_iterator.
2863 * Note that dereferencing the iterator will yield a @c const @c T* rather
2868typename DATAVECTOR::const_reverse_iterator DATAVECTOR::crbegin() const noexcept
2870 return const_reverse_iterator (m_pCont.end());
2875 * @brief Return a @c const_reverse_iterator pointing at the beginning
2876 * of the collection.
2877 * @return A @c const_reverse_iterator.
2879 * Note that dereferencing the iterator will yield a @c const @c T* rather
2884typename DATAVECTOR::const_reverse_iterator DATAVECTOR::crend() const noexcept
2886 return const_reverse_iterator (const_iterator (m_pCont.begin()));
2890//=== Insertion operations.
2894 * @brief Add an element to the end of the collection.
2895 * @param pElem The element to add to the collection.
2897 * The container's ownership policy will determine if it takes ownership
2898 * of the new element.
2900 * Note: this method may only be called using the most derived
2901 * @c DataVector in the hierarchy.
2903 * Returns the pushed pointer.
2907typename DATAVECTOR::value_type
2908DATAVECTOR::push_back(value_type pElem)
2910 // Ensure we're not being called via a base class.
2911 testInsert ("push_back");
2912 m_pCont.push_back(pElem);
2913 SG::AuxVectorBase::resize<DataVector> (this->size());
2914 if (pElem && this->trackIndices())
2915 this->moveAuxNoClear (this->size()-1, pElem);
2921 * @brief Add an element to the end of the collection.
2922 * @param pElem The element to add to the collection.
2924 * The container must be an owning container.
2926 * Note: this method may only be called using the most derived
2927 * @c DataVector in the hierarchy.
2929 * Returns the pushed pointer.
2933typename DATAVECTOR::value_type
2934DATAVECTOR::push_back(std::unique_ptr<base_value_type> pElem)
2936 // Container must own its elements.
2937 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
2938 SG::throwExcNonowningContainer();
2940 // Ensure we're not being called via a base class.
2941 testInsert ("push_back");
2942 value_type ptr = pElem.release();
2943 m_pCont.push_back(ptr);
2944 SG::AuxVectorBase::resize<DataVector> (this->size());
2945 if (ptr && this->trackIndices())
2946 this->moveAuxNoClear (this->size()-1, ptr);
2952 * @brief Create and add a number of new elements to the end of the container.
2953 * @param n The number of new elements to add.
2954 * @param alloc Functional to call to allocate a new element to push.
2955 * Should be callable like <code>T* = alloc();</code>
2958 * dv.push_new (n, [](){ return new Foo; });
2960 * It may also be useful to allocate from a @c DataPool.
2962 * Note: this method may only be called using the most derived
2963 * @c DataVector in the hierarchy.
2965 * Returns the original size of the vector.
2968template <CxxUtils::detail::AllocationFunction<T> F>
2970DATAVECTOR::push_new(size_type n, F alloc) -> size_type
2972 // Ensure we're not being called via a base class.
2973 testInsert ("push_new");
2974 bool trackIndices = this->trackIndices();
2976 size_t sz = this->m_pCont.size();
2977 this->m_pCont.resize (sz + n);
2978 SG::AuxVectorBase::resize<DataVector> (this->size());
2979 for (size_t i = 0; i < n; i++) {
2981 this->m_pCont[sz+i] = p;
2983 this->moveAuxNoClear (sz+i, p);
2991 * @brief Add an element to the end of the collection.
2992 * @param pElem The element to add to the collection.
2994 * The container's ownership policy will determine if it takes ownership
2995 * of the new element.
2997 * Note: this method may only be called using the most derived
2998 * @c DataVector in the hierarchy.
3000 * For @c DataVector, this is like the same as @c push_back, and
3001 * it returns the pushed element.
3002 * It's included just for interface compatibility with `std::vector`.
3006typename DATAVECTOR::value_type
3007DATAVECTOR::emplace_back(value_type pElem)
3009 return this->push_back (pElem);
3014 * @brief Add a new element to the collection.
3015 * @param position Iterator before which the element will be added.
3016 * @param pElem The element to add to the collection.
3017 * @return An iterator that points to the inserted data.
3019 * The container's ownership policy will determine if it takes ownership
3020 * of the new element.
3022 * Note: this method may only be called using the most derived
3023 * @c DataVector in the hierarchy.
3027typename DATAVECTOR::iterator
3028DATAVECTOR::insert(iterator position, value_type pElem)
3030 // Ensure we're not being called via a base class.
3031 testInsert ("insert");
3032 iterator ret (m_pCont.insert(position.base(), pElem), this);
3033 this->shift (ret - this->begin(), 1);
3034 if (this->trackIndices())
3035 this->moveAuxNoClear (ret-this->begin(), pElem);
3041 * @brief Add a new element to the collection.
3042 * @param position Iterator before which the element will be added.
3043 * @param pElem The element to add to the collection.
3044 * @return An iterator that points to the inserted data.
3046 * The container must be an owning container.
3048 * Note: this method may only be called using the most derived
3049 * @c DataVector in the hierarchy.
3053typename DATAVECTOR::iterator
3054DATAVECTOR::insert(iterator position, std::unique_ptr<base_value_type> pElem)
3056 // Container must own its elements.
3057 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3058 SG::throwExcNonowningContainer();
3060 // Ensure we're not being called via a base class.
3061 testInsert ("insert");
3062 value_type ptr = pElem.release();
3063 iterator ret (m_pCont.insert(position.base(), ptr), this);
3064 this->shift (ret - this->begin(), 1);
3065 if (this->trackIndices())
3066 this->moveAuxNoClear (ret-this->begin(), ptr);
3072 * @brief Add a new element to the collection.
3073 * @param position Iterator before which the element will be added.
3074 * @param pElem The element to add to the collection.
3075 * @return An iterator that points to the inserted data.
3077 * The container's ownership policy will determine if it takes ownership
3078 * of the new element.
3080 * Note: this method may only be called using the most derived
3081 * @c DataVector in the hierarchy.
3083 * For @c DataVector, this is just the same as @c insert.
3084 * It's included just for interface compatibility with `std::vector`.
3088typename DATAVECTOR::iterator
3089DATAVECTOR::emplace(iterator position, value_type pElem)
3091 return this->insert (position, pElem);
3096 * @brief Add a group of new elements to the collection.
3097 * @param position Iterator before which the element will be added.
3098 * @param first The start of the range to put in the container.
3099 * @param last The end of the range to put in the container.
3101 * The container's ownership policy will determine if it takes ownership
3102 * of the new element.
3104 * Note: this method may only be called using the most derived
3105 * @c DataVector in the hierarchy.
3107 * This overload is for the case where the iterator value type
3108 * is convertible to T*.
3111template <class InputIterator,
3112 DataVector_detail::enable_if_ptr_itr<InputIterator, T> >
3115DATAVECTOR::insert (iterator position, InputIterator first, InputIterator last)
3117 static_assert (std::is_same_v<T*, typename DATAVECTOR::value_type>);
3118 // Ensure we're not being called via a base class.
3119 testInsert ("insert");
3120 size_t idx = position - this->begin();
3121 size_t old_sz = this->m_pCont.size();
3122 m_pCont.insert(position.base(), first, last);
3123 size_t n = this->m_pCont.size() - old_sz;
3124 this->shift (idx, n);
3125 this->moveAux (idx, first, last, false, true);
3130 * @brief Add a group of new elements to the collection.
3131 * @param position Iterator before which the element will be added.
3132 * @param first The start of the range to put in the container.
3133 * @param last The end of the range to put in the container.
3135 * The container's ownership policy will determine if it takes ownership
3136 * of the new element.
3138 * Note: this method may only be called using the most derived
3139 * @c DataVector in the hierarchy.
3141 * This overload is for the case where the iterator value type
3142 * is convertible to unique_ptr<T>.
3145template <class InputIterator,
3146 DataVector_detail::enable_if_up_itr<InputIterator, T> >
3149DATAVECTOR::insert (iterator position, InputIterator first, InputIterator last)
3151 // Ensure we're not being called via a base class.
3152 testInsert ("insert");
3153 size_t idx = position - this->begin();
3154 size_t old_sz = this->m_pCont.size();
3155 using CxxUtils::releasing_iterator;
3156 m_pCont.insert(position.base(), releasing_iterator(first), releasing_iterator(last));
3157 size_t n = this->m_pCont.size() - old_sz;
3158 this->shift (idx, n);
3159 this->moveAux (idx, this->begin()+idx, this->begin()+idx+n);
3164 * @brief Add a group of new elements to the collection.
3165 * @param position Iterator before which the element will be added.
3166 * @param l An initializer list.
3168 * The container's ownership policy will determine if it takes ownership
3169 * of the new element.
3171 * Note: this method may only be called using the most derived
3172 * @c DataVector in the hierarchy.
3177DATAVECTOR::insert (iterator position, std::initializer_list<value_type> l)
3179 this->insert (position, l.begin(), l.end());
3184 * @brief Insert the contents of another @c DataVector,
3185 * with auxiliary data copied via move semantics.
3186 * @param position Iterator before which the new elements will be added.
3187 * @param other The vector to add.
3189 * The ownership mode of this vector must be the same as @c other;
3190 * otherwise, an exception will be thrown.
3192 * If both vectors are view vectors, then this is the same
3193 * as <code> insert (position, other.begin(), other.end()) </code>.
3195 * Otherwise, the elements from @c other will be inserted into this vector.
3196 * This vector will take ownership of the elements, and the ownership
3197 * mode of @c other will be changed to @c VIEW_ELEMENTS.
3198 * Auxiliary data for these elements will be transferred,
3199 * using move semantics if possible. (Thus, the auxiliary store
3200 * for @c other may be modified and must not be locked.)
3201 * Finally, the auxiliary store pointer for @c other will be cleared
3202 * (but the store itself will not be deleted since it's not owned
3205 * Note: this method may only be called using the most derived
3206 * @c DataVector in the hierarchy.
3210DATAVECTOR::insertMove (iterator position, DataVector& other)
3212 if (this->m_ownPolicy != other.ownPolicy())
3213 throw SG::ExcInsertMoveOwnershipMismatch();
3215 if (this->m_ownPolicy == SG::VIEW_ELEMENTS) {
3216 this->insert (position, other.begin(), other.end());
3220 testInsert ("insertMove");
3221 size_t pos = position.base() - this->m_pCont.begin();
3222 this->m_pCont.insert (position.base(), other.begin(), other.end());
3223 this->setIndices (this->begin()+pos, this->end(), pos);
3224 other.m_ownPolicy = SG::VIEW_ELEMENTS;
3226 SG::IAuxStore* otherStore = other.getStore();
3228 SG::IAuxStore* store = this->getStore();
3230 if (!store->insertMove (pos, *otherStore))
3233 else if (this->hasStore())
3234 throw SG::ExcConstAuxData ("insertMove");
3235 other.setStore (static_cast<SG::IAuxStore*>(nullptr));
3237 else if (other.hasStore())
3238 throw SG::ExcConstAuxData ("insertMove");
3242//=== Erasure operations.
3246 * @brief Remove element at a given position.
3247 * @param position Iterator pointing to the element to be removed.
3248 * @return An iterator pointing to the next element (or @c end()).
3250 * If the container owns its elements, then the pointed-to element
3255typename DATAVECTOR::iterator DATAVECTOR::erase(iterator position)
3257 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3258 this->clearIndex (position);
3259 iterator ret (this->erase_base (position.base()), this);
3260 this->shift (ret - this->begin() + 1, -1);
3266 * @brief Remove a range of elements.
3267 * @param first Iterator pointing to the first element to be removed.
3268 * @param last Iterator pointing one past the last element to be removed.
3269 * @return An iterator pointing to the element pointed to by @a last
3270 * prior to erasing (or @c end()).
3272 * If the container owns its elements, then the removed elements
3273 * will be deleted. Any duplicates will be removed in this process,
3274 * but don't rely on this.
3278typename DATAVECTOR::iterator DATAVECTOR::erase(iterator first, iterator last)
3280 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3281 this->clearIndices (first, last);
3282 iterator ret (this->erase_base (first.base(), last.base()), this);
3283 this->shift (ret - this->begin() + (last-first), -(last-first));
3289 * @brief Remove the last element from the collection.
3291 * If the container owns its elements, then the removed element
3295void DATAVECTOR::pop_back()
3297 if (!m_pCont.empty()) {
3298 if (m_ownPolicy == SG::OWN_ELEMENTS)
3299 this->doDelete (m_pCont.back());
3301 this->clearIndex (m_pCont.end() - 1);
3303 SG::AuxVectorBase::resize<DataVector> (this->m_pCont.size());
3309 * @brief Erase all the elements in the collection.
3311 * If the container owns its elements, then the removed elements
3312 * will be deleted. Any duplicates will be removed in this process,
3313 * but don't rely on this.
3317void DATAVECTOR::clear()
3319 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3320 this->clearIndices (begin(), end());
3321 this->erase_base (m_pCont.begin(), m_pCont.end());
3322 SG::AuxVectorBase::resize<DataVector> (0);
3330 * @brief Swap this collection with another.
3331 * @param rhs The collection with which to swap.
3333 * Ownership is swapped along with the collection content.
3335 * Note: this method may only be called using the most-derived
3336 * @c DataVector in the hierarchy. The @a rhs must also be
3337 * referenced using the most-derived @c DataVector.
3339 * Warning: If this container has auxiliary data, then this
3340 * is an O(N) operation, not O(1).
3343void DATAVECTOR::swap(DataVector& rhs)
3345 testInsert ("swap");
3346 rhs.testInsert ("swap");
3347 std::swap(m_ownPolicy, rhs.m_ownPolicy);
3348 SG::AuxVectorBase::swap (rhs);
3349 m_pCont.swap(rhs.m_pCont);
3350 std::swap (this->m_deleter, rhs.m_deleter);
3351 this->setIndices (this->begin(), this->end());
3352 rhs.setIndices (rhs.begin(), rhs.end());
3357 * @brief Swap the referents of two @c DataVector iterators.
3358 * @param a The first iterator for the swap.
3359 * @param b The second iterator for the swap.
3362void DATAVECTOR::iter_swap (iterator a, iterator b)
3364 if (a.ownPolicy() != b.ownPolicy()) {
3365 throw SG::ExcBadIterSwap();
3367 a.testInsert ("iter_swap");
3368 b.testInsert ("iter_swap");
3369 std::iter_swap (a.base(), b.base());
3370 DataVector* acont = a.container();
3371 DataVector* bcont = b.container();
3372 if (typename SG::AuxStore_traits<DataVector>::flag())
3373 acont->swapElementsAux (a.base() - acont->stdcont().begin(),
3374 b.base() - bcont->stdcont().begin(),
3382 * @brief Sort the container.
3384 * This just sorts by pointer value, so it's probably not very useful.
3387void DATAVECTOR::sort()
3389 std::sort(m_pCont.begin(), m_pCont.end());
3390 this->resortAux (this->begin(), this->end());
3395 * @brief Sort the container with a user-specified comparison operator.
3396 * @param comp Functional to compare two values.
3399template <class COMPARE>
3400void DATAVECTOR::sort(COMPARE comp)
3402 std::sort(m_pCont.begin(), m_pCont.end(), comp);
3403 this->resortAux (this->begin(), this->end());
3407//=== Non-standard operations.
3411 * @brief Swap one element out of the container.
3412 * @param index Index of the element in the container to swap.
3413 * @param newElement New element to put in the container.
3415 * @param oldElem Reference to receive the element removed from the
3418 * Reference @a oldElem is initialized with element @a index of the
3419 * collection (no bounds checking). Then element @a index is set
3420 * to @newElem. If the collection owns its elements, then it will
3421 * take ownership of @a newElem and release (without deleting)
3422 * the element returned through @a oldElem.
3424 * Note: this method may only be called using the most derived
3425 * @c DataVector in the hierarchy.
3429DATAVECTOR::swapElement (size_type index,
3433 testInsert ("swapElement");
3434 oldElem = m_pCont[index];
3435 this->clearIndex (iterator (m_pCont.begin() + index, this));
3436 m_pCont[index] = newElem;
3437 this->moveAux (index, newElem);
3442 * @brief Swap one element out of the container.
3443 * @param pos The element in the container to swap.
3444 * @param newElem New element to put in the container.
3446 * @param oldElem Reference to receive the element removed from the
3449 * Reference @a oldElem is initialized with element @a pos of the
3450 * collection (no bounds checking). Then element @a index is set
3451 * to @c newElem. If the collection owns its elements, then it will
3452 * take ownership of @a newElem and release (without deleting)
3453 * the element returned through @a oldElem.
3455 * Note: this method may only be called using the most derived
3456 * @c DataVector in the hierarchy.
3460DATAVECTOR::swapElement (iterator pos,
3464 testInsert ("swapElement");
3465 oldElem = *pos.base();
3466 this->clearIndex (pos);
3467 *pos.base() = newElem;
3468 this->moveAux (pos.base() - this->m_pCont.begin(), newElem);
3473 * @brief Swap one element out of the container.
3474 * @param index Index of the element in the container to swap.
3475 * @param newElement New element to put in the container.
3477 * @param oldElem Reference to receive the element removed from the
3480 * Reference @a oldElem is initialized with element @a index of the
3481 * collection (no bounds checking). Then element @a index is set
3484 * The collection must own its elements to use its interface.
3485 * The collection will take ownership of @c newElem and will return
3486 * ownership of @c oldElem.
3488 * Note: this method may only be called using the most derived
3489 * @c DataVector in the hierarchy.
3493DATAVECTOR::swapElement (size_type index,
3494 std::unique_ptr<base_value_type> newElem,
3495 std::unique_ptr<base_value_type>& oldElem)
3497 // Container must own its elements.
3498 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3499 SG::throwExcNonowningContainer();
3501 testInsert ("swapElement");
3502 oldElem = std::unique_ptr<base_value_type> (m_pCont[index]);
3503 this->clearIndex (iterator (m_pCont.begin() + index, this));
3504 value_type ptr = newElem.release();
3505 m_pCont[index] = ptr;
3506 this->moveAux (index, ptr);
3511 * @brief Swap one element out of the container.
3512 * @param pos The element in the container to swap.
3513 * @param newElem New element to put in the container.
3515 * @param oldElem Reference to receive the element removed from the
3518 * Reference @a oldElem is initialized with element @a pos of the
3519 * collection (no bounds checking). Then element @a index is set
3522 * The collection must own its elements to use its interface.
3523 * The collection will take ownership of @c newElem and will return
3524 * ownership of @c oldElem.
3526 * Note: this method may only be called using the most derived
3527 * @c DataVector in the hierarchy.
3531DATAVECTOR::swapElement (iterator pos,
3532 std::unique_ptr<base_value_type> newElem,
3533 std::unique_ptr<base_value_type>& oldElem)
3535 // Container must own its elements.
3536 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3537 SG::throwExcNonowningContainer();
3539 testInsert ("swapElement");
3540 oldElem = std::unique_ptr<base_value_type> (*pos.base());
3541 this->clearIndex (pos);
3542 value_type ptr = newElem.release();
3544 this->moveAux (pos.base() - this->m_pCont.begin(), ptr);
3549 * @brief Return the underlying @c std::vector of the container.
3550 * @return Reference to the @c std::vector actually holding the collection.
3552 * Note that @c DataVector<T>::stdcont does not necessarily return
3553 * a @c std::vector<T*> if @c DataVector inheritance is being used.
3557const typename DATAVECTOR::PtrVector& DATAVECTOR::stdcont() const
3564 * @brief Return the ownership policy setting for this container.
3568SG::OwnershipPolicy DATAVECTOR::ownPolicy() const
3575 * @brief Erase all the elements in the collection, and reset
3576 * the ownership mode.
3577 * @param ownPolicy The new ownership policy of the container.
3578 * @param trackIndices The index tracking policy.
3580 * If the container owns its elements, then the removed elements
3581 * will be deleted. Any duplicates will be removed in this process,
3582 * but don't rely on this.
3585void DATAVECTOR::clear (SG::OwnershipPolicy ownPolicy)
3588 m_ownPolicy = ownPolicy;
3589 this->template initAuxVectorBase<DataVector> (ownPolicy,
3590 SG::DEFAULT_TRACK_INDICES);
3595 * @brief Erase all the elements in the collection, and reset
3596 * the ownership mode.
3597 * @param ownPolicy The new ownership policy of the container.
3598 * @param trackIndices The index tracking policy.
3600 * If the container owns its elements, then the removed elements
3601 * will be deleted. Any duplicates will be removed in this process,
3602 * but don't rely on this.
3605void DATAVECTOR::clear (SG::OwnershipPolicy ownPolicy,
3606 SG::IndexTrackingPolicy trackIndices)
3609 m_ownPolicy = ownPolicy;
3610 this->template initAuxVectorBase<DataVector> (ownPolicy, trackIndices);
3615 * @brief Return the DV/DL info struct for this class.
3617 * This can be used to make sure that it's instantiated.
3620const DataModel_detail::DVLInfoBase& DATAVECTOR::dvlinfo()
3622 static const DataModel_detail::DVLInfo<DataVector<T, DataModel_detail::NoBase> > info;
3628 * @brief Erase all the elements in the collection, and change
3629 * how elements are to be deleted.
3630 * @param deleter Object to be used to delete object.
3631 * (The DataVector does not take ownership.)
3632 * Passing nullptr will change back to the default.
3634 * If the container owns its elements, then the removed elements
3635 * will be deleted. Any duplicates will be removed in this process,
3636 * but don't rely on this.
3637 * After the current elements are deleted, the Deleter object is changed.
3640void DATAVECTOR::clear (std::unique_ptr<Deleter> deleter)
3643 delete this->m_deleter;
3644 this->m_deleter = deleter.release();
3649 * @brief Return the DV/DL info struct for this class.
3651 * This can be used to make sure that it's instantiated.
3654const DataModel_detail::DVLInfoBase& DATAVECTOR::dvlinfo_v() const
3656 return DATAVECTOR::dvlinfo();
3661 * @brief Return the offset of a base @c DataVector class.
3662 * @param ti @c std::type_info of the desired class.
3664 * If @c ti represents a @c DataVector base class of this one,
3665 * then return the offset of that base class. Otherwise, return -1.
3667 * This function is here due to limitations of root 6, which can't
3668 * calculate these offsets correctly from the dictionary if
3669 * virtual derivation is used.
3672int DATAVECTOR::baseOffset (const std::type_info& ti)
3674 if (typeid(DataVector) == ti)
3681 * @brief Convert to @c AuxVectorBase.
3683 * Needed to get @x AuxVectorBase from a @c ConstDataVector.
3684 * Present in @c DataVector as well for consistency.
3688const SG::AuxVectorBase& DATAVECTOR::auxbase() const
3694//=== Internal operations.
3698 * @brief Helper for @c baseOffset.
3699 * @param p Pointer to the start of the top-level object.
3700 * @param dv Reference to the DataVector object.
3701 * @param ti @c std::type_info of the desired class.
3703 * If @c ti represents a @c DataVector base class of this one,
3704 * then return the offset of that base class. Otherwise, return -1.
3708int DATAVECTOR::baseOffset1 (const char* p, const DataVector& dv,
3709 const std::type_info& ti)
3711 if (typeid(DataVector) == ti)
3712 return reinterpret_cast<const char*>(&dv) - p;
3718 * @brief Reset indices / reorder aux data after elements have been permuted.
3719 * @param beg Start of the range of elements to process.
3720 * @param end End of the range of elements to process.
3722 * Call this after some operation that has permuted the elements in the
3723 * container (such as sort). The index information in the elements
3724 * will be used to permute all auxiliary data in the same way.
3725 * Finally, all the indices will be reset in the correct order.
3728void DATAVECTOR::resortAux (iterator beg, iterator end)
3730 if (typename SG::AuxStore_traits<DataVector>::flag() &&
3731 beg >= this->begin() && end <= this->end())
3733 SG::AuxVectorBase::resortAux (beg-begin(), beg, end);
3739 * @brief Handle element assignment.
3740 * @param pos Position in the container to assign.
3741 * @param newElem The new element to assign.
3743 * The old element is freed if this container owns elements.
3744 * Auxiliary data are copied if appropriate.
3747void DATAVECTOR::assignElement (typename BaseContainer::iterator pos,
3750 testInsert ("assignElement");
3751 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
3752 this->doDelete (*pos);
3754 this->clearIndex (iterator (pos, this));
3756 this->moveAux (pos - this->m_pCont.begin(), newElem);
3761 * @brief Handle element assignment.
3762 * @param pos Position in the container to assign.
3763 * @param newElem The new element to assign.
3765 * The container must own its elements.
3766 * Auxiliary data are copied if appropriate.
3770DATAVECTOR::assignElement (typename BaseContainer::iterator pos,
3771 std::unique_ptr<base_value_type> newElem)
3773 // Container must own its elements.
3774 if (this->m_ownPolicy != SG::OWN_ELEMENTS)
3775 SG::throwExcNonowningContainer();
3777 testInsert ("assignElement");
3778 this->doDelete (*pos);
3779 value_type ptr = newElem.release();
3781 this->moveAux (pos - this->m_pCont.begin(), ptr);
3786 * @brief Handle element assignment from a base pointer.
3787 * @param pos Position in the container to assign.
3788 * @param newElem The new element to assign.
3790 * The old element is freed if this container owns elements.
3791 * Auxiliary data are copied if appropriate.
3794void DATAVECTOR::assignBaseElement (typename BaseContainer::iterator pos,
3795 typename BaseContainer::value_type newElem)
3797 testInsert ("assignBaseElement");
3798 if (this->m_ownPolicy == SG::OWN_ELEMENTS)
3799 this->doDelete (*pos);
3801 this->clearIndex (iterator (pos, this));
3803 if (typename SG::AuxStore_traits<DataVector>::flag())
3804 this->moveAux (pos - this->m_pCont.begin(), newElem);
3809 * @brief Shift the auxiliary elements of the container.
3810 * @param pos The starting index for the shift.
3811 * @param offs The (signed) amount of the shift.
3813 * The elements in the container should have already been shifted;
3814 * this operation will then adjust the element indices and also shift
3815 * the elements in the vectors for all aux data items.
3816 * @c offs may be either positive or negative.
3818 * If @c offs is positive, then the container is growing.
3819 * The container size should be increased by @c offs,
3820 * the element at @c pos moved to @c pos + @c offs,
3821 * and similarly for following elements.
3822 * The elements between @c pos and @c pos + @c offs should
3823 * be default-initialized.
3825 * If @c offs is negative, then the container is shrinking.
3826 * The element at @c pos should be moved to @c pos + @c offs,
3827 * and similarly for following elements.
3828 * The container should then be shrunk by @c -offs elements
3829 * (running destructors as appropriate).
3832void DATAVECTOR::shift (size_t pos, ptrdiff_t offs)
3834 SG::AuxVectorBase::shift (*this, pos, offs);
3839 * @brief Helper to shorten calls to @c DataModel_detail::DVLCast.
3840 * @param p The value to convert.
3841 * @return The value as a @c const @c T*.
3843 * This is a no-op for the base class.
3847const T* DATAVECTOR::do_cast (const typename PtrVector::value_type p)
3854 * @brief Helper to shorten calls to @c DataModel_detail::DVLCast.
3855 * @param p The value to convert.
3856 * @return The value as a @c T*.
3858 * This is a no-op for the base class.
3862T* DATAVECTOR::do_cast_nc (typename PtrVector::value_type p)
3869 * @brief Find the most-derived @c DataVector class in the hierarchy.
3870 * @return The @c type_info for the class for which this method gets run.
3872 * This is used to generate a nice error message when the most-derived
3873 * check for insertions fails.
3874 * Every @c DataVector defines this virtual method, so when it's
3875 * called, the one corresponding to the most-derived @c DataVector
3879const std::type_info& DATAVECTOR::dv_typeid() const
3881 return typeid(DataVector);
3886 * @brief Helper for @c erase(). Remove element at a given position.
3887 * @param position Iterator pointing to the element to be removed.
3888 * @return An iterator pointing to the next element (or @c end()).
3890 * This function factors out common code between @c erase() in the
3891 * base and derived @c DataVector classes. It deals with the
3892 * @c std::vector iterators directly.
3895typename DATAVECTOR::PtrVector::iterator
3896DATAVECTOR::erase_base(typename PtrVector::iterator position)
3898 if (m_ownPolicy == SG::OWN_ELEMENTS && position != m_pCont.end())
3899 this->doDelete (*position);
3900 return m_pCont.erase(position);
3905 * @brief Helper for @c erase(). Remove a range of elements.
3906 * @param first Iterator pointing to the first element to be removed.
3907 * @param last Iterator pointing one past the last element to be removed.
3908 * @return An iterator pointing to the element pointed to by @a last
3909 * prior to erasing (or @c end()).
3911 * This function factors out common code between @c erase() in the
3912 * base and derived @c DataVector classes. It deals with the
3913 * @c std::vector iterators directly.
3916typename DATAVECTOR::PtrVector::iterator
3917DATAVECTOR::erase_base(typename PtrVector::iterator first,
3918 typename PtrVector::iterator last)
3920 if (first == last) return first;
3921 if (m_ownPolicy == SG::OWN_ELEMENTS) {
3922 typename PtrVector::iterator new_end =
3923 DataVector_detail::remove_duplicates(first, last);
3924 this->doDelete (first, new_end);
3926 return m_pCont.erase(first, last);
3931 * @brief Delete an element
3932 * @param p The element to delete.
3936void DATAVECTOR::doDelete (value_type p)
3939 m_deleter->doDelete (p);
3948 * @brief Delete a range of elements
3949 * @param first Start of range to delete.
3950 * @param last End of range to delete.
3954void DATAVECTOR::doDelete (typename PtrVector::iterator first,
3955 typename PtrVector::iterator last)
3958 m_deleter->doDelete (first, last);
3961 for (; first != last; ++first) {
3969 * @brief Test if we can insert; raise an exception if not.
3970 * @param op Description of the attempted operation.
3972 * In order to maintain type-safety, we can only allow insertions
3973 * using the most-derived instance of @c DataVector. This checks
3974 * this by testing the @c m_isMostDerived, which is set by the constructors
3975 * to true only for the most-derived instance.
3976 * If the test fails, we call to potentially out-of-line code to continue.
3980void DATAVECTOR::testInsert (const char* op)
3982 if (ATHCONTAINERS_LIKELY (m_isMostDerived))
3984 this->testInsertOol (op);
3989 * @brief Test if we can insert; raise an exception if not.
3990 * @param op Description of the attempted operation.
3992 * This continues the test of @c testInsert. There is one case
3993 * where @c m_isMostDerived may not be set correctly. If this container
3994 * was made via copy construction, then all the @c m_isMostDerived flags
3995 * will be false. So we call @c setMostDerived to set the flags correctly
3996 * and test again. If the test fails again, then we raise an exception.
3999void DATAVECTOR::testInsertOol (const char* op)
4001 this->setMostDerived();
4002 if (!m_isMostDerived)
4003 throw SG::ExcInsertionInBaseClass (op, typeid(DataVector), dv_typeid());
4008 * @brief Clear @c m_isMostDerived for this instance and for all bases.
4010 * Called from the constructor after setting @c m_isMostDerived.
4014void DATAVECTOR::clearMostDerived()
4016 this->m_isMostDerived = false;
4021 * @brief Set @c m_isMostDerived for this instance and clear it for all bases.
4023 * Called from @c testInsert if the test fails. The flag may not have
4024 * been set if this container was made via copy construction, so set
4025 * it appropriately now so we can test again.
4028void DATAVECTOR::setMostDerived()
4030 m_isMostDerived = true;
4037//****************************************************************************
4038// Free function implementations.
4043 * @brief Vector equality comparison.
4044 * @param a A @c DataVector.
4045 * @param b A @c DataVector of the same type as @a b.
4046 * @return True iff the size and elements of the vectors are equal.
4048 * This is an equivalence relation. It is linear in the size of the
4049 * vectors. Vectors are considered equivalent if their sizes are equal,
4050 * and if corresponding elements compare equal.
4053bool operator== (const DataVector<T>& a, const DataVector<T>& b)
4055 return a.stdcont() == b.stdcont();
4059/// Based on operator==
4061bool operator!= (const DataVector<T>& a, const DataVector<T>& b)
4063 return a.stdcont() != b.stdcont();
4068 * @brief Vector ordering relation.
4069 * @param a A @c DataVector.
4070 * @param b A @c DataVector of the same type as @a x.
4071 * @return True iff @a a is lexicographically less than @a b.
4073 * This is a total ordering relation. It is linear in the size of the
4074 * vectors. Comparisons are done on the pointer values of the elements.
4076 * See @c std::lexicographical_compare() for how the determination is made.
4079bool operator< (const DataVector<T>& a, const DataVector<T>& b)
4081 return a.stdcont() < b.stdcont();
4085/// Based on operator<
4087bool operator> (const DataVector<T>& a, const DataVector<T>& b)
4089 return a.stdcont() > b.stdcont();
4093/// Based on operator<
4095bool operator<= (const DataVector<T>& a, const DataVector<T>& b)
4097 return a.stdcont() <= b.stdcont();
4101/// Based on operator<
4103bool operator>= (const DataVector<T>& a, const DataVector<T>& b)
4105 return a.stdcont() >= b.stdcont();
4109/// See @c DataVector<T, BASE>::swap().
4111void swap (DataVector<T>& a, DataVector<T>& b)
4118 * @brief Specialization of @c ClassName for @c DataVector.
4120 * This overrides the default implementation of @c ClassName
4121 * to hide @c DataVector's second template parameter.
4124std::string ClassName<DataVector<T> >::name()
4126 std::string out = "DataVector<";
4127 out += ClassName<T>::name();
4128 if (out[out.size()-1] == '>')
4135#ifndef XAOD_STANDALONE
4138// Set up initialization of element type BaseInfo
4139namespace DataVector_detail {
4140#define DVLTYPE DataVector
4141#include "AthContainers/tools/DVLEltBaseInfo.icc"
4143} // namespace DataVector_detail
4149namespace DataVector_detail {
4150/// Dummy implementation for the DVLEltBaseInit structure
4152struct DVLEltBaseInit {};
4153} // namespace DataVector_detail
4156#endif // not XAOD_STANDALONE
4160// We need to specialize the function that DVLInfo uses to create the container
4163 * @brief Construct a new container.
4164 * @param nreserve Number of elements for which to reserve space.
4165 * (Ignored if not appropriate.)
4166 * @param cont[out] Pointer to the constructed container.
4167 * (Returned via an argument to allow for template
4168 * argument deduction.)
4170 * Specialization for DataVector.
4173void dvl_makecontainer (size_t nreserve, DataVector<T>*& cont)
4175 cont = new DataVector<T> (SG::VIEW_ELEMENTS);
4176 cont->reserve (nreserve);