Skip to content

Commit 092ea87

Browse files
committed
optimization
1 parent ddd9e24 commit 092ea87

File tree

3 files changed

+128
-15
lines changed

3 files changed

+128
-15
lines changed

stl/inc/algorithm

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,18 +1506,30 @@ namespace ranges {
15061506
} // namespace ranges
15071507
#endif // _HAS_CXX20
15081508

1509+
struct _All_of_vbool_traits;
1510+
struct _Any_of_vbool_traits;
1511+
struct _None_of_vbool_traits;
1512+
1513+
template <class _Traits, class _VbIt, class _Mapped_fn>
1514+
_NODISCARD _CONSTEXPR20 bool _Meow_of_vbool(_VbIt _First, _VbIt _Last, _Mapped_fn _Mapped_func);
1515+
15091516
_EXPORT_STD template <class _InIt, class _Pr>
15101517
_NODISCARD _CONSTEXPR20 bool all_of(_InIt _First, _InIt _Last, _Pr _Pred) { // test if all elements satisfy _Pred
15111518
_STD _Adl_verify_range(_First, _Last);
15121519
auto _UFirst = _STD _Get_unwrapped(_First);
15131520
const auto _ULast = _STD _Get_unwrapped(_Last);
1514-
for (; _UFirst != _ULast; ++_UFirst) {
1515-
if (!_Pred(*_UFirst)) {
1516-
return false;
1521+
1522+
if constexpr (_Is_vb_iterator<decltype(_UFirst)> && !is_void_v<_Map_vb_functor_t<_Pr>>) {
1523+
return _Meow_of_vbool<_All_of_vbool_traits>(_UFirst, _ULast, _Map_vb_functor_t<_Pr>{});
1524+
} else {
1525+
for (; _UFirst != _ULast; ++_UFirst) {
1526+
if (!_Pred(*_UFirst)) {
1527+
return false;
1528+
}
15171529
}
1518-
}
15191530

1520-
return true;
1531+
return true;
1532+
}
15211533
}
15221534

15231535
#if _HAS_CXX17
@@ -1573,13 +1585,18 @@ _NODISCARD _CONSTEXPR20 bool any_of(const _InIt _First, const _InIt _Last, _Pr _
15731585
_STD _Adl_verify_range(_First, _Last);
15741586
auto _UFirst = _STD _Get_unwrapped(_First);
15751587
const auto _ULast = _STD _Get_unwrapped(_Last);
1576-
for (; _UFirst != _ULast; ++_UFirst) {
1577-
if (_Pred(*_UFirst)) {
1578-
return true;
1588+
1589+
if constexpr (_Is_vb_iterator<decltype(_UFirst)> && !is_void_v<_Map_vb_functor_t<_Pr>>) {
1590+
return _Meow_of_vbool<_Any_of_vbool_traits>(_UFirst, _ULast, _Map_vb_functor_t<_Pr>{});
1591+
} else {
1592+
for (; _UFirst != _ULast; ++_UFirst) {
1593+
if (_Pred(*_UFirst)) {
1594+
return true;
1595+
}
15791596
}
1580-
}
15811597

1582-
return false;
1598+
return false;
1599+
}
15831600
}
15841601

15851602
#if _HAS_CXX17
@@ -1635,13 +1652,17 @@ _NODISCARD _CONSTEXPR20 bool none_of(const _InIt _First, const _InIt _Last, _Pr
16351652
_STD _Adl_verify_range(_First, _Last);
16361653
auto _UFirst = _STD _Get_unwrapped(_First);
16371654
const auto _ULast = _STD _Get_unwrapped(_Last);
1638-
for (; _UFirst != _ULast; ++_UFirst) {
1639-
if (_Pred(*_UFirst)) {
1640-
return false;
1655+
if constexpr (_Is_vb_iterator<decltype(_UFirst)> && !is_void_v<_Map_vb_functor_t<_Pr>>) {
1656+
return _Meow_of_vbool<_None_of_vbool_traits>(_UFirst, _ULast, _Map_vb_functor_t<_Pr>{});
1657+
} else {
1658+
for (; _UFirst != _ULast; ++_UFirst) {
1659+
if (_Pred(*_UFirst)) {
1660+
return false;
1661+
}
16411662
}
1642-
}
16431663

1644-
return true;
1664+
return true;
1665+
}
16451666
}
16461667

16471668
#if _HAS_CXX17

stl/inc/vector

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,91 @@ _CONSTEXPR20 _OutIt _Transform_vbool_aligned(
40474047
return _Dest;
40484048
}
40494049

4050+
struct _All_of_vbool_traits {
4051+
static constexpr bool _Default_result = true;
4052+
4053+
static _CONSTEXPR20 bool _Check(const _Vbase _Value) {
4054+
return _Value != ~_Vbase{0};
4055+
}
4056+
4057+
static _CONSTEXPR20 bool _Check(const _Vbase _Value, const _Vbase _Mask) {
4058+
return (_Value & _Mask) != _Mask;
4059+
}
4060+
};
4061+
4062+
struct _Any_of_vbool_traits_base {
4063+
static _CONSTEXPR20 bool _Check(const _Vbase _Value) {
4064+
return _Value != 0;
4065+
}
4066+
4067+
static _CONSTEXPR20 bool _Check(const _Vbase _Value, const _Vbase _Mask) {
4068+
return (_Value & _Mask) != 0;
4069+
}
4070+
};
4071+
4072+
struct _Any_of_vbool_traits : _Any_of_vbool_traits_base {
4073+
static constexpr bool _Default_result = false;
4074+
};
4075+
4076+
struct _None_of_vbool_traits : _Any_of_vbool_traits_base {
4077+
static constexpr bool _Default_result = true;
4078+
};
4079+
4080+
template <class _Traits, class _VbIt, class _Mapped_fn>
4081+
_NODISCARD _CONSTEXPR20 bool _Meow_of_vbool(const _VbIt _First, const _VbIt _Last, const _Mapped_fn _Mapped_func) {
4082+
constexpr bool _Early_result = !_Traits::_Default_result;
4083+
auto _First_ptr = _First._Myptr;
4084+
const auto _Last_ptr = _Last._Myptr;
4085+
4086+
if (_First_ptr == _Last_ptr) {
4087+
const _Vbase _Mask = (_Vbase{1} << _Last._Myoff) - (_Vbase{1} << _First._Myoff);
4088+
if (_Mask == 0) {
4089+
return _Traits::_Default_result;
4090+
} else {
4091+
return _Traits::_Check(_Mapped_func(*_First_ptr), _Mask) ? _Early_result : _Traits::_Default_result;
4092+
}
4093+
}
4094+
4095+
if (_First._Myoff != 0) {
4096+
const _Vbase _Mask = static_cast<_Vbase>(-1) << _First._Myoff;
4097+
if (_Traits::_Check(_Mapped_func(*_First_ptr), _Mask)) {
4098+
return _Early_result;
4099+
}
4100+
4101+
++_First_ptr;
4102+
}
4103+
4104+
for (; _First_ptr != _Last_ptr; ++_First_ptr) {
4105+
if (_Traits::_Check(_Mapped_func(*_First_ptr))) {
4106+
return _Early_result;
4107+
}
4108+
}
4109+
4110+
if (_Last._Myoff != 0) {
4111+
const _Vbase _Mask = (_Vbase{1} << _Last._Myoff) - 1;
4112+
if (_Traits::_Check(_Mapped_func(*_First_ptr), _Mask)) {
4113+
return _Early_result;
4114+
}
4115+
}
4116+
4117+
return _Traits::_Default_result;
4118+
}
4119+
4120+
template <class _VbIt, class _Mapped_fn>
4121+
_NODISCARD _CONSTEXPR20 bool _All_of_vbool(const _VbIt _First, const _VbIt _Last, const _Mapped_fn _Mapped_func) {
4122+
return _Meow_of_vbool<_All_of_vbool_traits>(_First, _Last, _Mapped_func);
4123+
}
4124+
4125+
template <class _VbIt, class _Mapped_fn>
4126+
_NODISCARD _CONSTEXPR20 bool _Any_of_vbool(const _VbIt _First, const _VbIt _Last, const _Mapped_fn _Mapped_func) {
4127+
return _Meow_of_vbool<_Any_of_vbool_traits>(_First, _Last, _Mapped_func);
4128+
}
4129+
4130+
template <class _VbIt, class _Mapped_fn>
4131+
_NODISCARD _CONSTEXPR20 bool _None_of_vbool(const _VbIt _First, const _VbIt _Last, const _Mapped_fn _Mapped_func) {
4132+
return _Meow_of_vbool<_None_of_vbool_traits>(_First, _Last, _Mapped_func);
4133+
}
4134+
40504135
#undef _ASAN_VECTOR_MODIFY
40514136
#undef _ASAN_VECTOR_REMOVE
40524137
#undef _ASAN_VECTOR_CREATE

stl/inc/xutility

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4873,6 +4873,13 @@ struct _Map_vb_functor {
48734873
using type = void;
48744874
};
48754875

4876+
#if _HAS_CXX20
4877+
template <>
4878+
struct _Map_vb_functor<identity> {
4879+
using type = identity;
4880+
};
4881+
#endif // _HAS_CXX20
4882+
48764883
template <class _Fn>
48774884
using _Map_vb_functor_t = typename _Map_vb_functor<_Fn>::type;
48784885

0 commit comments

Comments
 (0)