GEOS
Span.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_SPAN_HPP
21 #define GEOS_COMMON_SPAN_HPP
22 
23 #include "codingUtilities/traits.hpp"
24 #include "common/logger/Logger.hpp"
25 
26 #include <memory>
27 #include <iterator>
28 #include <type_traits>
29 
30 namespace geos
31 {
32 
40 template< typename T >
41 class Span
42 {
43 public:
44 
46  using element_type = T;
47 
49  using value_type = std::remove_cv_t< T >;
50 
53 
55  using difference_type = std::ptrdiff_t;
56 
60  constexpr Span() noexcept = default;
61 
67  Span( T * const ptr, size_type const size ) noexcept
68  : m_data( ptr ),
69  m_size( size )
70  {}
71 
78  template< typename ITER >
79  constexpr Span( ITER const begin, ITER const end ) noexcept
80  : Span( std::addressof( *begin ), std::distance( begin, end ) )
81  {}
82 
88  template< int N >
89  constexpr Span( T (& arr)[N] ) noexcept
90  : Span( std::addressof( arr[0] ), N )
91  {}
92 
99  template< typename R, typename std::enable_if_t< traits::is_range_like< R > > * = nullptr >
100  constexpr Span( R const & range )
101  : Span( range.begin(), range.end() )
102  {}
103 
107  constexpr size_type size() const noexcept
108  {
109  return m_size;
110  }
111 
115  constexpr size_type size_bytes() const noexcept
116  {
117  return size() * sizeof( element_type );
118  }
119 
123  constexpr bool empty() const noexcept
124  {
125  return m_size == 0;
126  }
127 
131  constexpr T * data() const noexcept
132  {
133  return m_data;
134  }
135 
139  constexpr T * begin() const noexcept
140  {
141  return m_data;
142  }
143 
147  constexpr T * end() const noexcept
148  {
149  return m_data + m_size;
150  }
151 
155  constexpr std::reverse_iterator< T * > rbegin() const noexcept
156  {
157  return std::reverse_iterator< T * >( m_data + m_size - 1 );
158  }
159 
163  constexpr std::reverse_iterator< T * > rend() const noexcept
164  {
165  return std::reverse_iterator< T * >( m_data - 1 );
166  }
167 
172  T & front() const
173  {
174  GEOS_ASSERT_GT( m_size, 0 );
175  return m_data[0];
176  }
177 
182  T & back() const
183  {
184  GEOS_ASSERT_GT( m_size, 0 );
185  return m_data[m_size-1];
186  }
187 
193  T & operator[]( size_type const i ) const
194  {
195  GEOS_ASSERT_GT( m_size, i );
196  return m_data[i];
197  }
198 
203  Span< element_type > first( size_type const count ) const
204  {
205  GEOS_ASSERT_GE( m_size, count );
206  return { m_data, count };
207  }
208 
213  Span< element_type > last( size_type const count ) const
214  {
215  GEOS_ASSERT_GE( m_size, count );
216  return { m_data + (m_size - count), count };
217  }
218 
224  Span< element_type > subspan( size_type const offset, size_type const count ) const
225  {
226  GEOS_ASSERT_GE( m_size, offset + count );
227  return { m_data + offset, count };
228  }
229 
230 private:
231 
233  T * m_data{};
234 
236  size_type m_size{};
237 
238 };
239 
240 }
241 
242 #endif //GEOS_COMMON_SPAN_HPP
#define GEOS_ASSERT_GT(lhs, rhs)
Assert that one value compares greater than the other in debug builds.
Definition: Logger.hpp:440
#define GEOS_ASSERT_GE(lhs, rhs)
Assert that one value compares greater than or equal to the other in debug builds.
Definition: Logger.hpp:455
Lightweight non-owning wrapper over a contiguous range of elements.
Definition: Span.hpp:42
Span< element_type > last(size_type const count) const
Definition: Span.hpp:213
constexpr bool empty() const noexcept
Definition: Span.hpp:123
constexpr Span(R const &range)
Construct a span from a range-like object (anything that has begin() and end()).
Definition: Span.hpp:100
constexpr T * data() const noexcept
Definition: Span.hpp:131
std::remove_cv_t< T > value_type
Type of underlying value.
Definition: Span.hpp:49
constexpr std::reverse_iterator< T * > rend() const noexcept
Definition: Span.hpp:163
constexpr Span() noexcept=default
Construct an empty span.
T & front() const
Definition: Span.hpp:172
T & operator[](size_type const i) const
Definition: Span.hpp:193
constexpr T * begin() const noexcept
Definition: Span.hpp:139
constexpr size_type size_bytes() const noexcept
Definition: Span.hpp:115
constexpr Span(T(&arr)[N]) noexcept
Construct a span from a c-array.
Definition: Span.hpp:89
constexpr T * end() const noexcept
Definition: Span.hpp:147
T element_type
Type of range element.
Definition: Span.hpp:46
constexpr std::reverse_iterator< T * > rbegin() const noexcept
Definition: Span.hpp:155
std::ptrdiff_t difference_type
Type used for indexing the range.
Definition: Span.hpp:55
Span< element_type > subspan(size_type const offset, size_type const count) const
Definition: Span.hpp:224
constexpr size_type size() const noexcept
Definition: Span.hpp:107
T & back() const
Definition: Span.hpp:182
constexpr Span(ITER const begin, ITER const end) noexcept
Construct a span from pair of iterators.
Definition: Span.hpp:79
Span< element_type > first(size_type const count) const
Definition: Span.hpp:203
std::size_t size_type
Type used for indexing the range.
Definition: Span.hpp:52
std::size_t size_t
Unsigned size type.
Definition: DataTypes.hpp:79