From 6e257ee19c56be730ada83ef40308922df6e83a0 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Tue, 17 Dec 2024 12:35:28 -0500 Subject: [PATCH] Simplify std::fill and std::count for bit iterator --- libcxx/include/__algorithm/count.h | 14 ++++++-------- libcxx/include/__algorithm/fill_n.h | 18 +++++++----------- libcxx/include/__algorithm/find.h | 14 ++++++-------- libcxx/include/__bit/invert_if.h | 6 +++--- libcxx/include/__bit_reference | 14 +++++++------- libcxx/include/__cxx03/__algorithm/count.h | 14 ++++++-------- libcxx/include/__cxx03/__algorithm/fill_n.h | 18 +++++++----------- libcxx/include/__cxx03/__algorithm/find.h | 14 ++++++-------- libcxx/include/__cxx03/__bit/invert_if.h | 6 +++--- libcxx/include/__cxx03/__bit_reference | 14 +++++++------- 10 files changed, 58 insertions(+), 74 deletions(-) diff --git a/libcxx/include/__algorithm/count.h b/libcxx/include/__algorithm/count.h index 6910b4f43e993..a2115f744b394 100644 --- a/libcxx/include/__algorithm/count.h +++ b/libcxx/include/__algorithm/count.h @@ -42,9 +42,9 @@ __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) { } // __bit_iterator implementation -template +template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type -__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { +__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool __to_count) { using _It = __bit_iterator<_Cp, _IsConst>; using __storage_type = typename _It::__storage_type; using difference_type = typename _It::difference_type; @@ -56,17 +56,17 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __dn = std::min(__clz_f, __n); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); - __r = std::__libcpp_popcount(std::__invert_if(*__first.__seg_) & __m); + __r = std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !__to_count) & __m); __n -= __dn; ++__first.__seg_; } // do middle whole words for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) - __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_)); + __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !__to_count)); // do last partial word if (__n > 0) { __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_) & __m); + __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !__to_count) & __m); } return __r; } @@ -74,9 +74,7 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> > __count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { - if (__value) - return std::__count_bool(__first, static_cast(__last - __first)); - return std::__count_bool(__first, static_cast(__last - __first)); + return std::__count_bool(__first, static_cast(__last - __first), static_cast(__value)); } template diff --git a/libcxx/include/__algorithm/fill_n.h b/libcxx/include/__algorithm/fill_n.h index 5069a72783f34..f3af58f4d9664 100644 --- a/libcxx/include/__algorithm/fill_n.h +++ b/libcxx/include/__algorithm/fill_n.h @@ -30,9 +30,9 @@ template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value); -template +template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void -__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { +__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __to_fill) { using _It = __bit_iterator<_Cp, false>; using __storage_type = typename _It::__storage_type; @@ -42,7 +42,7 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __dn = std::min(__clz_f, __n); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); - if (_FillVal) + if (__to_fill) *__first.__seg_ |= __m; else *__first.__seg_ &= ~__m; @@ -51,13 +51,13 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { } // do middle whole words __storage_type __nw = __n / __bits_per_word; - std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0); + std::__fill_n(std::__to_address(__first.__seg_), __nw, __to_fill ? static_cast<__storage_type>(-1) : 0); __n -= __nw * __bits_per_word; // do last partial word if (__n > 0) { __first.__seg_ += __nw; __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - if (_FillVal) + if (__to_fill) *__first.__seg_ |= __m; else *__first.__seg_ &= ~__m; @@ -67,12 +67,8 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> __fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) { - if (__n > 0) { - if (__value) - std::__fill_n_bool(__first, __n); - else - std::__fill_n_bool(__first, __n); - } + if (__n > 0) + std::__fill_n_bool(__first, __n, __value); return __first + __n; } diff --git a/libcxx/include/__algorithm/find.h b/libcxx/include/__algorithm/find.h index a05d50718595e..b16ca8fcb8fd5 100644 --- a/libcxx/include/__algorithm/find.h +++ b/libcxx/include/__algorithm/find.h @@ -95,9 +95,9 @@ __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) { } // __bit_iterator implementation -template +template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst> -__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { +__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool __to_find) { using _It = __bit_iterator<_Cp, _IsConst>; using __storage_type = typename _It::__storage_type; @@ -107,7 +107,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __dn = std::min(__clz_f, __n); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); - __storage_type __b = std::__invert_if(*__first.__seg_) & __m; + __storage_type __b = std::__invert_if(*__first.__seg_, !__to_find) & __m; if (__b) return _It(__first.__seg_, static_cast(std::__libcpp_ctz(__b))); if (__n == __dn) @@ -117,14 +117,14 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) } // do middle whole words for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) { - __storage_type __b = std::__invert_if(*__first.__seg_); + __storage_type __b = std::__invert_if(*__first.__seg_, !__to_find); if (__b) return _It(__first.__seg_, static_cast(std::__libcpp_ctz(__b))); } // do last partial word if (__n > 0) { __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - __storage_type __b = std::__invert_if(*__first.__seg_) & __m; + __storage_type __b = std::__invert_if(*__first.__seg_, !__to_find) & __m; if (__b) return _It(__first.__seg_, static_cast(std::__libcpp_ctz(__b))); } @@ -134,9 +134,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) template ::value, int> = 0> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst> __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { - if (static_cast(__value)) - return std::__find_bool(__first, static_cast(__last - __first)); - return std::__find_bool(__first, static_cast(__last - __first)); + return std::__find_bool(__first, static_cast(__last - __first), static_cast(__value)); } // segmented iterator implementation diff --git a/libcxx/include/__bit/invert_if.h b/libcxx/include/__bit/invert_if.h index f7606ede26da0..59be02445cea8 100644 --- a/libcxx/include/__bit/invert_if.h +++ b/libcxx/include/__bit/invert_if.h @@ -18,9 +18,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD -template -_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v) { - if (_Invert) +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v, bool __invert) { + if (__invert) return ~__v; return __v; } diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference index 9fa24c98d493f..0cc6892dba062 100644 --- a/libcxx/include/__bit_reference +++ b/libcxx/include/__bit_reference @@ -964,9 +964,9 @@ private: template friend struct __bit_array; - template + template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend void - __fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); + __fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n, bool __to_fill); template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned( @@ -1007,12 +1007,12 @@ private: template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool equal(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>); - template + template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC> - __find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); - template - friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI - _LIBCPP_CONSTEXPR_SINCE_CXX20 __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); + __find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool __to_find); + template + friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 + __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool __to_count); }; _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__cxx03/__algorithm/count.h b/libcxx/include/__cxx03/__algorithm/count.h index 7c1fc3e579898..e7675923d905b 100644 --- a/libcxx/include/__cxx03/__algorithm/count.h +++ b/libcxx/include/__cxx03/__algorithm/count.h @@ -41,9 +41,9 @@ __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) { } // __bit_iterator implementation -template +template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type -__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { +__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool __to_count) { using _It = __bit_iterator<_Cp, _IsConst>; using __storage_type = typename _It::__storage_type; using difference_type = typename _It::difference_type; @@ -55,17 +55,17 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __dn = std::min(__clz_f, __n); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); - __r = std::__libcpp_popcount(std::__invert_if(*__first.__seg_) & __m); + __r = std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !__to_count) & __m); __n -= __dn; ++__first.__seg_; } // do middle whole words for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) - __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_)); + __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !__to_count)); // do last partial word if (__n > 0) { __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_) & __m); + __r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !__to_count) & __m); } return __r; } @@ -73,9 +73,7 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> > __count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { - if (__value) - return std::__count_bool(__first, static_cast(__last - __first)); - return std::__count_bool(__first, static_cast(__last - __first)); + return std::__count_bool(__first, static_cast(__last - __first), static_cast(__value)); } template diff --git a/libcxx/include/__cxx03/__algorithm/fill_n.h b/libcxx/include/__cxx03/__algorithm/fill_n.h index 99b712c7b0360..ac96e53cfa24c 100644 --- a/libcxx/include/__cxx03/__algorithm/fill_n.h +++ b/libcxx/include/__cxx03/__algorithm/fill_n.h @@ -31,9 +31,9 @@ template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value); -template +template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void -__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { +__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __to_fill) { using _It = __bit_iterator<_Cp, false>; using __storage_type = typename _It::__storage_type; @@ -43,7 +43,7 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __dn = std::min(__clz_f, __n); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); - if (_FillVal) + if (__to_fill) *__first.__seg_ |= __m; else *__first.__seg_ &= ~__m; @@ -52,13 +52,13 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { } // do middle whole words __storage_type __nw = __n / __bits_per_word; - std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0); + std::__fill_n(std::__to_address(__first.__seg_), __nw, __to_fill ? static_cast<__storage_type>(-1) : 0); __n -= __nw * __bits_per_word; // do last partial word if (__n > 0) { __first.__seg_ += __nw; __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - if (_FillVal) + if (__to_fill) *__first.__seg_ |= __m; else *__first.__seg_ &= ~__m; @@ -68,12 +68,8 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> __fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) { - if (__n > 0) { - if (__value) - std::__fill_n_bool(__first, __n); - else - std::__fill_n_bool(__first, __n); - } + if (__n > 0) + std::__fill_n_bool(__first, __n, __value); return __first + __n; } diff --git a/libcxx/include/__cxx03/__algorithm/find.h b/libcxx/include/__cxx03/__algorithm/find.h index ff5ac9b8b1bd0..23263da2dd1e8 100644 --- a/libcxx/include/__cxx03/__algorithm/find.h +++ b/libcxx/include/__cxx03/__algorithm/find.h @@ -94,9 +94,9 @@ __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) { } // __bit_iterator implementation -template +template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst> -__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { +__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool __to_find) { using _It = __bit_iterator<_Cp, _IsConst>; using __storage_type = typename _It::__storage_type; @@ -106,7 +106,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __dn = std::min(__clz_f, __n); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); - __storage_type __b = std::__invert_if(*__first.__seg_) & __m; + __storage_type __b = std::__invert_if(*__first.__seg_, !__to_find) & __m; if (__b) return _It(__first.__seg_, static_cast(std::__libcpp_ctz(__b))); if (__n == __dn) @@ -116,14 +116,14 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) } // do middle whole words for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) { - __storage_type __b = std::__invert_if(*__first.__seg_); + __storage_type __b = std::__invert_if(*__first.__seg_, !__to_find); if (__b) return _It(__first.__seg_, static_cast(std::__libcpp_ctz(__b))); } // do last partial word if (__n > 0) { __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - __storage_type __b = std::__invert_if(*__first.__seg_) & __m; + __storage_type __b = std::__invert_if(*__first.__seg_, !__to_find) & __m; if (__b) return _It(__first.__seg_, static_cast(std::__libcpp_ctz(__b))); } @@ -133,9 +133,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) template ::value, int> = 0> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst> __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { - if (static_cast(__value)) - return std::__find_bool(__first, static_cast(__last - __first)); - return std::__find_bool(__first, static_cast(__last - __first)); + return std::__find_bool(__first, static_cast(__last - __first), static_cast(__value)); } // segmented iterator implementation diff --git a/libcxx/include/__cxx03/__bit/invert_if.h b/libcxx/include/__cxx03/__bit/invert_if.h index b111d702ea755..46e16728ca494 100644 --- a/libcxx/include/__cxx03/__bit/invert_if.h +++ b/libcxx/include/__cxx03/__bit/invert_if.h @@ -18,9 +18,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD -template -_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v) { - if (_Invert) +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v, bool __invert) { + if (__invert) return ~__v; return __v; } diff --git a/libcxx/include/__cxx03/__bit_reference b/libcxx/include/__cxx03/__bit_reference index bf86f9a76e24a..a0d7fbf4a1e56 100644 --- a/libcxx/include/__cxx03/__bit_reference +++ b/libcxx/include/__cxx03/__bit_reference @@ -966,9 +966,9 @@ private: template friend struct __bit_array; - template + template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend void - __fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); + __fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n, bool __to_fill); template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned( @@ -1009,12 +1009,12 @@ private: template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool equal(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>); - template + template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC> - __find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); - template - friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI - _LIBCPP_CONSTEXPR_SINCE_CXX20 __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); + __find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool __to_find); + template + friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 + __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool __to_count); }; _LIBCPP_END_NAMESPACE_STD