6 #ifndef _INC_cBitArray_H
7 #define _INC_cBitArray_H
12 #include "../GrayLibBase.h"
37 #if defined(USE_64BIT)
52 #if defined(USE_64BIT)
54 #if ( defined(__MSC_VER) && ( defined(_M_AMD64) || defined(_M_X64) || defined(_M_IA64)))
58 #if ( defined(__GNUC__) && ( \
59 defined(__amd64__) || defined(__x86_64__) || \
60 defined(__ppc64__) || defined(__powerpc64__) || \
61 defined(__ia64__) || defined(__alpha__) || \
62 (defined(__sparc__) && defined(__arch64__)) || \
63 defined(__s390x__) ) )
64 typedef unsigned int BLOCKDBL_t __attribute__((mode(TI)));
65 #define GRAY_BLOCK_UDBL
69 typedef UINT32 BLOCKD_t;
70 #define GRAY_BLOCK_UDBL
73 typedef UINT64 BLOCKDBL_t;
74 #define GRAY_BLOCK_UDBL
80 static const size_t k_SIZE_MAX = k_BLOCK_MAX *
sizeof(
BLOCK_t);
98 #define BLOCKS_FROM_BITS(nBits) ((cBitArrayStatic::BLOCK_ENUM_t)(((nBits) + (cBitArrayStatic::k_BLOCK_BITS - 1)) / cBitArrayStatic::k_BLOCK_BITS))
118 void SetBlockSizeReAlloc(BLOCK_ENUM_t nBlocksNew);
119 void SetBlockSizeUninit(BLOCK_ENUM_t nBlocksNew);
120 void SetBlockSizeGrow(BLOCK_ENUM_t nBlocksNew);
126 while (nBlocks > 0 && GetBlockInt(nBlocks - 1) == 0)
134 return FindUsedBlocksQty(get_BlocksCap());
147 , m_nBlocks(GetBlocksFromBits(nBits))
149 m_pBlocks =
new BLOCK_t[m_nBlocks];
154 , m_nBlocks(GetBlocksFromBits(nBits))
157 ASSERT(IsReasonableBlockQty(m_nBlocks));
158 m_pBlocks =
new BLOCK_t[m_nBlocks];
161 m_pBlocks[0] = uValueMask;
162 cValArray::FillQty<BLOCK_t>(m_pBlocks + 1, m_nBlocks - 1, 0);
168 , m_pBlocks(new
BLOCK_t[nBlocks])
171 ASSERT(IsReasonableBlockQty(m_nBlocks));
172 cValArray::CopyQty<BLOCK_t>(m_pBlocks, pData, nBlocks);
177 , m_pBlocks(const_cast<
BLOCK_t*>(pBlocksInit))
181 ASSERT(IsReasonableBlockQty(m_nBlocks));
182 ASSERT((m_nBlocks > 0) ^ (m_pBlocks ==
nullptr));
186 : m_bStatic(ref.m_bStatic)
187 , m_nBlocks(ref.m_nBlocks)
188 , m_pBlocks(ref.m_bStatic ? ref.m_pBlocks : (new
BLOCK_t[ref.m_nBlocks]))
191 ASSERT(IsReasonableBlockQty(m_nBlocks));
194 cValArray::CopyQty<BLOCK_t>(m_pBlocks, ref.m_pBlocks, m_nBlocks);
198 ASSERT((m_nBlocks > 0) ^ (m_pBlocks ==
nullptr));
202 : m_bStatic(ref.m_bStatic)
203 , m_nBlocks(ref.m_nBlocks)
204 , m_pBlocks(ref.m_pBlocks)
208 ref.m_bStatic =
false;
210 ref.m_pBlocks =
nullptr;
221 return m_pBlocks ==
nullptr;
226 return isNullVal() || (m_nBlocks <= 0);
235 return m_nBlocks *
sizeof(
BLOCK_t);
239 ASSERT(IsValidBlockIndex(i));
258 ASSERT(IsValidBlockIndex(i));
265 #if defined(USE_64BIT)
266 #ifdef USE_LITTLE_ENDIAN
267 ASSERT(IsValidBlockIndex(j / 2));
268 return ((
const UINT32*)m_pBlocks)[j];
270 BLOCK_t v = GetBlockInt(j / 2);
271 return (j & 1) ? (v >> 32) : ((UINT32)v);
274 return GetBlockInt(j);
280 #if defined(USE_64BIT)
282 #ifdef USE_LITTLE_ENDIAN
283 ASSERT(IsValidBlockIndex(j / 2));
284 ((UINT32*)m_pBlocks)[j] = val32;
291 v &= 0x00000000FFFFFFFF;
296 v &= 0xFFFFFFFF00000000;
302 RefBlockInt(j) = val32;
315 m_bStatic = ref.m_bStatic;
316 m_nBlocks = ref.m_nBlocks;
317 m_pBlocks = ref.m_pBlocks;
318 ref.m_pBlocks =
nullptr;
319 ref.m_bStatic =
false;
329 void SetBlocksStatic(BLOCK_ENUM_t nBlocks,
const BLOCK_t* pBlocks);
340 return((
BIT_ENUM_t)(get_BlocksCap() * k_BLOCK_BITS));
354 if (!IsValidBlockIndex(nBlock))
356 return cBits::IsSet(GetBlockInt(nBlock), nBit % k_BLOCK_BITS);
362 return(m_pBlocks[0] & 1);
369 if (!IsValidBlockIndex(nBlock))
371 RefBlockInt(nBlock) &= ~(cBits::Mask1<BLOCK_t>(nBit % k_BLOCK_BITS));
377 RefBlockInt(nBlock) |= cBits::Mask1<BLOCK_t>(nBit % k_BLOCK_BITS);
390 void SetNullVal() noexcept;
395 cValArray::ZeroQty<BLOCK_t>(get_BlockPtr(), get_BlocksCap());
401 ::memset(get_BlockPtr(), 0xFF, get_BlocksCap() *
sizeof(
BLOCK_t));
407 SetBlockSizeUninit(GetBlocksFromBits(nBits));
411 void SetInvertBytes(
size_t nSize);
412 void SetCopyBits(
const THIS_t& ref);
435 return FindUsedBlocksQty() == m_nBlocksUse;
441 return FindUsedBlocksQty() <= m_nBlocksUse;
451 ASSERT(isHighBlockUseValid());
459 while (m_nBlocksUse > 0 && GetBlockInt(m_nBlocksUse - 1) == 0)
466 DEBUG_ASSERT(nBlocksUseMax <= get_BlocksCap(), __FUNCTION__);
467 m_nBlocksUse = nBlocksUseMax;
473 m_nBlocksUse = get_BlocksCap();
480 SetBlockSizeGrow(nBlock + 1);
481 return RefBlockInt(nBlock);
489 m_nBlocksUse = nBlocksUse;
498 return GetBlockInt(nBlock);
505 if (m_nBlocksUse >= get_BlocksCap())
507 SetBlockSizeReAlloc(m_nBlocksUse + 1);
509 RefBlockInt(m_nBlocksUse) = nVal;
540 , m_nBlocksUse(uValueMask ? 1 : 0)
547 m_nBlocksUse = nBlocks;
551 :
SUPER_t(nBlocks, pBlocks, bStatic)
555 m_nBlocksUse = nBlocks;
564 void SetBlocksStatic(BLOCK_ENUM_t nBlocks,
const BLOCK_t* pBlocks);
567 HRESULT SetBlockSizeGrow(BLOCK_ENUM_t nBlocksNew);
568 HRESULT SetBlockSizeShrink(BLOCK_ENUM_t nBlocksNew);
569 void SetBlockSizeUse(BLOCK_ENUM_t nBlocksNew);
574 if (nBlocks != x.get_BlocksUse())
584 return GetBlockInt(0) == x;
599 return m_nBlocksUse == 0;
610 nBit %= k_BLOCK_BITS;
621 for (
BLOCK_ENUM_t nBlock = 0; nBlock < m_nBlocksUse; nBlock++)
635 RefBlock(nBlock) |= cBits::Mask1<BLOCK_t>(nBit % k_BLOCK_BITS);
643 RefBlockInt(nBlock) &= ~(cBits::Mask1<BLOCK_t>(nBit % k_BLOCK_BITS));
662 if (nBits < 3 || nBits > k_BIT_MAX)
664 HRESULT hRes = SetRandomBits(nBits, pRandom);
669 void SetCopyBits(
const THIS_t& ref);
670 HRESULT SetCopySecure(
const THIS_t& val,
bool assign);
678 SUPER_t::SetZeroAll();
685 SUPER_t::SetNullVal();
695 HRESULT GetBinaryBE(BYTE* pOut,
size_t nSizeOut)
const;
697 HRESULT SetBinaryBE(
const BYTE* pData,
size_t nSize);
698 HRESULT SetBinaryLE(
const BYTE* pData,
size_t nSize);
708 #if defined(USE_64BIT)
710 #define BLOCKS_FROM_BYTES( a, b, c, d, e, f, g, h ) \
711 ( (cBitArray::BLOCK_t) a << 0 ) | \
712 ( (cBitArray::BLOCK_t) b << 8 ) | \
713 ( (cBitArray::BLOCK_t) c << 16 ) | \
714 ( (cBitArray::BLOCK_t) d << 24 ) | \
715 ( (cBitArray::BLOCK_t) e << 32 ) | \
716 ( (cBitArray::BLOCK_t) f << 40 ) | \
717 ( (cBitArray::BLOCK_t) g << 48 ) | \
718 ( (cBitArray::BLOCK_t) h << 56 )
720 #define BLOCK_FROM_BYTES( a, b, c, d ) BLOCKS_FROM_BYTES( a, b, c, d, 0, 0, 0, 0 )
724 #define BLOCK_FROM_BYTES( a, b, c, d ) \
725 ( (cBitArray::BLOCK_t) a << 0 ) | \
726 ( (cBitArray::BLOCK_t) b << 8 ) | \
727 ( (cBitArray::BLOCK_t) c << 16 ) | \
728 ( (cBitArray::BLOCK_t) d << 24 )
730 #define BLOCKS_FROM_BYTES( a, b, c, d, e, f, g, h ) BLOCK_FROM_BYTES( a, b, c, d ), BLOCK_FROM_BYTES( e, f, g, h )
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define IS_INDEX_GOOD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:35
#define IS_INDEX_BAD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:34
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define BLOCKS_FROM_BITS(nBits)
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define DEBUG_ASSERT(exp, sDesc)
Definition: cDebugAssert.h:93
Definition: cBitArray.h:28
void SetBit(BIT_ENUM_t nBit)
Definition: cBitArray.h:373
void SetBitsSizeZ(BIT_ENUM_t nBits)
Definition: cBitArray.h:404
static BLOCK_ENUM_t GetBlocksFromBits(BIT_ENUM_t nBits) noexcept
Definition: cBitArray.h:95
size_t get_BytesCap() const noexcept
Definition: cBitArray.h:233
cBitArrayStatic(BIT_ENUM_t nBits)
Definition: cBitArray.h:145
bool IsSet(BIT_ENUM_t nBit) const
Definition: cBitArray.h:348
bool IsValidBit(BIT_ENUM_t nBit) const noexcept
Definition: cBitArray.h:342
bool isNullVal() const noexcept
Definition: cBitArray.h:218
static bool IsReasonableBlockQty(BLOCK_ENUM_t nBlocks) noexcept
Definition: cBitArray.h:106
static BLOCK_ENUM_t GetBlocksFromBytes(size_t nBytes) noexcept
Definition: cBitArray.h:101
void SetU32Int(BLOCK_ENUM_t j, UINT32 val32)
Definition: cBitArray.h:277
~cBitArrayStatic()
Definition: cBitArray.h:212
static const cBitArrayStatic k_Zero
Definition: cBitArray.h:83
BIT_ENUM_t get_BitsCap() const noexcept
Definition: cBitArray.h:337
unsigned int BLOCK_ENUM_t
Type for the index of a BLOCK_t in the array. NOT bits BIT_ENUM_t.
Definition: cBitArray.h:78
cBitArrayStatic() noexcept
Definition: cBitArray.h:138
cBitArrayStatic(BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocksInit, bool bStatic)
Definition: cBitArray.h:174
bool isOdd() const
Definition: cBitArray.h:358
INT32 BLOCKI_t
The biggest signed type I can do atomic math on.
Definition: cBitArray.h:47
bool isReadOnly() const noexcept
Definition: cBitArray.h:331
void ModBit(BIT_ENUM_t nBit, bool bVal)
Definition: cBitArray.h:380
cBitArrayStatic(BLOCK_ENUM_t nBlocks, const BLOCK_t *pData)
Definition: cBitArray.h:165
bool IsValidBlockIndex(BLOCK_ENUM_t i) const noexcept
Definition: cBitArray.h:113
UINT32 GetU32Int(BLOCK_ENUM_t j) const
Definition: cBitArray.h:262
BLOCK_ENUM_t FindUsedBlocksQty() const
Definition: cBitArray.h:130
bool isZeroVal() const noexcept
Definition: cBitArray.h:223
BLOCK_t * get_BlockPtr()
Definition: cBitArray.h:247
void SetAll1()
Definition: cBitArray.h:397
cBitArrayStatic(const THIS_t &ref)
Definition: cBitArray.h:185
BLOCK_ENUM_t get_BlocksCap() const noexcept
Definition: cBitArray.h:228
cBitArrayStatic(BIT_ENUM_t nBits, BLOCK_t uValueMask)
Definition: cBitArray.h:152
BLOCK_ENUM_t FindUsedBlocksQty(BLOCK_ENUM_t nBlocks) const
Definition: cBitArray.h:122
const BLOCK_t * get_BlockPtrC() const noexcept
Definition: cBitArray.h:242
BLOCK_t GetBlockInt(BLOCK_ENUM_t i) const
Definition: cBitArray.h:237
UINT32 BLOCK_t
The biggest unsigned type I can do atomic math on for this architecture.
Definition: cBitArray.h:46
BLOCK_t & RefBlockInt(BLOCK_ENUM_t i)
Definition: cBitArray.h:253
bool isStatic() const noexcept
Definition: cBitArray.h:324
WORD BLOCKH_t
half sized BLOCK_t for multiplication overflows.
Definition: cBitArray.h:48
void ClrBit(BIT_ENUM_t nBit)
Definition: cBitArray.h:365
cBitArrayStatic(THIS_t &&ref) noexcept
Definition: cBitArray.h:201
Definition: cBitArray.h:416
HRESULT SetRandomBitsLarge(BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
Definition: cBitArray.h:659
void UpdateBlocksUse()
Definition: cBitArray.h:455
cBitArray(BIT_ENUM_t nBits, BLOCK_t uValueMask)
Definition: cBitArray.h:538
size_t get_BinarySize() const
Definition: cBitArray.h:688
BLOCK_ENUM_t m_nBlocksUse
Qty of blocks that is non zero. ASSUME anything above this is 0 blocks.
Definition: cBitArray.h:428
bool IsEqual(const THIS_t &x) const
Definition: cBitArray.h:571
void UpdateBlocksUseCap()
Definition: cBitArray.h:470
~cBitArray()
Definition: cBitArray.h:558
void AddBlockGrow(BLOCK_t nVal)
Definition: cBitArray.h:500
cBitArray(BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
Definition: cBitArray.h:543
bool isEmptyBits() const
Definition: cBitArray.h:595
void SetNullVal()
Definition: cBitArray.h:681
bool IsSet(BIT_ENUM_t nBit) const
Definition: cBitArray.h:602
bool isHighBlockUseExact() const
Definition: cBitArray.h:431
void SetBit(BIT_ENUM_t nBit)
Definition: cBitArray.h:631
BLOCK_ENUM_t get_BlocksUse() const
Definition: cBitArray.h:443
BIT_ENUM_t get_Count1Bits() const
Definition: cBitArray.h:617
void ModBit(BIT_ENUM_t nBit, bool bVal)
Definition: cBitArray.h:646
void put_BlocksUse(BLOCK_ENUM_t nBlocksUse)
Definition: cBitArray.h:486
void UpdateBlocksUseMax(BLOCK_ENUM_t nBlocksUseMax)
Definition: cBitArray.h:463
cBitArray(BIT_ENUM_t nBits)
Definition: cBitArray.h:533
BLOCK_t & RefBlock(BLOCK_ENUM_t nBlock)
Definition: cBitArray.h:476
void SetZeroAll()
Definition: cBitArray.h:673
bool isHighBlockUseValid() const
Definition: cBitArray.h:437
bool IsEqualU(BLOCK_t x) const
Definition: cBitArray.h:578
void SetBlock(BLOCK_ENUM_t i, BLOCK_t nVal)
Definition: cBitArray.h:512
BLOCK_t GetBlock(BLOCK_ENUM_t nBlock) const
Definition: cBitArray.h:493
UNITTEST_FRIEND(cBitArray)
cBitArray()
Definition: cBitArray.h:528
cBitArray(BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks, bool bStatic)
Definition: cBitArray.h:550
void ClrBit(BIT_ENUM_t nBit)
Definition: cBitArray.h:637
UNITTEST2_PREDEF(cQuadtree)
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
DECLARE_INTERFACE(IRandomNoise)
unsigned int BIT_ENUM_t
Enumerate number of bits or address a single bit in some array of bits.
Definition: cBits.h:20
bool operator!=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:254
bool operator==(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:250
static BIT_ENUM_t Count1Bits(TYPE nMask) noexcept
Definition: cBits.h:127
static constexpr size_t GetSizeBytes(BIT_ENUM_t nBits) noexcept
Definition: cBits.h:54
static constexpr bool IsSet(TYPE nVal, BIT_ENUM_t nBit) noexcept
Definition: cBits.h:77
static bool IsEqualQty(const TYPE *pArray1, const TYPE *pArray2, ITERATE_t nBlocks) noexcept
Definition: cValT.h:87