size_array.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2016-2024 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. #include <cstddef> // metaprogramming stuff
  10. namespace boost { namespace pfr { namespace detail {
  11. ///////////////////// Array that has the constexpr
  12. template <std::size_t N>
  13. struct size_array { // libc++ misses constexpr on operator[]
  14. typedef std::size_t type;
  15. std::size_t data[N];
  16. static constexpr std::size_t size() noexcept { return N; }
  17. constexpr std::size_t count_nonzeros() const noexcept {
  18. std::size_t count = 0;
  19. for (std::size_t i = 0; i < size(); ++i) {
  20. if (data[i]) {
  21. ++ count;
  22. }
  23. }
  24. return count;
  25. }
  26. 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 {
  27. if (data[from] != opening_parenthis) {
  28. return 0;
  29. }
  30. std::size_t unclosed_parnthesis = 0;
  31. std::size_t count = 0;
  32. for (; ; ++from) {
  33. if (data[from] == opening_parenthis) {
  34. ++ unclosed_parnthesis;
  35. } else if (data[from] == closing_parenthis) {
  36. -- unclosed_parnthesis;
  37. }
  38. ++ count;
  39. if (unclosed_parnthesis == 0) {
  40. return count;
  41. }
  42. }
  43. return count;
  44. }
  45. };
  46. template <>
  47. struct size_array<0> { // libc++ misses constexpr on operator[]
  48. typedef std::size_t type;
  49. std::size_t data[1];
  50. static constexpr std::size_t size() noexcept { return 0; }
  51. constexpr std::size_t count_nonzeros() const noexcept {
  52. return 0;
  53. }
  54. };
  55. template <std::size_t I, std::size_t N>
  56. constexpr std::size_t get(const size_array<N>& a) noexcept {
  57. static_assert(I < N, "====================> Boost.PFR: Array index out of bounds");
  58. return a.data[I];
  59. }
  60. }}} // namespace boost::pfr::detail
  61. #endif // BOOST_PFR_DETAIL_SIZE_ARRAY_HPP