sub_array.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. template <class T, std::size_t N>
  15. class sub_array {
  16. constexpr bool swap_element_is_noexcept() noexcept {
  17. using std::swap;
  18. return noexcept(swap(std::declval<T&>(), std::declval<T&>()));
  19. }
  20. public:
  21. using value_type = T;
  22. using size_type = std::size_t;
  23. using reference = T&;
  24. using const_reference = const T&;
  25. using pointer = T*;
  26. using const_pointer = const T*;
  27. using iterator = pointer;
  28. using const_iterator = const_pointer;
  29. sub_array() = default;
  30. explicit sub_array(std::size_t s) noexcept : size_(s) { assert(size_ <= N); }
  31. sub_array(std::size_t s, const T& value) noexcept(
  32. std::is_nothrow_assignable<T, const_reference>::value)
  33. : sub_array(s) {
  34. fill(value);
  35. }
  36. reference at(size_type pos) noexcept {
  37. if (pos >= size()) BOOST_THROW_EXCEPTION(std::out_of_range{"pos is out of range"});
  38. return data_[pos];
  39. }
  40. const_reference at(size_type pos) const noexcept {
  41. if (pos >= size()) BOOST_THROW_EXCEPTION(std::out_of_range{"pos is out of range"});
  42. return data_[pos];
  43. }
  44. reference operator[](size_type pos) noexcept { return data_[pos]; }
  45. const_reference operator[](size_type pos) const noexcept { return data_[pos]; }
  46. reference front() noexcept { return data_[0]; }
  47. const_reference front() const noexcept { return data_[0]; }
  48. reference back() noexcept { return data_[size_ - 1]; }
  49. const_reference back() const noexcept { return data_[size_ - 1]; }
  50. pointer data() noexcept { return static_cast<pointer>(data_); }
  51. const_pointer data() const noexcept { return static_cast<const_pointer>(data_); }
  52. iterator begin() noexcept { return data_; }
  53. const_iterator begin() const noexcept { return data_; }
  54. iterator end() noexcept { return begin() + size_; }
  55. const_iterator end() const noexcept { return begin() + size_; }
  56. const_iterator cbegin() noexcept { return data_; }
  57. const_iterator cbegin() const noexcept { return data_; }
  58. const_iterator cend() noexcept { return cbegin() + size_; }
  59. const_iterator cend() const noexcept { return cbegin() + size_; }
  60. constexpr size_type max_size() const noexcept { return N; }
  61. size_type size() const noexcept { return size_; }
  62. bool empty() const noexcept { return size_ == 0; }
  63. void fill(const_reference value) noexcept(
  64. std::is_nothrow_assignable<T, const_reference>::value) {
  65. std::fill(begin(), end(), value);
  66. }
  67. void swap(sub_array& other) noexcept(swap_element_is_noexcept()) {
  68. using std::swap;
  69. for (auto i = begin(), j = other.begin(); i != end(); ++i, ++j) swap(*i, *j);
  70. }
  71. private:
  72. size_type size_ = 0;
  73. value_type data_[N];
  74. };
  75. template <class T, std::size_t N>
  76. bool operator==(const sub_array<T, N>& a, const sub_array<T, N>& b) noexcept {
  77. return std::equal(a.begin(), a.end(), b.begin());
  78. }
  79. template <class T, std::size_t N>
  80. bool operator!=(const sub_array<T, N>& a, const sub_array<T, N>& b) noexcept {
  81. return !(a == b);
  82. }
  83. } // namespace detail
  84. } // namespace histogram
  85. } // namespace boost
  86. namespace std {
  87. template <class T, std::size_t N>
  88. void swap(::boost::histogram::detail::sub_array<T, N>& a,
  89. ::boost::histogram::detail::sub_array<T, N>& b) noexcept(noexcept(a.swap(b))) {
  90. a.swap(b);
  91. }
  92. } // namespace std
  93. #endif