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  : m_data( begin == end ? nullptr : std::addressof( *begin ) ),
81  m_size( std::distance( begin, end ) )
82  {}
83 
89  template< int N >
90  constexpr Span( T (& arr)[N] ) noexcept
91  : Span( std::addressof( arr[0] ), N )
92  {}
93 
100  template< typename R, typename std::enable_if_t< traits::is_range_like< R > > * = nullptr >
101  constexpr Span( R const & range )
102  : Span( range.begin(), range.end() )
103  {}
104 
108  constexpr size_type size() const noexcept
109  {
110  return m_size;
111  }
112 
116  constexpr size_type size_bytes() const noexcept
117  {
118  return size() * sizeof( element_type );
119  }
120 
124  constexpr bool empty() const noexcept
125  {
126  return m_size == 0;
127  }
128 
132  constexpr T * data() const noexcept
133  {
134  return m_data;
135  }
136 
140  constexpr T * begin() const noexcept
141  {
142  return m_data;
143  }
144 
148  constexpr T * end() const noexcept
149  {
150  return m_data + m_size;
151  }
152 
156  constexpr std::reverse_iterator< T * > rbegin() const noexcept
157  {
158  return std::reverse_iterator< T * >( m_data + m_size - 1 );
159  }
160 
164  constexpr std::reverse_iterator< T * > rend() const noexcept
165  {
166  return std::reverse_iterator< T * >( m_data - 1 );
167  }
168 
173  T & front() const
174  {
175  GEOS_ASSERT_GT( m_size, 0 );
176  return m_data[0];
177  }
178 
183  T & back() const
184  {
185  GEOS_ASSERT_GT( m_size, 0 );
186  return m_data[m_size-1];
187  }
188 
194  T & operator[]( size_type const i ) const
195  {
196  GEOS_ASSERT_GT( m_size, i );
197  return m_data[i];
198  }
199 
204  Span< element_type > first( size_type const count ) const
205  {
206  GEOS_ASSERT_GE( m_size, count );
207  return { m_data, count };
208  }
209 
214  Span< element_type > last( size_type const count ) const
215  {
216  GEOS_ASSERT_GE( m_size, count );
217  return { m_data + (m_size - count), count };
218  }
219 
225  Span< element_type > subspan( size_type const offset, size_type const count ) const
226  {
227  GEOS_ASSERT_GE( m_size, offset + count );
228  return { m_data + offset, count };
229  }
230 
231 private:
232 
234  T * m_data{};
235 
237  size_type m_size{};
238 
239 };
240 
241 }
242 
243 #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:916
#define GEOS_ASSERT_GE(lhs, rhs)
Assert that one value compares greater than or equal to the other in debug builds.
Definition: Logger.hpp:933
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:214
constexpr bool empty() const noexcept
Definition: Span.hpp:124
constexpr Span(R const &range)
Construct a span from a range-like object (anything that has begin() and end()).
Definition: Span.hpp:101
constexpr T * data() const noexcept
Definition: Span.hpp:132
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:164
constexpr Span() noexcept=default
Construct an empty span.
T & front() const
Definition: Span.hpp:173
T & operator[](size_type const i) const
Definition: Span.hpp:194
constexpr T * begin() const noexcept
Definition: Span.hpp:140
constexpr size_type size_bytes() const noexcept
Definition: Span.hpp:116
constexpr Span(T(&arr)[N]) noexcept
Construct a span from a c-array.
Definition: Span.hpp:90
constexpr T * end() const noexcept
Definition: Span.hpp:148
T element_type
Type of range element.
Definition: Span.hpp:46
constexpr std::reverse_iterator< T * > rbegin() const noexcept
Definition: Span.hpp:156
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:225
constexpr size_type size() const noexcept
Definition: Span.hpp:108
T & back() const
Definition: Span.hpp:183
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:204
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:78