8#ifndef INCLUDED_BDLC_BITARRAY
9#define INCLUDED_BDLC_BITARRAY
474#include <bdlscm_version.h>
491#include <bsl_cstddef.h>
492#include <bsl_cstdint.h>
493#include <bsl_climits.h>
494#include <bsl_cstring.h>
495#include <bsl_iosfwd.h>
496#include <bsl_vector.h>
498#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
499#include <bsl_algorithm.h>
534 bsl::size_t d_length;
538 static const bsl::uint64_t s_one = 1;
539 static const bsl::uint64_t s_minusOne = ~static_cast<bsl::uint64_t>(0);
549 static bsl::size_t arraySize(bsl::size_t numBits);
555 bsl::uint64_t *data();
561 const bsl::uint64_t *data()
const;
673 void andEqual(bsl::size_t index,
bool value);
682 bsl::size_t srcIndex,
683 bsl::size_t numBits);
697 void append(
bool value, bsl::size_t numBits);
714 bsl::size_t srcIndex,
715 bsl::size_t numBits);
720 void assign(bsl::size_t index,
bool value);
725 void assign(bsl::size_t index,
bool value, bsl::size_t numBits);
738 void assign(bsl::size_t dstIndex,
740 bsl::size_t srcIndex,
741 bsl::size_t numBits);
745 void assign0(bsl::size_t index);
750 void assign0(bsl::size_t index, bsl::size_t numBits);
754 void assign1(bsl::size_t index);
759 void assign1(bsl::size_t index, bsl::size_t numBits);
775 bsl::uint64_t srcBits,
776 bsl::size_t numBits);
782 void insert(bsl::size_t dstIndex,
bool value);
788 void insert(bsl::size_t dstIndex,
bool value, bsl::size_t numBits);
805 bsl::size_t srcIndex,
806 bsl::size_t numBits);
812 void minusEqual(bsl::size_t index,
bool value);
823 bsl::size_t srcIndex,
824 bsl::size_t numBits);
829 void orEqual(bsl::size_t index,
bool value);
836 void orEqual(bsl::size_t dstIndex,
838 bsl::size_t srcIndex,
839 bsl::size_t numBits);
845 void remove(bsl::size_t index);
852 void remove(bsl::size_t index, bsl::size_t numBits);
884 void setLength(bsl::size_t newLength,
bool value =
false);
889 void swapBits(bsl::size_t index1, bsl::size_t index2);
893 void toggle(bsl::size_t index);
898 void toggle(bsl::size_t index, bsl::size_t numBits);
908 void xorEqual(bsl::size_t index,
bool value);
917 bsl::size_t srcIndex,
918 bsl::size_t numBits);
932 template <
class STREAM>
952 bsl::uint64_t
bits(bsl::size_t index, bsl::size_t numBits)
const;
1003 bsl::size_t
length()
const;
1010 bsl::size_t
num0(bsl::size_t begin = 0,
1018 bsl::size_t
num1(bsl::size_t begin = 0,
1034 template <
class STREAM>
1048 bsl::ostream&
print(bsl::ostream& stream,
1050 int spacesPerLevel = 4)
const;
1052#ifndef BDE_OPENSOURCE_PUBLICATION
1147bsl::size_t BitArray::arraySize(bsl::size_t numBits)
1154 return ret ? ret : 1;
1159bsl::uint64_t *BitArray::data()
1163 return d_array.
data();
1168const bsl::uint64_t *BitArray::data()
const
1172 return d_array.
data();
1190 bsl::memcpy(d_array.
data(),
1192 d_array.
size() *
sizeof(bsl::uint64_t));
1193 d_length = rhs.d_length;
1205 const bsl::size_t rLen = rhs.d_length;
1207 if (rLen > d_length) {
1210 else if (rLen < d_length) {
1211 assign0(rLen, d_length - rLen);
1227 if (d_length < rhs.d_length) {
1242 if (d_length < rhs.d_length) {
1259 if (d_length < rhs.d_length) {
1273 if (d_length > numBits) {
1274 const bsl::size_t remBits = d_length - numBits;
1293 if (d_length > numBits) {
1294 const bsl::size_t remBits = d_length - numBits;
1320 bsl::size_t srcIndex,
1321 bsl::size_t numBits)
1324 BSLS_ASSERT(srcIndex + numBits <= srcArray.d_length);
1348 insert(d_length, value, numBits);
1354 insert(d_length, srcArray, 0, srcArray.d_length);
1359 bsl::size_t srcIndex,
1360 bsl::size_t numBits)
1362 BSLS_ASSERT(srcIndex + numBits <= srcArray.d_length);
1364 insert(d_length, srcArray, srcIndex, numBits);
1386 bsl::size_t srcIndex,
1387 bsl::size_t numBits)
1390 BSLS_ASSERT(srcIndex + numBits <= srcArray.d_length);
1392 if (&srcArray ==
this) {
1469 bsl::uint64_t srcBits,
1470 bsl::size_t numBits)
1483 insert(dstIndex, srcArray, 0, srcArray.d_length);
1521 bsl::size_t srcIndex,
1522 bsl::size_t numBits)
1525 BSLS_ASSERT(srcIndex + numBits <= srcArray.d_length);
1547 bsl::size_t srcIndex,
1548 bsl::size_t numBits)
1551 BSLS_ASSERT(srcIndex + numBits <= srcArray.d_length);
1588 d_array.
reserve(arraySize(numBits));
1597 if (index1 != index2) {
1598 const bool tmp = (*this)[index1];
1599 assign(index1, (*
this)[index2]);
1612 d_array[idx] ^= (s_one << pos);
1644 bsl::size_t srcIndex,
1645 bsl::size_t numBits)
1648 BSLS_ASSERT(srcIndex + numBits <= srcArray.d_length);
1659template <
class STREAM>
1666 stream.getLength(newLength);
1671 if (0 == newLength) {
1677 const bsl::size_t len = arraySize(newLength);
1686 d_length = newLength;
1688 stream.getArrayUint64(
1690 static_cast<int>(len));
1698 const int rem =
static_cast<unsigned>(d_length) %
1701 const bsl::uint64_t mask = (s_one << rem) - 1;
1702 if (d_array.
back() & ~mask) {
1706 d_array.
back() &= mask;
1707 stream.invalidate();
1713 stream.invalidate();
1811 return 0 == d_length;
1852template <
class STREAM>
1859 stream.putLength(
static_cast<int>(d_length));
1860 if (0 != d_length) {
1861 stream.putArrayUint64(
1863 static_cast<int>(d_array.
size()));
1867 stream.invalidate();
1874#ifndef BDE_OPENSOURCE_PUBLICATION
1891 if (lhs.d_length != rhs.d_length) {
1903 return !(lhs == rhs);
1909 BitArray tmp(array);
1951 BitArray tmp(array);
1961 BitArray tmp(array);
1969 return rhs.
print(stream, 0, -1);
Definition bdlc_bitarray.h:521
bsl::size_t num0(bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const
Definition bdlc_bitarray.h:1821
void insert(bsl::size_t dstIndex, bool value)
Definition bdlc_bitarray.h:1487
bsl::size_t length() const
Return the number of bits in this array.
Definition bdlc_bitarray.h:1815
bool operator[](bsl::size_t index) const
Definition bdlc_bitarray.h:1733
BitArray & operator&=(const BitArray &rhs)
Definition bdlc_bitarray.h:1199
bool isAny1() const
Definition bdlc_bitarray.h:1803
void assignAll1()
Set to 1 the value of every bit in this array.
Definition bdlc_bitarray.h:1462
bsl::size_t num1(bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const
Definition bdlc_bitarray.h:1833
bsl::uint64_t bits(bsl::size_t index, bsl::size_t numBits) const
Definition bdlc_bitarray.h:1741
BitArray & operator^=(const BitArray &rhs)
Definition bdlc_bitarray.h:1251
void removeAll()
Definition bdlc_bitarray.h:1578
bsl::size_t find0AtMinIndex(bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const
Definition bdlc_bitarray.h:1761
void assign1(bsl::size_t index)
Definition bdlc_bitarray.h:1429
void andEqual(bsl::size_t index, bool value)
Definition bdlc_bitarray.h:1308
void toggleAll()
Definition bdlc_bitarray.h:1626
BitArray(bsl::size_t initialLength, bslma::Allocator *basicAllocator=0)
void assign(bsl::size_t index, bool value)
Definition bdlc_bitarray.h:1368
void append(bool value)
Definition bdlc_bitarray.h:1334
void rotateRight(bsl::size_t numBits)
void toggle(bsl::size_t index)
Definition bdlc_bitarray.h:1605
BitArray & operator>>=(bsl::size_t numBits)
Definition bdlc_bitarray.h:1268
void swapBits(bsl::size_t index1, bsl::size_t index2)
Definition bdlc_bitarray.h:1592
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlc_bitarray.h:1853
void assignAll0()
Set to 0 the value of every bit in this array.
Definition bdlc_bitarray.h:1456
void swap(BitArray &other)
Definition bdlc_bitarray.h:1721
void assign0(bsl::size_t index)
Definition bdlc_bitarray.h:1413
friend bool operator==(const BitArray &, const BitArray &)
bsl::size_t find1AtMaxIndex(bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const
Definition bdlc_bitarray.h:1773
void orEqual(bsl::size_t index, bool value)
Definition bdlc_bitarray.h:1535
BitArray & operator=(const BitArray &rhs)
Definition bdlc_bitarray.h:1187
BitArray & operator-=(const BitArray &rhs)
Definition bdlc_bitarray.h:1219
void xorEqual(bsl::size_t index, bool value)
Definition bdlc_bitarray.h:1632
BitArray(bsl::size_t initialLength, bool value, bslma::Allocator *basicAllocator=0)
void reserveCapacity(bsl::size_t numBits)
Definition bdlc_bitarray.h:1586
static int maxSupportedBdexVersion()
Definition bdlc_bitarray.h:1878
bsl::size_t find0AtMaxIndex(bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const
Definition bdlc_bitarray.h:1749
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlc_bitarray.h:1847
bool isEmpty() const
Definition bdlc_bitarray.h:1809
BitArray & operator<<=(bsl::size_t numBits)
Definition bdlc_bitarray.h:1288
void setLength(bsl::size_t newLength, bool value=false)
static const bsl::size_t k_INVALID_INDEX
Definition bdlc_bitarray.h:528
bool isAny0() const
Definition bdlc_bitarray.h:1797
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void minusEqual(bsl::size_t index, bool value)
Definition bdlc_bitarray.h:1509
void remove(bsl::size_t index)
Definition bdlc_bitarray.h:1561
~BitArray()
Destroy this object.
bsl::size_t find1AtMinIndex(bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const
Definition bdlc_bitarray.h:1785
void assignBits(bsl::size_t index, bsl::uint64_t srcBits, bsl::size_t numBits)
Definition bdlc_bitarray.h:1468
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlc_bitarray.h:1660
BitArray & operator|=(const BitArray &rhs)
Definition bdlc_bitarray.h:1236
void rotateLeft(bsl::size_t numBits)
BitArray(bslma::Allocator *basicAllocator=0)
void assignAll(bool value)
Set all bits in this array to the specified value.
Definition bdlc_bitarray.h:1445
void insert(bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits)
@ k_BITS_PER_UINT64
Definition bdlc_bitarray.h:525
BitArray(const BitArray &original, bslma::Allocator *basicAllocator=0)
BloombergLP::bslma::Allocator * mechanism() const
Definition bslma_bslallocator.h:1126
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
reference back()
Definition bslstl_vector.h:2577
bool empty() const BSLS_KEYWORD_NOEXCEPT
Return true if this vector has size 0, and false otherwise.
Definition bslstl_vector.h:2679
VALUE_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2587
Definition bslstl_vector.h:1025
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:4019
void reserve(size_type newCapacity)
Definition bslstl_vector.h:3690
void push_back(const VALUE_TYPE &value)
Definition bslstl_vector.h:3760
void swap(vector &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:1712
void resize(size_type newSize)
Definition bslstl_vector.h:3616
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:194
Definition bslma_allocator.h:457
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlc_bitarray.h:503
BitArray operator|(const BitArray &lhs, const BitArray &rhs)
BitArray operator&(const BitArray &lhs, const BitArray &rhs)
bool operator==(const BitArray &lhs, const BitArray &rhs)
BitArray operator>>(const BitArray &array, bsl::size_t numBits)
BitArray operator~(const BitArray &array)
Return the bitwise complement ("toggle") of the specified array.
bool operator!=(const BitArray &lhs, const BitArray &rhs)
BitArray operator^(const BitArray &lhs, const BitArray &rhs)
BitArray operator-(const BitArray &lhs, const BitArray &rhs)
BitArray operator<<(const BitArray &array, bsl::size_t numBits)
Definition balxml_encoderoptions.h:68
Definition bdlbb_blob.h:576
static bsl::size_t find0AtMinIndex(const bsl::uint64_t *bitString, bsl::size_t length)
static void assign(bsl::uint64_t *bitString, bsl::size_t index, bool value)
Definition bdlb_bitstringutil.h:839
static void toggle(bsl::uint64_t *bitString, bsl::size_t index, bsl::size_t numBits)
static void assignBits(bsl::uint64_t *bitString, bsl::size_t index, bsl::uint64_t srcValue, bsl::size_t numBits)
static void assign0(bsl::uint64_t *bitString, bsl::size_t index)
Definition bdlb_bitstringutil.h:857
static bool bit(const bsl::uint64_t *bitString, bsl::size_t index)
Definition bdlb_bitstringutil.h:945
static void orEqual(bsl::uint64_t *dstBitString, bsl::size_t dstIndex, const bsl::uint64_t *srcBitString, bsl::size_t srcIndex, bsl::size_t numBits)
static const bsl::size_t k_INVALID_INDEX
Definition bdlb_bitstringutil.h:420
static bsl::uint64_t bits(const bsl::uint64_t *bitString, bsl::size_t index, bsl::size_t numBits)
static bool areEqual(const bsl::uint64_t *bitString1, const bsl::uint64_t *bitString2, bsl::size_t numBits)
static void copyRaw(bsl::uint64_t *dstBitString, bsl::size_t dstIndex, const bsl::uint64_t *srcBitString, bsl::size_t srcIndex, bsl::size_t numBits)
static bsl::size_t find0AtMaxIndex(const bsl::uint64_t *bitString, bsl::size_t length)
static bsl::size_t find1AtMaxIndex(const bsl::uint64_t *bitString, bsl::size_t length)
static void assign1(bsl::uint64_t *bitString, bsl::size_t index)
Definition bdlb_bitstringutil.h:868
static void remove(bsl::uint64_t *bitString, bsl::size_t length, bsl::size_t index, bsl::size_t numBits)
static bool isAny0(const bsl::uint64_t *bitString, bsl::size_t index, bsl::size_t numBits)
static void andEqual(bsl::uint64_t *dstBitString, bsl::size_t dstIndex, const bsl::uint64_t *srcBitString, bsl::size_t srcIndex, bsl::size_t numBits)
static void minusEqual(bsl::uint64_t *dstBitString, bsl::size_t dstIndex, const bsl::uint64_t *srcBitString, bsl::size_t srcIndex, bsl::size_t numBits)
static bsl::size_t find1AtMinIndex(const bsl::uint64_t *bitString, bsl::size_t length)
static bsl::size_t num0(const bsl::uint64_t *bitString, bsl::size_t index, bsl::size_t numBits)
Definition bdlb_bitstringutil.h:958
static bool isAny1(const bsl::uint64_t *bitString, bsl::size_t index, bsl::size_t numBits)
static void copy(bsl::uint64_t *dstBitString, bsl::size_t dstIndex, const bsl::uint64_t *srcBitString, bsl::size_t srcIndex, bsl::size_t numBits)
static void insert(bsl::uint64_t *bitString, bsl::size_t initialLength, bsl::size_t dstIndex, bool value, bsl::size_t numBits)
Definition bdlb_bitstringutil.h:881
static void xorEqual(bsl::uint64_t *dstBitString, bsl::size_t dstIndex, const bsl::uint64_t *srcBitString, bsl::size_t srcIndex, bsl::size_t numBits)
static bsl::size_t num1(const bsl::uint64_t *bitString, bsl::size_t index, bsl::size_t numBits)
Definition bslma_usesbslmaallocator.h:343
Definition bslmf_isbitwisemoveable.h:718
unsigned long long Uint64
Definition bsls_types.h:137