FixedBitVector.h 19 KB

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