My Project
span.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_SPAN_HPP
29 #define BSL_SPAN_HPP
30 
31 #include "contiguous_iterator.hpp"
32 #include "cstdint.hpp"
33 #include "cstring.hpp"
34 #include "min_of.hpp"
35 #include "npos.hpp"
37 #include "is_fundamental.hpp"
38 #include "numeric_limits.hpp"
39 #include "reverse_iterator.hpp"
40 
41 namespace bsl
42 {
89  template<typename T>
90  class span final // NOLINT
91  {
92  public:
94  using value_type = T;
100  using reference_type = T &;
102  using const_reference_type = T const &;
104  using pointer_type = T *;
106  using const_pointer_type = T const *;
115 
133  constexpr span() noexcept = default;
134 
146  constexpr span(pointer_type const ptr, size_type const count) noexcept // --
147  : m_ptr{ptr}, m_count{count}
148  {
149  if ((nullptr == m_ptr) || (0U == m_count)) {
150  *this = span{};
151  }
152  }
153 
172  [[nodiscard]] constexpr pointer_type
173  at_if(size_type const index) noexcept
174  {
175  if ((nullptr == m_ptr) || (index >= m_count)) {
176  return nullptr;
177  }
178 
179  return &m_ptr[index]; // PRQA S 4024 // NOLINT
180  }
181 
194  [[nodiscard]] constexpr const_pointer_type
195  at_if(size_type const index) const noexcept
196  {
197  if ((nullptr == m_ptr) || (index >= m_count)) {
198  return nullptr;
199  }
200 
201  return &m_ptr[index]; // NOLINT
202  }
203 
215  [[nodiscard]] constexpr pointer_type
216  front_if() noexcept
217  {
218  return this->at_if(0U);
219  }
220 
232  [[nodiscard]] constexpr const_pointer_type
233  front_if() const noexcept
234  {
235  return this->at_if(0U);
236  }
237 
249  [[nodiscard]] constexpr pointer_type
250  back_if() noexcept
251  {
252  return this->at_if((m_count > 0U) ? (m_count - 1U) : 0U);
253  }
254 
266  [[nodiscard]] constexpr const_pointer_type
267  back_if() const noexcept
268  {
269  return this->at_if((m_count > 0U) ? (m_count - 1U) : 0U);
270  }
271 
283  [[nodiscard]] constexpr pointer_type
284  data() noexcept
285  {
286  return m_ptr;
287  }
288 
300  [[nodiscard]] constexpr const_pointer_type
301  data() const noexcept
302  {
303  return m_ptr;
304  }
305 
313  [[nodiscard]] constexpr iterator_type
314  begin() noexcept
315  {
316  return iterator_type{m_ptr, m_count, 0U};
317  }
318 
326  [[nodiscard]] constexpr const_iterator_type
327  begin() const noexcept
328  {
329  return const_iterator_type{m_ptr, m_count, 0U};
330  }
331 
339  [[nodiscard]] constexpr const_iterator_type
340  cbegin() const noexcept
341  {
342  return const_iterator_type{m_ptr, m_count, 0U};
343  }
344 
353  [[nodiscard]] constexpr iterator_type
354  iter(size_type const i) noexcept
355  {
356  return iterator_type{m_ptr, m_count, i};
357  }
358 
367  [[nodiscard]] constexpr const_iterator_type
368  iter(size_type const i) const noexcept
369  {
370  return const_iterator_type{m_ptr, m_count, i};
371  }
372 
381  [[nodiscard]] constexpr const_iterator_type
382  citer(size_type const i) const noexcept
383  {
384  return const_iterator_type{m_ptr, m_count, i};
385  }
386 
398  [[nodiscard]] constexpr iterator_type
399  end() noexcept
400  {
401  return iterator_type{m_ptr, m_count, m_count};
402  }
403 
415  [[nodiscard]] constexpr const_iterator_type
416  end() const noexcept
417  {
418  return const_iterator_type{m_ptr, m_count, m_count};
419  }
420 
432  [[nodiscard]] constexpr const_iterator_type
433  cend() const noexcept
434  {
435  return const_iterator_type{m_ptr, m_count, m_count};
436  }
437 
451  [[nodiscard]] constexpr reverse_iterator_type
452  rbegin() noexcept
453  {
454  return reverse_iterator_type{this->end()};
455  }
456 
470  [[nodiscard]] constexpr const_reverse_iterator_type
471  rbegin() const noexcept
472  {
473  return const_reverse_iterator_type{this->end()};
474  }
475 
489  [[nodiscard]] constexpr const_reverse_iterator_type
490  crbegin() const noexcept
491  {
492  return const_reverse_iterator_type{this->cend()};
493  }
494 
509  [[nodiscard]] constexpr reverse_iterator_type
510  riter(size_type const i) noexcept
511  {
512  size_type const ai{(i >= m_count) ? m_count : (i + 1U)};
513  return reverse_iterator_type{this->iter(ai)};
514  }
515 
530  [[nodiscard]] constexpr const_reverse_iterator_type
531  riter(size_type const i) const noexcept
532  {
533  size_type const ai{(i >= m_count) ? m_count : (i + 1U)};
534  return const_reverse_iterator_type{this->iter(ai)};
535  }
536 
551  [[nodiscard]] constexpr const_reverse_iterator_type
552  criter(size_type const i) const noexcept
553  {
554  size_type const ai{(i >= m_count) ? m_count : (i + 1U)};
555  return const_reverse_iterator_type{this->citer(ai)};
556  }
557 
571  [[nodiscard]] constexpr reverse_iterator_type
572  rend() noexcept
573  {
574  return reverse_iterator_type{this->begin()};
575  }
576 
590  [[nodiscard]] constexpr const_reverse_iterator_type
591  rend() const noexcept
592  {
593  return const_reverse_iterator_type{this->begin()};
594  }
595 
609  [[nodiscard]] constexpr const_reverse_iterator_type
610  crend() const noexcept
611  {
612  return const_reverse_iterator_type{this->cbegin()};
613  }
614 
622  [[nodiscard]] constexpr bool
623  empty() const noexcept
624  {
625  return 0U == m_count;
626  }
627 
639  [[nodiscard]] constexpr size_type
640  size() const noexcept
641  {
642  return m_count;
643  }
644 
652  [[nodiscard]] static constexpr size_type
653  max_size() noexcept
654  {
655  return numeric_limits<size_type>::max() / sizeof(T);
656  }
657 
665  [[nodiscard]] constexpr size_type
666  size_bytes() const noexcept
667  {
668  return m_count * sizeof(T);
669  }
670 
681  [[nodiscard]] constexpr span<T>
682  first(size_type const count = npos) noexcept
683  {
684  return this->subspan(0U, count);
685  }
686 
697  [[nodiscard]] constexpr span<T>
698  first(size_type const count = npos) const noexcept
699  {
700  return this->subspan(0U, count);
701  }
702 
717  [[nodiscard]] constexpr span<T>
718  last(size_type count = npos) noexcept
719  {
720  if (count > this->size()) {
721  count = this->size();
722  }
723 
724  return this->subspan(this->size() - count, count);
725  }
726 
741  [[nodiscard]] constexpr span<T>
742  last(size_type count = npos) const noexcept
743  {
744  if (count > this->size()) {
745  count = this->size();
746  }
747 
748  return this->subspan(this->size() - count, count);
749  }
750 
764  [[nodiscard]] constexpr span<T>
765  subspan(size_type const pos, size_type const count = npos) noexcept
766  {
767  if ((nullptr == m_ptr) || (pos >= m_count)) {
768  return {};
769  }
770 
771  return span<T>{&m_ptr[pos], min_of(count, m_count - pos)}; // NOLINT
772  }
773 
787  [[nodiscard]] constexpr span<T>
788  subspan(size_type const pos, size_type const count = npos) const noexcept
789  {
790  if ((nullptr == m_ptr) || (pos >= m_count)) {
791  return {};
792  }
793 
794  return span<T>{&m_ptr[pos], min_of(count, m_count - pos)}; // NOLINT
795  }
796 
797  private:
799  pointer_type m_ptr;
801  size_type m_count;
802  };
803 
817  template<typename T>
818  constexpr bool
819  operator==(span<T> const &lhs, span<T> const &rhs) noexcept
820  {
821  if (lhs.size() != rhs.size()) {
822  return false;
823  }
824 
826  return bsl::builtin_memcmp(lhs.data(), rhs.data(), lhs.size_bytes()) == 0;
827  }
828 
829  for (bsl::uintmax i{}; i < lhs.size(); ++i) {
830  if (*lhs.at_if(i) != *rhs.at_if(i)) {
831  return false;
832  }
833  }
834 
835  return true;
836  }
837 
851  template<typename T>
852  constexpr bool
853  operator!=(span<T> const &lhs, span<T> const &rhs) noexcept
854  {
855  return !(lhs == rhs);
856  }
857 }
858 
859 #endif
constexpr const_reverse_iterator_type crbegin() const noexcept
Returns a reverse iterator to one past the last element of the view. When accessing the iterator,...
Definition: span.hpp:490
constexpr size_type size() const noexcept
Returns the number of elements in the array being viewed. If this is a default constructed view,...
Definition: span.hpp:640
T * pointer_type
alias for: T *
Definition: span.hpp:104
contiguous_iterator< T > iterator_type
alias for: contiguous_iterator<T>
Definition: span.hpp:108
constexpr pointer_type front_if() noexcept
Returns a pointer to the instance of T stored at index "0". If the index is out of bounds,...
Definition: span.hpp:216
If the provided type is a fundamental type, provides the member constant value equal to true....
Definition: is_fundamental.hpp:76
constexpr const_reverse_iterator_type riter(size_type const i) const noexcept
Returns a reverse iterator element "i" in the view. When accessing the iterator, the iterator will al...
Definition: span.hpp:531
constexpr pointer_type data() noexcept
Returns a pointer to the array being viewed. If this is a default constructed view,...
Definition: span.hpp:284
constexpr pointer_type at_if(size_type const index) noexcept
Returns a pointer to the instance of T stored at index "index". If the index is out of bounds,...
Definition: span.hpp:173
constexpr span< T > subspan(size_type const pos, size_type const count=npos) noexcept
Returns span{at_if(pos), min_of(count, size() - pos)}. If the provided "pos" is greater than or equal...
Definition: span.hpp:765
constexpr reverse_iterator_type rend() noexcept
Returns a reverse iterator first element of the view. When accessing the iterator,...
Definition: span.hpp:572
constexpr span< T > subspan(size_type const pos, size_type const count=npos) const noexcept
Returns span{at_if(pos), min_of(count, size() - pos)}. If the provided "pos" is greater than or equal...
Definition: span.hpp:788
constexpr const_pointer_type front_if() const noexcept
Returns a pointer to the instance of T stored at index "0". If the index is out of bounds,...
Definition: span.hpp:233
constexpr const_iterator_type begin() const noexcept
Returns an iterator to the first element of the view.
Definition: span.hpp:327
constexpr span< T > first(size_type const count=npos) const noexcept
Returns subspan(0, count). If count is 0, an invalid span is returned.
Definition: span.hpp:698
constexpr const_iterator_type cbegin() const noexcept
Returns an iterator to the first element of the view.
Definition: span.hpp:340
T const & const_reference_type
alias for: T &
Definition: span.hpp:102
bsl::uintmax difference_type
alias for: bsl::uintmax
Definition: span.hpp:98
constexpr iterator_type iter(size_type const i) noexcept
Returns an iterator to the element "i" in the view.
Definition: span.hpp:354
reverse_iterator< iterator_type > reverse_iterator_type
alias for: reverse_iterator<iterator>
Definition: span.hpp:112
constexpr const_pointer_type data() const noexcept
Returns a pointer to the array being viewed. If this is a default constructed view,...
Definition: span.hpp:301
contiguous_iterator< T const > const_iterator_type
alias for: contiguous_iterator<T const>
Definition: span.hpp:110
T const * const_pointer_type
alias for: T const *
Definition: span.hpp:106
constexpr const_reverse_iterator_type crend() const noexcept
Returns a reverse iterator first element of the view. When accessing the iterator,...
Definition: span.hpp:610
constexpr const_iterator_type cend() const noexcept
Returns an iterator to one past the last element of the view. If you attempt to access this iterator,...
Definition: span.hpp:433
constexpr span< T > last(size_type count=npos) const noexcept
Returns subspan(this->size() - count, count). If count is greater than the size of the current span,...
Definition: span.hpp:742
A bsl::span is a non-owning view of an array type. Unlike a bsl::array, the bsl::span does not own th...
Definition: span.hpp:90
T value_type
alias for: T
Definition: span.hpp:94
constexpr reverse_iterator_type riter(size_type const i) noexcept
Returns a reverse iterator element "i" in the view. When accessing the iterator, the iterator will al...
Definition: span.hpp:510
static constexpr T max() noexcept
Returns the max value of T.
Definition: numeric_limits.hpp:154
constexpr bool operator==(span< T > const &lhs, span< T > const &rhs) noexcept
Returns true if two spans have the same size and contain the same contents. Returns false otherwise.
Definition: span.hpp:819
constexpr const_iterator_type end() const noexcept
Returns an iterator to one past the last element of the view. If you attempt to access this iterator,...
Definition: span.hpp:416
Provides a reverse iterator as defined by the C++ specification, with the follwing differences:
Definition: reverse_iterator.hpp:78
constexpr span< T > last(size_type count=npos) noexcept
Returns subspan(this->size() - count, count). If count is greater than the size of the current span,...
Definition: span.hpp:718
constexpr bool empty() const noexcept
Returns size() == 0.
Definition: span.hpp:623
constexpr const_reverse_iterator_type rend() const noexcept
Returns a reverse iterator first element of the view. When accessing the iterator,...
Definition: span.hpp:591
constexpr const_pointer_type back_if() const noexcept
Returns a pointer to the instance of T stored at index "size() - 1". If the index is out of bounds,...
Definition: span.hpp:267
constexpr iterator_type begin() noexcept
Returns an iterator to the first element of the view.
Definition: span.hpp:314
constexpr iterator_type end() noexcept
Returns an iterator to one past the last element of the view. If you attempt to access this iterator,...
Definition: span.hpp:399
constexpr const_iterator_type citer(size_type const i) const noexcept
Returns an iterator to the element "i" in the view.
Definition: span.hpp:382
constexpr bool operator!=(span< T > const &lhs, span< T > const &rhs) noexcept
Returns false if two spans have the same size and contain the same contents. Returns true otherwise.
Definition: span.hpp:853
constexpr const_reverse_iterator_type criter(size_type const i) const noexcept
Returns a reverse iterator element "i" in the view. When accessing the iterator, the iterator will al...
Definition: span.hpp:552
constexpr span< T > first(size_type const count=npos) noexcept
Returns subspan(0, count). If count is 0, an invalid span is returned.
Definition: span.hpp:682
constexpr const_reverse_iterator_type rbegin() const noexcept
Returns a reverse iterator to one past the last element of the view. When accessing the iterator,...
Definition: span.hpp:471
static constexpr size_type max_size() noexcept
Returns the max number of elements the BSL supports.
Definition: span.hpp:653
reverse_iterator< const_iterator_type > const_reverse_iterator_type
alias for: reverse_iterator<const_iterator>
Definition: span.hpp:114
constexpr pointer_type back_if() noexcept
Returns a pointer to the instance of T stored at index "size() - 1". If the index is out of bounds,...
Definition: span.hpp:250
T & reference_type
alias for: T &
Definition: span.hpp:100
constexpr size_type size_bytes() const noexcept
Returns size() * sizeof(T)
Definition: span.hpp:666
bsl::uintmax size_type
alias for: bsl::uintmax
Definition: span.hpp:96
constexpr span() noexcept=default
Default constructor that creates a span with data() == nullptr and size() == 0. All accessors will re...
constexpr bool is_constant_evaluated() noexcept
Detects whether the function call occurs within a constant-evaluated context. Returns true if the eva...
Definition: is_constant_evaluated.hpp:46
constexpr const_pointer_type at_if(size_type const index) const noexcept
Returns a pointer to the instance of T stored at index "index". If the index is out of bounds,...
Definition: span.hpp:195
constexpr T const & min_of(T const &a, T const &b) noexcept
Returns a if a is smaller than b, otherwise returns b. Note that this function is called min_of to pr...
Definition: min_of.hpp:47
::uintmax_t uintmax
defines a unsigned integer with the maximum possible size
Definition: cstdint.hpp:97
Provides a contiguous iterator as defined by the C++ specification, with the follwing differences:
Definition: contiguous_iterator.hpp:76
constexpr reverse_iterator_type rbegin() noexcept
Returns a reverse iterator to one past the last element of the view. When accessing the iterator,...
Definition: span.hpp:452
constexpr const_iterator_type iter(size_type const i) const noexcept
Returns an iterator to the element "i" in the view.
Definition: span.hpp:368
bsl::int32 builtin_memcmp(void const *const lhs, void const *const rhs, bsl::uintmax const count) noexcept
Same as std::memcmp with parameter checks. If lhs, rhs are a nullptr, or count is 0,...
Definition: cstring.hpp:108