Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xutility] Modernize _Ptr_meow_cat to use variable templates #872

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,8 @@ namespace ranges {
template <class _It1, class _It2, class _Size, class _Pr, class _Pj1, class _Pj2>
_NODISCARD static constexpr bool _Equal_count(
_It1 _First1, _It2 _First2, _Size _Count, _Pr _Pred, _Pj1 _Proj1, _Pj2 _Proj2) {
if constexpr (_Equal_memcmp_is_safe<_It1, _It2,
_Pr> && same_as<_Pj1, identity> && same_as<_Pj2, identity>) {
if constexpr (_Memcmp_in_equal_is_safe<_It1, _It2, _Pr> //
&& same_as<_Pj1, identity> && same_as<_Pj2, identity>) {
if (!_STD is_constant_evaluated()) {
return _Memcmp_count(_First1, _First2, static_cast<size_t>(_Count)) == 0;
}
Expand Down Expand Up @@ -1347,10 +1347,10 @@ namespace ranges {
requires indirectly_copyable<_It, _Out>
constexpr copy_n_result<_It, _Out> operator()(_It _First, iter_difference_t<_It> _Count, _Out _Result) const {
auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
if constexpr (_Ptr_copy_cat<decltype(_UFirst), _Out>::_Trivially_copyable) {
if constexpr (_Memmove_in_copy_is_safe<decltype(_UFirst), _Out>) {
if (!_STD is_constant_evaluated()) {
auto _Final = _UFirst + _Count;
_Result = _Copy_memmove(_STD move(_UFirst), _Final, _STD move(_Result));
_Result = _Memmove_forward(_STD move(_UFirst), _Final, _STD move(_Result));
_Seek_wrapped(_First, _STD move(_Final));
return {_STD move(_First), _STD move(_Result)};
}
Expand Down Expand Up @@ -1501,10 +1501,10 @@ namespace ranges {
requires indirectly_movable<_It, _Out>
constexpr move_result<_It, _Out> _Move_unchecked(_It _First, _Se _Last, _Out _Result) {
// clang-format on
if constexpr (_Ptr_move_cat<_It, _Out>::_Trivially_copyable) {
if constexpr (_Memmove_in_move_is_safe<_It, _Out>) {
if (!_STD is_constant_evaluated()) {
auto _Final = _RANGES next(_First, _STD move(_Last));
_Result = _Copy_memmove(_STD move(_First), _Final, _STD move(_Result));
_Result = _Memmove_forward(_STD move(_First), _Final, _STD move(_Result));
return {_STD move(_Final), _STD move(_Result)};
}
}
Expand Down Expand Up @@ -1559,9 +1559,9 @@ namespace ranges {
template <bidirectional_iterator _It1, bidirectional_iterator _It2>
requires indirectly_movable<_It1, _It2>
constexpr _It2 _Move_backward_common(const _It1 _First, _It1 _Last, _It2 _Result) {
if constexpr (_Ptr_move_cat<_It1, _It2>::_Trivially_copyable) {
if constexpr (_Memmove_in_move_is_safe<_It1, _It2>) {
if (!_STD is_constant_evaluated()) {
return _Copy_backward_memmove(_First, _Last, _Result);
return _Memmove_backward(_First, _Last, _Result);
}
}

Expand Down Expand Up @@ -1925,7 +1925,7 @@ namespace ranges {
template <class _InIt1, class _InIt2, class _Pr>
_NODISCARD _CONSTEXPR20 bool _Equal_rev_pred_unchecked(_InIt1 _First1, _InIt2 _First2, const _InIt2 _Last2, _Pr _Pred) {
// compare [_First1, ...) to [_First2, _Last2)
if constexpr (_Equal_memcmp_is_safe<_InIt1, _InIt2, _Pr>) {
if constexpr (_Memcmp_in_equal_is_safe<_InIt1, _InIt2, _Pr>) {
#ifdef __cpp_lib_is_constant_evaluated
if (!_STD is_constant_evaluated())
#endif // __cpp_lib_is_constant_evaluated
Expand Down Expand Up @@ -2022,7 +2022,7 @@ namespace ranges {
// clang-format off
template <class _It1, class _It2, class _Se2, class _Pr, class _Pj1, class _Pj2>
concept _Equal_rev_pred_can_memcmp = is_same_v<_Pj1, identity> && is_same_v<_Pj2, identity>
&& sized_sentinel_for<_Se2, _It2> && _Equal_memcmp_is_safe<_It1, _It2, _Pr>;
&& sized_sentinel_for<_Se2, _It2> && _Memcmp_in_equal_is_safe<_It1, _It2, _Pr>;

template <input_iterator _It1, input_iterator _It2, sentinel_for<_It2> _Se2, class _Pr, class _Pj1, class _Pj2>
requires indirectly_comparable<_It1, _It2, _Pr, _Pj1, _Pj2>
Expand Down Expand Up @@ -3399,12 +3399,12 @@ namespace ranges {
const auto _ULast = _Get_unwrapped(_STD move(_Last));
if (!_STD is_constant_evaluated()) {
if constexpr (sized_sentinel_for<decltype(_ULast), decltype(_UFirst)>) {
if constexpr (_Fill_memset_is_safe<decltype(_UFirst), _Ty>) {
if constexpr (_Memset_in_fill_is_safe<decltype(_UFirst), _Ty>) {
const auto _Distance = static_cast<size_t>(_ULast - _UFirst);
_Fill_memset(_UFirst, _Value, _Distance);
_Seek_wrapped(_First, _UFirst + _Distance);
return _First;
} else if constexpr (_Fill_zero_memset_is_safe<decltype(_UFirst), _Ty>) {
} else if constexpr (_Memset_in_fill_zero_is_safe<decltype(_UFirst), _Ty>) {
if (_Is_all_bits_zero(_Value)) {
const auto _Distance = static_cast<size_t>(_ULast - _UFirst);
_Fill_zero_memset(_UFirst, _Distance);
Expand Down
62 changes: 33 additions & 29 deletions stl/inc/memory
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ namespace ranges {

constexpr bool _Is_sized1 = sized_sentinel_for<_Se, _It>;
constexpr bool _Is_sized2 = sized_sentinel_for<_OSe, _Out>;
if constexpr (_Ptr_copy_cat<_It, _Out>::_Really_trivial && _Sized_or_unreachable_sentinel_for<_Se, _It> //
if constexpr (_Memmove_in_uninitialized_copy_is_safe<_It, _Out> //
&& _Sized_or_unreachable_sentinel_for<_Se, _It> //
&& _Sized_or_unreachable_sentinel_for<_OSe, _Out>) {
if constexpr (_Is_sized1 && _Is_sized2) {
return _Copy_memcpy_common(_IFirst, _RANGES next(_IFirst, _STD move(_ILast)), _OFirst,
return _Memmove_common(_IFirst, _RANGES next(_IFirst, _STD move(_ILast)), _OFirst,
_RANGES next(_OFirst, _STD move(_OLast)));
} else if constexpr (_Is_sized1) {
return _Copy_memcpy_distance(_IFirst, _OFirst, _IFirst, _RANGES next(_IFirst, _STD move(_ILast)));
return _Memmove_distance(_IFirst, _OFirst, _IFirst, _RANGES next(_IFirst, _STD move(_ILast)));
} else if constexpr (_Is_sized2) {
return _Copy_memcpy_distance(_IFirst, _OFirst, _OFirst, _RANGES next(_OFirst, _STD move(_OLast)));
return _Memmove_distance(_IFirst, _OFirst, _OFirst, _RANGES next(_OFirst, _STD move(_OLast)));
} else {
_STL_ASSERT(false, "Tried to uninitialized_copy two ranges with unreachable sentinels");
}
Expand Down Expand Up @@ -119,8 +120,8 @@ _NoThrowFwdIt uninitialized_copy_n(const _InIt _First, const _Diff _Count_raw, _

auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
if constexpr (_Ptr_copy_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial) {
_UDest = _Copy_memmove(_UFirst, _UFirst + _Count, _UDest);
if constexpr (_Memmove_in_uninitialized_copy_is_safe<decltype(_UFirst), decltype(_UDest)>) {
_UDest = _Memmove_forward(_UFirst, _UFirst + _Count, _UDest);
} else {
_Uninitialized_backout<decltype(_UDest)> _Backout{_UDest};

Expand Down Expand Up @@ -160,14 +161,15 @@ namespace ranges {
auto _IFirst = _Get_unwrapped_n(_STD move(_First1), _Count);
auto _OFirst = _Get_unwrapped(_STD move(_First2));
auto _OLast = _Get_unwrapped(_STD move(_Last2));
if constexpr (_Ptr_copy_cat<_It, _Out>::_Really_trivial && _Sized_or_unreachable_sentinel_for<_OSe, _Out>) {
if constexpr (_Memmove_in_uninitialized_copy_is_safe<_It,
_Out> && _Sized_or_unreachable_sentinel_for<_OSe, _Out>) {
if constexpr (sized_sentinel_for<_OSe, _Out>) {
auto _UResult = _Copy_memcpy_common(
_IFirst, _IFirst + _Count, _OFirst, _RANGES next(_OFirst, _STD move(_OLast)));
auto _UResult =
_Memmove_common(_IFirst, _IFirst + _Count, _OFirst, _RANGES next(_OFirst, _STD move(_OLast)));
_IFirst = _STD move(_UResult.in);
_OFirst = _STD move(_UResult.out);
} else {
auto _UResult = _Copy_memcpy_count(_IFirst, _OFirst, static_cast<size_t>(_Count));
auto _UResult = _Memmove_count(_IFirst, _OFirst, static_cast<size_t>(_Count));
_IFirst = _STD move(_UResult.in);
_OFirst = _STD move(_UResult.out);
}
Expand Down Expand Up @@ -258,8 +260,8 @@ pair<_InIt, _NoThrowFwdIt> uninitialized_move_n(_InIt _First, const _Diff _Count

auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
if constexpr (_Ptr_move_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial) {
_UDest = _Copy_memmove(_UFirst, _UFirst + _Count, _UDest);
if constexpr (_Memmove_in_uninitialized_move_is_safe<decltype(_UFirst), decltype(_UDest)>) {
_UDest = _Memmove_forward(_UFirst, _UFirst + _Count, _UDest);
_UFirst += _Count;
} else {
_Uninitialized_backout<decltype(_UDest)> _Backout{_UDest};
Expand Down Expand Up @@ -303,14 +305,15 @@ namespace ranges {
auto _IFirst = _Get_unwrapped_n(_STD move(_First1), _Count);
auto _OFirst = _Get_unwrapped(_STD move(_First2));
const auto _OLast = _Get_unwrapped(_STD move(_Last2));
if constexpr (_Ptr_move_cat<_It, _Out>::_Really_trivial && _Sized_or_unreachable_sentinel_for<_OSe, _Out>) {
if constexpr (_Memmove_in_uninitialized_move_is_safe<_It,
_Out> && _Sized_or_unreachable_sentinel_for<_OSe, _Out>) {
if constexpr (sized_sentinel_for<_OSe, _Out>) {
auto _UResult = _Copy_memcpy_common(
_IFirst, _IFirst + _Count, _OFirst, _RANGES next(_OFirst, _STD move(_OLast)));
auto _UResult =
_Memmove_common(_IFirst, _IFirst + _Count, _OFirst, _RANGES next(_OFirst, _STD move(_OLast)));
_IFirst = _STD move(_UResult.in);
_OFirst = _STD move(_UResult.out);
} else {
auto _UResult = _Copy_memcpy_count(_IFirst, _OFirst, static_cast<size_t>(_Count));
auto _UResult = _Memmove_count(_IFirst, _OFirst, static_cast<size_t>(_Count));
_IFirst = _STD move(_UResult.in);
_OFirst = _STD move(_UResult.out);
}
Expand Down Expand Up @@ -365,12 +368,12 @@ namespace ranges {
_STL_INTERNAL_STATIC_ASSERT(_No_throw_sentinel_for<_Se, _It>);
_STL_INTERNAL_STATIC_ASSERT(constructible_from<iter_value_t<_It>, const _Ty&>);

if constexpr (_Fill_memset_is_safe<_It, _Ty>) {
if constexpr (_Memset_in_fill_is_safe<_It, _Ty>) {
const auto _OFinal = _RANGES next(_OFirst, _STD move(_OLast));
_Fill_memset(_OFirst, _Val, static_cast<size_t>(_OFinal - _OFirst));
return _OFinal;
} else {
if constexpr (_Fill_zero_memset_is_safe<_It, _Ty>) {
if constexpr (_Memset_in_fill_zero_is_safe<_It, _Ty>) {
if (_Is_all_bits_zero(_Val)) {
const auto _OFinal = _RANGES next(_OFirst, _STD move(_OLast));
_Fill_zero_memset(_OFirst, static_cast<size_t>(_OFinal - _OFirst));
Expand Down Expand Up @@ -403,11 +406,11 @@ _NoThrowFwdIt uninitialized_fill_n(_NoThrowFwdIt _First, const _Diff _Count_raw,
}

auto _UFirst = _Get_unwrapped_n(_First, _Count);
if constexpr (_Fill_memset_is_safe<decltype(_UFirst), _Tval>) {
if constexpr (_Memset_in_fill_is_safe<decltype(_UFirst), _Tval>) {
_Fill_memset(_UFirst, _Val, static_cast<size_t>(_Count));
_UFirst += _Count;
} else {
if constexpr (_Fill_zero_memset_is_safe<decltype(_UFirst), _Tval>) {
if constexpr (_Memset_in_fill_zero_is_safe<decltype(_UFirst), _Tval>) {
if (_Is_all_bits_zero(_Val)) {
_Fill_zero_memset(_UFirst, static_cast<size_t>(_Count));
_Seek_wrapped(_First, _UFirst + _Count);
Expand Down Expand Up @@ -445,11 +448,11 @@ namespace ranges {
}

auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
if constexpr (_Fill_memset_is_safe<decltype(_UFirst), _Ty>) {
if constexpr (_Memset_in_fill_is_safe<_It, _Ty>) {
_Fill_memset(_UFirst, _Val, static_cast<size_t>(_Count));
_Seek_wrapped(_First, _UFirst + _Count);
} else {
if constexpr (_Fill_zero_memset_is_safe<decltype(_UFirst), _Ty>) {
if constexpr (_Memset_in_fill_zero_is_safe<decltype(_UFirst), _Ty>) {
if (_Is_all_bits_zero(_Val)) {
_Fill_zero_memset(_UFirst, static_cast<size_t>(_Count));
_Seek_wrapped(_First, _UFirst + _Count);
Expand Down Expand Up @@ -2207,7 +2210,7 @@ struct _NODISCARD _Reverse_destroy_multidimensional_n_guard {
template <class _Ty, size_t _Size>
void _Uninitialized_copy_multidimensional(const _Ty (&_In)[_Size], _Ty (&_Out)[_Size]) {
if constexpr (is_trivial_v<_Ty>) {
_Copy_memmove(_In, _In + _Size, _Out);
_Memmove_forward(_In, _In + _Size, _Out);
} else if constexpr (is_array_v<_Ty>) {
_Reverse_destroy_multidimensional_n_guard<_Ty> _Guard{_Out, 0};
for (size_t& _Idx = _Guard._Index; _Idx < _Size; ++_Idx) {
Expand Down Expand Up @@ -2270,10 +2273,10 @@ void _Uninitialized_fill_multidimensional_n(_Ty* const _Out, const size_t _Size,
_Uninitialized_copy_multidimensional(_Val, _Out[_Idx]); // intentionally copy, not fill
}
_Guard._Target = nullptr;
} else if constexpr (_Fill_memset_is_safe<_Ty*, _Ty>) {
} else if constexpr (_Memset_in_fill_is_safe<_Ty*, _Ty>) {
_Fill_memset(_Out, _Val, _Size);
} else {
if constexpr (_Fill_zero_memset_is_safe<_Ty*, _Ty>) {
if constexpr (_Memset_in_fill_zero_is_safe<_Ty*, _Ty>) {
if (_Is_all_bits_zero(_Val)) {
_Fill_zero_memset(_Out, _Size);
return;
Expand Down Expand Up @@ -2576,7 +2579,7 @@ template <class _Ty, size_t _Size, class _Alloc>
void _Uninitialized_copy_multidimensional_al(const _Ty (&_In)[_Size], _Ty (&_Out)[_Size], _Alloc& _Al) {
using _Item = remove_all_extents_t<_Ty>;
if constexpr (conjunction_v<is_trivial<_Ty>, _Uses_default_construct<_Alloc, _Item*, const _Item&>>) {
_Copy_memmove(_In, _In + _Size, _Out);
_Memmove_forward(_In, _In + _Size, _Out);
} else if constexpr (is_array_v<_Ty>) {
_Reverse_destroy_multidimensional_n_al_guard<_Ty, _Alloc> _Guard{_Out, 0, _Al};
for (size_t& _Idx = _Guard._Index; _Idx < _Size; ++_Idx) {
Expand Down Expand Up @@ -2620,11 +2623,12 @@ void _Uninitialized_fill_multidimensional_n_al(_Ty* const _Out, const size_t _Si
_Uninitialized_copy_multidimensional_al(_Val, _Out[_Idx], _Al); // intentionally copy, not fill
}
_Guard._Target = nullptr;
} else if constexpr (_Fill_memset_is_safe<_Ty*, _Ty> && _Uses_default_construct<_Alloc, _Ty*, const _Ty&>::value) {
} else if constexpr (_Memset_in_fill_is_safe<_Ty*, _Ty> //
&& _Uses_default_construct<_Alloc, _Ty*, const _Ty&>::value) {
_Fill_memset(_Out, _Val, _Size);
} else {
if constexpr (_Fill_zero_memset_is_safe<_Ty*,
_Ty> && _Uses_default_construct<_Alloc, _Ty*, const _Ty&>::value) {
if constexpr (_Memset_in_fill_zero_is_safe<_Ty*, _Ty> //
&& _Uses_default_construct<_Alloc, _Ty*, const _Ty&>::value) {
if (_Is_all_bits_zero(_Val)) {
_Fill_zero_memset(_Out, _Size);
return;
Expand Down
8 changes: 4 additions & 4 deletions stl/inc/vector
Original file line number Diff line number Diff line change
Expand Up @@ -669,13 +669,13 @@ private:
_My_data._Orphan_all();

const auto _Oldcapacity = static_cast<size_type>(_My_data._Myend - _Myfirst);
if constexpr (conjunction_v<bool_constant<_Ptr_copy_cat<_Ty*, _Ty*>::_Trivially_copyable>,
if constexpr (conjunction_v<bool_constant<_Memmove_in_copy_is_safe<_Ty*, _Ty*>>,
_Uses_default_construct<_Alty, _Ty*, _Ty>, _Uses_default_destroy<_Alty, _Ty*>>) {
if (_Newsize > _Oldcapacity) {
_Clear_and_reserve_geometric(_Newsize);
}

_Mylast = _Refancy<pointer>(_Copy_memmove(_Unfancy(_First), _Unfancy(_Last), _Unfancy(_Myfirst)));
_Mylast = _Refancy<pointer>(_Memmove_forward(_Unfancy(_First), _Unfancy(_Last), _Unfancy(_Myfirst)));
} else {
auto _Oldsize = static_cast<size_type>(_Mylast - _Myfirst);

Expand Down Expand Up @@ -1145,7 +1145,7 @@ private:

_My_data._Orphan_all();

if constexpr (conjunction_v<bool_constant<_Ptr_copy_cat<_Iter, _Ty*>::_Trivially_copyable>,
if constexpr (conjunction_v<bool_constant<_Memmove_in_copy_is_safe<_Iter, _Ty*>>,
_Uses_default_construct<_Alty, _Ty*, decltype(*_First)>,
_Uses_default_destroy<_Alty, _Ty*>>) {
#ifdef __cpp_lib_constexpr_dynamic_alloc
Expand All @@ -1157,7 +1157,7 @@ private:
_Clear_and_reserve_geometric(_Newsize);
}

_Mylast = _Refancy<pointer>(_Copy_memmove(_First, _Last, _Unfancy(_Myfirst)));
_Mylast = _Refancy<pointer>(_Memmove_forward(_First, _Last, _Unfancy(_Myfirst)));
return;
}
}
Expand Down
Loading