sub_array.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_DETAIL_SUB_ARRAY_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_SUB_ARRAY_HPP
  8. #include <algorithm>
  9. #include <boost/throw_exception.hpp>
  10. #include <stdexcept>
  11. namespace boost {
  12. namespace histogram {
  13. namespace detail {
  14. // Like std::array, but allows to use less than maximum capacity.
  15. // Cannot inherit from std::array, since this confuses span.
  16. template <class T, std::size_t N>
  17. class sub_array {
  18. static constexpr bool swap_element_is_noexcept() noexcept {
  19. using std::swap;
  20. return noexcept(swap(std::declval<T&>(), std::declval<T&>()));
  21. }
  22. public:
  23. using element_type = T;
  24. using size_type = std::size_t;
  25. using reference = T&;
  26. using const_reference = const T&;
  27. using pointer = T*;
  28. using const_pointer = const T*;
  29. using iterator = pointer;
  30. using const_iterator = const_pointer;
  31. sub_array() = default;
  32. explicit sub_array(std::size_t s) noexcept : size_(s) { assert(size_ <= N); }
  33. sub_array(std::size_t s, const T& value) noexcept(
  34. std::is_nothrow_assignable<T, const_reference>::value)
  35. : sub_array(s) {
  36. fill(value);
  37. }
  38. sub_array(std::initializer_list<T> il) noexcept(
  39. std::is_nothrow_assignable<T, const_reference>::value)
  40. : sub_array(il.size()) {
  41. std::copy(il.begin(), il.end(), data_);
  42. }
  43. reference at(size_type pos) noexcept {
  44. if (pos >= size()) BOOST_THROW_EXCEPTION(std::out_of_range{"pos is out of range"});
  45. return data_[pos];
  46. }
  47. const_reference at(size_type pos) const noexcept {
  48. if (pos >= size()) BOOST_THROW_EXCEPTION(std::out_of_range{"pos is out of range"});
  49. return data_[pos];
  50. }
  51. reference operator[](size_type pos) noexcept { return data_[pos]; }
  52. const_reference operator[](size_type pos) const noexcept { return data_[pos]; }
  53. reference front() noexcept { return data_[0]; }
  54. const_reference front() const noexcept { return data_[0]; }
  55. reference back() noexcept { return data_[size_ - 1]; }
  56. const_reference back() const noexcept { return data_[size_ - 1]; }
  57. pointer data() noexcept { return static_cast<pointer>(data_); }
  58. const_pointer data() const noexcept { return static_cast<const_pointer>(data_); }
  59. iterator begin() noexcept { return data_; }
  60. const_iterator begin() const noexcept { return data_; }
  61. iterator end() noexcept { return begin() + size_; }
  62. const_iterator end() const noexcept { return begin() + size_; }
  63. const_iterator cbegin() const noexcept { return data_; }
  64. const_iterator cend() const noexcept { return cbegin() + size_; }
  65. constexpr size_type max_size() const noexcept { return N; }
  66. size_type size() const noexcept { return size_; }
  67. bool empty() const noexcept { return size_ == 0; }
  68. void fill(const_reference value) noexcept(
  69. std::is_nothrow_assignable<T, const_reference>::value) {
  70. std::fill(begin(), end(), value);
  71. }
  72. void swap(sub_array& other) noexcept(swap_element_is_noexcept()) {
  73. using std::swap;
  74. const size_type s = (std::max)(size(), other.size());
  75. for (auto i = begin(), j = other.begin(), end = begin() + s; i != end; ++i, ++j)
  76. swap(*i, *j);
  77. swap(size_, other.size_);
  78. }
  79. private:
  80. size_type size_ = 0;
  81. element_type data_[N];
  82. };
  83. template <class T, std::size_t N>
  84. bool operator==(const sub_array<T, N>& a, const sub_array<T, N>& b) noexcept {
  85. return std::equal(a.begin(), a.end(), b.begin(), b.end());
  86. }
  87. template <class T, std::size_t N>
  88. bool operator!=(const sub_array<T, N>& a, const sub_array<T, N>& b) noexcept {
  89. return !(a == b);
  90. }
  91. } // namespace detail
  92. } // namespace histogram
  93. } // namespace boost
  94. namespace std {
  95. template <class T, std::size_t N>
  96. void swap(::boost::histogram::detail::sub_array<T, N>& a,
  97. ::boost::histogram::detail::sub_array<T, N>& b) noexcept(noexcept(a.swap(b))) {
  98. a.swap(b);
  99. }
  100. } // namespace std
  101. #endif