[C++11] More 'nullptr' conversion or in some cases just using a boolean check instead...
[oota-llvm.git] / include / llvm / ADT / BitVector.h
1 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- 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 implements the BitVector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
16
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MathExtras.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <climits>
23 #include <cstdlib>
24
25 namespace llvm {
26
27 class BitVector {
28   typedef unsigned long BitWord;
29
30   enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
31
32   BitWord  *Bits;        // Actual bits.
33   unsigned Size;         // Size of bitvector in bits.
34   unsigned Capacity;     // Size of allocated memory in BitWord.
35
36 public:
37   // Encapsulation of a single bit.
38   class reference {
39     friend class BitVector;
40
41     BitWord *WordRef;
42     unsigned BitPos;
43
44     reference();  // Undefined
45
46   public:
47     reference(BitVector &b, unsigned Idx) {
48       WordRef = &b.Bits[Idx / BITWORD_SIZE];
49       BitPos = Idx % BITWORD_SIZE;
50     }
51
52     ~reference() {}
53
54     reference &operator=(reference t) {
55       *this = bool(t);
56       return *this;
57     }
58
59     reference& operator=(bool t) {
60       if (t)
61         *WordRef |= BitWord(1) << BitPos;
62       else
63         *WordRef &= ~(BitWord(1) << BitPos);
64       return *this;
65     }
66
67     operator bool() const {
68       return ((*WordRef) & (BitWord(1) << BitPos)) ? true : false;
69     }
70   };
71
72
73   /// BitVector default ctor - Creates an empty bitvector.
74   BitVector() : Size(0), Capacity(0) {
75     Bits = 0;
76   }
77
78   /// BitVector ctor - Creates a bitvector of specified number of bits. All
79   /// bits are initialized to the specified value.
80   explicit BitVector(unsigned s, bool t = false) : Size(s) {
81     Capacity = NumBitWords(s);
82     Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
83     init_words(Bits, Capacity, t);
84     if (t)
85       clear_unused_bits();
86   }
87
88   /// BitVector copy ctor.
89   BitVector(const BitVector &RHS) : Size(RHS.size()) {
90     if (Size == 0) {
91       Bits = 0;
92       Capacity = 0;
93       return;
94     }
95
96     Capacity = NumBitWords(RHS.size());
97     Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
98     std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
99   }
100
101   BitVector(BitVector &&RHS)
102     : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
103     RHS.Bits = 0;
104   }
105
106   ~BitVector() {
107     std::free(Bits);
108   }
109
110   /// empty - Tests whether there are no bits in this bitvector.
111   bool empty() const { return Size == 0; }
112
113   /// size - Returns the number of bits in this bitvector.
114   unsigned size() const { return Size; }
115
116   /// count - Returns the number of bits which are set.
117   unsigned count() const {
118     unsigned NumBits = 0;
119     for (unsigned i = 0; i < NumBitWords(size()); ++i)
120       if (sizeof(BitWord) == 4)
121         NumBits += CountPopulation_32((uint32_t)Bits[i]);
122       else if (sizeof(BitWord) == 8)
123         NumBits += CountPopulation_64(Bits[i]);
124       else
125         llvm_unreachable("Unsupported!");
126     return NumBits;
127   }
128
129   /// any - Returns true if any bit is set.
130   bool any() const {
131     for (unsigned i = 0; i < NumBitWords(size()); ++i)
132       if (Bits[i] != 0)
133         return true;
134     return false;
135   }
136
137   /// all - Returns true if all bits are set.
138   bool all() const {
139     for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
140       if (Bits[i] != ~0UL)
141         return false;
142
143     // If bits remain check that they are ones. The unused bits are always zero.
144     if (unsigned Remainder = Size % BITWORD_SIZE)
145       return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
146
147     return true;
148   }
149
150   /// none - Returns true if none of the bits are set.
151   bool none() const {
152     return !any();
153   }
154
155   /// find_first - Returns the index of the first set bit, -1 if none
156   /// of the bits are set.
157   int find_first() const {
158     for (unsigned i = 0; i < NumBitWords(size()); ++i)
159       if (Bits[i] != 0) {
160         if (sizeof(BitWord) == 4)
161           return i * BITWORD_SIZE + countTrailingZeros((uint32_t)Bits[i]);
162         if (sizeof(BitWord) == 8)
163           return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
164         llvm_unreachable("Unsupported!");
165       }
166     return -1;
167   }
168
169   /// find_next - Returns the index of the next set bit following the
170   /// "Prev" bit. Returns -1 if the next set bit is not found.
171   int find_next(unsigned Prev) const {
172     ++Prev;
173     if (Prev >= Size)
174       return -1;
175
176     unsigned WordPos = Prev / BITWORD_SIZE;
177     unsigned BitPos = Prev % BITWORD_SIZE;
178     BitWord Copy = Bits[WordPos];
179     // Mask off previous bits.
180     Copy &= ~0UL << BitPos;
181
182     if (Copy != 0) {
183       if (sizeof(BitWord) == 4)
184         return WordPos * BITWORD_SIZE + countTrailingZeros((uint32_t)Copy);
185       if (sizeof(BitWord) == 8)
186         return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
187       llvm_unreachable("Unsupported!");
188     }
189
190     // Check subsequent words.
191     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
192       if (Bits[i] != 0) {
193         if (sizeof(BitWord) == 4)
194           return i * BITWORD_SIZE + countTrailingZeros((uint32_t)Bits[i]);
195         if (sizeof(BitWord) == 8)
196           return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
197         llvm_unreachable("Unsupported!");
198       }
199     return -1;
200   }
201
202   /// clear - Clear all bits.
203   void clear() {
204     Size = 0;
205   }
206
207   /// resize - Grow or shrink the bitvector.
208   void resize(unsigned N, bool t = false) {
209     if (N > Capacity * BITWORD_SIZE) {
210       unsigned OldCapacity = Capacity;
211       grow(N);
212       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
213     }
214
215     // Set any old unused bits that are now included in the BitVector. This
216     // may set bits that are not included in the new vector, but we will clear
217     // them back out below.
218     if (N > Size)
219       set_unused_bits(t);
220
221     // Update the size, and clear out any bits that are now unused
222     unsigned OldSize = Size;
223     Size = N;
224     if (t || N < OldSize)
225       clear_unused_bits();
226   }
227
228   void reserve(unsigned N) {
229     if (N > Capacity * BITWORD_SIZE)
230       grow(N);
231   }
232
233   // Set, reset, flip
234   BitVector &set() {
235     init_words(Bits, Capacity, true);
236     clear_unused_bits();
237     return *this;
238   }
239
240   BitVector &set(unsigned Idx) {
241     Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
242     return *this;
243   }
244
245   /// set - Efficiently set a range of bits in [I, E)
246   BitVector &set(unsigned I, unsigned E) {
247     assert(I <= E && "Attempted to set backwards range!");
248     assert(E <= size() && "Attempted to set out-of-bounds range!");
249
250     if (I == E) return *this;
251
252     if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
253       BitWord EMask = 1UL << (E % BITWORD_SIZE);
254       BitWord IMask = 1UL << (I % BITWORD_SIZE);
255       BitWord Mask = EMask - IMask;
256       Bits[I / BITWORD_SIZE] |= Mask;
257       return *this;
258     }
259
260     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
261     Bits[I / BITWORD_SIZE] |= PrefixMask;
262     I = RoundUpToAlignment(I, BITWORD_SIZE);
263
264     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
265       Bits[I / BITWORD_SIZE] = ~0UL;
266
267     BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
268     if (I < E)
269       Bits[I / BITWORD_SIZE] |= PostfixMask;
270
271     return *this;
272   }
273
274   BitVector &reset() {
275     init_words(Bits, Capacity, false);
276     return *this;
277   }
278
279   BitVector &reset(unsigned Idx) {
280     Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
281     return *this;
282   }
283
284   /// reset - Efficiently reset a range of bits in [I, E)
285   BitVector &reset(unsigned I, unsigned E) {
286     assert(I <= E && "Attempted to reset backwards range!");
287     assert(E <= size() && "Attempted to reset out-of-bounds range!");
288
289     if (I == E) return *this;
290
291     if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
292       BitWord EMask = 1UL << (E % BITWORD_SIZE);
293       BitWord IMask = 1UL << (I % BITWORD_SIZE);
294       BitWord Mask = EMask - IMask;
295       Bits[I / BITWORD_SIZE] &= ~Mask;
296       return *this;
297     }
298
299     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
300     Bits[I / BITWORD_SIZE] &= ~PrefixMask;
301     I = RoundUpToAlignment(I, BITWORD_SIZE);
302
303     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
304       Bits[I / BITWORD_SIZE] = 0UL;
305
306     BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
307     if (I < E)
308       Bits[I / BITWORD_SIZE] &= ~PostfixMask;
309
310     return *this;
311   }
312
313   BitVector &flip() {
314     for (unsigned i = 0; i < NumBitWords(size()); ++i)
315       Bits[i] = ~Bits[i];
316     clear_unused_bits();
317     return *this;
318   }
319
320   BitVector &flip(unsigned Idx) {
321     Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
322     return *this;
323   }
324
325   // Indexing.
326   reference operator[](unsigned Idx) {
327     assert (Idx < Size && "Out-of-bounds Bit access.");
328     return reference(*this, Idx);
329   }
330
331   bool operator[](unsigned Idx) const {
332     assert (Idx < Size && "Out-of-bounds Bit access.");
333     BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
334     return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
335   }
336
337   bool test(unsigned Idx) const {
338     return (*this)[Idx];
339   }
340
341   /// Test if any common bits are set.
342   bool anyCommon(const BitVector &RHS) const {
343     unsigned ThisWords = NumBitWords(size());
344     unsigned RHSWords  = NumBitWords(RHS.size());
345     for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
346       if (Bits[i] & RHS.Bits[i])
347         return true;
348     return false;
349   }
350
351   // Comparison operators.
352   bool operator==(const BitVector &RHS) const {
353     unsigned ThisWords = NumBitWords(size());
354     unsigned RHSWords  = NumBitWords(RHS.size());
355     unsigned i;
356     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
357       if (Bits[i] != RHS.Bits[i])
358         return false;
359
360     // Verify that any extra words are all zeros.
361     if (i != ThisWords) {
362       for (; i != ThisWords; ++i)
363         if (Bits[i])
364           return false;
365     } else if (i != RHSWords) {
366       for (; i != RHSWords; ++i)
367         if (RHS.Bits[i])
368           return false;
369     }
370     return true;
371   }
372
373   bool operator!=(const BitVector &RHS) const {
374     return !(*this == RHS);
375   }
376
377   /// Intersection, union, disjoint union.
378   BitVector &operator&=(const BitVector &RHS) {
379     unsigned ThisWords = NumBitWords(size());
380     unsigned RHSWords  = NumBitWords(RHS.size());
381     unsigned i;
382     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
383       Bits[i] &= RHS.Bits[i];
384
385     // Any bits that are just in this bitvector become zero, because they aren't
386     // in the RHS bit vector.  Any words only in RHS are ignored because they
387     // are already zero in the LHS.
388     for (; i != ThisWords; ++i)
389       Bits[i] = 0;
390
391     return *this;
392   }
393
394   /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
395   BitVector &reset(const BitVector &RHS) {
396     unsigned ThisWords = NumBitWords(size());
397     unsigned RHSWords  = NumBitWords(RHS.size());
398     unsigned i;
399     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
400       Bits[i] &= ~RHS.Bits[i];
401     return *this;
402   }
403
404   /// test - Check if (This - RHS) is zero.
405   /// This is the same as reset(RHS) and any().
406   bool test(const BitVector &RHS) const {
407     unsigned ThisWords = NumBitWords(size());
408     unsigned RHSWords  = NumBitWords(RHS.size());
409     unsigned i;
410     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
411       if ((Bits[i] & ~RHS.Bits[i]) != 0)
412         return true;
413
414     for (; i != ThisWords ; ++i)
415       if (Bits[i] != 0)
416         return true;
417
418     return false;
419   }
420
421   BitVector &operator|=(const BitVector &RHS) {
422     if (size() < RHS.size())
423       resize(RHS.size());
424     for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
425       Bits[i] |= RHS.Bits[i];
426     return *this;
427   }
428
429   BitVector &operator^=(const BitVector &RHS) {
430     if (size() < RHS.size())
431       resize(RHS.size());
432     for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
433       Bits[i] ^= RHS.Bits[i];
434     return *this;
435   }
436
437   // Assignment operator.
438   const BitVector &operator=(const BitVector &RHS) {
439     if (this == &RHS) return *this;
440
441     Size = RHS.size();
442     unsigned RHSWords = NumBitWords(Size);
443     if (Size <= Capacity * BITWORD_SIZE) {
444       if (Size)
445         std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
446       clear_unused_bits();
447       return *this;
448     }
449
450     // Grow the bitvector to have enough elements.
451     Capacity = RHSWords;
452     BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
453     std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
454
455     // Destroy the old bits.
456     std::free(Bits);
457     Bits = NewBits;
458
459     return *this;
460   }
461
462   const BitVector &operator=(BitVector &&RHS) {
463     if (this == &RHS) return *this;
464
465     std::free(Bits);
466     Bits = RHS.Bits;
467     Size = RHS.Size;
468     Capacity = RHS.Capacity;
469
470     RHS.Bits = 0;
471
472     return *this;
473   }
474
475   void swap(BitVector &RHS) {
476     std::swap(Bits, RHS.Bits);
477     std::swap(Size, RHS.Size);
478     std::swap(Capacity, RHS.Capacity);
479   }
480
481   //===--------------------------------------------------------------------===//
482   // Portable bit mask operations.
483   //===--------------------------------------------------------------------===//
484   //
485   // These methods all operate on arrays of uint32_t, each holding 32 bits. The
486   // fixed word size makes it easier to work with literal bit vector constants
487   // in portable code.
488   //
489   // The LSB in each word is the lowest numbered bit.  The size of a portable
490   // bit mask is always a whole multiple of 32 bits.  If no bit mask size is
491   // given, the bit mask is assumed to cover the entire BitVector.
492
493   /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
494   /// This computes "*this |= Mask".
495   void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
496     applyMask<true, false>(Mask, MaskWords);
497   }
498
499   /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
500   /// Don't resize. This computes "*this &= ~Mask".
501   void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
502     applyMask<false, false>(Mask, MaskWords);
503   }
504
505   /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
506   /// Don't resize.  This computes "*this |= ~Mask".
507   void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
508     applyMask<true, true>(Mask, MaskWords);
509   }
510
511   /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
512   /// Don't resize.  This computes "*this &= Mask".
513   void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
514     applyMask<false, true>(Mask, MaskWords);
515   }
516
517 private:
518   unsigned NumBitWords(unsigned S) const {
519     return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
520   }
521
522   // Set the unused bits in the high words.
523   void set_unused_bits(bool t = true) {
524     //  Set high words first.
525     unsigned UsedWords = NumBitWords(Size);
526     if (Capacity > UsedWords)
527       init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
528
529     //  Then set any stray high bits of the last used word.
530     unsigned ExtraBits = Size % BITWORD_SIZE;
531     if (ExtraBits) {
532       BitWord ExtraBitMask = ~0UL << ExtraBits;
533       if (t)
534         Bits[UsedWords-1] |= ExtraBitMask;
535       else
536         Bits[UsedWords-1] &= ~ExtraBitMask;
537     }
538   }
539
540   // Clear the unused bits in the high words.
541   void clear_unused_bits() {
542     set_unused_bits(false);
543   }
544
545   void grow(unsigned NewSize) {
546     Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
547     Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
548
549     clear_unused_bits();
550   }
551
552   void init_words(BitWord *B, unsigned NumWords, bool t) {
553     memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
554   }
555
556   template<bool AddBits, bool InvertMask>
557   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
558     assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
559     MaskWords = std::min(MaskWords, (size() + 31) / 32);
560     const unsigned Scale = BITWORD_SIZE / 32;
561     unsigned i;
562     for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
563       BitWord BW = Bits[i];
564       // This inner loop should unroll completely when BITWORD_SIZE > 32.
565       for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
566         uint32_t M = *Mask++;
567         if (InvertMask) M = ~M;
568         if (AddBits) BW |=   BitWord(M) << b;
569         else         BW &= ~(BitWord(M) << b);
570       }
571       Bits[i] = BW;
572     }
573     for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
574       uint32_t M = *Mask++;
575       if (InvertMask) M = ~M;
576       if (AddBits) Bits[i] |=   BitWord(M) << b;
577       else         Bits[i] &= ~(BitWord(M) << b);
578     }
579     if (AddBits)
580       clear_unused_bits();
581   }
582 };
583
584 } // End llvm namespace
585
586 namespace std {
587   /// Implement std::swap in terms of BitVector swap.
588   inline void
589   swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
590     LHS.swap(RHS);
591   }
592 }
593
594 #endif