Skip to content

Commit 333a568

Browse files
committed
简化实现
1 parent 74c806c commit 333a568

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

include/base/platform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565

6666
// 无操作
6767
#define CPPFASTBOX_NOP void(nullptr)
68+
// 恒内联函数
69+
#define CPPFASTBOX_ALWAYS_INLINE [[using gnu: always_inline, artificial]]
6870

6971
namespace cppfastbox
7072
{

include/container/array.h

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,13 @@ namespace cppfastbox::detail
330330
{
331331
consteval inline get_value_from_native_array_impl(::std::index_sequence<index...>, type&) noexcept {};
332332

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
334338
{
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]) + ...);
341340
}
342341
};
343342

@@ -467,7 +466,8 @@ namespace cppfastbox
467466
using reverse_iterator = ::std::reverse_iterator<iterator>;
468467
using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
469468

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
471471
{
472472
static_assert(sizeof...(index_next) + 1 <= rank(), "The number of dereferencings cannot exceed the array dimension.");
473473
using array_type = ::std::remove_reference_t<decltype(self)>;
@@ -483,8 +483,10 @@ namespace cppfastbox
483483
self});
484484
constexpr auto is_const{::std::is_const_v<array_type>};
485485
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));
488490
}
489491
}
490492

@@ -621,9 +623,10 @@ namespace cppfastbox
621623
template <typename type>
622624
[[nodiscard]] constexpr inline auto make_array(type&& native_array) noexcept
623625
{
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{};
627630
::cppfastbox::copy_native_array(array.array, native_array);
628631
return array;
629632
}
@@ -632,15 +635,10 @@ namespace cppfastbox
632635
namespace cppfastbox::detail
633636
{
634637
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>>};
642640
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};
644642
} // namespace cppfastbox::detail
645643

646644
namespace cppfastbox
@@ -650,7 +648,7 @@ namespace cppfastbox
650648
*
651649
*/
652650
template <typename type>
653-
concept is_array = detail::is_array_impl<type>;
651+
concept is_array = ::cppfastbox::detail::is_array_impl<type>;
654652

655653
template <::std::size_t index, typename type>
656654
requires (::cppfastbox::is_array<::std::remove_reference_t<type>>)
@@ -663,7 +661,7 @@ namespace cppfastbox
663661

664662
namespace std
665663
{
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>
667665
struct tuple_element<index, ::cppfastbox::array<type_in, n, next...>>
668666
{
669667
static_assert(n != 0 && ((next != 0) && ...), "The array must be non-empty.");

0 commit comments

Comments
 (0)