@@ -136,6 +136,8 @@ template <size_t N> struct hash<std::bitset<N>>;
136
136
# include < __algorithm/fill_n.h>
137
137
# include < __algorithm/find.h>
138
138
# include < __assert>
139
+ # include < __bit/countr.h>
140
+ # include < __bit/invert_if.h>
139
141
# include < __bit_reference>
140
142
# include < __config>
141
143
# include < __cstddef/ptrdiff_t.h>
@@ -223,6 +225,10 @@ protected:
223
225
return to_ullong (integral_constant < bool , _Size< sizeof (unsigned long long ) * CHAR_BIT>());
224
226
}
225
227
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
+
226
232
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
227
233
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
228
234
_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 {
389
395
return __r;
390
396
}
391
397
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
+
392
414
template <size_t _N_words, size_t _Size>
393
415
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {
394
416
// do middle whole words
@@ -480,6 +502,10 @@ protected:
480
502
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const ;
481
503
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const ;
482
504
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
+
483
509
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT;
484
510
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT;
485
511
@@ -529,6 +555,21 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __
529
555
return __first_;
530
556
}
531
557
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
+
532
573
template <size_t _Size>
533
574
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1 , _Size>::all() const _NOEXCEPT {
534
575
__storage_type __m = ~__storage_type (0 ) >> (__bits_per_word - _Size);
@@ -593,6 +634,12 @@ protected:
593
634
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const { return 0 ; }
594
635
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const { return 0 ; }
595
636
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
+
596
643
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT { return true ; }
597
644
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any () const _NOEXCEPT { return false ; }
598
645
@@ -848,12 +895,11 @@ template <size_t _Size>
848
895
template <class _CharT , class _Traits , class _Allocator >
849
896
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
850
897
bitset<_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);
857
903
}
858
904
859
905
template <size_t _Size>
0 commit comments