sanitizer_bitvector.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //===-- sanitizer_bitvector.h -----------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Specializer BitVector implementation.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #ifndef SANITIZER_BITVECTOR_H
  12. #define SANITIZER_BITVECTOR_H
  13. #include "sanitizer_common.h"
  14. namespace __sanitizer {
  15. // Fixed size bit vector based on a single basic integer.
  16. template <class basic_int_t = uptr>
  17. class BasicBitVector {
  18. public:
  19. enum SizeEnum { kSize = sizeof(basic_int_t) * 8 };
  20. uptr size() const { return kSize; }
  21. // No CTOR.
  22. void clear() { bits_ = 0; }
  23. void setAll() { bits_ = ~(basic_int_t)0; }
  24. bool empty() const { return bits_ == 0; }
  25. // Returns true if the bit has changed from 0 to 1.
  26. bool setBit(uptr idx) {
  27. basic_int_t old = bits_;
  28. bits_ |= mask(idx);
  29. return bits_ != old;
  30. }
  31. // Returns true if the bit has changed from 1 to 0.
  32. bool clearBit(uptr idx) {
  33. basic_int_t old = bits_;
  34. bits_ &= ~mask(idx);
  35. return bits_ != old;
  36. }
  37. bool getBit(uptr idx) const { return (bits_ & mask(idx)) != 0; }
  38. uptr getAndClearFirstOne() {
  39. CHECK(!empty());
  40. uptr idx = LeastSignificantSetBitIndex(bits_);
  41. clearBit(idx);
  42. return idx;
  43. }
  44. // Do "this |= v" and return whether new bits have been added.
  45. bool setUnion(const BasicBitVector &v) {
  46. basic_int_t old = bits_;
  47. bits_ |= v.bits_;
  48. return bits_ != old;
  49. }
  50. // Do "this &= v" and return whether any bits have been removed.
  51. bool setIntersection(const BasicBitVector &v) {
  52. basic_int_t old = bits_;
  53. bits_ &= v.bits_;
  54. return bits_ != old;
  55. }
  56. // Do "this &= ~v" and return whether any bits have been removed.
  57. bool setDifference(const BasicBitVector &v) {
  58. basic_int_t old = bits_;
  59. bits_ &= ~v.bits_;
  60. return bits_ != old;
  61. }
  62. void copyFrom(const BasicBitVector &v) { bits_ = v.bits_; }
  63. // Returns true if 'this' intersects with 'v'.
  64. bool intersectsWith(const BasicBitVector &v) const {
  65. return (bits_ & v.bits_) != 0;
  66. }
  67. // for (BasicBitVector<>::Iterator it(bv); it.hasNext();) {
  68. // uptr idx = it.next();
  69. // use(idx);
  70. // }
  71. class Iterator {
  72. public:
  73. Iterator() { }
  74. explicit Iterator(const BasicBitVector &bv) : bv_(bv) {}
  75. bool hasNext() const { return !bv_.empty(); }
  76. uptr next() { return bv_.getAndClearFirstOne(); }
  77. void clear() { bv_.clear(); }
  78. private:
  79. BasicBitVector bv_;
  80. };
  81. private:
  82. basic_int_t mask(uptr idx) const {
  83. CHECK_LT(idx, size());
  84. return (basic_int_t)1UL << idx;
  85. }
  86. basic_int_t bits_;
  87. };
  88. // Fixed size bit vector of (kLevel1Size*BV::kSize**2) bits.
  89. // The implementation is optimized for better performance on
  90. // sparse bit vectors, i.e. the those with few set bits.
  91. template <uptr kLevel1Size = 1, class BV = BasicBitVector<> >
  92. class TwoLevelBitVector {
  93. // This is essentially a 2-level bit vector.
  94. // Set bit in the first level BV indicates that there are set bits
  95. // in the corresponding BV of the second level.
  96. // This structure allows O(kLevel1Size) time for clear() and empty(),
  97. // as well fast handling of sparse BVs.
  98. public:
  99. enum SizeEnum { kSize = BV::kSize * BV::kSize * kLevel1Size };
  100. // No CTOR.
  101. uptr size() const { return kSize; }
  102. void clear() {
  103. for (uptr i = 0; i < kLevel1Size; i++)
  104. l1_[i].clear();
  105. }
  106. void setAll() {
  107. for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
  108. l1_[i0].setAll();
  109. for (uptr i1 = 0; i1 < BV::kSize; i1++)
  110. l2_[i0][i1].setAll();
  111. }
  112. }
  113. bool empty() const {
  114. for (uptr i = 0; i < kLevel1Size; i++)
  115. if (!l1_[i].empty())
  116. return false;
  117. return true;
  118. }
  119. // Returns true if the bit has changed from 0 to 1.
  120. bool setBit(uptr idx) {
  121. check(idx);
  122. uptr i0 = idx0(idx);
  123. uptr i1 = idx1(idx);
  124. uptr i2 = idx2(idx);
  125. if (!l1_[i0].getBit(i1)) {
  126. l1_[i0].setBit(i1);
  127. l2_[i0][i1].clear();
  128. }
  129. bool res = l2_[i0][i1].setBit(i2);
  130. // Printf("%s: %zd => %zd %zd %zd; %d\n", __func__,
  131. // idx, i0, i1, i2, res);
  132. return res;
  133. }
  134. bool clearBit(uptr idx) {
  135. check(idx);
  136. uptr i0 = idx0(idx);
  137. uptr i1 = idx1(idx);
  138. uptr i2 = idx2(idx);
  139. bool res = false;
  140. if (l1_[i0].getBit(i1)) {
  141. res = l2_[i0][i1].clearBit(i2);
  142. if (l2_[i0][i1].empty())
  143. l1_[i0].clearBit(i1);
  144. }
  145. return res;
  146. }
  147. bool getBit(uptr idx) const {
  148. check(idx);
  149. uptr i0 = idx0(idx);
  150. uptr i1 = idx1(idx);
  151. uptr i2 = idx2(idx);
  152. // Printf("%s: %zd => %zd %zd %zd\n", __func__, idx, i0, i1, i2);
  153. return l1_[i0].getBit(i1) && l2_[i0][i1].getBit(i2);
  154. }
  155. uptr getAndClearFirstOne() {
  156. for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
  157. if (l1_[i0].empty()) continue;
  158. uptr i1 = l1_[i0].getAndClearFirstOne();
  159. uptr i2 = l2_[i0][i1].getAndClearFirstOne();
  160. if (!l2_[i0][i1].empty())
  161. l1_[i0].setBit(i1);
  162. uptr res = i0 * BV::kSize * BV::kSize + i1 * BV::kSize + i2;
  163. // Printf("getAndClearFirstOne: %zd %zd %zd => %zd\n", i0, i1, i2, res);
  164. return res;
  165. }
  166. CHECK(0);
  167. return 0;
  168. }
  169. // Do "this |= v" and return whether new bits have been added.
  170. bool setUnion(const TwoLevelBitVector &v) {
  171. bool res = false;
  172. for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
  173. BV t = v.l1_[i0];
  174. while (!t.empty()) {
  175. uptr i1 = t.getAndClearFirstOne();
  176. if (l1_[i0].setBit(i1))
  177. l2_[i0][i1].clear();
  178. if (l2_[i0][i1].setUnion(v.l2_[i0][i1]))
  179. res = true;
  180. }
  181. }
  182. return res;
  183. }
  184. // Do "this &= v" and return whether any bits have been removed.
  185. bool setIntersection(const TwoLevelBitVector &v) {
  186. bool res = false;
  187. for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
  188. if (l1_[i0].setIntersection(v.l1_[i0]))
  189. res = true;
  190. if (!l1_[i0].empty()) {
  191. BV t = l1_[i0];
  192. while (!t.empty()) {
  193. uptr i1 = t.getAndClearFirstOne();
  194. if (l2_[i0][i1].setIntersection(v.l2_[i0][i1]))
  195. res = true;
  196. if (l2_[i0][i1].empty())
  197. l1_[i0].clearBit(i1);
  198. }
  199. }
  200. }
  201. return res;
  202. }
  203. // Do "this &= ~v" and return whether any bits have been removed.
  204. bool setDifference(const TwoLevelBitVector &v) {
  205. bool res = false;
  206. for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
  207. BV t = l1_[i0];
  208. t.setIntersection(v.l1_[i0]);
  209. while (!t.empty()) {
  210. uptr i1 = t.getAndClearFirstOne();
  211. if (l2_[i0][i1].setDifference(v.l2_[i0][i1]))
  212. res = true;
  213. if (l2_[i0][i1].empty())
  214. l1_[i0].clearBit(i1);
  215. }
  216. }
  217. return res;
  218. }
  219. void copyFrom(const TwoLevelBitVector &v) {
  220. clear();
  221. setUnion(v);
  222. }
  223. // Returns true if 'this' intersects with 'v'.
  224. bool intersectsWith(const TwoLevelBitVector &v) const {
  225. for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
  226. BV t = l1_[i0];
  227. t.setIntersection(v.l1_[i0]);
  228. while (!t.empty()) {
  229. uptr i1 = t.getAndClearFirstOne();
  230. if (!v.l1_[i0].getBit(i1)) continue;
  231. if (l2_[i0][i1].intersectsWith(v.l2_[i0][i1]))
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. // for (TwoLevelBitVector<>::Iterator it(bv); it.hasNext();) {
  238. // uptr idx = it.next();
  239. // use(idx);
  240. // }
  241. class Iterator {
  242. public:
  243. Iterator() { }
  244. explicit Iterator(const TwoLevelBitVector &bv) : bv_(bv), i0_(0), i1_(0) {
  245. it1_.clear();
  246. it2_.clear();
  247. }
  248. bool hasNext() const {
  249. if (it1_.hasNext()) return true;
  250. for (uptr i = i0_; i < kLevel1Size; i++)
  251. if (!bv_.l1_[i].empty()) return true;
  252. return false;
  253. }
  254. uptr next() {
  255. // Printf("++++: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
  256. // it2_.hasNext(), kSize);
  257. if (!it1_.hasNext() && !it2_.hasNext()) {
  258. for (; i0_ < kLevel1Size; i0_++) {
  259. if (bv_.l1_[i0_].empty()) continue;
  260. it1_ = typename BV::Iterator(bv_.l1_[i0_]);
  261. // Printf("+i0: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
  262. // it2_.hasNext(), kSize);
  263. break;
  264. }
  265. }
  266. if (!it2_.hasNext()) {
  267. CHECK(it1_.hasNext());
  268. i1_ = it1_.next();
  269. it2_ = typename BV::Iterator(bv_.l2_[i0_][i1_]);
  270. // Printf("++i1: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
  271. // it2_.hasNext(), kSize);
  272. }
  273. CHECK(it2_.hasNext());
  274. uptr i2 = it2_.next();
  275. uptr res = i0_ * BV::kSize * BV::kSize + i1_ * BV::kSize + i2;
  276. // Printf("+ret: %zd %zd; %d %d; size %zd; res: %zd\n", i0_, i1_,
  277. // it1_.hasNext(), it2_.hasNext(), kSize, res);
  278. if (!it1_.hasNext() && !it2_.hasNext())
  279. i0_++;
  280. return res;
  281. }
  282. private:
  283. const TwoLevelBitVector &bv_;
  284. uptr i0_, i1_;
  285. typename BV::Iterator it1_, it2_;
  286. };
  287. private:
  288. void check(uptr idx) const { CHECK_LE(idx, size()); }
  289. uptr idx0(uptr idx) const {
  290. uptr res = idx / (BV::kSize * BV::kSize);
  291. CHECK_LE(res, kLevel1Size);
  292. return res;
  293. }
  294. uptr idx1(uptr idx) const {
  295. uptr res = (idx / BV::kSize) % BV::kSize;
  296. CHECK_LE(res, BV::kSize);
  297. return res;
  298. }
  299. uptr idx2(uptr idx) const {
  300. uptr res = idx % BV::kSize;
  301. CHECK_LE(res, BV::kSize);
  302. return res;
  303. }
  304. BV l1_[kLevel1Size];
  305. BV l2_[kLevel1Size][BV::kSize];
  306. };
  307. } // namespace __sanitizer
  308. #endif // SANITIZER_BITVECTOR_H