My Project
contiguous_iterator.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_CONTIGUOUS_ITERATOR_HPP
29 #define BSL_CONTIGUOUS_ITERATOR_HPP
30 
31 #include "cstdint.hpp"
32 
33 // TODO
34 // - We need to implement the remianing functions that are part of the
35 // contiguous iterator specification. Specifically, the increment and
36 // decrement by "n" functions as they all require the safe_int class
37 // to be effective at preventing wrapping, overruns and underruns.
38 // Currently we only support the ++/-- functions as those are simple
39 // to implement without the need for safe_int. Also note that we would
40 // need some extra logic to ensure the iterator stays in-bounds.
41 //
42 
43 namespace bsl
44 {
75  template<typename T>
76  class contiguous_iterator final
77  {
82  constexpr contiguous_iterator() noexcept // --
83  : m_ptr{}, m_count{}, m_i{}
84  {}
85 
86  public:
88  using value_type = T;
94  using reference_type = T &;
96  using const_reference_type = T const &;
98  using pointer_type = T *;
100  using const_pointer_type = T const *;
101 
113  constexpr contiguous_iterator( // --
114  pointer_type const ptr, // --
115  size_type const count, // --
116  size_type const i = 0U) noexcept
117  : m_ptr{ptr}, m_count{count}, m_i{i}
118  {
119  if ((nullptr == m_ptr) || (0U == m_count)) {
120  *this = contiguous_iterator{};
121  }
122 
123  if (m_i > count) {
124  m_i = count;
125  }
126  }
127 
135  [[nodiscard]] constexpr pointer_type
136  data() noexcept
137  {
138  return m_ptr;
139  }
140 
148  [[nodiscard]] constexpr const_pointer_type
149  data() const noexcept
150  {
151  return m_ptr;
152  }
153 
161  [[nodiscard]] constexpr size_type
162  size() const noexcept
163  {
164  return m_count;
165  }
166 
175  [[nodiscard]] constexpr size_type
176  index() const noexcept
177  {
178  return m_i;
179  }
180 
188  [[nodiscard]] constexpr bool
189  empty() const noexcept
190  {
191  return nullptr == this->data();
192  }
193 
201  [[nodiscard]] constexpr bool
202  is_end() const noexcept
203  {
204  return this->index() == this->size();
205  }
206 
224  [[nodiscard]] constexpr pointer_type
225  get_if() noexcept
226  {
227  if (nullptr == m_ptr) {
228  return nullptr;
229  }
230 
231  if (m_i == m_count) {
232  return nullptr;
233  }
234 
235  return &m_ptr[m_i]; // PRQA S 4024 // NOLINT
236  }
237 
245  [[maybe_unused]] constexpr contiguous_iterator &
246  operator++() noexcept
247  {
248  if (nullptr == m_ptr) {
249  return *this;
250  }
251 
252  if (m_count == m_i) {
253  return *this;
254  }
255 
256  ++m_i;
257  return *this;
258  }
259 
267  [[maybe_unused]] constexpr contiguous_iterator &
268  operator--() noexcept
269  {
270  if (nullptr == m_ptr) {
271  return *this;
272  }
273 
274  if (0U == m_i) {
275  return *this;
276  }
277 
278  --m_i;
279  return *this;
280  }
281 
282  private:
284  pointer_type m_ptr;
286  size_type m_count;
288  size_type m_i;
289  };
290 
304  template<typename T>
305  [[nodiscard]] constexpr bool
307  {
308  return (lhs.data() == rhs.data()) && (lhs.index() == rhs.index());
309  }
310 
324  template<typename T>
325  [[nodiscard]] constexpr bool
327  {
328  return !(lhs == rhs);
329  }
330 
342  template<typename T>
343  [[nodiscard]] constexpr bool
344  operator<(contiguous_iterator<T> const &lhs, contiguous_iterator<T> const &rhs) noexcept
345  {
346  return lhs.index() < rhs.index();
347  }
348 
360  template<typename T>
361  [[nodiscard]] constexpr bool
362  operator<=(contiguous_iterator<T> const &lhs, contiguous_iterator<T> const &rhs) noexcept
363  {
364  return lhs.index() <= rhs.index();
365  }
366 
378  template<typename T>
379  [[nodiscard]] constexpr bool
380  operator>(contiguous_iterator<T> const &lhs, contiguous_iterator<T> const &rhs) noexcept
381  {
382  return lhs.index() > rhs.index();
383  }
384 
396  template<typename T>
397  [[nodiscard]] constexpr bool
399  {
400  return lhs.index() >= rhs.index();
401  }
402 }
403 
404 #endif
constexpr contiguous_iterator(pointer_type const ptr, size_type const count, size_type const i=0U) noexcept
Creates a contiguous iterator given a ptr to an array and the total number of elements in the array....
Definition: contiguous_iterator.hpp:113
constexpr size_type index() const noexcept
Returns the iterator's current index. If the iterator is at the end, this function returns size().
Definition: contiguous_iterator.hpp:176
constexpr bool empty() const noexcept
Returns nullptr == data()
Definition: contiguous_iterator.hpp:189
T const * const_pointer_type
alias for: T const *
Definition: contiguous_iterator.hpp:100
constexpr contiguous_iterator & operator++() noexcept
Increments the iterator.
Definition: contiguous_iterator.hpp:246
constexpr size_type size() const noexcept
Returns the number of elements in the array being iterated.
Definition: contiguous_iterator.hpp:162
constexpr bool operator>(contiguous_iterator< T > const &lhs, contiguous_iterator< T > const &rhs) noexcept
Returns lhs.index() > rhs.index()
Definition: contiguous_iterator.hpp:380
constexpr bool is_end() const noexcept
Returns index() == size()
Definition: contiguous_iterator.hpp:202
T & reference_type
alias for: T &
Definition: contiguous_iterator.hpp:94
constexpr bool operator>=(contiguous_iterator< T > const &lhs, contiguous_iterator< T > const &rhs) noexcept
Returns lhs.index() >= rhs.index()
Definition: contiguous_iterator.hpp:398
constexpr contiguous_iterator & operator--() noexcept
Decrements the iterator.
Definition: contiguous_iterator.hpp:268
constexpr pointer_type get_if() noexcept
Returns a pointer to the instance of T stored at the iterator's current index. If the index is out of...
Definition: contiguous_iterator.hpp:225
bsl::uintmax size_type
alias for: bsl::uintmax
Definition: contiguous_iterator.hpp:90
constexpr pointer_type data() noexcept
Returns a pointer to the array being iterated.
Definition: contiguous_iterator.hpp:136
constexpr const_pointer_type data() const noexcept
Returns a pointer to the array being iterated.
Definition: contiguous_iterator.hpp:149
bsl::uintmax difference_type
alias for: bsl::uintmax
Definition: contiguous_iterator.hpp:92
T const & const_reference_type
alias for: T const &
Definition: contiguous_iterator.hpp:96
::uintmax_t uintmax
defines a unsigned integer with the maximum possible size
Definition: cstdint.hpp:97
constexpr bool operator!=(contiguous_iterator< T > const &lhs, contiguous_iterator< T > const &rhs) noexcept
Returns true if the provided contiguous iterators do not point to the same array or the same index.
Definition: contiguous_iterator.hpp:326
constexpr bool operator==(contiguous_iterator< T > const &lhs, contiguous_iterator< T > const &rhs) noexcept
Returns true if the provided contiguous iterators point to the same array and the same index.
Definition: contiguous_iterator.hpp:306
T * pointer_type
alias for: T *
Definition: contiguous_iterator.hpp:98
Provides a contiguous iterator as defined by the C++ specification, with the follwing differences:
Definition: contiguous_iterator.hpp:76
T value_type
alias for: T
Definition: contiguous_iterator.hpp:88