@@ -137,6 +137,8 @@ template <size_t N> struct hash<std::bitset<N>>;
137137# include < __algorithm/fill_n.h>
138138# include < __algorithm/find.h>
139139# include < __assert>
140+ # include < __bit/countr.h>
141+ # include < __bit/invert_if.h>
140142# include < __bit_reference>
141143# include < __config>
142144# include < __cstddef/ptrdiff_t.h>
@@ -224,6 +226,10 @@ protected:
224226 return to_ullong (integral_constant < bool , _Size< sizeof (unsigned long long ) * CHAR_BIT>());
225227 }
226228
229+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
230+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
231+ __to_string (_CharT __zero, _CharT __one) const ;
232+
227233 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
228234 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
229235 _LIBCPP_HIDE_FROM_ABI size_t __hash_code () const _NOEXCEPT;
@@ -388,6 +394,22 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
388394 return __r;
389395}
390396
397+ template <size_t _N_words, size_t _Size>
398+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
399+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
400+ __bitset<_N_words, _Size>::__to_string(_CharT __zero, _CharT __one) const {
401+ basic_string<_CharT, _Traits, _Allocator> __r (_Size, __zero);
402+ for (size_t __i = 0 , __bits = 0 ; __i < _N_words; ++__i, __bits += __bits_per_word) {
403+ __storage_type __word = std::__invert_if<!_Spare>(__first_[__i]);
404+ if (__i == _N_words - 1 && _Size - __bits < __bits_per_word)
405+ __word &= (__storage_type (1 ) << (_Size - __bits)) - 1 ;
406+ for (; __word; __word &= (__word - 1 ))
407+ __r[_Size - 1 - (__bits + std::__countr_zero (__word))] = __one;
408+ }
409+
410+ return __r;
411+ }
412+
391413template <size_t _N_words, size_t _Size>
392414_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {
393415 // do middle whole words
@@ -483,6 +505,10 @@ protected:
483505 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const ;
484506 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const ;
485507
508+ template <bool _Sparse, class _CharT , class _Traits , class _Allocator >
509+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
510+ __to_string (_CharT __zero, _CharT __one) const ;
511+
486512 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
487513 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
488514
@@ -530,6 +556,21 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __
530556 return __first_;
531557}
532558
559+ template <size_t _Size>
560+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
561+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
562+ __bitset<1 , _Size>::__to_string(_CharT __zero, _CharT __one) const {
563+ basic_string<_CharT, _Traits, _Allocator> __r (_Size, __zero);
564+ __storage_type __word = std::__invert_if<!_Spare>(__first_);
565+ if (_Size < __bits_per_word)
566+ __word &= (__storage_type (1 ) << _Size) - 1 ;
567+ for (; __word; __word &= (__word - 1 )) {
568+ size_t __pos = std::__countr_zero (__word);
569+ __r[_Size - 1 - __pos] = __one;
570+ }
571+ return __r;
572+ }
573+
533574template <size_t _Size>
534575inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1 , _Size>::all() const _NOEXCEPT {
535576 __storage_type __m = ~__storage_type (0 ) >> (__bits_per_word - _Size);
@@ -594,6 +635,12 @@ protected:
594635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const { return 0 ; }
595636 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const { return 0 ; }
596637
638+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
639+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
640+ __to_string (_CharT, _CharT) const {
641+ return basic_string<_CharT, _Traits, _Allocator>();
642+ }
643+
597644 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT { return true ; }
598645 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT { return false ; }
599646
@@ -847,12 +894,11 @@ template <size_t _Size>
847894template <class _CharT , class _Traits , class _Allocator >
848895_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
849896bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
850- basic_string<_CharT, _Traits, _Allocator> __r (_Size, __zero);
851- for (size_t __i = 0 ; __i != _Size; ++__i) {
852- if ((*this )[__i])
853- __r[_Size - 1 - __i] = __one;
854- }
855- return __r;
897+ bool __sparse = size_t (std::count (__base::__make_iter (0 ), __base::__make_iter (_Size), true )) < _Size / 2 ;
898+ if (__sparse)
899+ return __base::template __to_string<true , _CharT, _Traits, _Allocator>(__zero, __one);
900+ else
901+ return __base::template __to_string<false , _CharT, _Traits, _Allocator>(__one, __zero);
856902}
857903
858904template <size_t _Size>
0 commit comments