Skip to content

Commit 7e29624

Browse files
committed
Simplify std::fill and std::count for bit iterator
1 parent e0a79ee commit 7e29624

File tree

10 files changed

+47
-63
lines changed

10 files changed

+47
-63
lines changed

libcxx/include/__algorithm/count.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
4242
}
4343

4444
// __bit_iterator implementation
45-
template <bool _ToCount, class _Cp, bool _IsConst>
45+
template <class _Cp, bool _IsConst>
4646
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type
47-
__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
47+
__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool _ToCount) {
4848
using _It = __bit_iterator<_Cp, _IsConst>;
4949
using __storage_type = typename _It::__storage_type;
5050
using difference_type = typename _It::difference_type;
@@ -56,27 +56,25 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
5656
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
5757
__storage_type __dn = std::min(__clz_f, __n);
5858
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
59-
__r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
59+
__r = std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !_ToCount) & __m);
6060
__n -= __dn;
6161
++__first.__seg_;
6262
}
6363
// do middle whole words
6464
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
65-
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
65+
__r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !_ToCount));
6666
// do last partial word
6767
if (__n > 0) {
6868
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
69-
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
69+
__r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !_ToCount) & __m);
7070
}
7171
return __r;
7272
}
7373

7474
template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
7575
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >
7676
__count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
77-
if (__value)
78-
return std::__count_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
79-
return std::__count_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
77+
return std::__count_bool(__first, static_cast<typename _Cp::size_type>(__last - __first), static_cast<bool>(__value));
8078
}
8179

8280
template <class _InputIterator, class _Tp>

libcxx/include/__algorithm/fill_n.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ template <class _OutputIterator, class _Size, class _Tp>
3131
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
3232
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);
3333

34-
template <bool _FillVal, class _Cp>
34+
template <class _Cp>
3535
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
36-
__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {
36+
__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool _FillVal) {
3737
using _It = __bit_iterator<_Cp, false>;
3838
using __storage_type = typename _It::__storage_type;
3939

@@ -68,12 +68,8 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {
6868
template <class _Cp, class _Size>
6969
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false>
7070
__fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) {
71-
if (__n > 0) {
72-
if (__value)
73-
std::__fill_n_bool<true>(__first, __n);
74-
else
75-
std::__fill_n_bool<false>(__first, __n);
76-
}
71+
if (__n > 0)
72+
std::__fill_n_bool(__first, __n, __value);
7773
return __first + __n;
7874
}
7975

libcxx/include/__algorithm/find.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
9696
}
9797

9898
// __bit_iterator implementation
99-
template <bool _ToFind, class _Cp, bool _IsConst>
99+
template <class _Cp, bool _IsConst>
100100
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
101-
__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
101+
__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool _ToFind) {
102102
using _It = __bit_iterator<_Cp, _IsConst>;
103103
using __storage_type = typename _It::__storage_type;
104104

@@ -108,7 +108,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
108108
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
109109
__storage_type __dn = std::min(__clz_f, __n);
110110
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
111-
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
111+
__storage_type __b = std::__invert_if(*__first.__seg_, !_ToFind) & __m;
112112
if (__b)
113113
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
114114
if (__n == __dn)
@@ -118,14 +118,14 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
118118
}
119119
// do middle whole words
120120
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
121-
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
121+
__storage_type __b = std::__invert_if(*__first.__seg_, !_ToFind);
122122
if (__b)
123123
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
124124
}
125125
// do last partial word
126126
if (__n > 0) {
127127
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
128-
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
128+
__storage_type __b = std::__invert_if(*__first.__seg_, !_ToFind) & __m;
129129
if (__b)
130130
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
131131
}
@@ -135,9 +135,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
135135
template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
136136
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
137137
__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
138-
if (static_cast<bool>(__value))
139-
return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
140-
return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
138+
return std::__find_bool(__first, static_cast<typename _Cp::size_type>(__last - __first), static_cast<bool>(__value));
141139
}
142140

143141
// segmented iterator implementation

libcxx/include/__bit/invert_if.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
_LIBCPP_BEGIN_NAMESPACE_STD
2020

21-
template <bool _Invert, class _Tp>
22-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v) {
21+
template <class _Tp>
22+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v, bool _Invert) {
2323
if (_Invert)
2424
return ~__v;
2525
return __v;

libcxx/include/__bit_reference

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,9 @@ private:
966966
template <class _Dp>
967967
friend struct __bit_array;
968968

969-
template <bool _FillVal, class _Dp>
969+
template <class _Dp>
970970
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
971-
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
971+
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n, bool _FillVal);
972972

973973
template <class _Dp, bool _IC>
974974
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned(
@@ -1009,12 +1009,12 @@ private:
10091009
template <class _Dp, bool _IC1, bool _IC2>
10101010
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
10111011
equal(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>);
1012-
template <bool _ToFind, class _Dp, bool _IC>
1012+
template <class _Dp, bool _IC>
10131013
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC>
1014-
__find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
1015-
template <bool _ToCount, class _Dp, bool _IC>
1016-
friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI
1017-
_LIBCPP_CONSTEXPR_SINCE_CXX20 __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
1014+
__find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool _ToFind);
1015+
template <class _Dp, bool _IC>
1016+
friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1017+
__count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool _ToCount);
10181018
};
10191019

10201020
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__cxx03/__algorithm/count.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
4141
}
4242

4343
// __bit_iterator implementation
44-
template <bool _ToCount, class _Cp, bool _IsConst>
44+
template <class _Cp, bool _IsConst>
4545
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type
46-
__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
46+
__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool _ToCount) {
4747
using _It = __bit_iterator<_Cp, _IsConst>;
4848
using __storage_type = typename _It::__storage_type;
4949
using difference_type = typename _It::difference_type;
@@ -55,27 +55,25 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
5555
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
5656
__storage_type __dn = std::min(__clz_f, __n);
5757
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
58-
__r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
58+
__r = std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !_ToCount) & __m);
5959
__n -= __dn;
6060
++__first.__seg_;
6161
}
6262
// do middle whole words
6363
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
64-
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
64+
__r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !_ToCount));
6565
// do last partial word
6666
if (__n > 0) {
6767
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
68-
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
68+
__r += std::__libcpp_popcount(std::__invert_if(*__first.__seg_, !_ToCount) & __m);
6969
}
7070
return __r;
7171
}
7272

7373
template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
7474
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >
7575
__count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
76-
if (__value)
77-
return std::__count_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
78-
return std::__count_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
76+
return std::__count_bool(__first, static_cast<typename _Cp::size_type>(__last - __first), static_cast<bool>(__value));
7977
}
8078

8179
template <class _InputIterator, class _Tp>

libcxx/include/__cxx03/__algorithm/fill_n.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ template <class _OutputIterator, class _Size, class _Tp>
3131
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
3232
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);
3333

34-
template <bool _FillVal, class _Cp>
34+
template <class _Cp>
3535
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
36-
__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {
36+
__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool _FillVal) {
3737
using _It = __bit_iterator<_Cp, false>;
3838
using __storage_type = typename _It::__storage_type;
3939

@@ -68,12 +68,8 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {
6868
template <class _Cp, class _Size>
6969
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false>
7070
__fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) {
71-
if (__n > 0) {
72-
if (__value)
73-
std::__fill_n_bool<true>(__first, __n);
74-
else
75-
std::__fill_n_bool<false>(__first, __n);
76-
}
71+
if (__n > 0)
72+
std::__fill_n_bool(__first, __n, __value);
7773
return __first + __n;
7874
}
7975

libcxx/include/__cxx03/__algorithm/find.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
9494
}
9595

9696
// __bit_iterator implementation
97-
template <bool _ToFind, class _Cp, bool _IsConst>
97+
template <class _Cp, bool _IsConst>
9898
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
99-
__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
99+
__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n, bool _ToFind) {
100100
using _It = __bit_iterator<_Cp, _IsConst>;
101101
using __storage_type = typename _It::__storage_type;
102102

@@ -106,7 +106,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
106106
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
107107
__storage_type __dn = std::min(__clz_f, __n);
108108
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
109-
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
109+
__storage_type __b = std::__invert_if(*__first.__seg_, !_ToFind) & __m;
110110
if (__b)
111111
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
112112
if (__n == __dn)
@@ -116,14 +116,14 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
116116
}
117117
// do middle whole words
118118
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
119-
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
119+
__storage_type __b = std::__invert_if(*__first.__seg_, !_ToFind);
120120
if (__b)
121121
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
122122
}
123123
// do last partial word
124124
if (__n > 0) {
125125
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
126-
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
126+
__storage_type __b = std::__invert_if(*__first.__seg_, !_ToFind) & __m;
127127
if (__b)
128128
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
129129
}
@@ -133,9 +133,7 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
133133
template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
134134
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
135135
__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
136-
if (static_cast<bool>(__value))
137-
return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
138-
return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
136+
return std::__find_bool(__first, static_cast<typename _Cp::size_type>(__last - __first), static_cast<bool>(__value));
139137
}
140138

141139
// segmented iterator implementation

libcxx/include/__cxx03/__bit/invert_if.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
_LIBCPP_BEGIN_NAMESPACE_STD
2020

21-
template <bool _Invert, class _Tp>
22-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v) {
21+
template <class _Tp>
22+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __invert_if(_Tp __v, bool _Invert) {
2323
if (_Invert)
2424
return ~__v;
2525
return __v;

libcxx/include/__cxx03/__bit_reference

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,9 @@ private:
966966
template <class _Dp>
967967
friend struct __bit_array;
968968

969-
template <bool _FillVal, class _Dp>
969+
template <class _Dp>
970970
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
971-
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
971+
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n, bool _FillVal);
972972

973973
template <class _Dp, bool _IC>
974974
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned(
@@ -1009,9 +1009,9 @@ private:
10091009
template <class _Dp, bool _IC1, bool _IC2>
10101010
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
10111011
equal(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>);
1012-
template <bool _ToFind, class _Dp, bool _IC>
1012+
template <class _Dp, bool _IC>
10131013
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC>
1014-
__find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
1014+
__find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type, bool _ToFind);
10151015
template <bool _ToCount, class _Dp, bool _IC>
10161016
friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI
10171017
_LIBCPP_CONSTEXPR_SINCE_CXX20 __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);

0 commit comments

Comments
 (0)