@@ -137,6 +137,8 @@ template <size_t N> struct hash<std::bitset<N>>;
137
137
# include < __algorithm/fill_n.h>
138
138
# include < __algorithm/find.h>
139
139
# include < __assert>
140
+ # include < __bit/countr.h>
141
+ # include < __bit/invert_if.h>
140
142
# include < __bit_reference>
141
143
# include < __config>
142
144
# include < __cstddef/ptrdiff_t.h>
@@ -228,6 +230,21 @@ protected:
228
230
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
229
231
_LIBCPP_HIDE_FROM_ABI size_t __hash_code () const _NOEXCEPT;
230
232
233
+ template <bool _Sparse, class _CharT , class _Traits , class _Allocator >
234
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
235
+ __to_string (_CharT __zero, _CharT __one) const {
236
+ basic_string<_CharT, _Traits, _Allocator> __r (_Size, _Sparse ? __zero : __one);
237
+ for (size_t __i = 0 , __bits = 0 ; __i < _N_words; ++__i, __bits += __bits_per_word) {
238
+ __storage_type __word = std::__invert_if<!_Sparse>(__first_[__i]);
239
+ if (__i == _N_words - 1 && _Size - __bits < __bits_per_word)
240
+ __word &= (__storage_type (1 ) << (_Size - __bits)) - 1 ;
241
+ for (; __word; __word &= (__word - 1 ))
242
+ __r[_Size - 1 - (__bits + std::__countr_zero (__word))] = _Sparse ? __one : __zero;
243
+ }
244
+
245
+ return __r;
246
+ }
247
+
231
248
private:
232
249
# ifdef _LIBCPP_CXX03_LANG
233
250
void __init (unsigned long long __v, false_type) _NOEXCEPT;
@@ -483,6 +500,20 @@ protected:
483
500
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const ;
484
501
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const ;
485
502
503
+ template <bool _Sparse, class _CharT , class _Traits , class _Allocator >
504
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
505
+ __to_string (_CharT __zero, _CharT __one) const {
506
+ basic_string<_CharT, _Traits, _Allocator> __r (_Size, _Sparse ? __zero : __one);
507
+ __storage_type __word = std::__invert_if<!_Sparse>(__first_);
508
+ if (_Size < __bits_per_word)
509
+ __word &= (__storage_type (1 ) << _Size) - 1 ;
510
+ for (; __word; __word &= (__word - 1 )) {
511
+ size_t __pos = std::__countr_zero (__word);
512
+ __r[_Size - 1 - __pos] = _Sparse ? __one : __zero;
513
+ }
514
+ return __r;
515
+ }
516
+
486
517
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
487
518
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
488
519
@@ -594,6 +625,12 @@ protected:
594
625
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const { return 0 ; }
595
626
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const { return 0 ; }
596
627
628
+ template <bool _Sparse, class _CharT , class _Traits , class _Allocator >
629
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
630
+ __to_string (_CharT, _CharT) const {
631
+ return basic_string<_CharT, _Traits, _Allocator>();
632
+ }
633
+
597
634
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT { return true ; }
598
635
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT { return false ; }
599
636
@@ -847,12 +884,11 @@ template <size_t _Size>
847
884
template <class _CharT , class _Traits , class _Allocator >
848
885
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
849
886
bitset<_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;
887
+ bool __sparse = size_t (std::count (__base::__make_iter (0 ), __base::__make_iter (_Size), true )) < _Size / 2 ;
888
+ if (__sparse)
889
+ return __base::template __to_string<true , _CharT, _Traits, _Allocator>(__zero, __one);
890
+ else
891
+ return __base::template __to_string<false , _CharT, _Traits, _Allocator>(__zero, __one);
856
892
}
857
893
858
894
template <size_t _Size>
0 commit comments