2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
6 * @file AlignedDynArray.icc
7 * @date 26th November 2019
8 * @author Anthony Morley, Christos Anastopoulos
9 * @brief AlignedDynArray implementation
16 template<typename T, size_t ALIGNMENT>
17 inline AlignedDynArray<T, ALIGNMENT>::AlignedDynArray(const size_type n)
22 const size_t bufferSize = n * sizeof(T);
23 m_buffer = static_cast<T*>(std::aligned_alloc(ALIGNMENT, bufferSize));
24 std::uninitialized_default_construct(m_buffer, m_buffer + m_size);
27 template<typename T, size_t ALIGNMENT>
28 inline AlignedDynArray<T, ALIGNMENT>::AlignedDynArray(const size_type n,
33 const size_t bufferSize = n * sizeof(T);
34 m_buffer = static_cast<T*>(std::aligned_alloc(ALIGNMENT, bufferSize));
35 std::uninitialized_fill(m_buffer, m_buffer + m_size, value);
38 template<typename T, size_t ALIGNMENT>
39 inline AlignedDynArray<T, ALIGNMENT>::AlignedDynArray(
40 AlignedDynArray&& other) noexcept
45 m_buffer = other.m_buffer;
46 m_size = other.m_size;
47 // set other to invalid
48 other.m_buffer = nullptr;
52 template<typename T, size_t ALIGNMENT>
53 inline AlignedDynArray<T, ALIGNMENT>&
54 AlignedDynArray<T, ALIGNMENT>::operator=(AlignedDynArray&& other) noexcept
56 // cleanup this object
59 m_buffer = other.m_buffer;
60 m_size = other.m_size;
61 // set other to invalid
62 other.m_buffer = nullptr;
68 template<typename T, size_t ALIGNMENT>
69 inline AlignedDynArray<T, ALIGNMENT>::~AlignedDynArray()
74 template<typename T, size_t ALIGNMENT>
75 inline typename AlignedDynArray<T, ALIGNMENT>::pointer
76 AlignedDynArray<T, ALIGNMENT>::buffer() noexcept
78 return std::assume_aligned<ALIGNMENT>(m_buffer);
81 template<typename T, size_t ALIGNMENT>
82 inline typename AlignedDynArray<T, ALIGNMENT>::const_pointer
83 AlignedDynArray<T, ALIGNMENT>::buffer() const noexcept
85 return std::assume_aligned<ALIGNMENT>(std::as_const(m_buffer));
88 template<typename T, size_t ALIGNMENT>
89 inline typename AlignedDynArray<T, ALIGNMENT>::reference
90 AlignedDynArray<T, ALIGNMENT>::operator[](
91 const AlignedDynArray<T, ALIGNMENT>::size_type pos) noexcept
96 template<typename T, size_t ALIGNMENT>
97 inline typename AlignedDynArray<T, ALIGNMENT>::const_reference
98 AlignedDynArray<T, ALIGNMENT>::operator[](
99 const AlignedDynArray<T, ALIGNMENT>::size_type pos) const noexcept
101 return m_buffer[pos];
104 template<typename T, size_t ALIGNMENT>
105 inline typename AlignedDynArray<T, ALIGNMENT>::iterator
106 AlignedDynArray<T, ALIGNMENT>::begin() noexcept
108 return std::assume_aligned<ALIGNMENT>(iterator(buffer()));
111 template<typename T, size_t ALIGNMENT>
112 inline typename AlignedDynArray<T, ALIGNMENT>::const_iterator
113 AlignedDynArray<T, ALIGNMENT>::begin() const noexcept
115 return std::assume_aligned<ALIGNMENT>(const_iterator(buffer()));
118 template<typename T, size_t ALIGNMENT>
119 inline typename AlignedDynArray<T, ALIGNMENT>::iterator
120 AlignedDynArray<T, ALIGNMENT>::end() noexcept
122 return iterator(buffer() + m_size);
125 template<typename T, size_t ALIGNMENT>
126 inline typename AlignedDynArray<T, ALIGNMENT>::const_iterator
127 AlignedDynArray<T, ALIGNMENT>::end() const noexcept
129 return const_iterator(buffer() + m_size);
132 template<typename T, size_t ALIGNMENT>
133 inline typename AlignedDynArray<T, ALIGNMENT>::size_type
134 AlignedDynArray<T, ALIGNMENT>::size() const noexcept
139 template<typename T, size_t ALIGNMENT>
141 AlignedDynArray<T, ALIGNMENT>::empty() const noexcept
143 return (size() == 0);
146 // private cleanup helper
147 template<typename T, size_t ALIGNMENT>
149 AlignedDynArray<T, ALIGNMENT>::cleanup()
152 for (size_type pos = 0; pos < m_size; ++pos) {
159 } // namespace GSFUtils