array.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*!
  2. @file
  3. Defines `boost::hana::detail::array`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_DETAIL_ARRAY_HPP
  9. #define BOOST_HANA_DETAIL_ARRAY_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/detail/algorithm.hpp>
  12. #include <boost/hana/functional/placeholder.hpp>
  13. #include <cstddef>
  14. BOOST_HANA_NAMESPACE_BEGIN namespace detail {
  15. template <typename N>
  16. constexpr N factorial(N n) {
  17. N result = 1;
  18. while (n != 0)
  19. result *= n--;
  20. return result;
  21. }
  22. //! @ingroup group-details
  23. //! A minimal `std::array` with better `constexpr` support.
  24. //!
  25. //! We also provide some algorithms from the `constexpr/algorithm.hpp`
  26. //! header as member functions to make them easier to use in constexpr
  27. //! contexts, since a `constexpr` `array` can't be mutated in place.
  28. template <typename T, std::size_t Size>
  29. struct array {
  30. T elems_[Size > 0 ? Size : 1];
  31. constexpr T& operator[](std::size_t n)
  32. { return elems_[n]; }
  33. constexpr T const& operator[](std::size_t n) const
  34. { return elems_[n]; }
  35. constexpr std::size_t size() const noexcept
  36. { return Size; }
  37. constexpr T* begin() noexcept { return elems_; }
  38. constexpr T const* begin() const noexcept { return elems_; }
  39. constexpr T* end() noexcept { return elems_ + Size; }
  40. constexpr T const* end() const noexcept { return elems_ + Size; }
  41. // Algorithms from constexpr/algorithm.hpp
  42. constexpr array reverse() const {
  43. array result = *this;
  44. detail::reverse(result.begin(), result.end());
  45. return result;
  46. }
  47. template <typename BinaryPred>
  48. constexpr auto permutations(BinaryPred pred) const {
  49. array<array<T, Size>, detail::factorial(Size)> result{};
  50. auto out = result.begin();
  51. array copy = *this;
  52. do *out++ = copy;
  53. while (detail::next_permutation(copy.begin(), copy.end(), pred));
  54. return result;
  55. }
  56. constexpr auto permutations() const
  57. { return this->permutations(hana::_ < hana::_); }
  58. template <typename BinaryPred>
  59. constexpr auto sort(BinaryPred pred) const {
  60. array result = *this;
  61. detail::sort(result.begin(), result.end(), pred);
  62. return result;
  63. }
  64. constexpr auto sort() const
  65. { return this->sort(hana::_ < hana::_); }
  66. template <typename U>
  67. constexpr auto iota(U value) const {
  68. array result = *this;
  69. detail::iota(result.begin(), result.end(), value);
  70. return result;
  71. }
  72. };
  73. template <typename T, std::size_t M, typename U, std::size_t N>
  74. constexpr bool operator==(array<T, M> a, array<U, N> b)
  75. { return M == N && detail::equal(a.begin(), a.end(), b.begin(), b.end()); }
  76. template <typename T, std::size_t M, typename U, std::size_t N>
  77. constexpr bool operator<(array<T, M> a, array<U, N> b) {
  78. return M < N || detail::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
  79. }
  80. } BOOST_HANA_NAMESPACE_END
  81. #endif // !BOOST_HANA_DETAIL_ARRAY_HPP