Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

897 lines
26 KiB

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