SparseBitVector.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the SparseBitVector class. See the doxygen comment for
  15. // SparseBitVector for more details on the algorithm used.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_SPARSEBITVECTOR_H
  19. #define LLVM_ADT_SPARSEBITVECTOR_H
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/MathExtras.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <cassert>
  24. #include <climits>
  25. #include <cstring>
  26. #include <iterator>
  27. #include <list>
  28. namespace llvm {
  29. /// SparseBitVector is an implementation of a bitvector that is sparse by only
  30. /// storing the elements that have non-zero bits set. In order to make this
  31. /// fast for the most common cases, SparseBitVector is implemented as a linked
  32. /// list of SparseBitVectorElements. We maintain a pointer to the last
  33. /// SparseBitVectorElement accessed (in the form of a list iterator), in order
  34. /// to make multiple in-order test/set constant time after the first one is
  35. /// executed. Note that using vectors to store SparseBitVectorElement's does
  36. /// not work out very well because it causes insertion in the middle to take
  37. /// enormous amounts of time with a large amount of bits. Other structures that
  38. /// have better worst cases for insertion in the middle (various balanced trees,
  39. /// etc) do not perform as well in practice as a linked list with this iterator
  40. /// kept up to date. They are also significantly more memory intensive.
  41. template <unsigned ElementSize = 128> struct SparseBitVectorElement {
  42. public:
  43. using BitWord = unsigned long;
  44. using size_type = unsigned;
  45. enum {
  46. BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
  47. BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
  48. BITS_PER_ELEMENT = ElementSize
  49. };
  50. private:
  51. // Index of Element in terms of where first bit starts.
  52. unsigned ElementIndex;
  53. BitWord Bits[BITWORDS_PER_ELEMENT];
  54. SparseBitVectorElement() {
  55. ElementIndex = ~0U;
  56. memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
  57. }
  58. public:
  59. explicit SparseBitVectorElement(unsigned Idx) {
  60. ElementIndex = Idx;
  61. memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
  62. }
  63. // Comparison.
  64. bool operator==(const SparseBitVectorElement &RHS) const {
  65. if (ElementIndex != RHS.ElementIndex)
  66. return false;
  67. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
  68. if (Bits[i] != RHS.Bits[i])
  69. return false;
  70. return true;
  71. }
  72. bool operator!=(const SparseBitVectorElement &RHS) const {
  73. return !(*this == RHS);
  74. }
  75. // Return the bits that make up word Idx in our element.
  76. BitWord word(unsigned Idx) const {
  77. assert(Idx < BITWORDS_PER_ELEMENT);
  78. return Bits[Idx];
  79. }
  80. unsigned index() const {
  81. return ElementIndex;
  82. }
  83. bool empty() const {
  84. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
  85. if (Bits[i])
  86. return false;
  87. return true;
  88. }
  89. void set(unsigned Idx) {
  90. Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
  91. }
  92. bool test_and_set(unsigned Idx) {
  93. bool old = test(Idx);
  94. if (!old) {
  95. set(Idx);
  96. return true;
  97. }
  98. return false;
  99. }
  100. void reset(unsigned Idx) {
  101. Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
  102. }
  103. bool test(unsigned Idx) const {
  104. return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
  105. }
  106. size_type count() const {
  107. unsigned NumBits = 0;
  108. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
  109. NumBits += countPopulation(Bits[i]);
  110. return NumBits;
  111. }
  112. /// find_first - Returns the index of the first set bit.
  113. int find_first() const {
  114. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
  115. if (Bits[i] != 0)
  116. return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
  117. llvm_unreachable("Illegal empty element");
  118. }
  119. /// find_last - Returns the index of the last set bit.
  120. int find_last() const {
  121. for (unsigned I = 0; I < BITWORDS_PER_ELEMENT; ++I) {
  122. unsigned Idx = BITWORDS_PER_ELEMENT - I - 1;
  123. if (Bits[Idx] != 0)
  124. return Idx * BITWORD_SIZE + BITWORD_SIZE -
  125. countLeadingZeros(Bits[Idx]) - 1;
  126. }
  127. llvm_unreachable("Illegal empty element");
  128. }
  129. /// find_next - Returns the index of the next set bit starting from the
  130. /// "Curr" bit. Returns -1 if the next set bit is not found.
  131. int find_next(unsigned Curr) const {
  132. if (Curr >= BITS_PER_ELEMENT)
  133. return -1;
  134. unsigned WordPos = Curr / BITWORD_SIZE;
  135. unsigned BitPos = Curr % BITWORD_SIZE;
  136. BitWord Copy = Bits[WordPos];
  137. assert(WordPos <= BITWORDS_PER_ELEMENT
  138. && "Word Position outside of element");
  139. // Mask off previous bits.
  140. Copy &= ~0UL << BitPos;
  141. if (Copy != 0)
  142. return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
  143. // Check subsequent words.
  144. for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
  145. if (Bits[i] != 0)
  146. return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
  147. return -1;
  148. }
  149. // Union this element with RHS and return true if this one changed.
  150. bool unionWith(const SparseBitVectorElement &RHS) {
  151. bool changed = false;
  152. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
  153. BitWord old = changed ? 0 : Bits[i];
  154. Bits[i] |= RHS.Bits[i];
  155. if (!changed && old != Bits[i])
  156. changed = true;
  157. }
  158. return changed;
  159. }
  160. // Return true if we have any bits in common with RHS
  161. bool intersects(const SparseBitVectorElement &RHS) const {
  162. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
  163. if (RHS.Bits[i] & Bits[i])
  164. return true;
  165. }
  166. return false;
  167. }
  168. // Intersect this Element with RHS and return true if this one changed.
  169. // BecameZero is set to true if this element became all-zero bits.
  170. bool intersectWith(const SparseBitVectorElement &RHS,
  171. bool &BecameZero) {
  172. bool changed = false;
  173. bool allzero = true;
  174. BecameZero = false;
  175. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
  176. BitWord old = changed ? 0 : Bits[i];
  177. Bits[i] &= RHS.Bits[i];
  178. if (Bits[i] != 0)
  179. allzero = false;
  180. if (!changed && old != Bits[i])
  181. changed = true;
  182. }
  183. BecameZero = allzero;
  184. return changed;
  185. }
  186. // Intersect this Element with the complement of RHS and return true if this
  187. // one changed. BecameZero is set to true if this element became all-zero
  188. // bits.
  189. bool intersectWithComplement(const SparseBitVectorElement &RHS,
  190. bool &BecameZero) {
  191. bool changed = false;
  192. bool allzero = true;
  193. BecameZero = false;
  194. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
  195. BitWord old = changed ? 0 : Bits[i];
  196. Bits[i] &= ~RHS.Bits[i];
  197. if (Bits[i] != 0)
  198. allzero = false;
  199. if (!changed && old != Bits[i])
  200. changed = true;
  201. }
  202. BecameZero = allzero;
  203. return changed;
  204. }
  205. // Three argument version of intersectWithComplement that intersects
  206. // RHS1 & ~RHS2 into this element
  207. void intersectWithComplement(const SparseBitVectorElement &RHS1,
  208. const SparseBitVectorElement &RHS2,
  209. bool &BecameZero) {
  210. bool allzero = true;
  211. BecameZero = false;
  212. for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
  213. Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
  214. if (Bits[i] != 0)
  215. allzero = false;
  216. }
  217. BecameZero = allzero;
  218. }
  219. };
  220. template <unsigned ElementSize = 128>
  221. class SparseBitVector {
  222. using ElementList = std::list<SparseBitVectorElement<ElementSize>>;
  223. using ElementListIter = typename ElementList::iterator;
  224. using ElementListConstIter = typename ElementList::const_iterator;
  225. enum {
  226. BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
  227. };
  228. ElementList Elements;
  229. // Pointer to our current Element. This has no visible effect on the external
  230. // state of a SparseBitVector, it's just used to improve performance in the
  231. // common case of testing/modifying bits with similar indices.
  232. mutable ElementListIter CurrElementIter;
  233. // This is like std::lower_bound, except we do linear searching from the
  234. // current position.
  235. ElementListIter FindLowerBoundImpl(unsigned ElementIndex) const {
  236. // We cache a non-const iterator so we're forced to resort to const_cast to
  237. // get the begin/end in the case where 'this' is const. To avoid duplication
  238. // of code with the only difference being whether the const cast is present
  239. // 'this' is always const in this particular function and we sort out the
  240. // difference in FindLowerBound and FindLowerBoundConst.
  241. ElementListIter Begin =
  242. const_cast<SparseBitVector<ElementSize> *>(this)->Elements.begin();
  243. ElementListIter End =
  244. const_cast<SparseBitVector<ElementSize> *>(this)->Elements.end();
  245. if (Elements.empty()) {
  246. CurrElementIter = Begin;
  247. return CurrElementIter;
  248. }
  249. // Make sure our current iterator is valid.
  250. if (CurrElementIter == End)
  251. --CurrElementIter;
  252. // Search from our current iterator, either backwards or forwards,
  253. // depending on what element we are looking for.
  254. ElementListIter ElementIter = CurrElementIter;
  255. if (CurrElementIter->index() == ElementIndex) {
  256. return ElementIter;
  257. } else if (CurrElementIter->index() > ElementIndex) {
  258. while (ElementIter != Begin
  259. && ElementIter->index() > ElementIndex)
  260. --ElementIter;
  261. } else {
  262. while (ElementIter != End &&
  263. ElementIter->index() < ElementIndex)
  264. ++ElementIter;
  265. }
  266. CurrElementIter = ElementIter;
  267. return ElementIter;
  268. }
  269. ElementListConstIter FindLowerBoundConst(unsigned ElementIndex) const {
  270. return FindLowerBoundImpl(ElementIndex);
  271. }
  272. ElementListIter FindLowerBound(unsigned ElementIndex) {
  273. return FindLowerBoundImpl(ElementIndex);
  274. }
  275. // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
  276. // than it would be, in order to be efficient.
  277. class SparseBitVectorIterator {
  278. private:
  279. bool AtEnd;
  280. const SparseBitVector<ElementSize> *BitVector = nullptr;
  281. // Current element inside of bitmap.
  282. ElementListConstIter Iter;
  283. // Current bit number inside of our bitmap.
  284. unsigned BitNumber;
  285. // Current word number inside of our element.
  286. unsigned WordNumber;
  287. // Current bits from the element.
  288. typename SparseBitVectorElement<ElementSize>::BitWord Bits;
  289. // Move our iterator to the first non-zero bit in the bitmap.
  290. void AdvanceToFirstNonZero() {
  291. if (AtEnd)
  292. return;
  293. if (BitVector->Elements.empty()) {
  294. AtEnd = true;
  295. return;
  296. }
  297. Iter = BitVector->Elements.begin();
  298. BitNumber = Iter->index() * ElementSize;
  299. unsigned BitPos = Iter->find_first();
  300. BitNumber += BitPos;
  301. WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
  302. Bits = Iter->word(WordNumber);
  303. Bits >>= BitPos % BITWORD_SIZE;
  304. }
  305. // Move our iterator to the next non-zero bit.
  306. void AdvanceToNextNonZero() {
  307. if (AtEnd)
  308. return;
  309. while (Bits && !(Bits & 1)) {
  310. Bits >>= 1;
  311. BitNumber += 1;
  312. }
  313. // See if we ran out of Bits in this word.
  314. if (!Bits) {
  315. int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
  316. // If we ran out of set bits in this element, move to next element.
  317. if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
  318. ++Iter;
  319. WordNumber = 0;
  320. // We may run out of elements in the bitmap.
  321. if (Iter == BitVector->Elements.end()) {
  322. AtEnd = true;
  323. return;
  324. }
  325. // Set up for next non-zero word in bitmap.
  326. BitNumber = Iter->index() * ElementSize;
  327. NextSetBitNumber = Iter->find_first();
  328. BitNumber += NextSetBitNumber;
  329. WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
  330. Bits = Iter->word(WordNumber);
  331. Bits >>= NextSetBitNumber % BITWORD_SIZE;
  332. } else {
  333. WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
  334. Bits = Iter->word(WordNumber);
  335. Bits >>= NextSetBitNumber % BITWORD_SIZE;
  336. BitNumber = Iter->index() * ElementSize;
  337. BitNumber += NextSetBitNumber;
  338. }
  339. }
  340. }
  341. public:
  342. SparseBitVectorIterator() = default;
  343. SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
  344. bool end = false):BitVector(RHS) {
  345. Iter = BitVector->Elements.begin();
  346. BitNumber = 0;
  347. Bits = 0;
  348. WordNumber = ~0;
  349. AtEnd = end;
  350. AdvanceToFirstNonZero();
  351. }
  352. // Preincrement.
  353. inline SparseBitVectorIterator& operator++() {
  354. ++BitNumber;
  355. Bits >>= 1;
  356. AdvanceToNextNonZero();
  357. return *this;
  358. }
  359. // Postincrement.
  360. inline SparseBitVectorIterator operator++(int) {
  361. SparseBitVectorIterator tmp = *this;
  362. ++*this;
  363. return tmp;
  364. }
  365. // Return the current set bit number.
  366. unsigned operator*() const {
  367. return BitNumber;
  368. }
  369. bool operator==(const SparseBitVectorIterator &RHS) const {
  370. // If they are both at the end, ignore the rest of the fields.
  371. if (AtEnd && RHS.AtEnd)
  372. return true;
  373. // Otherwise they are the same if they have the same bit number and
  374. // bitmap.
  375. return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
  376. }
  377. bool operator!=(const SparseBitVectorIterator &RHS) const {
  378. return !(*this == RHS);
  379. }
  380. };
  381. public:
  382. using iterator = SparseBitVectorIterator;
  383. SparseBitVector() : Elements(), CurrElementIter(Elements.begin()) {}
  384. SparseBitVector(const SparseBitVector &RHS)
  385. : Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
  386. SparseBitVector(SparseBitVector &&RHS)
  387. : Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
  388. // Clear.
  389. void clear() {
  390. Elements.clear();
  391. }
  392. // Assignment
  393. SparseBitVector& operator=(const SparseBitVector& RHS) {
  394. if (this == &RHS)
  395. return *this;
  396. Elements = RHS.Elements;
  397. CurrElementIter = Elements.begin();
  398. return *this;
  399. }
  400. SparseBitVector &operator=(SparseBitVector &&RHS) {
  401. Elements = std::move(RHS.Elements);
  402. CurrElementIter = Elements.begin();
  403. return *this;
  404. }
  405. // Test, Reset, and Set a bit in the bitmap.
  406. bool test(unsigned Idx) const {
  407. if (Elements.empty())
  408. return false;
  409. unsigned ElementIndex = Idx / ElementSize;
  410. ElementListConstIter ElementIter = FindLowerBoundConst(ElementIndex);
  411. // If we can't find an element that is supposed to contain this bit, there
  412. // is nothing more to do.
  413. if (ElementIter == Elements.end() ||
  414. ElementIter->index() != ElementIndex)
  415. return false;
  416. return ElementIter->test(Idx % ElementSize);
  417. }
  418. void reset(unsigned Idx) {
  419. if (Elements.empty())
  420. return;
  421. unsigned ElementIndex = Idx / ElementSize;
  422. ElementListIter ElementIter = FindLowerBound(ElementIndex);
  423. // If we can't find an element that is supposed to contain this bit, there
  424. // is nothing more to do.
  425. if (ElementIter == Elements.end() ||
  426. ElementIter->index() != ElementIndex)
  427. return;
  428. ElementIter->reset(Idx % ElementSize);
  429. // When the element is zeroed out, delete it.
  430. if (ElementIter->empty()) {
  431. ++CurrElementIter;
  432. Elements.erase(ElementIter);
  433. }
  434. }
  435. void set(unsigned Idx) {
  436. unsigned ElementIndex = Idx / ElementSize;
  437. ElementListIter ElementIter;
  438. if (Elements.empty()) {
  439. ElementIter = Elements.emplace(Elements.end(), ElementIndex);
  440. } else {
  441. ElementIter = FindLowerBound(ElementIndex);
  442. if (ElementIter == Elements.end() ||
  443. ElementIter->index() != ElementIndex) {
  444. // We may have hit the beginning of our SparseBitVector, in which case,
  445. // we may need to insert right after this element, which requires moving
  446. // the current iterator forward one, because insert does insert before.
  447. if (ElementIter != Elements.end() &&
  448. ElementIter->index() < ElementIndex)
  449. ++ElementIter;
  450. ElementIter = Elements.emplace(ElementIter, ElementIndex);
  451. }
  452. }
  453. CurrElementIter = ElementIter;
  454. ElementIter->set(Idx % ElementSize);
  455. }
  456. bool test_and_set(unsigned Idx) {
  457. bool old = test(Idx);
  458. if (!old) {
  459. set(Idx);
  460. return true;
  461. }
  462. return false;
  463. }
  464. bool operator!=(const SparseBitVector &RHS) const {
  465. return !(*this == RHS);
  466. }
  467. bool operator==(const SparseBitVector &RHS) const {
  468. ElementListConstIter Iter1 = Elements.begin();
  469. ElementListConstIter Iter2 = RHS.Elements.begin();
  470. for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
  471. ++Iter1, ++Iter2) {
  472. if (*Iter1 != *Iter2)
  473. return false;
  474. }
  475. return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
  476. }
  477. // Union our bitmap with the RHS and return true if we changed.
  478. bool operator|=(const SparseBitVector &RHS) {
  479. if (this == &RHS)
  480. return false;
  481. bool changed = false;
  482. ElementListIter Iter1 = Elements.begin();
  483. ElementListConstIter Iter2 = RHS.Elements.begin();
  484. // If RHS is empty, we are done
  485. if (RHS.Elements.empty())
  486. return false;
  487. while (Iter2 != RHS.Elements.end()) {
  488. if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
  489. Elements.insert(Iter1, *Iter2);
  490. ++Iter2;
  491. changed = true;
  492. } else if (Iter1->index() == Iter2->index()) {
  493. changed |= Iter1->unionWith(*Iter2);
  494. ++Iter1;
  495. ++Iter2;
  496. } else {
  497. ++Iter1;
  498. }
  499. }
  500. CurrElementIter = Elements.begin();
  501. return changed;
  502. }
  503. // Intersect our bitmap with the RHS and return true if ours changed.
  504. bool operator&=(const SparseBitVector &RHS) {
  505. if (this == &RHS)
  506. return false;
  507. bool changed = false;
  508. ElementListIter Iter1 = Elements.begin();
  509. ElementListConstIter Iter2 = RHS.Elements.begin();
  510. // Check if both bitmaps are empty.
  511. if (Elements.empty() && RHS.Elements.empty())
  512. return false;
  513. // Loop through, intersecting as we go, erasing elements when necessary.
  514. while (Iter2 != RHS.Elements.end()) {
  515. if (Iter1 == Elements.end()) {
  516. CurrElementIter = Elements.begin();
  517. return changed;
  518. }
  519. if (Iter1->index() > Iter2->index()) {
  520. ++Iter2;
  521. } else if (Iter1->index() == Iter2->index()) {
  522. bool BecameZero;
  523. changed |= Iter1->intersectWith(*Iter2, BecameZero);
  524. if (BecameZero) {
  525. ElementListIter IterTmp = Iter1;
  526. ++Iter1;
  527. Elements.erase(IterTmp);
  528. } else {
  529. ++Iter1;
  530. }
  531. ++Iter2;
  532. } else {
  533. ElementListIter IterTmp = Iter1;
  534. ++Iter1;
  535. Elements.erase(IterTmp);
  536. changed = true;
  537. }
  538. }
  539. if (Iter1 != Elements.end()) {
  540. Elements.erase(Iter1, Elements.end());
  541. changed = true;
  542. }
  543. CurrElementIter = Elements.begin();
  544. return changed;
  545. }
  546. // Intersect our bitmap with the complement of the RHS and return true
  547. // if ours changed.
  548. bool intersectWithComplement(const SparseBitVector &RHS) {
  549. if (this == &RHS) {
  550. if (!empty()) {
  551. clear();
  552. return true;
  553. }
  554. return false;
  555. }
  556. bool changed = false;
  557. ElementListIter Iter1 = Elements.begin();
  558. ElementListConstIter Iter2 = RHS.Elements.begin();
  559. // If either our bitmap or RHS is empty, we are done
  560. if (Elements.empty() || RHS.Elements.empty())
  561. return false;
  562. // Loop through, intersecting as we go, erasing elements when necessary.
  563. while (Iter2 != RHS.Elements.end()) {
  564. if (Iter1 == Elements.end()) {
  565. CurrElementIter = Elements.begin();
  566. return changed;
  567. }
  568. if (Iter1->index() > Iter2->index()) {
  569. ++Iter2;
  570. } else if (Iter1->index() == Iter2->index()) {
  571. bool BecameZero;
  572. changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
  573. if (BecameZero) {
  574. ElementListIter IterTmp = Iter1;
  575. ++Iter1;
  576. Elements.erase(IterTmp);
  577. } else {
  578. ++Iter1;
  579. }
  580. ++Iter2;
  581. } else {
  582. ++Iter1;
  583. }
  584. }
  585. CurrElementIter = Elements.begin();
  586. return changed;
  587. }
  588. bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
  589. return intersectWithComplement(*RHS);
  590. }
  591. // Three argument version of intersectWithComplement.
  592. // Result of RHS1 & ~RHS2 is stored into this bitmap.
  593. void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
  594. const SparseBitVector<ElementSize> &RHS2)
  595. {
  596. if (this == &RHS1) {
  597. intersectWithComplement(RHS2);
  598. return;
  599. } else if (this == &RHS2) {
  600. SparseBitVector RHS2Copy(RHS2);
  601. intersectWithComplement(RHS1, RHS2Copy);
  602. return;
  603. }
  604. Elements.clear();
  605. CurrElementIter = Elements.begin();
  606. ElementListConstIter Iter1 = RHS1.Elements.begin();
  607. ElementListConstIter Iter2 = RHS2.Elements.begin();
  608. // If RHS1 is empty, we are done
  609. // If RHS2 is empty, we still have to copy RHS1
  610. if (RHS1.Elements.empty())
  611. return;
  612. // Loop through, intersecting as we go, erasing elements when necessary.
  613. while (Iter2 != RHS2.Elements.end()) {
  614. if (Iter1 == RHS1.Elements.end())
  615. return;
  616. if (Iter1->index() > Iter2->index()) {
  617. ++Iter2;
  618. } else if (Iter1->index() == Iter2->index()) {
  619. bool BecameZero = false;
  620. Elements.emplace_back(Iter1->index());
  621. Elements.back().intersectWithComplement(*Iter1, *Iter2, BecameZero);
  622. if (BecameZero)
  623. Elements.pop_back();
  624. ++Iter1;
  625. ++Iter2;
  626. } else {
  627. Elements.push_back(*Iter1++);
  628. }
  629. }
  630. // copy the remaining elements
  631. std::copy(Iter1, RHS1.Elements.end(), std::back_inserter(Elements));
  632. }
  633. void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
  634. const SparseBitVector<ElementSize> *RHS2) {
  635. intersectWithComplement(*RHS1, *RHS2);
  636. }
  637. bool intersects(const SparseBitVector<ElementSize> *RHS) const {
  638. return intersects(*RHS);
  639. }
  640. // Return true if we share any bits in common with RHS
  641. bool intersects(const SparseBitVector<ElementSize> &RHS) const {
  642. ElementListConstIter Iter1 = Elements.begin();
  643. ElementListConstIter Iter2 = RHS.Elements.begin();
  644. // Check if both bitmaps are empty.
  645. if (Elements.empty() && RHS.Elements.empty())
  646. return false;
  647. // Loop through, intersecting stopping when we hit bits in common.
  648. while (Iter2 != RHS.Elements.end()) {
  649. if (Iter1 == Elements.end())
  650. return false;
  651. if (Iter1->index() > Iter2->index()) {
  652. ++Iter2;
  653. } else if (Iter1->index() == Iter2->index()) {
  654. if (Iter1->intersects(*Iter2))
  655. return true;
  656. ++Iter1;
  657. ++Iter2;
  658. } else {
  659. ++Iter1;
  660. }
  661. }
  662. return false;
  663. }
  664. // Return true iff all bits set in this SparseBitVector are
  665. // also set in RHS.
  666. bool contains(const SparseBitVector<ElementSize> &RHS) const {
  667. SparseBitVector<ElementSize> Result(*this);
  668. Result &= RHS;
  669. return (Result == RHS);
  670. }
  671. // Return the first set bit in the bitmap. Return -1 if no bits are set.
  672. int find_first() const {
  673. if (Elements.empty())
  674. return -1;
  675. const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
  676. return (First.index() * ElementSize) + First.find_first();
  677. }
  678. // Return the last set bit in the bitmap. Return -1 if no bits are set.
  679. int find_last() const {
  680. if (Elements.empty())
  681. return -1;
  682. const SparseBitVectorElement<ElementSize> &Last = *(Elements.rbegin());
  683. return (Last.index() * ElementSize) + Last.find_last();
  684. }
  685. // Return true if the SparseBitVector is empty
  686. bool empty() const {
  687. return Elements.empty();
  688. }
  689. unsigned count() const {
  690. unsigned BitCount = 0;
  691. for (ElementListConstIter Iter = Elements.begin();
  692. Iter != Elements.end();
  693. ++Iter)
  694. BitCount += Iter->count();
  695. return BitCount;
  696. }
  697. iterator begin() const {
  698. return iterator(this);
  699. }
  700. iterator end() const {
  701. return iterator(this, true);
  702. }
  703. };
  704. // Convenience functions to allow Or and And without dereferencing in the user
  705. // code.
  706. template <unsigned ElementSize>
  707. inline bool operator |=(SparseBitVector<ElementSize> &LHS,
  708. const SparseBitVector<ElementSize> *RHS) {
  709. return LHS |= *RHS;
  710. }
  711. template <unsigned ElementSize>
  712. inline bool operator |=(SparseBitVector<ElementSize> *LHS,
  713. const SparseBitVector<ElementSize> &RHS) {
  714. return LHS->operator|=(RHS);
  715. }
  716. template <unsigned ElementSize>
  717. inline bool operator &=(SparseBitVector<ElementSize> *LHS,
  718. const SparseBitVector<ElementSize> &RHS) {
  719. return LHS->operator&=(RHS);
  720. }
  721. template <unsigned ElementSize>
  722. inline bool operator &=(SparseBitVector<ElementSize> &LHS,
  723. const SparseBitVector<ElementSize> *RHS) {
  724. return LHS &= *RHS;
  725. }
  726. // Convenience functions for infix union, intersection, difference operators.
  727. template <unsigned ElementSize>
  728. inline SparseBitVector<ElementSize>
  729. operator|(const SparseBitVector<ElementSize> &LHS,
  730. const SparseBitVector<ElementSize> &RHS) {
  731. SparseBitVector<ElementSize> Result(LHS);
  732. Result |= RHS;
  733. return Result;
  734. }
  735. template <unsigned ElementSize>
  736. inline SparseBitVector<ElementSize>
  737. operator&(const SparseBitVector<ElementSize> &LHS,
  738. const SparseBitVector<ElementSize> &RHS) {
  739. SparseBitVector<ElementSize> Result(LHS);
  740. Result &= RHS;
  741. return Result;
  742. }
  743. template <unsigned ElementSize>
  744. inline SparseBitVector<ElementSize>
  745. operator-(const SparseBitVector<ElementSize> &LHS,
  746. const SparseBitVector<ElementSize> &RHS) {
  747. SparseBitVector<ElementSize> Result;
  748. Result.intersectWithComplement(LHS, RHS);
  749. return Result;
  750. }
  751. // Dump a SparseBitVector to a stream
  752. template <unsigned ElementSize>
  753. void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
  754. out << "[";
  755. typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
  756. be = LHS.end();
  757. if (bi != be) {
  758. out << *bi;
  759. for (++bi; bi != be; ++bi) {
  760. out << " " << *bi;
  761. }
  762. }
  763. out << "]\n";
  764. }
  765. } // end namespace llvm
  766. #endif // LLVM_ADT_SPARSEBITVECTOR_H
  767. #ifdef __GNUC__
  768. #pragma GCC diagnostic pop
  769. #endif