Skip to content

Commit

Permalink
do the other changes
Browse files Browse the repository at this point in the history
also, switch to having a `_Is_ranges_fwd_iter_v` type trait
  • Loading branch information
strega-nil committed Jul 27, 2022
1 parent c14cf5f commit f7d037e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
18 changes: 7 additions & 11 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,9 @@ _NODISCARD _CONSTEXPR20 bool is_permutation(
}

return true;
} else if constexpr (_Is_fwd_iter_v<_FwdIt1> && _Is_fwd_iter_v<_FwdIt2>) {
} else {
static_assert(_Is_ranges_fwd_iter_v<_FwdIt1> && _Is_ranges_fwd_iter_v<_FwdIt2>,
"Iterators must be at least forward iterators");
for (;; ++_UFirst1, (void) ++_UFirst2) { // trim matching prefix
if (_UFirst1 == _ULast1) {
return _UFirst2 == _ULast2;
Expand Down Expand Up @@ -896,8 +898,6 @@ _NODISCARD _CONSTEXPR20 bool is_permutation(
return false; // sequence 1 is longer than sequence 2, not a permutation
}
}
} else {
static_assert(_Always_false<_FwdIt1>, "Iterators must be at least forward iterators");
}
}

Expand Down Expand Up @@ -4096,14 +4096,9 @@ namespace ranges {
} // namespace ranges
#endif // __cpp_lib_concepts

#ifdef __cpp_lib_concepts
template <class _InIt, class _OutIt>
concept _Can_reread_dest = forward_iterator<_OutIt> && same_as<iter_value_t<_InIt>, iter_value_t<_OutIt>>;
#else // ^^^ defined(__cpp_lib_concepts) / !defined(__cpp_lib_concepts) vvv
template <class _InIt, class _OutIt>
_INLINE_VAR constexpr bool _Can_reread_dest = _Is_fwd_iter_v<_OutIt> //
&& is_same_v<_Iter_value_t<_InIt>, _Iter_value_t<_OutIt>>;
#endif // __cpp_lib_concepts

template <class _InIt, class _OutIt, class _Pr>
_CONSTEXPR20 _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred) {
Expand All @@ -4119,7 +4114,7 @@ _CONSTEXPR20 _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pr

auto _UDest = _Get_unwrapped_unverified(_Dest);

if constexpr (_Is_fwd_iter_v<_InIt>) { // can reread the source for comparison
if constexpr (_Is_ranges_fwd_iter_v<_InIt>) { // can reread the source for comparison
auto _Firstb = _UFirst;

*_UDest = *_Firstb;
Expand Down Expand Up @@ -4728,16 +4723,17 @@ _SampleIt _Sample_selection_unchecked(
template <class _PopIt, class _SampleIt, class _Diff, class _Urng>
_SampleIt sample(_PopIt _First, _PopIt _Last, _SampleIt _Dest, _Diff _Count, _Urng&& _Func) {
// randomly select _Count elements from [_First, _Last) into _Dest
static_assert(_Is_fwd_iter_v<_PopIt> || _Is_random_iter_v<_SampleIt>,
static_assert(_Is_ranges_fwd_iter_v<_PopIt> || _Is_random_iter_v<_SampleIt>,
"If the source range is not forward, the destination range must be random-access.");

static_assert(is_integral_v<_Diff>, "The sample size must have an integer type.");
_Adl_verify_range(_First, _Last);
if (0 < _Count) {
auto _UFirst = _Get_unwrapped(_First);
auto _ULast = _Get_unwrapped(_Last);
using _PopDiff = _Iter_diff_t<_PopIt>;
_Rng_from_urng<_PopDiff, remove_reference_t<_Urng>> _RngFunc(_Func);
if constexpr (_Is_fwd_iter_v<_PopIt>) {
if constexpr (_Is_ranges_fwd_iter_v<_PopIt>) {
// source is forward: use selection sampling (stable)
using _CT = common_type_t<_Diff, _PopDiff>;
const auto _Pop_size = _STD distance(_UFirst, _ULast);
Expand Down
6 changes: 3 additions & 3 deletions stl/inc/functional
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,9 @@ _CONSTEXPR20 pair<_FwdItHaystack, _FwdItHaystack> _Search_pair_unchecked(
}

return {_Last1, _Last1};
} else if constexpr (_Is_fwd_iter_v<_FwdItHaystack> && _Is_fwd_iter_v<_FwdItPat>) {
} else {
static_assert(_Is_ranges_fwd_iter_v<_FwdItHaystack> && _Is_ranges_fwd_iter_v<_FwdItPat>,
"Iterators must be at least forward iterators");
for (;; ++_First1) { // loop until match or end of a sequence
_FwdItHaystack _Mid1 = _First1;
for (_FwdItPat _Mid2 = _First2;; ++_Mid1, (void) ++_Mid2) {
Expand All @@ -2221,8 +2223,6 @@ _CONSTEXPR20 pair<_FwdItHaystack, _FwdItHaystack> _Search_pair_unchecked(
}
}
}
} else {
static_assert(_Always_false<_FwdItHaystack>, "Iterators must be at least forward iterators");
}
}

Expand Down
18 changes: 4 additions & 14 deletions stl/inc/vector
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ public:
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
auto _ULast = _Get_unwrapped(_Last);
if constexpr (_Is_fwd_iter_v<_Iter>) {
if constexpr (_Is_ranges_fwd_iter_v<_Iter>) {
const auto _Count = _Convert_size<size_type>(static_cast<size_t>(_STD distance(_UFirst, _ULast)));
_Construct_n(_Count, _STD move(_UFirst), _STD move(_ULast));
#ifdef __cpp_lib_concepts
Expand Down Expand Up @@ -1206,12 +1206,7 @@ public:

_Adl_verify_range(_First, _Last);
const auto _Whereoff = static_cast<size_type>(_Whereptr - _Oldfirst);
#ifdef __cpp_lib_concepts
constexpr bool _Is_fwd = _Is_fwd_iter_v<_Iter> || forward_iterator<_Iter>;
#else // ^^^ __cpp_lib_concepts ^^^ / vvv !__cpp_lib_concepts vvv
constexpr bool _Is_fwd = _Is_fwd_iter_v<_Iter>;
#endif // ^^^ !__cpp_lib_concepts ^^^
if constexpr (_Is_fwd) {
if constexpr (_Is_ranges_fwd_iter_v<_Iter>) {
_Insert_forward_range(_Where, _Get_unwrapped(_First), _Get_unwrapped(_Last));
} else {
_Insert_input_range(_Where, _Get_unwrapped(_First), _Get_unwrapped(_Last));
Expand Down Expand Up @@ -1362,12 +1357,7 @@ public:
template <class _Iter, enable_if_t<_Is_iterator_v<_Iter>, int> = 0>
_CONSTEXPR20 void assign(_Iter _First, _Iter _Last) {
_Adl_verify_range(_First, _Last);
#ifdef __cpp_lib_concepts
constexpr bool _Is_fwd = _Is_fwd_iter_v<_Iter> || forward_iterator<_Iter>;
#else // ^^^ __cpp_lib_concepts ^^^ / vvv !__cpp_lib_concepts vvv
constexpr bool _Is_fwd = _Is_fwd_iter_v<_Iter>;
#endif // ^^^ !__cpp_lib_concepts ^^^
if constexpr (_Is_fwd) {
if constexpr (_Is_ranges_fwd_iter_v<_Iter>) {
_Assign_forward_range(_Get_unwrapped(_First), _Get_unwrapped(_Last));
} else {
_Assign_input_range(_Get_unwrapped(_First), _Get_unwrapped(_Last));
Expand Down Expand Up @@ -3145,7 +3135,7 @@ public:
_CONSTEXPR20 iterator insert(const_iterator _Where, _Iter _First, _Iter _Last) {
const difference_type _Saved_offset = _Where - begin();

if constexpr (_Is_fwd_iter_v<_Iter>) {
if constexpr (_Is_ranges_fwd_iter_v<_Iter>) {
_Adl_verify_range(_First, _Last);
const auto _Count = _Convert_size<size_type>(static_cast<size_t>(_STD distance(_First, _Last)));
const size_type _Off = _Insert_x(_Where, _Count);
Expand Down
20 changes: 10 additions & 10 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,15 @@ template <class _Iter>
_INLINE_VAR constexpr bool _Is_fwd_iter_v = is_convertible_v<_Iter_cat_t<_Iter>, forward_iterator_tag>;

template <class _Iter>
_INLINE_VAR constexpr bool _Is_bidi_iter_v = is_convertible_v<_Iter_cat_t<_Iter>, bidirectional_iterator_tag>;
_INLINE_VAR constexpr bool _Is_ranges_fwd_iter_v =
#if defined(__cpp_lib_concepts)
forward_iterator<_Iter>;
#else
is_convertible_v<_Iter_cat_t<_Iter>, forward_iterator_tag>;
#endif

template <class _Iter>
_INLINE_VAR constexpr bool _Is_random_iter_v = is_convertible_v<_Iter_cat_t<_Iter>, random_access_iterator_tag>;
_INLINE_VAR constexpr bool _Is_bidi_iter_v = is_convertible_v<_Iter_cat_t<_Iter>, bidirectional_iterator_tag>;

template <class, class = void>
struct _Is_checked_helper {}; // default definition, no longer used, retained due to pseudo-documentation
Expand Down Expand Up @@ -1039,13 +1044,8 @@ using _Enable_if_execution_policy_t = typename remove_reference_t<_ExPo>::_Stand
#define _REQUIRE_MUTABLE_PARALLEL_ITERATOR(_Iter) \
static_assert(_Is_fwd_iter_v<_Iter>, \
"Parallel algorithms require that mutable iterators be Cpp17ForwardIterators or stronger.")
#ifdef __cpp_lib_concepts
#define _REQUIRE_CONST_PARALLEL_ITERATOR(_Iter) \
static_assert(_STD forward_iterator<_Iter>, "Parallel algorithms require forward iterators or stronger.")
#else
#define _REQUIRE_CONST_PARALLEL_ITERATOR(_Iter) \
static_assert(_Is_fwd_iter_v<_Iter>, "Parallel algorithms require forward iterators or stronger.")
#endif
static_assert(_Is_ranges_fwd_iter_v, "Parallel algorithms require forward iterators or stronger.")

#endif // _HAS_CXX17

Expand Down Expand Up @@ -1106,7 +1106,7 @@ constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) noexcept
template <class _InIt, class _Sentinel, class _Pr>
constexpr void _Debug_order_unchecked(_InIt _First, _Sentinel _Last, _Pr&& _Pred) {
// test if range is ordered by predicate
if constexpr (_Is_fwd_iter_v<_InIt>) {
if constexpr (_Is_ranges_fwd_iter_v<_InIt>) {
if (_First != _Last) {
for (auto _Next = _First; ++_Next != _Last; _First = _Next) {
_STL_VERIFY(!static_cast<bool>(_Pred(*_Next, *_First)), "sequence not ordered");
Expand All @@ -1118,7 +1118,7 @@ constexpr void _Debug_order_unchecked(_InIt _First, _Sentinel _Last, _Pr&& _Pred
template <class _OtherIt, class _InIt, class _Pr>
constexpr void _Debug_order_set_unchecked(_InIt _First, _InIt _Last, _Pr&& _Pred) {
// test if range is ordered by predicate
if constexpr (is_same_v<_Iter_value_t<_OtherIt>, _Iter_value_t<_InIt>> && _Is_fwd_iter_v<_InIt>) {
if constexpr (is_same_v<_Iter_value_t<_OtherIt>, _Iter_value_t<_InIt>>) {
_Debug_order_unchecked(_First, _Last, _Pred);
}
}
Expand Down

0 comments on commit f7d037e

Please sign in to comment.