@@ -136,6 +136,8 @@ template <size_t N> struct hash<std::bitset<N>>;
136136# include < __algorithm/fill_n.h>
137137# include < __algorithm/find.h>
138138# include < __assert>
139+ # include < __bit/countr.h>
140+ # include < __bit/invert_if.h>
139141# include < __bit_reference>
140142# include < __config>
141143# include < __cstddef/ptrdiff_t.h>
@@ -223,6 +225,10 @@ protected:
223225 return to_ullong (integral_constant < bool , _Size< sizeof (unsigned long long ) * CHAR_BIT>());
224226 }
225227
228+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
229+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
230+ __to_string (_CharT __zero, _CharT __one) const ;
231+
226232 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
227233 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
228234 _LIBCPP_HIDE_FROM_ABI size_t __hash_code () const _NOEXCEPT;
@@ -389,6 +395,22 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
389395 return __r;
390396}
391397
398+ template <size_t _N_words, size_t _Size>
399+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
400+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
401+ __bitset<_N_words, _Size>::__to_string(_CharT __zero, _CharT __one) const {
402+ basic_string<_CharT, _Traits, _Allocator> __r (_Size, __zero);
403+ for (size_t __i = 0 , __bits = 0 ; __i < _N_words; ++__i, __bits += __bits_per_word) {
404+ __storage_type __word = std::__invert_if<!_Spare>(__first_[__i]);
405+ if (__i == _N_words - 1 && _Size - __bits < __bits_per_word)
406+ __word &= (__storage_type (1 ) << (_Size - __bits)) - 1 ;
407+ for (; __word; __word &= (__word - 1 ))
408+ __r[_Size - 1 - (__bits + std::__countr_zero (__word))] = __one;
409+ }
410+
411+ return __r;
412+ }
413+
392414template <size_t _N_words, size_t _Size>
393415_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {
394416 // do middle whole words
@@ -480,6 +502,10 @@ protected:
480502 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const ;
481503 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const ;
482504
505+ template <bool _Sparse, class _CharT , class _Traits , class _Allocator >
506+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
507+ __to_string (_CharT __zero, _CharT __one) const ;
508+
483509 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
484510 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
485511
@@ -529,6 +555,21 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __
529555 return __first_;
530556}
531557
558+ template <size_t _Size>
559+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
560+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
561+ __bitset<1 , _Size>::__to_string(_CharT __zero, _CharT __one) const {
562+ basic_string<_CharT, _Traits, _Allocator> __r (_Size, __zero);
563+ __storage_type __word = std::__invert_if<!_Spare>(__first_);
564+ if (_Size < __bits_per_word)
565+ __word &= (__storage_type (1 ) << _Size) - 1 ;
566+ for (; __word; __word &= (__word - 1 )) {
567+ size_t __pos = std::__countr_zero (__word);
568+ __r[_Size - 1 - __pos] = __one;
569+ }
570+ return __r;
571+ }
572+
532573template <size_t _Size>
533574inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1 , _Size>::all() const _NOEXCEPT {
534575 __storage_type __m = ~__storage_type (0 ) >> (__bits_per_word - _Size);
@@ -593,6 +634,12 @@ protected:
593634 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const { return 0 ; }
594635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const { return 0 ; }
595636
637+ template <bool _Spare, class _CharT , class _Traits , class _Allocator >
638+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
639+ __to_string (_CharT, _CharT) const {
640+ return basic_string<_CharT, _Traits, _Allocator>();
641+ }
642+
596643 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT { return true ; }
597644 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT { return false ; }
598645
@@ -848,12 +895,11 @@ template <size_t _Size>
848895template <class _CharT , class _Traits , class _Allocator >
849896_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
850897bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
851- basic_string<_CharT, _Traits, _Allocator> __r (_Size, __zero);
852- for (size_t __i = 0 ; __i != _Size; ++__i) {
853- if ((*this )[__i])
854- __r[_Size - 1 - __i] = __one;
855- }
856- return __r;
898+ bool __sparse = size_t (std::count (__base::__make_iter (0 ), __base::__make_iter (_Size), true )) < _Size / 2 ;
899+ if (__sparse)
900+ return __base::template __to_string<true , _CharT, _Traits, _Allocator>(__zero, __one);
901+ else
902+ return __base::template __to_string<false , _CharT, _Traits, _Allocator>(__one, __zero);
857903}
858904
859905template <size_t _Size>
0 commit comments