FixedBitVector.h 21 KB

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