2 * Copyright Michael Schellenberger Costa
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 /*! \brief Definition of a span object
35 * \details A "span" is an "array view" type for holding a view of contiguous
36 * data. The "span" object does not own the data itself.
42 using pointer
= value_type
*;
43 using const_pointer
= const value_type
*;
44 using reference
= value_type
&;
45 using const_reference
= const value_type
&;
46 using iterator
= pointer
;
47 using const_iterator
= const_pointer
;
48 using reverse_iterator
= std::reverse_iterator
<iterator
>;
49 using const_reverse_iterator
= std::reverse_iterator
<const_iterator
>;
50 using size_type
= std::size_t;
51 using difference_type
= std::ptrdiff_t;
53 /*! \brief Compiler generated default constructor
55 constexpr span() = default;
57 /*! \brief Constructor taking a pointer and the length of the span
58 * \param[in] data Pointer to the underlying data array
59 * \param[in] length The size of the span
61 constexpr span(pointer data
, const size_type length
)
62 : data
{ data
} , length
{ length
} {}
64 /*! \brief Returns an iterator to the begin of the span
67 constexpr iterator
begin() noexcept
{
71 /*! \brief Returns a const_iterator to the begin of the span
74 constexpr const_iterator
begin() const noexcept
{
78 /*! \brief Returns an iterator to the end of the span
79 * \return data + length
81 constexpr iterator
end() noexcept
{
82 return std::next(data
, length
);
85 /*! \brief Returns a const_iterator to the end of the span
86 * \return data + length
88 constexpr const_iterator
end() const noexcept
{
89 return std::next(data
, length
);
92 /*! \brief Returns a const_iterator to the begin of the span
95 constexpr const_iterator
cbegin() const noexcept
{
99 /*! \brief Returns a const_iterator to the end of the span
100 * \return data + length
102 constexpr const_iterator
cend() const noexcept
{
103 return std::next(data
, length
);
106 /*! \brief Returns a reverse_iterator to the end of the span
107 * \return reverse_iterator(end())
109 constexpr reverse_iterator
rbegin() noexcept
{
110 return reverse_iterator(end());
113 /*! \brief Returns a const_reverse_iterator to the end of the span
114 * \return reverse_iterator(end())
116 constexpr const_reverse_iterator
rbegin() const noexcept
{
117 return const_reverse_iterator(end());
120 /*! \brief Returns a reverse_iterator to the begin of the span
121 * \return reverse_iterator(begin())
123 constexpr reverse_iterator
rend() noexcept
{
124 return reverse_iterator(begin());
127 /*! \brief Returns a const_reverse_iterator to the begin of the span
128 * \return reverse_iterator(begin())
130 constexpr const_reverse_iterator
rend() const noexcept
{
131 return const_reverse_iterator(begin());
134 /*! \brief Returns a const_reverse_iterator to the end of the span
137 constexpr const_reverse_iterator
crbegin() const noexcept
{
138 return const_reverse_iterator(cend());
141 /*! \brief Returns a const_reverse_iterator to the begin of the span
144 constexpr const_reverse_iterator
crend() const noexcept
{
145 return const_reverse_iterator(cbegin());
148 /*! \brief Unchecked access operator
149 * \param[in] index Index of the element we want to access
150 * \return *(std::next(data, index))
152 constexpr reference
operator[](const size_type index
) noexcept
{
153 assert(length
> index
);
154 return *(std::next(data
, index
));
157 /*! \brief Unchecked const access operator
158 * \param[in] index Index of the element we want to access
159 * \return *(std::next(data, index))
161 constexpr const_reference
operator[](const size_type index
) const noexcept
{
162 assert(length
> index
);
163 return *(std::next(data
, index
));
166 /*! \brief Returns a reference to the last element of the span
167 * \return *(std::next(data, length - 1))
169 constexpr reference
back() noexcept
{
171 return *(std::next(data
, length
- 1));
174 /*! \brief Returns a const_reference to the last element of the span
175 * \return *(std::next(data, length - 1))
177 constexpr const_reference
back() const noexcept
{
179 return *(std::next(data
, length
- 1));
182 /*! \brief Returns a reference to the first element of the span
185 constexpr reference
front() noexcept
{
190 /*! \brief Returns a const_reference to the first element of the span
193 constexpr const_reference
front() const noexcept
{
198 /*! \brief Returns true if the span is empty
199 * \return length == 0
201 constexpr bool empty() const noexcept
{
205 /*! \brief Returns the size of the span
206 * \return length == 0
208 constexpr size_type
size() const noexcept
{
212 /*! \brief Decreases the size of the span by 1
214 constexpr void pop_back() noexcept
{
219 /*! \brief Clears the span
221 constexpr void clear() noexcept
{
227 pointer data
{ nullptr }; //!> Pointer to the underlying data array
228 size_type length
{ 0 }; //!> Size of the span