size_array.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2016-2025 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PFR_DETAIL_SIZE_ARRAY_HPP
  6. #define BOOST_PFR_DETAIL_SIZE_ARRAY_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #if !defined(BOOST_PFR_INTERFACE_UNIT)
  10. #include <cstddef>
  11. #endif
  12. namespace boost { namespace pfr { namespace detail {
  13. ///////////////////// Array that has the constexpr
  14. template <std::size_t N>
  15. struct size_array { // libc++ misses constexpr on operator[]
  16. typedef std::size_t type;
  17. std::size_t data[N];
  18. static constexpr std::size_t size() noexcept { return N; }
  19. constexpr std::size_t count_nonzeros() const noexcept {
  20. std::size_t count = 0;
  21. for (std::size_t i = 0; i < size(); ++i) {
  22. if (data[i]) {
  23. ++ count;
  24. }
  25. }
  26. return count;
  27. }
  28. constexpr std::size_t count_from_opening_till_matching_parenthis_seq(std::size_t from, std::size_t opening_parenthis, std::size_t closing_parenthis) const noexcept {
  29. if (data[from] != opening_parenthis) {
  30. return 0;
  31. }
  32. std::size_t unclosed_parnthesis = 0;
  33. std::size_t count = 0;
  34. for (; ; ++from) {
  35. if (data[from] == opening_parenthis) {
  36. ++ unclosed_parnthesis;
  37. } else if (data[from] == closing_parenthis) {
  38. -- unclosed_parnthesis;
  39. }
  40. ++ count;
  41. if (unclosed_parnthesis == 0) {
  42. return count;
  43. }
  44. }
  45. return count;
  46. }
  47. };
  48. template <>
  49. struct size_array<0> { // libc++ misses constexpr on operator[]
  50. typedef std::size_t type;
  51. std::size_t data[1];
  52. static constexpr std::size_t size() noexcept { return 0; }
  53. constexpr std::size_t count_nonzeros() const noexcept {
  54. return 0;
  55. }
  56. };
  57. template <std::size_t I, std::size_t N>
  58. constexpr std::size_t get(const size_array<N>& a) noexcept {
  59. static_assert(I < N, "====================> Boost.PFR: Array index out of bounds");
  60. return a.data[I];
  61. }
  62. }}} // namespace boost::pfr::detail
  63. #endif // BOOST_PFR_DETAIL_SIZE_ARRAY_HPP