Skip to content

Commit 28fb22d

Browse files
committed
Refactor any and all
1 parent 5b6f12d commit 28fb22d

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

libcxx/include/bitset

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ template <size_t N> struct hash<std::bitset<N>>;
142142
# include <__cstddef/ptrdiff_t.h>
143143
# include <__cstddef/size_t.h>
144144
# include <__functional/hash.h>
145+
# include <__functional/identity.h>
145146
# include <__functional/unary_function.h>
146147
# include <__type_traits/is_char_like_type.h>
147148
# include <climits>
@@ -225,14 +226,20 @@ protected:
225226
}
226227

227228
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {
228-
return std::find(__make_iter(0), __make_iter(_Size), false) == __make_iter(_Size);
229+
return __scan_bits<false>(__bit_not());
229230
}
230231
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {
231-
return std::find(__make_iter(0), __make_iter(_Size), true) != __make_iter(_Size);
232+
return __scan_bits<true>(std::__identity());
232233
}
233234
_LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
234235

235236
private:
237+
struct __bit_not {
238+
template <class _Tp>
239+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp operator()(const _Tp& __x) const _NOEXCEPT {
240+
return ~__x;
241+
}
242+
};
236243
# ifdef _LIBCPP_CXX03_LANG
237244
void __init(unsigned long long __v, false_type) _NOEXCEPT;
238245
_LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;
@@ -243,6 +250,23 @@ private:
243250
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type) const;
244251
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const;
245252
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, true_type) const;
253+
254+
template <bool _Early_return, typename _Proj>
255+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool __scan_bits(_Proj __proj) const _NOEXCEPT {
256+
size_t __n = _Size;
257+
__const_storage_pointer __p = __first_;
258+
// do middle whole words
259+
for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
260+
if (__proj(*__p))
261+
return _Early_return;
262+
// do last partial word
263+
if (__n > 0) {
264+
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
265+
if (__proj(*__p) & __m)
266+
return _Early_return;
267+
}
268+
return !_Early_return;
269+
}
246270
};
247271

248272
template <size_t _N_words, size_t _Size>

0 commit comments

Comments
 (0)