@@ -147,8 +147,10 @@ template <size_t N> struct hash<std::bitset<N>>;
147
147
# include < __functional/hash.h>
148
148
# include < __functional/identity.h>
149
149
# include < __functional/unary_function.h>
150
+ # include < __type_traits/enable_if.h>
150
151
# include < __type_traits/integral_constant.h>
151
152
# include < __type_traits/is_char_like_type.h>
153
+ # include < __utility/integer_sequence.h>
152
154
# include < climits>
153
155
# include < stdexcept>
154
156
# include < string_view>
@@ -223,10 +225,10 @@ protected:
223
225
224
226
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip () _NOEXCEPT;
225
227
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong () const {
226
- return __to_ulong (_BoolConstant<_Size < sizeof (unsigned long ) * CHAR_BIT>());
228
+ return __to_ulong (_BoolConstant<_Size <= sizeof (unsigned long ) * CHAR_BIT>());
227
229
}
228
230
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong () const {
229
- return __to_ullong (_BoolConstant<_Size < sizeof (unsigned long long ) * CHAR_BIT>());
231
+ return __to_ullong (_BoolConstant<_Size <= sizeof (unsigned long long ) * CHAR_BIT>());
230
232
}
231
233
232
234
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all () const _NOEXCEPT { return !__scan_bits (__bit_not ()); }
@@ -287,6 +289,11 @@ private:
287
289
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong (true_type) const ;
288
290
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong (true_type, false_type) const ;
289
291
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong (true_type, true_type) const ;
292
+ # if _LIBCPP_STD_VER >= 14
293
+ template <size_t ... _Indices>
294
+ _LIBCPP_HIDE_FROM_ABI constexpr __bitset (unsigned long long __v, std::index_sequence<_Indices...>) _NOEXCEPT
295
+ : __first_{static_cast <__storage_type>(__v >> (_Indices * __bits_per_word))...} {}
296
+ # endif
290
297
};
291
298
292
299
template <size_t _N_words, size_t _Size>
@@ -318,35 +325,47 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
318
325
319
326
# endif // _LIBCPP_CXX03_LANG
320
327
328
+ # ifdef _LIBCPP_CXX03_LANG
329
+ template <size_t _N_words, size_t _Size>
330
+ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT {
331
+ __init (__v, _BoolConstant<sizeof (unsigned long long ) == sizeof (__storage_type)>());
332
+ }
333
+ # elif _LIBCPP_STD_VER >= 14
321
334
template <size_t _N_words, size_t _Size>
322
335
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
323
- # ifndef _LIBCPP_CXX03_LANG
336
+ : __bitset{__v,
337
+ std::make_index_sequence<
338
+ std::min<size_t >(_N_words, (sizeof (unsigned long long ) - 1 ) / sizeof (__storage_type) + 1 )>{}} {}
339
+ # else
324
340
# if __SIZEOF_LONG_LONG__ <= __SIZEOF_SIZE_T__
325
- : __first_{static_cast <__storage_type>(__v)}
326
- # elif __SIZEOF_LONG_LONG__ <= 2 * __SIZEOF_SIZE_T__
327
- : __first_{static_cast <__storage_type>(__v), static_cast <__storage_type>(__v >> __bits_per_word)}
328
- # elif __SIZEOF_LONG_LONG__ <= 4 * __SIZEOF_SIZE_T__
329
- # if _N_words == 2
330
- : __first_{static_cast <__storage_type>(__v), static_cast <__storage_type>(__v >> __bits_per_word)}
331
- # elif _N_words == 3
341
+ template <size_t _N_words, size_t _Size>
342
+ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
343
+ : __first_{static_cast <__storage_type>(__v)} {}
344
+ # elif __SIZEOF_LONG_LONG__ == 2 * __SIZEOF_SIZE_T__
345
+ template <size_t _N_words, size_t _Size>
346
+ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
347
+ : __first_{static_cast <__storage_type>(__v), static_cast <__storage_type>(__v >> __bits_per_word)} {}
348
+ # elif __SIZEOF_LONG_LONG__ == 4 * __SIZEOF_SIZE_T__
349
+ template <size_t _N_words, size_t _Size, __enable_if_t <_N_words == 2 , int > = 0 >
350
+ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
351
+ : __first_{static_cast <__storage_type>(__v), static_cast <__storage_type>(__v >> __bits_per_word)} {}
352
+
353
+ template <size_t _N_words, size_t _Size, __enable_if_t <_N_words == 3 , int > = 0 >
354
+ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
332
355
: __first_{static_cast <__storage_type>(__v),
333
356
static_cast <__storage_type>(__v >> __bits_per_word),
334
- static_cast <__storage_type>(__v >> (__bits_per_word * 2 ))}
335
- # else
357
+ static_cast <__storage_type>(__v >> (__bits_per_word * 2 ))} {}
358
+
359
+ template <size_t _N_words, size_t _Size, __enable_if_t <_N_words >= 4 , int > = 0 >
360
+ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
336
361
: __first_{static_cast <__storage_type>(__v),
337
362
static_cast <__storage_type>(__v >> __bits_per_word),
338
363
static_cast <__storage_type>(__v >> (__bits_per_word * 2 )),
339
- static_cast <__storage_type>(__v >> (__bits_per_word * 3 ))}
340
- # endif
364
+ static_cast <__storage_type>(__v >> (__bits_per_word * 3 ))} {}
341
365
# else
342
366
# error This constructor has not been ported to this platform
343
367
# endif
344
- # endif
345
- {
346
- # ifdef _LIBCPP_CXX03_LANG
347
- __init (__v, _BoolConstant<sizeof (unsigned long long ) <= sizeof (__storage_type)>());
348
- # endif
349
- }
368
+ # endif // _LIBCPP_CXX03_LANG
350
369
351
370
template <size_t _N_words, size_t _Size>
352
371
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -407,12 +426,11 @@ __bitset<_N_words, _Size>::__to_ulong(true_type, false_type) const {
407
426
template <size_t _N_words, size_t _Size>
408
427
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
409
428
__bitset<_N_words, _Size>::__to_ulong(true_type, true_type) const {
410
- unsigned long __r = static_cast < unsigned long >(__first_[ 0 ]) ;
411
- _LIBCPP_DIAGNOSTIC_PUSH
412
- _LIBCPP_GCC_DIAGNOSTIC_IGNORED ( " -Wshift-count-overflow " )
413
- for (size_t __i = 1 ; __i < _N_words ; ++__i)
429
+ const size_t __ul_wrods = ( sizeof ( unsigned long ) - 1 ) / sizeof (__storage_type) + 1 ;
430
+ const size_t __n_words = _N_words < __ul_wrods ? _N_words : __ul_wrods;
431
+ unsigned long __r = static_cast < unsigned long >(__first_[ 0 ]);
432
+ for (size_t __i = 1 ; __i < __n_words ; ++__i)
414
433
__r |= static_cast <unsigned long >(__first_[__i]) << (__bits_per_word * __i);
415
- _LIBCPP_DIAGNOSTIC_POP
416
434
return __r;
417
435
}
418
436
@@ -440,14 +458,11 @@ __bitset<_N_words, _Size>::__to_ullong(true_type, false_type) const {
440
458
template <size_t _N_words, size_t _Size>
441
459
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
442
460
__bitset<_N_words, _Size>::__to_ullong(true_type, true_type) const {
443
- unsigned long long __r = static_cast <unsigned long long >(__first_[0 ]);
444
- _LIBCPP_DIAGNOSTIC_PUSH
445
- _LIBCPP_GCC_DIAGNOSTIC_IGNORED (" -Wshift-count-overflow" )
446
461
const size_t __ull_wrods = (sizeof (unsigned long long ) - 1 ) / sizeof (__storage_type) + 1 ;
447
462
const size_t __n_words = _N_words < __ull_wrods ? _N_words : __ull_wrods;
463
+ unsigned long long __r = static_cast <unsigned long long >(__first_[0 ]);
448
464
for (size_t __i = 1 ; __i < __n_words; ++__i)
449
465
__r |= static_cast <unsigned long long >(__first_[__i]) << (__bits_per_word * __i);
450
- _LIBCPP_DIAGNOSTIC_POP
451
466
return __r;
452
467
}
453
468
@@ -534,6 +549,8 @@ protected:
534
549
private:
535
550
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong (false_type) const ;
536
551
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong (true_type) const ;
552
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong (false_type) const ;
553
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong (true_type) const ;
537
554
};
538
555
539
556
template <size_t _Size>
@@ -568,7 +585,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
568
585
569
586
template <size_t _Size>
570
587
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1 , _Size>::to_ulong() const {
571
- return __to_ulong (_BoolConstant < _Size< sizeof (unsigned long ) * CHAR_BIT>());
588
+ return __to_ulong (_BoolConstant< _Size <= sizeof (unsigned long ) * CHAR_BIT>());
572
589
}
573
590
574
591
template <size_t _Size>
@@ -586,6 +603,21 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _S
586
603
587
604
template <size_t _Size>
588
605
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1 , _Size>::to_ullong() const {
606
+ return __to_ullong (_BoolConstant<_Size <= sizeof (unsigned long long ) * CHAR_BIT>());
607
+ }
608
+
609
+ template <size_t _Size>
610
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
611
+ __bitset<1 , _Size>::__to_ullong(false_type) const {
612
+ if (auto __e = __make_iter (_Size); std::find (__make_iter (sizeof (unsigned long long ) * CHAR_BIT), __e, true ) != __e)
613
+ __throw_overflow_error (" __bitset<1, _Size>::__to_ullong overflow error" );
614
+
615
+ return static_cast <unsigned long long >(__first_);
616
+ }
617
+
618
+ template <size_t _Size>
619
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
620
+ __bitset<1 , _Size>::__to_ullong(true_type) const {
589
621
return static_cast <unsigned long long >(__first_);
590
622
}
591
623
0 commit comments