FixedBitVector.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #define FOREACH_BITSET_IN_FIXEDBV(index, bv) \
  7. { \
  8. BVIndex index; \
  9. for(JsUtil::FBVEnumerator _bvenum = bv->BeginSetBits(); \
  10. !_bvenum.End(); \
  11. _bvenum++) \
  12. { \
  13. index = _bvenum.GetCurrent(); \
  14. #define NEXT_BITSET_IN_FIXEDBV }}
  15. class BVFixed
  16. {
  17. // Data
  18. protected:
  19. BVIndex len;
  20. BVUnit data[];
  21. private:
  22. BVFixed(BVFixed * initBv);
  23. BVFixed(BVIndex length, bool initialSet = false);
  24. void ClearEnd();
  25. // Creation Factory
  26. public:
  27. template <typename TAllocator>
  28. static BVFixed * New(TAllocator* alloc, BVFixed * initBv);
  29. template <typename TAllocator>
  30. static BVFixed * New(DECLSPEC_GUARD_OVERFLOW BVIndex length, TAllocator* alloc, bool initialSet = false);
  31. template <typename TAllocator>
  32. static BVFixed * NewNoThrow(DECLSPEC_GUARD_OVERFLOW BVIndex length, TAllocator* alloc, bool initialSet = false);
  33. template <typename TAllocator>
  34. void Delete(TAllocator * alloc);
  35. // For preallocated memory
  36. static size_t GetAllocSize(BVIndex length);
  37. void Init(BVIndex length);
  38. // Implementation
  39. protected:
  40. void AssertRange(BVIndex i) const;
  41. void AssertBV(const BVFixed * bv) const;
  42. static BVIndex WordCount(BVIndex length);
  43. const BVUnit * BitsFromIndex(BVIndex i) const;
  44. BVUnit * BitsFromIndex(BVIndex i);
  45. const BVUnit * BeginUnit() const;
  46. BVUnit * BeginUnit();
  47. const BVUnit * EndUnit() const;
  48. BVUnit * EndUnit();
  49. template<class Fn>
  50. inline void for_each(const BVFixed *bv2, const Fn callback)
  51. {
  52. AssertMsg(this->len == bv2->len, "Fatal: The 2 bitvectors should have had the same length.");
  53. BVUnit * i;
  54. const BVUnit * j;
  55. for(i = this->BeginUnit(), j = bv2->BeginUnit();
  56. i != this->EndUnit() ;
  57. i++, j++)
  58. {
  59. (i->*callback)(*j);
  60. }
  61. }
  62. // Methods
  63. public:
  64. void Set(BVIndex i)
  65. {
  66. AssertRange(i);
  67. this->BitsFromIndex(i)->Set(BVUnit::Offset(i));
  68. }
  69. void Clear(BVIndex i)
  70. {
  71. AssertRange(i);
  72. this->BitsFromIndex(i)->Clear(BVUnit::Offset(i));
  73. }
  74. void Compliment(BVIndex i)
  75. {
  76. AssertRange(i);
  77. this->BitsFromIndex(i)->Complement(BVUnit::Offset(i));
  78. }
  79. BOOLEAN Test(BVIndex i) const
  80. {
  81. AssertRange(i);
  82. return this->BitsFromIndex(i)->Test(BVUnit::Offset(i));
  83. }
  84. BOOLEAN operator[](BVIndex i) const;
  85. BVIndex GetNextBit(BVIndex i) const;
  86. BOOLEAN TestAndSet(BVIndex i);
  87. BOOLEAN TestAndClear(BVIndex i);
  88. void OrComplimented(const BVFixed * bv);
  89. void Or(const BVFixed *bv);
  90. uint DiffCount(const BVFixed* bv) const;
  91. void And(const BVFixed *bv);
  92. void Minus(const BVFixed *bv);
  93. void Copy(const BVFixed *bv);
  94. void CopyBits(const BVFixed * bv, BVIndex i);
  95. void ComplimentAll();
  96. void SetAll();
  97. void ClearAll();
  98. BVIndex Count() const;
  99. BVIndex Length() const;
  100. JsUtil::FBVEnumerator BeginSetBits();
  101. BVIndex WordCount() const;
  102. bool IsAllClear() const;
  103. template<typename Container>
  104. Container GetRange(BVIndex start, BVIndex len) const;
  105. template<typename Container>
  106. void SetRange(Container* value, BVIndex start, BVIndex len);
  107. BVUnit* GetData() const
  108. {
  109. return (BVUnit*)data;
  110. }
  111. #if DBG_DUMP
  112. void Dump() const;
  113. #endif
  114. };
  115. template <typename TAllocator>
  116. BVFixed * BVFixed::New(TAllocator * alloc, BVFixed * initBv)
  117. {
  118. BVIndex length = initBv->Length();
  119. BVFixed *result = AllocatorNewPlusLeaf(TAllocator, alloc, sizeof(BVUnit) * BVFixed::WordCount(length), BVFixed, initBv);
  120. return result;
  121. }
  122. template <typename TAllocator>
  123. BVFixed * BVFixed::New(DECLSPEC_GUARD_OVERFLOW BVIndex length, TAllocator * alloc, bool initialSet)
  124. {
  125. BVFixed *result = AllocatorNewPlusLeaf(TAllocator, alloc, sizeof(BVUnit) * BVFixed::WordCount(length), BVFixed, length, initialSet);
  126. return result;
  127. }
  128. template <typename TAllocator>
  129. BVFixed * BVFixed::NewNoThrow(DECLSPEC_GUARD_OVERFLOW BVIndex length, TAllocator * alloc, bool initialSet)
  130. {
  131. BVFixed *result = AllocatorNewNoThrowNoRecoveryPlus(TAllocator, alloc, sizeof(BVUnit) * BVFixed::WordCount(length), BVFixed, length, initialSet);
  132. return result;
  133. }
  134. template <typename TAllocator>
  135. void BVFixed::Delete(TAllocator * alloc)
  136. {
  137. AllocatorDeletePlus(TAllocator, alloc, sizeof(BVUnit) * this->WordCount(), this);
  138. }
  139. template<typename Container>
  140. Container BVFixed::GetRange(BVIndex start, BVIndex len) const
  141. {
  142. AssertRange(start);
  143. if (len == 0)
  144. {
  145. return Container(0);
  146. }
  147. Assert(len <= sizeof(Container) * MachBits);
  148. AssertMsg(len <= 64, "Currently doesn't support range bigger than 64 bits");
  149. BVIndex end = start + len - 1;
  150. AssertRange(end);
  151. BVIndex iStart = BVUnit::Position(start);
  152. BVIndex iEnd = BVUnit::Position(end);
  153. BVIndex oStart = BVUnit::Offset(start);
  154. BVIndex oEnd = BVUnit::Offset(end);
  155. // Simply using uint64 because it is much easier than to juggle with BVUnit::BVUnitTContainer's size
  156. // Special case, if oEnd == 63, 1 << 64 == 1. Therefore the result is incorrect
  157. uint64 mask = oEnd < 63 ? (((uint64)1 << (oEnd + 1)) - 1) : 0xFFFFFFFFFFFFFFFF;
  158. uint64 range;
  159. // Trivial case
  160. if (iStart == iEnd)
  161. {
  162. // remove the bits after oEnd with mask, then remove the bits before start with shift
  163. range = (mask & this->data[iStart].GetWord()) >> oStart;
  164. }
  165. // Still simple enough
  166. else if (iStart + 1 == iEnd)
  167. {
  168. auto startWord = this->data[iStart].GetWord();
  169. auto endWord = this->data[iEnd].GetWord();
  170. // remove the bits before start with shift
  171. range = startWord >> oStart;
  172. // remove the bits after oEnd with mask then position it after start bits
  173. range |= (mask & endWord) << (BVUnit::BitsPerWord - oStart);
  174. }
  175. // Spans over multiple value, need to loop
  176. else
  177. {
  178. // Get the first bits and move them to the beginning
  179. range = this->data[iStart].GetWord() >> oStart;
  180. // track how many bits have been read so far
  181. int nBitsUsed = BVUnit::BitsPerWord - oStart;
  182. for (uint i = iStart + 1; i < iEnd; ++i)
  183. {
  184. // put all bits from the data in the mid-range. Use the tracked read bits to position them
  185. range |= ((uint64)(this->data[i].GetWord())) << nBitsUsed;
  186. nBitsUsed += BVUnit::BitsPerWord;
  187. }
  188. // Read the last bits and remove those after oEnd with mask
  189. range |= (mask & this->data[iEnd].GetWord()) << nBitsUsed;
  190. }
  191. return Container(range);
  192. }
  193. template<typename Container>
  194. #ifdef NO_SANITIZE_ADDRESS_FIXVC
  195. NO_SANITIZE_ADDRESS
  196. #endif
  197. void BVFixed::SetRange(Container* value, BVIndex start, BVIndex len)
  198. {
  199. AssertRange(start);
  200. if (len == 0)
  201. {
  202. return;
  203. }
  204. Assert(len <= sizeof(Container) * MachBits);
  205. BVIndex end = start + len - 1;
  206. AssertRange(end);
  207. BVIndex iStart = BVUnit::Position(start);
  208. BVIndex iEnd = BVUnit::Position(end);
  209. BVIndex oStart = BVUnit::Offset(start);
  210. BVIndex oEnd = BVUnit::Offset(end);
  211. BVUnit::BVUnitTContainer temp;
  212. BVUnit::BVUnitTContainer* bits;
  213. static_assert(sizeof(Container) == 1 || sizeof(Container) == sizeof(BVUnit::BVUnitTContainer),
  214. "Container is not suitable to represent the calculated value");
  215. if (sizeof(BVUnit::BVUnitTContainer) == 1)
  216. {
  217. temp = *((BVUnit::BVUnitTContainer*)value);
  218. bits = &temp;
  219. }
  220. else
  221. {
  222. bits = (BVUnit::BVUnitTContainer*)value;
  223. }
  224. const int oStartComplement = BVUnit::BitsPerWord - oStart;
  225. static_assert((BVUnit::BVUnitTContainer)BVUnit::AllOnesMask > 0, "Container type of BVFixed must be unsigned");
  226. //When making the mask, check the special case when we need all bits
  227. #define MAKE_MASK(start, end) ( ((end) == BVUnit::BitsPerWord ? BVUnit::AllOnesMask : (((BVUnit::BVUnitTContainer)1 << ((end) - (start))) - 1)) << (start))
  228. // Or the value to set the bits to 1. And the value to set the bits to 0
  229. // The mask is used to make sure we don't modify the bits outside the range
  230. #define SET_RANGE(i, value, mask) \
  231. this->data[i].Or((value) & mask);\
  232. this->data[i].And((value) | ~mask);
  233. BVUnit::BVUnitTContainer bitsToSet;
  234. // Fast Path
  235. if (iEnd == iStart)
  236. {
  237. const BVUnit::BVUnitTContainer mask = MAKE_MASK(oStart, oEnd + 1);
  238. // Shift to position the bits
  239. bitsToSet = (*bits << oStart);
  240. SET_RANGE(iStart, bitsToSet, mask);
  241. }
  242. // TODO: case iEnd == iStart + 1 to avoid a loop
  243. else if (oStart == 0)
  244. {
  245. // Simpler case where we don't have to shift the bits around
  246. for (uint i = iStart; i < iEnd; ++i)
  247. {
  248. SET_RANGE(i, *bits, BVUnit::AllOnesMask);
  249. ++bits;
  250. }
  251. // We still need to use a mask to remove the unused bits
  252. const BVUnit::BVUnitTContainer mask = MAKE_MASK(0, oEnd + 1);
  253. SET_RANGE(iEnd, *bits, mask);
  254. }
  255. else
  256. {
  257. // Default case. We need to process everything 1 at a time
  258. {
  259. // First set the first bits
  260. const BVUnit::BVUnitTContainer mask = MAKE_MASK(oStart, BVUnit::BitsPerWord);
  261. SET_RANGE(iStart, *bits << oStart, mask);
  262. }
  263. // Set the bits in the middle
  264. for (uint i = iStart + 1; i < iEnd; ++i)
  265. {
  266. bitsToSet = *bits >> oStartComplement;
  267. ++bits;
  268. bitsToSet |= *bits << oStart;
  269. SET_RANGE(i, bitsToSet, BVUnit::AllOnesMask);
  270. }
  271. // Set the last bits
  272. bitsToSet = *bits >> oStartComplement;
  273. ++bits;
  274. bitsToSet |= *bits << oStart;
  275. {
  276. const BVUnit::BVUnitTContainer mask = MAKE_MASK(0, oEnd + 1);
  277. SET_RANGE(iEnd, bitsToSet, mask);
  278. }
  279. }
  280. if (sizeof(Container) == 1)
  281. {
  282. // Calculation above might overflow the original container.
  283. // normalize the overflow value. LE only
  284. temp = (*((char*)bits)) + (*(((char*)bits) + 1));
  285. memcpy(value, bits, 1);
  286. }
  287. #undef MAKE_MASK
  288. #undef SET_RANGE
  289. }
  290. template <size_t bitCount>
  291. class BVStatic
  292. {
  293. public:
  294. // Made public to allow for compile-time use
  295. static const size_t wordCount = ((bitCount - 1) >> BVUnit::ShiftValue) + 1;
  296. // Data
  297. private:
  298. Field(BVUnit) data[wordCount];
  299. public:
  300. // Break on member changes. We rely on the layout of this class being static so we can
  301. // use initializer lists to generate collections of BVStatic.
  302. BVStatic()
  303. {
  304. Assert(sizeof(BVStatic<bitCount>) == sizeof(data));
  305. Assert((void*)this == (void*)&this->data);
  306. }
  307. // Implementation
  308. private:
  309. void AssertRange(BVIndex i) const { Assert(i < bitCount); }
  310. const BVUnit * BitsFromIndex(BVIndex i) const { AssertRange(i); return &this->data[BVUnit::Position(i)]; }
  311. BVUnit * BitsFromIndex(BVIndex i) { AssertRange(i); return &this->data[BVUnit::Position(i)]; }
  312. const BVUnit * BeginUnit() const { return &this->data[0]; }
  313. BVUnit * BeginUnit() { return &this->data[0]; }
  314. const BVUnit * EndUnit() const { return &this->data[wordCount]; }
  315. BVUnit * EndUnit() { return &this->data[wordCount]; }
  316. template<class Fn>
  317. inline void for_each(const BVStatic *bv2, const Fn callback)
  318. {
  319. BVUnit * i;
  320. const BVUnit * j;
  321. for(i = this->BeginUnit(), j = bv2->BeginUnit();
  322. i != this->EndUnit() ;
  323. i++, j++)
  324. {
  325. (i->*callback)(*j);
  326. }
  327. }
  328. template<class Fn>
  329. static bool MapUntil(const BVStatic *bv1, const BVStatic *bv2, const Fn callback)
  330. {
  331. const BVUnit * i;
  332. const BVUnit * j;
  333. for(i = bv1->BeginUnit(), j = bv2->BeginUnit();
  334. i != bv1->EndUnit() ;
  335. i++, j++)
  336. {
  337. if (!callback(*i, *j))
  338. {
  339. return false;
  340. }
  341. }
  342. return true;
  343. }
  344. void ClearEnd()
  345. {
  346. uint offset = BVUnit::Offset(bitCount);
  347. if (offset != 0)
  348. {
  349. this->data[wordCount - 1].And((1 << offset) - 1);
  350. }
  351. }
  352. // Methods
  353. public:
  354. void Set(BVIndex i)
  355. {
  356. AssertRange(i);
  357. this->BitsFromIndex(i)->Set(BVUnit::Offset(i));
  358. }
  359. void Clear(BVIndex i)
  360. {
  361. AssertRange(i);
  362. this->BitsFromIndex(i)->Clear(BVUnit::Offset(i));
  363. }
  364. void Compliment(BVIndex i)
  365. {
  366. AssertRange(i);
  367. this->BitsFromIndex(i)->Complement(BVUnit::Offset(i));
  368. }
  369. BOOLEAN Equal(BVStatic<bitCount> const * o)
  370. {
  371. return MapUntil(this, o, [](BVUnit const& i, BVUnit const &j) { return i.Equal(j); });
  372. }
  373. BOOLEAN Test(BVIndex i) const
  374. {
  375. AssertRange(i);
  376. return this->BitsFromIndex(i)->Test(BVUnit::Offset(i));
  377. }
  378. BOOLEAN TestAndSet(BVIndex i)
  379. {
  380. AssertRange(i);
  381. return PlatformAgnostic::_BitTestAndSet((LONG *)this->data, (LONG) i);
  382. }
  383. BOOLEAN TestIntrinsic(BVIndex i) const
  384. {
  385. AssertRange(i);
  386. return PlatformAgnostic::_BitTest((LONG *)this->data, (LONG) i);
  387. }
  388. BOOLEAN TestAndSetInterlocked(BVIndex i)
  389. {
  390. AssertRange(i);
  391. return PlatformAgnostic::_InterlockedBitTestAndSet((LONG *)this->data, (LONG) i);
  392. }
  393. BOOLEAN TestAndClear(BVIndex i)
  394. {
  395. AssertRange(i);
  396. BVUnit * bvUnit = this->BitsFromIndex(i);
  397. BVIndex offset = BVUnit::Offset(i);
  398. BOOLEAN bit = bvUnit->Test(offset);
  399. bvUnit->Clear(offset);
  400. return bit;
  401. }
  402. BOOLEAN TestAndClearInterlocked(BVIndex i)
  403. {
  404. AssertRange(i);
  405. return PlatformAgnostic::_InterlockedBitTestAndReset((LONG *)this->data, (LONG)i);
  406. }
  407. void OrComplimented(const BVStatic * bv) { this->for_each(bv, &BVUnit::OrComplimented); ClearEnd(); }
  408. void Or(const BVStatic *bv) { this->for_each(bv, &BVUnit::Or); }
  409. void And(const BVStatic *bv) { this->for_each(bv, &BVUnit::And); }
  410. void Minus(const BVStatic *bv) { this->for_each(bv, &BVUnit::Minus); }
  411. void Copy(const BVStatic *bv) { js_memcpy_s(&this->data[0], wordCount * sizeof(BVUnit), &bv->data[0], wordCount * sizeof(BVUnit)); }
  412. void SetAll() { memset(&this->data[0], -1, wordCount * sizeof(BVUnit)); ClearEnd(); }
  413. void ClearAll() { memset(&this->data[0], 0, wordCount * sizeof(BVUnit)); }
  414. void ComplimentAll()
  415. {
  416. for (BVIndex i = 0; i < wordCount; i++)
  417. {
  418. this->data[i].ComplimentAll();
  419. }
  420. ClearEnd();
  421. }
  422. BVIndex Count() const
  423. {
  424. BVIndex sum = 0;
  425. for (BVIndex i = 0; i < wordCount; i++)
  426. {
  427. sum += this->data[i].Count();
  428. }
  429. Assert(sum <= bitCount);
  430. return sum;
  431. }
  432. BVIndex Length() const
  433. {
  434. return bitCount;
  435. }
  436. JsUtil::FBVEnumerator BeginSetBits() { return JsUtil::FBVEnumerator(this->BeginUnit(), this->EndUnit()); }
  437. BVIndex GetNextBit(BVIndex i) const
  438. {
  439. AssertRange(i);
  440. const BVUnit * chunk = BitsFromIndex(i);
  441. BVIndex base = BVUnit::Floor(i);
  442. BVIndex offset = chunk->GetNextBit(BVUnit::Offset(i));
  443. if (-1 != offset)
  444. {
  445. return base + offset;
  446. }
  447. while (++chunk != this->EndUnit())
  448. {
  449. base += BVUnit::BitsPerWord;
  450. offset = chunk->GetNextBit();
  451. if (-1 != offset)
  452. {
  453. return base + offset;
  454. }
  455. }
  456. return BVInvalidIndex;
  457. }
  458. const BVUnit * GetRawData() const { return data; }
  459. template <size_t rangeSize>
  460. BVStatic<rangeSize> * GetRange(BVIndex startOffset) const
  461. {
  462. AssertRange(startOffset);
  463. AssertRange(startOffset + rangeSize - 1);
  464. // Start offset and size must be word-aligned
  465. Assert(BVUnit::Offset(startOffset) == 0);
  466. Assert(BVUnit::Offset(rangeSize) == 0);
  467. return (BVStatic<rangeSize> *)BitsFromIndex(startOffset);
  468. }
  469. BOOLEAN TestRange(const BVIndex index, uint length) const
  470. {
  471. AssertRange(index);
  472. AssertRange(index + length - 1);
  473. const BVUnit * bvUnit = BitsFromIndex(index);
  474. uint offset = BVUnit::Offset(index);
  475. if (offset + length <= BVUnit::BitsPerWord)
  476. {
  477. // Bit range is in a single word
  478. return bvUnit->TestRange(offset, length);
  479. }
  480. // Bit range spans words.
  481. // Test the first word, from start offset to end of word
  482. if (!bvUnit->TestRange(offset, (BVUnit::BitsPerWord - offset)))
  483. {
  484. return FALSE;
  485. }
  486. bvUnit++;
  487. length -= (BVUnit::BitsPerWord - offset);
  488. // Test entire words until we are at the last word
  489. while (length >= BVUnit::BitsPerWord)
  490. {
  491. if (!bvUnit->IsFull())
  492. {
  493. return FALSE;
  494. }
  495. bvUnit++;
  496. length -= BVUnit::BitsPerWord;
  497. }
  498. // Test last word (unless we already ended on a word boundary)
  499. if (length > 0)
  500. {
  501. if (!bvUnit->TestRange(0, length))
  502. {
  503. return FALSE;
  504. }
  505. }
  506. return TRUE;
  507. }
  508. void SetRange(const BVIndex index, uint length)
  509. {
  510. AssertRange(index);
  511. AssertRange(index + length - 1);
  512. BVUnit * bvUnit = BitsFromIndex(index);
  513. uint offset = BVUnit::Offset(index);
  514. if (offset + length <= BVUnit::BitsPerWord)
  515. {
  516. // Bit range is in a single word
  517. return bvUnit->SetRange(offset, length);
  518. }
  519. // Bit range spans words.
  520. // Set the first word, from start offset to end of word
  521. bvUnit->SetRange(offset, (BVUnit::BitsPerWord - offset));
  522. bvUnit++;
  523. length -= (BVUnit::BitsPerWord - offset);
  524. // Set entire words until we are at the last word
  525. while (length >= BVUnit::BitsPerWord)
  526. {
  527. bvUnit->SetAll();
  528. bvUnit++;
  529. length -= BVUnit::BitsPerWord;
  530. }
  531. // Set last word (unless we already ended on a word boundary)
  532. if (length > 0)
  533. {
  534. bvUnit->SetRange(0, length);
  535. }
  536. }
  537. void ClearRange(const BVIndex index, uint length)
  538. {
  539. AssertRange(index);
  540. AssertRange(index + length - 1);
  541. BVUnit * bvUnit = BitsFromIndex(index);
  542. uint offset = BVUnit::Offset(index);
  543. if (offset + length <= BVUnit::BitsPerWord)
  544. {
  545. // Bit range is in a single word
  546. return bvUnit->ClearRange(offset, length);
  547. }
  548. // Bit range spans words.
  549. // Clear the first word, from start offset to end of word
  550. bvUnit->ClearRange(offset, (BVUnit::BitsPerWord - offset));
  551. bvUnit++;
  552. length -= (BVUnit::BitsPerWord - offset);
  553. // Set entire words until we are at the last word
  554. while (length >= BVUnit::BitsPerWord)
  555. {
  556. bvUnit->ClearAll();
  557. bvUnit++;
  558. length -= BVUnit::BitsPerWord;
  559. }
  560. // Set last word (unless we already ended on a word boundary)
  561. if (length > 0)
  562. {
  563. bvUnit->ClearRange(0, length);
  564. }
  565. }
  566. bool IsAllClear()
  567. {
  568. for (BVIndex i = 0; i < wordCount; i++)
  569. {
  570. if (!this->data[i].IsEmpty())
  571. {
  572. return false;
  573. }
  574. }
  575. return true;
  576. }
  577. bool IsAllSet() const
  578. {
  579. for (BVIndex i = 0; i < this->wordCount; i++)
  580. {
  581. if (i == this->wordCount - 1 && Length() % BVUnit::BitsPerWord != 0)
  582. {
  583. return this->data[i].Count() == Length() % BVUnit::BitsPerWord;
  584. }
  585. else if (!this->data[i].IsFull())
  586. {
  587. return false;
  588. }
  589. }
  590. return true;
  591. }
  592. #if DBG_DUMP
  593. void Dump() const
  594. {
  595. bool hasBits = false;
  596. Output::Print(_u("[ "));
  597. for (BVIndex i = 0; i < wordCount; i++)
  598. {
  599. hasBits = this->data[i].Dump(i * BVUnit::BitsPerWord, hasBits);
  600. }
  601. Output::Print(_u("]\n"));
  602. }
  603. #endif
  604. };