FixedBitVector.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 = AllocatorNewNoThrowPlus(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. void BVFixed::SetRange(Container* value, BVIndex start, BVIndex len)
  195. {
  196. AssertRange(start);
  197. if (len == 0)
  198. {
  199. return;
  200. }
  201. Assert(len <= sizeof(Container) * MachBits);
  202. BVIndex end = start + len - 1;
  203. AssertRange(end);
  204. BVIndex iStart = BVUnit::Position(start);
  205. BVIndex iEnd = BVUnit::Position(end);
  206. BVIndex oStart = BVUnit::Offset(start);
  207. BVIndex oEnd = BVUnit::Offset(end);
  208. BVUnit::BVUnitTContainer temp;
  209. BVUnit::BVUnitTContainer* bits;
  210. static_assert(sizeof(Container) == 1 || sizeof(Container) == sizeof(BVUnit::BVUnitTContainer),
  211. "Container is not suitable to represent the calculated value");
  212. if (sizeof(BVUnit::BVUnitTContainer) == 1)
  213. {
  214. temp = *((BVUnit::BVUnitTContainer*)value);
  215. bits = &temp;
  216. }
  217. else
  218. {
  219. bits = (BVUnit::BVUnitTContainer*)value;
  220. }
  221. const int oStartComplement = BVUnit::BitsPerWord - oStart;
  222. static_assert((BVUnit::BVUnitTContainer)BVUnit::AllOnesMask > 0, "Container type of BVFixed must be unsigned");
  223. //When making the mask, check the special case when we need all bits
  224. #define MAKE_MASK(start, end) ( ((end) == BVUnit::BitsPerWord ? BVUnit::AllOnesMask : (((BVUnit::BVUnitTContainer)1 << ((end) - (start))) - 1)) << (start))
  225. // Or the value to set the bits to 1. And the value to set the bits to 0
  226. // The mask is used to make sure we don't modify the bits outside the range
  227. #define SET_RANGE(i, value, mask) \
  228. this->data[i].Or((value) & mask);\
  229. this->data[i].And((value) | ~mask);
  230. BVUnit::BVUnitTContainer bitsToSet;
  231. // Fast Path
  232. if (iEnd == iStart)
  233. {
  234. const BVUnit::BVUnitTContainer mask = MAKE_MASK(oStart, oEnd + 1);
  235. // Shift to position the bits
  236. bitsToSet = (*bits << oStart);
  237. SET_RANGE(iStart, bitsToSet, mask);
  238. }
  239. // TODO: case iEnd == iStart + 1 to avoid a loop
  240. else if (oStart == 0)
  241. {
  242. // Simpler case where we don't have to shift the bits around
  243. for (uint i = iStart; i < iEnd; ++i)
  244. {
  245. SET_RANGE(i, *bits, BVUnit::AllOnesMask);
  246. ++bits;
  247. }
  248. // We still need to use a mask to remove the unused bits
  249. const BVUnit::BVUnitTContainer mask = MAKE_MASK(0, oEnd + 1);
  250. SET_RANGE(iEnd, *bits, mask);
  251. }
  252. else
  253. {
  254. // Default case. We need to process everything 1 at a time
  255. {
  256. // First set the first bits
  257. const BVUnit::BVUnitTContainer mask = MAKE_MASK(oStart, BVUnit::BitsPerWord);
  258. SET_RANGE(iStart, *bits << oStart, mask);
  259. }
  260. // Set the bits in the middle
  261. for (uint i = iStart + 1; i < iEnd; ++i)
  262. {
  263. bitsToSet = *bits >> oStartComplement;
  264. ++bits;
  265. bitsToSet |= *bits << oStart;
  266. SET_RANGE(i, bitsToSet, BVUnit::AllOnesMask);
  267. }
  268. // Set the last bits
  269. bitsToSet = *bits >> oStartComplement;
  270. ++bits;
  271. bitsToSet |= *bits << oStart;
  272. {
  273. const BVUnit::BVUnitTContainer mask = MAKE_MASK(0, oEnd + 1);
  274. SET_RANGE(iEnd, bitsToSet, mask);
  275. }
  276. }
  277. if (sizeof(Container) == 1)
  278. {
  279. // Calculation above might overflow the original container.
  280. // normalize the overflow value. LE only
  281. temp = (*((char*)bits)) + (*(((char*)bits) + 1));
  282. memcpy(value, bits, 1);
  283. }
  284. #undef MAKE_MASK
  285. #undef SET_RANGE
  286. }
  287. template <size_t bitCount>
  288. class BVStatic
  289. {
  290. public:
  291. // Made public to allow for compile-time use
  292. static const size_t wordCount = ((bitCount - 1) >> BVUnit::ShiftValue) + 1;
  293. // Data
  294. private:
  295. Field(BVUnit) data[wordCount];
  296. public:
  297. // Break on member changes. We rely on the layout of this class being static so we can
  298. // use initializer lists to generate collections of BVStatic.
  299. BVStatic()
  300. {
  301. Assert(sizeof(BVStatic<bitCount>) == sizeof(data));
  302. Assert((void*)this == (void*)&this->data);
  303. }
  304. // Implementation
  305. private:
  306. void AssertRange(BVIndex i) const { Assert(i < bitCount); }
  307. const BVUnit * BitsFromIndex(BVIndex i) const { AssertRange(i); return &this->data[BVUnit::Position(i)]; }
  308. BVUnit * BitsFromIndex(BVIndex i) { AssertRange(i); return &this->data[BVUnit::Position(i)]; }
  309. const BVUnit * BeginUnit() const { return &this->data[0]; }
  310. BVUnit * BeginUnit() { return &this->data[0]; }
  311. const BVUnit * EndUnit() const { return &this->data[wordCount]; }
  312. BVUnit * EndUnit() { return &this->data[wordCount]; }
  313. template<class Fn>
  314. inline void for_each(const BVStatic *bv2, const Fn callback)
  315. {
  316. BVUnit * i;
  317. const BVUnit * j;
  318. for(i = this->BeginUnit(), j = bv2->BeginUnit();
  319. i != this->EndUnit() ;
  320. i++, j++)
  321. {
  322. (i->*callback)(*j);
  323. }
  324. }
  325. template<class Fn>
  326. static bool MapUntil(const BVStatic *bv1, const BVStatic *bv2, const Fn callback)
  327. {
  328. const BVUnit * i;
  329. const BVUnit * j;
  330. for(i = bv1->BeginUnit(), j = bv2->BeginUnit();
  331. i != bv1->EndUnit() ;
  332. i++, j++)
  333. {
  334. if (!callback(*i, *j))
  335. {
  336. return false;
  337. }
  338. }
  339. return true;
  340. }
  341. void ClearEnd()
  342. {
  343. uint offset = BVUnit::Offset(bitCount);
  344. if (offset != 0)
  345. {
  346. this->data[wordCount - 1].And((1 << offset) - 1);
  347. }
  348. }
  349. // Methods
  350. public:
  351. void Set(BVIndex i)
  352. {
  353. AssertRange(i);
  354. this->BitsFromIndex(i)->Set(BVUnit::Offset(i));
  355. }
  356. void Clear(BVIndex i)
  357. {
  358. AssertRange(i);
  359. this->BitsFromIndex(i)->Clear(BVUnit::Offset(i));
  360. }
  361. void Compliment(BVIndex i)
  362. {
  363. AssertRange(i);
  364. this->BitsFromIndex(i)->Complement(BVUnit::Offset(i));
  365. }
  366. BOOLEAN Equal(BVStatic<bitCount> const * o)
  367. {
  368. return MapUntil(this, o, [](BVUnit const& i, BVUnit const &j) { return i.Equal(j); });
  369. }
  370. BOOLEAN Test(BVIndex i) const
  371. {
  372. AssertRange(i);
  373. return this->BitsFromIndex(i)->Test(BVUnit::Offset(i));
  374. }
  375. BOOLEAN TestAndSet(BVIndex i)
  376. {
  377. AssertRange(i);
  378. return PlatformAgnostic::_BitTestAndSet((LONG *)this->data, (LONG) i);
  379. }
  380. BOOLEAN TestIntrinsic(BVIndex i) const
  381. {
  382. AssertRange(i);
  383. return PlatformAgnostic::_BitTest((LONG *)this->data, (LONG) i);
  384. }
  385. BOOLEAN TestAndSetInterlocked(BVIndex i)
  386. {
  387. AssertRange(i);
  388. return PlatformAgnostic::_InterlockedBitTestAndSet((LONG *)this->data, (LONG) i);
  389. }
  390. BOOLEAN TestAndClear(BVIndex i)
  391. {
  392. AssertRange(i);
  393. BVUnit * bvUnit = this->BitsFromIndex(i);
  394. BVIndex offset = BVUnit::Offset(i);
  395. BOOLEAN bit = bvUnit->Test(offset);
  396. bvUnit->Clear(offset);
  397. return bit;
  398. }
  399. BOOLEAN TestAndClearInterlocked(BVIndex i)
  400. {
  401. AssertRange(i);
  402. return PlatformAgnostic::_InterlockedBitTestAndReset((LONG *)this->data, (LONG)i);
  403. }
  404. void OrComplimented(const BVStatic * bv) { this->for_each(bv, &BVUnit::OrComplimented); ClearEnd(); }
  405. void Or(const BVStatic *bv) { this->for_each(bv, &BVUnit::Or); }
  406. void And(const BVStatic *bv) { this->for_each(bv, &BVUnit::And); }
  407. void Minus(const BVStatic *bv) { this->for_each(bv, &BVUnit::Minus); }
  408. void Copy(const BVStatic *bv) { js_memcpy_s(&this->data[0], wordCount * sizeof(BVUnit), &bv->data[0], wordCount * sizeof(BVUnit)); }
  409. void SetAll() { memset(&this->data[0], -1, wordCount * sizeof(BVUnit)); ClearEnd(); }
  410. void ClearAll() { memset(&this->data[0], 0, wordCount * sizeof(BVUnit)); }
  411. void ComplimentAll()
  412. {
  413. for (BVIndex i = 0; i < wordCount; i++)
  414. {
  415. this->data[i].ComplimentAll();
  416. }
  417. ClearEnd();
  418. }
  419. BVIndex Count() const
  420. {
  421. BVIndex sum = 0;
  422. for (BVIndex i = 0; i < wordCount; i++)
  423. {
  424. sum += this->data[i].Count();
  425. }
  426. Assert(sum <= bitCount);
  427. return sum;
  428. }
  429. BVIndex Length() const
  430. {
  431. return bitCount;
  432. }
  433. JsUtil::FBVEnumerator BeginSetBits() { return JsUtil::FBVEnumerator(this->BeginUnit(), this->EndUnit()); }
  434. BVIndex GetNextBit(BVIndex i) const
  435. {
  436. AssertRange(i);
  437. const BVUnit * chunk = BitsFromIndex(i);
  438. BVIndex base = BVUnit::Floor(i);
  439. BVIndex offset = chunk->GetNextBit(BVUnit::Offset(i));
  440. if (-1 != offset)
  441. {
  442. return base + offset;
  443. }
  444. while (++chunk != this->EndUnit())
  445. {
  446. base += BVUnit::BitsPerWord;
  447. offset = chunk->GetNextBit();
  448. if (-1 != offset)
  449. {
  450. return base + offset;
  451. }
  452. }
  453. return BVInvalidIndex;
  454. }
  455. const BVUnit * GetRawData() const { return data; }
  456. template <size_t rangeSize>
  457. BVStatic<rangeSize> * GetRange(BVIndex startOffset)
  458. {
  459. AssertRange(startOffset);
  460. AssertRange(startOffset + rangeSize - 1);
  461. // Start offset and size must be word-aligned
  462. Assert(BVUnit::Offset(startOffset) == 0);
  463. Assert(BVUnit::Offset(rangeSize) == 0);
  464. return (BVStatic<rangeSize> *)BitsFromIndex(startOffset);
  465. }
  466. BOOLEAN TestRange(const BVIndex index, uint length) const
  467. {
  468. AssertRange(index);
  469. AssertRange(index + length - 1);
  470. const BVUnit * bvUnit = BitsFromIndex(index);
  471. uint offset = BVUnit::Offset(index);
  472. if (offset + length <= BVUnit::BitsPerWord)
  473. {
  474. // Bit range is in a single word
  475. return bvUnit->TestRange(offset, length);
  476. }
  477. // Bit range spans words.
  478. // Test the first word, from start offset to end of word
  479. if (!bvUnit->TestRange(offset, (BVUnit::BitsPerWord - offset)))
  480. {
  481. return FALSE;
  482. }
  483. bvUnit++;
  484. length -= (BVUnit::BitsPerWord - offset);
  485. // Test entire words until we are at the last word
  486. while (length >= BVUnit::BitsPerWord)
  487. {
  488. if (!bvUnit->IsFull())
  489. {
  490. return FALSE;
  491. }
  492. bvUnit++;
  493. length -= BVUnit::BitsPerWord;
  494. }
  495. // Test last word (unless we already ended on a word boundary)
  496. if (length > 0)
  497. {
  498. if (!bvUnit->TestRange(0, length))
  499. {
  500. return FALSE;
  501. }
  502. }
  503. return TRUE;
  504. }
  505. void SetRange(const BVIndex index, uint length)
  506. {
  507. AssertRange(index);
  508. AssertRange(index + length - 1);
  509. BVUnit * bvUnit = BitsFromIndex(index);
  510. uint offset = BVUnit::Offset(index);
  511. if (offset + length <= BVUnit::BitsPerWord)
  512. {
  513. // Bit range is in a single word
  514. return bvUnit->SetRange(offset, length);
  515. }
  516. // Bit range spans words.
  517. // Set the first word, from start offset to end of word
  518. bvUnit->SetRange(offset, (BVUnit::BitsPerWord - offset));
  519. bvUnit++;
  520. length -= (BVUnit::BitsPerWord - offset);
  521. // Set entire words until we are at the last word
  522. while (length >= BVUnit::BitsPerWord)
  523. {
  524. bvUnit->SetAll();
  525. bvUnit++;
  526. length -= BVUnit::BitsPerWord;
  527. }
  528. // Set last word (unless we already ended on a word boundary)
  529. if (length > 0)
  530. {
  531. bvUnit->SetRange(0, length);
  532. }
  533. }
  534. void ClearRange(const BVIndex index, uint length)
  535. {
  536. AssertRange(index);
  537. AssertRange(index + length - 1);
  538. BVUnit * bvUnit = BitsFromIndex(index);
  539. uint offset = BVUnit::Offset(index);
  540. if (offset + length <= BVUnit::BitsPerWord)
  541. {
  542. // Bit range is in a single word
  543. return bvUnit->ClearRange(offset, length);
  544. }
  545. // Bit range spans words.
  546. // Clear the first word, from start offset to end of word
  547. bvUnit->ClearRange(offset, (BVUnit::BitsPerWord - offset));
  548. bvUnit++;
  549. length -= (BVUnit::BitsPerWord - offset);
  550. // Set entire words until we are at the last word
  551. while (length >= BVUnit::BitsPerWord)
  552. {
  553. bvUnit->ClearAll();
  554. bvUnit++;
  555. length -= BVUnit::BitsPerWord;
  556. }
  557. // Set last word (unless we already ended on a word boundary)
  558. if (length > 0)
  559. {
  560. bvUnit->ClearRange(0, length);
  561. }
  562. }
  563. bool IsAllClear()
  564. {
  565. for (BVIndex i = 0; i < wordCount; i++)
  566. {
  567. if (!this->data[i].IsEmpty())
  568. {
  569. return false;
  570. }
  571. }
  572. return true;
  573. }
  574. #if DBG_DUMP
  575. void Dump() const
  576. {
  577. bool hasBits = false;
  578. Output::Print(_u("[ "));
  579. for (BVIndex i = 0; i < wordCount; i++)
  580. {
  581. hasBits = this->data[i].Dump(i * BVUnit::BitsPerWord, hasBits);
  582. }
  583. Output::Print(_u("]\n"));
  584. }
  585. #endif
  586. };