8#ifndef INCLUDED_BSLSTL_BITSET
9#define INCLUDED_BSLSTL_BITSET
179#include <bslscm_version.h>
204#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
208#if defined(BSLS_PLATFORM_CMP_MSVC) || \
209 (defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VERSION < 40400)
217# define BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS
222template <std::
size_t N>
245 unsigned long long val = 0);
260template <std::size_t BITSETSIZE,
261 std::size_t NUM_INIT =
267template <std::
size_t BITSETSIZE>
271 unsigned int d_data[BITSETSIZE];
290template <std::
size_t BITSETSIZE>
294 unsigned int d_data[BITSETSIZE];
324template <std::
size_t N>
326 private Bitset_ImpBase<N ? (N - 1) / (CHAR_BIT *
sizeof(
int)) + 1 : 1> {
330 k_BYTES_PER_INT =
sizeof(int),
331 k_BITS_PER_INT = CHAR_BIT * k_BYTES_PER_INT,
332 k_BITSETSIZE = N ? (N - 1) / k_BITS_PER_INT + 1 : 1
356 unsigned int *d_int_p;
357 unsigned int d_offset;
365 reference(
unsigned int *i,
unsigned int offset);
368#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
409 void clearUnusedBits();
424 template <class CHAR_TYPE, class TRAITS, class ALLOCATOR>
430 ALLOCATOR>::size_type pos,
433 ALLOCATOR>::size_type n,
436 template <class CHAR_TYPE, class TRAITS, class ALLOCATOR>
439 typename
bsl::
basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type pos,
447 std::
size_t numOneSet(
unsigned int src) const;
470#if !defined(BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS)
471 template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
473 const std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>& str,
475 std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type pos = 0,
477 std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type n =
478 std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::npos,
479 CHAR_TYPE zeroChar = CHAR_TYPE(
'0'),
480 CHAR_TYPE oneChar = CHAR_TYPE(
'1'));
482 template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
484 bitset(
const std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>& str,
487 CHAR_TYPE zeroChar = CHAR_TYPE(
'0'),
488 CHAR_TYPE oneChar = CHAR_TYPE(
'1'));
499#if !defined(BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS)
500 template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
507 CHAR_TYPE zeroChar = CHAR_TYPE(
'0'),
508 CHAR_TYPE oneChar = CHAR_TYPE(
'1'));
510 template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
514 CHAR_TYPE zeroChar = CHAR_TYPE(
'0'),
515 CHAR_TYPE oneChar = CHAR_TYPE(
'1'));
631 bool test(
size_t pos) const;
633#if __cplusplus >= 201103L
639 template <
class CHAR_TYPE = char,
640 class TRAITS = char_traits<CHAR_TYPE>,
643 template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
646 CHAR_TYPE zero = CHAR_TYPE(
'0'),
647 CHAR_TYPE one = CHAR_TYPE(
'1'))
const;
652 unsigned long to_ulong()
const;
659template <std::
size_t N>
665template <std::
size_t N>
671template <std::
size_t N>
675template <
class CHAR_TYPE,
class TRAITS, std::
size_t N>
676std::basic_istream<CHAR_TYPE, TRAITS>&
679template <
class CHAR_TYPE,
class TRAITS, std::
size_t N>
680std::basic_ostream<CHAR_TYPE, TRAITS>&
692template <std::
size_t BITSETSIZE>
699template <std::
size_t BITSETSIZE>
702#if defined(BSLS_COMPILERFEATURES_SUPPORT_GENERALIZED_INITIALIZERS) \
703 &&!(defined(BSLS_PLATFORM_CMP_MSVC) && BSLS_PLATFORM_CMP_VERSION < 1900)
704 : d_data{
static_cast<unsigned int>(val)}
717template <std::
size_t BITSETSIZE>
724template <std::
size_t BITSETSIZE>
727#if defined(BSLS_COMPILERFEATURES_SUPPORT_GENERALIZED_INITIALIZERS) \
728 &&!(defined(BSLS_PLATFORM_CMP_MSVC) && BSLS_PLATFORM_CMP_VERSION < 1900)
729 : d_data{
static_cast<unsigned int>(val),
730 static_cast<unsigned int>(val >> (
sizeof(
int) * CHAR_BIT))}
744template <std::
size_t N>
746bitset<N>::reference::reference(
unsigned int *i,
unsigned int offset)
754template <std::
size_t N>
756typename bitset<N>::reference&
760 *d_int_p |= (1 << d_offset);
763 *d_int_p &= ~(1 << d_offset);
768template <std::
size_t N>
774 *d_int_p |= (1 << d_offset);
777 *d_int_p &= ~(1 << d_offset);
782template <std::
size_t N>
787 *d_int_p ^= (1 << d_offset);
792template <std::
size_t N>
796 return ((*d_int_p & (1 << d_offset)) != 0);
799template <std::
size_t N>
803 return ((*d_int_p & (1 << d_offset)) == 0);
811template <std::
size_t N>
815 enum { k_VALUE = N % k_BITS_PER_INT ? 1 : 0 };
820template <std::
size_t N>
826template <std::
size_t N>
830 const unsigned int offset = N % k_BITS_PER_INT;
832 d_data[k_BITSETSIZE - 1] &= ~(~((
unsigned int)0) << offset);
835template <std::
size_t N>
836std::size_t bitset<N>::numOneSet(
unsigned int src)
const
839 unsigned input = src;
844 input -= (input >> 1) & 0x55555555;
849 const int mask = 0x33333333;
850 input = ((input >> 2) & mask) + (input & mask);
857 input = ((input >> 4) + input) & 0x0f0f0f0f;
864 input = (input >> 8) + input;
865 input = (input >> 16) + input;
867 return input & 0x000000ff;
870template <std::
size_t N>
871template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
872void bitset<N>::copyString(
873const std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>& str,
874typename std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type pos,
875typename std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type n,
879 typedef typename std::basic_string<CHAR_TYPE,
881 ALLOCATOR>::size_type size_type;
882 n = std::min(N, std::min(n, str.size() - pos));
883 for (size_type i = 0; i < n; ++i) {
884 typename TRAITS::int_type bit = TRAITS::to_int_type(
885 str[pos + n - i - 1]);
887 if (bit == oneChar) {
892 BloombergLP::bslstl::StdExceptUtil::throwInvalidArgument(
893 "string for bitset constructor "
894 "must be '0' or '1'");
899template <std::
size_t N>
900template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
901void bitset<N>::copyString(
910 ALLOCATOR>::size_type size_type;
911 n = std::min(N, std::min(n, str.
size() - pos));
912 for (size_type i = 0; i < n; ++i) {
913 typename TRAITS::int_type bit = TRAITS::to_int_type(
914 str[pos + n - i - 1]);
916 if (bit == oneChar) {
921 BloombergLP::bslstl::StdExceptUtil::throwInvalidArgument(
922 "string for bitset constructor "
923 "must be '0' or '1'");
929template <std::
size_t N>
935template <std::
size_t N>
941#if !defined(BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS)
942template <std::
size_t N>
943template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
946bitset(
const std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>& str,
947 typename std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type pos,
948 typename std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>::size_type n,
952template <std::
size_t N>
953template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
956bitset(
const std::basic_string<CHAR_TYPE, TRAITS, ALLOCATOR>& str,
965 BloombergLP::bslstl::StdExceptUtil::throwOutOfRange(
966 "'pos > str.size()' for bitset constructor");
968 memset(d_data, 0, k_BITSETSIZE * k_BYTES_PER_INT);
969 copyString(str, pos, n, zeroChar, oneChar);
973#if !defined(BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS)
974template <std::
size_t N>
975template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
984template <std::
size_t N>
985template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
997 BloombergLP::bslstl::StdExceptUtil::throwOutOfRange(
998 "'pos > str.size()' for bitset constructor");
1000 memset(d_data, 0, k_BITSETSIZE * k_BYTES_PER_INT);
1001 copyString(str, pos, n, zeroChar, oneChar);
1005template <std::
size_t N>
1008 for (std::size_t i = 0; i < k_BITSETSIZE; ++i) {
1009 d_data[i] &= rhs.d_data[i];
1014template <std::
size_t N>
1017 for (std::size_t i = 0; i < k_BITSETSIZE; ++i) {
1018 d_data[i] |= rhs.d_data[i];
1023template <std::
size_t N>
1026 for (std::size_t i = 0; i < k_BITSETSIZE; ++i) {
1027 d_data[i] ^= rhs.d_data[i];
1032template <std::
size_t N>
1038 const std::size_t shift = pos / k_BITS_PER_INT;
1039 const std::size_t offset = pos % k_BITS_PER_INT;
1042 memmove(d_data + shift,
1044 (k_BITSETSIZE - shift) * k_BYTES_PER_INT);
1045 memset(d_data, 0, shift * k_BYTES_PER_INT);
1049 for (std::size_t i = k_BITSETSIZE - 1; i > shift; --i) {
1050 d_data[i] = (d_data[i] << offset)
1051 | (d_data[i-1] >> (k_BITS_PER_INT - offset));
1053 d_data[shift] <<= offset;
1061template <std::
size_t N>
1067 const std::size_t shift = pos / k_BITS_PER_INT;
1068 const std::size_t offset = pos % k_BITS_PER_INT;
1073 (k_BITSETSIZE - shift) * k_BYTES_PER_INT);
1074 memset(d_data + k_BITSETSIZE - shift, 0, shift * k_BYTES_PER_INT);
1078 for (std::size_t i = 0; i < k_BITSETSIZE - shift - 1; ++i) {
1079 d_data[i] = (d_data[i] >> offset)
1080 | (d_data[i+1] << (k_BITS_PER_INT - offset));
1082 d_data[k_BITSETSIZE - shift - 1] >>= offset;
1090template <std::
size_t N>
1093 for (std::size_t i = 0; i < k_BITSETSIZE; ++i) {
1094 d_data[i] = ~d_data[i];
1100template <std::
size_t N>
1106 const std::size_t shift = pos / k_BITS_PER_INT;
1107 const std::size_t offset = pos % k_BITS_PER_INT;
1108 d_data[shift] ^= (1 << offset);
1112template <std::
size_t N>
1116 memset(d_data, 0, k_BITSETSIZE * k_BYTES_PER_INT);
1120template <std::
size_t N>
1126 const std::size_t shift = pos / k_BITS_PER_INT;
1127 const std::size_t offset = pos % k_BITS_PER_INT;
1128 d_data[shift] &= ~(1 << offset);
1132template <std::
size_t N>
1136 memset(d_data, 0xFF, k_BITSETSIZE * k_BYTES_PER_INT);
1141template <std::
size_t N>
1146 const std::size_t shift = pos / k_BITS_PER_INT;
1147 const std::size_t offset = pos % k_BITS_PER_INT;
1149 d_data[shift] |= (1 << offset);
1152 d_data[shift] &= ~(1 << offset);
1157template <std::
size_t N>
1163 const std::size_t shift = pos / k_BITS_PER_INT;
1164 const std::size_t offset = pos % k_BITS_PER_INT;
1166 static_cast<unsigned int>(offset));
1170template <std::
size_t N>
1180template <std::
size_t N>
1190template <std::
size_t N>
1198template <std::
size_t N>
1202#if defined(BSLSTL_BITSET_ALLOW_ASSERT_IN_CONSTEXPR)
1206 return 0 != (d_data[pos / k_BITS_PER_INT] & (1 << (pos % k_BITS_PER_INT)));
1209template <std::
size_t N>
1213 return memcmp(d_data, rhs.d_data, k_BITSETSIZE * k_BYTES_PER_INT) == 0;
1216template <std::
size_t N>
1223template <std::
size_t N>
1226 for (std::size_t i = 0; i < N / k_BITS_PER_INT; ++i) {
1227 if (d_data[i] != ~0u) {
1232 const std::size_t modulo = N % k_BITS_PER_INT;
1235 const std::size_t mask = ((1u << modulo) - 1);
1236 return d_data[k_BITSETSIZE - 1] == mask;
1242template <std::
size_t N>
1245 for (std::size_t i = 0; i < k_BITSETSIZE; ++i) {
1246 if (d_data[i] != 0) {
1253template <std::
size_t N>
1256 std::size_t sum = 0;
1257 for (std::size_t i = 0; i < k_BITSETSIZE; ++i) {
1258 sum += numOneSet(d_data[i]);
1263template <std::
size_t N>
1270template <std::
size_t N>
1277template <std::
size_t N>
1283 BloombergLP::bslstl::StdExceptUtil::throwOutOfRange(
1284 "out_of_range in bsl::bitset<>::test");
1289template <std::
size_t N>
1290template <
class CHAR_TYPE,
class TRAITS,
class ALLOCATOR>
1293 CHAR_TYPE one)
const
1296 for (std::size_t i = 0; i < N; ++i) {
1297 if (this->
operator[](i)) {
1298 str[N - i - 1] = one;
1304template <std::
size_t N>
1308 k_INTS_IN_LONG =
sizeof(
unsigned long) /
sizeof(
int)
1311 for (std::size_t i = k_INTS_IN_LONG; i < k_BITSETSIZE; ++i) {
1314 BloombergLP::bslstl::StdExceptUtil::throwOverflowError(
1315 "overflow in bsl::bitset<>::to_ulong");
1319 unsigned long value = 0;
1320 const unsigned int numInts = (
unsigned int) k_INTS_IN_LONG
1321 < (
unsigned int) k_BITSETSIZE
1322 ? (
unsigned int) k_INTS_IN_LONG
1323 : (
unsigned int) k_BITSETSIZE;
1325 for (
unsigned int i = 0; i < numInts; ++i) {
1326 value |= (
unsigned long) d_data[i] << (k_BITS_PER_INT * i);
1332template <std::
size_t N>
1340template <std::
size_t N>
1348template <std::
size_t N>
1356template <
class CHAR_TYPE,
class TRAITS, std::
size_t N>
1357std::basic_istream<CHAR_TYPE, TRAITS>&
1360 typedef typename TRAITS::int_type int_type;
1365 typename std::basic_istream<CHAR_TYPE, TRAITS>::sentry sen(is);
1367 std::basic_streambuf<CHAR_TYPE, TRAITS> *buffer = is.rdbuf();
1368 for (std::size_t i = 0; i < N; ++i) {
1369 static int_type eof = TRAITS::eof();
1370 int_type cint = buffer->sbumpc();
1371 if (TRAITS::eq_int_type(cint, eof)) {
1372 is.setstate(std::ios_base::eofbit);
1376 CHAR_TYPE cchar = TRAITS::to_char_type(cint);
1377 char c = is.narrow(cchar,
'*');
1379 if (c ==
'0' || c ==
'1') {
1382 else if (TRAITS::eq_int_type(buffer->sputbackc(cchar), eof)) {
1383 is.setstate(std::ios_base::failbit);
1390 is.setstate(std::ios_base::failbit);
1399template <
class CHAR_TYPE,
class TRAITS, std::
size_t N>
1401std::basic_ostream<CHAR_TYPE, TRAITS>&
1402operator<<(std::basic_ostream<CHAR_TYPE, TRAITS>& os,
const bitset<N>& x)
1404 basic_string<CHAR_TYPE, TRAITS, allocator<CHAR_TYPE> > tmp (
1405 x.template
to_string<CHAR_TYPE, TRAITS, allocator<CHAR_TYPE> >());
1411#if defined(BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS)
1412# undef BSLSTL_BITSET_MSVC_CANNOT_PARSE_DEFAULTS_WITH_COLONS
Definition bslstl_bitset.h:265
Definition bslma_bslallocator.h:580
Definition bslstl_string.h:1281
void reserve(size_type newCapacity=0)
Definition bslstl_string.h:5407
size_type size() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6592
AllocatorTraits::size_type size_type
Definition bslstl_string.h:1303
void push_back(CHAR_TYPE character)
Append the specified character to this string.
Definition bslstl_string.h:5699
static const size_type npos
Definition bslstl_string.h:1676
bool empty() const BSLS_KEYWORD_NOEXCEPT
Return true if this string has length 0, and false otherwise.
Definition bslstl_string.h:6631
Definition bslstl_bitset.h:350
reference & flip() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:785
bool operator~() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:801
reference & operator=(bool x) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:757
Definition bslstl_bitset.h:326
bitset operator>>(std::size_t pos) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1182
basic_string< CHAR_TYPE, TRAITS, ALLOCATOR > to_string(CHAR_TYPE zero=CHAR_TYPE('0'), CHAR_TYPE one=CHAR_TYPE('1')) const
Definition bslstl_bitset.h:1291
bool all() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1224
bool operator==(const bitset &rhs) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1211
bitset & set() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1134
bitset & operator^=(const bitset &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1024
bitset & flip() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1091
bitset & operator>>=(std::size_t pos) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1062
bool test(size_t pos) const
Definition bslstl_bitset.h:1279
unsigned long to_ulong() const
Definition bslstl_bitset.h:1305
BSLS_KEYWORD_CONSTEXPR bitset() BSLS_KEYWORD_NOEXCEPT
Create a bitset with all bits initialized to 0.
Definition bslstl_bitset.h:931
bool any() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1243
bitset & operator&=(const bitset &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1006
bitset operator~() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1192
bitset operator<<(std::size_t pos) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1172
reference operator[](std::size_t pos)
Definition bslstl_bitset.h:1159
bitset & operator|=(const bitset &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1015
std::size_t count() const BSLS_KEYWORD_NOEXCEPT
Return the number of bits in this bitset that have the value of 1.
Definition bslstl_bitset.h:1254
BSLS_KEYWORD_CONSTEXPR std::size_t size() const BSLS_KEYWORD_NOEXCEPT
Return the number of bits this bitset holds.
Definition bslstl_bitset.h:1272
bitset & operator<<=(std::size_t pos) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1033
bool operator!=(const bitset &rhs) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1218
bool none() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1265
bitset & reset() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1114
Definition bslstl_set.h:657
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:588
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:632
Definition bdlb_printmethods.h:283
bitset< N > operator|(const bitset< N > &lhs, const bitset< N > &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1341
bitset< N > operator^(const bitset< N > &lhs, const bitset< N > &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1349
std::basic_istream< CHAR_TYPE, TRAITS > & operator>>(std::basic_istream< CHAR_TYPE, TRAITS > &is, bitset< N > &x)
Definition bslstl_bitset.h:1358
std::basic_ostream< CHAR_TYPE, TRAITS > & operator<<(std::basic_ostream< CHAR_TYPE, TRAITS > &os, const bitset< N > &x)
Definition bslstl_bitset.h:1402
BSLS_KEYWORD_CONSTEXPR size_t size(const TYPE(&)[DIMENSION]) BSLS_KEYWORD_NOEXCEPT
Return the dimension of the specified array argument.
Definition bslstl_iterator.h:1331
string to_string(int value)
bitset< N > operator&(const bitset< N > &lhs, const bitset< N > &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslstl_bitset.h:1333
BSLS_KEYWORD_CONSTEXPR CONTAINER::value_type * data(CONTAINER &container)
Definition bslstl_iterator.h:1231
Definition bdldfp_decimal.h:5188
Definition bslstl_bitset.h:228
static void defaultInit(unsigned int *data, std::size_t size, unsigned long long val=0)
@ k_BITS_PER_INT
Definition bslstl_bitset.h:231
@ k_BYTES_PER_INT
Definition bslstl_bitset.h:230
@ k_INTS_IN_LLONG
Definition bslstl_bitset.h:233
@ k_INTS_IN_LONG
Definition bslstl_bitset.h:232
BSLMF_ASSERT(k_INTS_IN_LLONG==2)
Definition bslmf_integralconstant.h:244