@@ -330,14 +330,13 @@ namespace cppfastbox::detail
330
330
{
331
331
consteval inline get_value_from_native_array_impl (::std::index_sequence<index...>, type&) noexcept {};
332
332
333
- inline static auto && get_value_from_native_array(auto array, auto ... index_in) noexcept
333
+ /* *
334
+ * @brief 运行时计算多维数组的偏移量,使用折叠表达式而不是递归
335
+ *
336
+ */
337
+ CPPFASTBOX_ALWAYS_INLINE inline static auto && get_index_for_native_array(auto ... index_in) noexcept
334
338
{
335
- constexpr auto is_const{::std::is_const_v<type>};
336
- using array_type =
337
- ::std::conditional_t <is_const, ::std::add_const_t <typename type::native_array_type>, typename type::native_array_type>;
338
- using pointer = ::cppfastbox::detail::remove_extent_to_pointer_t <array_type, sizeof ...(index)>;
339
- auto offset{((index_in * type::stride_per_extent[index]) + ...)};
340
- return *reinterpret_cast <pointer>(array + offset);
339
+ return ((index_in * type::stride_per_extent[index]) + ...);
341
340
}
342
341
};
343
342
@@ -467,7 +466,8 @@ namespace cppfastbox
467
466
using reverse_iterator = ::std::reverse_iterator<iterator>;
468
467
using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
469
468
470
- [[nodiscard]] constexpr inline auto && operator [] (this auto && self, ::std::size_t index, ::std::integral auto ... index_next) noexcept
469
+ CPPFASTBOX_ALWAYS_INLINE [[nodiscard]] constexpr inline auto &&
470
+ operator [] (this auto && self, ::std::size_t index, ::std::integral auto ... index_next) noexcept
471
471
{
472
472
static_assert (sizeof ...(index_next) + 1 <= rank (), " The number of dereferencings cannot exceed the array dimension." );
473
473
using array_type = ::std::remove_reference_t <decltype (self)>;
@@ -483,8 +483,10 @@ namespace cppfastbox
483
483
self});
484
484
constexpr auto is_const{::std::is_const_v<array_type>};
485
485
using ptr = ::std::conditional_t <is_const, const_pointer, pointer>;
486
- return ::cppfastbox::cmove<is_rvalue>(
487
- helper::get_value_from_native_array (reinterpret_cast <ptr>(self.array ), index, static_cast <::std::size_t >(index_next)...));
486
+ using array_type = ::std::conditional_t <is_const, ::std::add_const_t <native_array_type>, native_array_type>;
487
+ using result = ::cppfastbox::detail::remove_extent_to_pointer_t <array_type, 1 + sizeof ...(index_next)>;
488
+ auto offset{helper::get_index_for_native_array (index, static_cast <::std::size_t >(index_next)...)};
489
+ return ::cppfastbox::cmove<is_rvalue>(*reinterpret_cast <result>(reinterpret_cast <ptr>(self.array ) + offset));
488
490
}
489
491
}
490
492
@@ -621,9 +623,10 @@ namespace cppfastbox
621
623
template <typename type>
622
624
[[nodiscard]] constexpr inline auto make_array (type&& native_array) noexcept
623
625
{
624
- static_assert (::std::is_bounded_array_v<::std::remove_reference_t <type>>, " The function argument should be a native array." );
625
- using array_type = ::cppfastbox::detail::make_array_from_native<::std::remove_reference_t <type>>;
626
- array_type array{};
626
+ using array_type = ::std::remove_reference_t <type>;
627
+ static_assert (::std::is_bounded_array_v<array_type>, " The function argument should be a native array." );
628
+ using my_array = ::cppfastbox::detail::make_array_from_native<array_type>;
629
+ my_array array{};
627
630
::cppfastbox::copy_native_array (array.array, native_array);
628
631
return array;
629
632
}
@@ -632,15 +635,10 @@ namespace cppfastbox
632
635
namespace cppfastbox ::detail
633
636
{
634
637
template <typename type>
635
- constexpr inline auto is_array_impl{false };
636
- template <typename type, ::std::size_t ... n>
637
- constexpr inline auto is_array_impl<::cppfastbox::array<type, n...>>{true };
638
- template <typename type, ::std::size_t ... n>
639
- constexpr inline auto is_array_impl<const ::cppfastbox::array<type, n...>>{true };
640
- template <typename type, ::std::size_t ... n>
641
- constexpr inline auto is_array_impl<volatile ::cppfastbox::array<type, n...>>{true };
638
+ constexpr inline bool is_array_impl{
639
+ ::std::same_as<type, ::std::remove_cv_t <type>> ? false : ::cppfastbox::detail::is_array_impl<::std::remove_cv_t <type>>};
642
640
template <typename type, ::std::size_t ... n>
643
- constexpr inline auto is_array_impl<const volatile ::cppfastbox::array<type, n...>>{true };
641
+ constexpr inline bool is_array_impl<::cppfastbox::array<type, n...>>{true };
644
642
} // namespace cppfastbox::detail
645
643
646
644
namespace cppfastbox
@@ -650,7 +648,7 @@ namespace cppfastbox
650
648
*
651
649
*/
652
650
template <typename type>
653
- concept is_array = detail::is_array_impl<type>;
651
+ concept is_array = ::cppfastbox:: detail::is_array_impl<type>;
654
652
655
653
template <::std::size_t index, typename type>
656
654
requires (::cppfastbox::is_array<::std::remove_reference_t <type>>)
@@ -663,7 +661,7 @@ namespace cppfastbox
663
661
664
662
namespace std
665
663
{
666
- template <size_t index, typename type_in, ::std::size_t n, ::std::size_t ... next>
664
+ template <::std:: size_t index, typename type_in, ::std::size_t n, ::std::size_t ... next>
667
665
struct tuple_element <index, ::cppfastbox::array<type_in, n, next...>>
668
666
{
669
667
static_assert (n != 0 && ((next != 0 ) && ...), " The array must be non-empty." );
0 commit comments