|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef jit_BitSet_h |
|
8 #define jit_BitSet_h |
|
9 |
|
10 #include "mozilla/MathAlgorithms.h" |
|
11 |
|
12 #include "jit/IonAllocPolicy.h" |
|
13 |
|
14 namespace js { |
|
15 namespace jit { |
|
16 |
|
17 // Provides constant time set insertion and removal, and fast linear |
|
18 // set operations such as intersection, difference, and union. |
|
19 // N.B. All set operations must be performed on sets with the same number |
|
20 // of bits. |
|
21 class BitSet : private TempObject |
|
22 { |
|
23 public: |
|
24 static const size_t BitsPerWord = 8 * sizeof(uint32_t); |
|
25 |
|
26 static size_t RawLengthForBits(size_t bits) { |
|
27 return (bits + BitsPerWord - 1) / BitsPerWord; |
|
28 } |
|
29 |
|
30 private: |
|
31 BitSet(unsigned int numBits) : |
|
32 bits_(nullptr), |
|
33 numBits_(numBits) {} |
|
34 |
|
35 uint32_t *bits_; |
|
36 const unsigned int numBits_; |
|
37 |
|
38 static inline uint32_t bitForValue(unsigned int value) { |
|
39 return 1l << uint32_t(value % BitsPerWord); |
|
40 } |
|
41 |
|
42 static inline unsigned int wordForValue(unsigned int value) { |
|
43 return value / BitsPerWord; |
|
44 } |
|
45 |
|
46 inline unsigned int numWords() const { |
|
47 return RawLengthForBits(numBits_); |
|
48 } |
|
49 |
|
50 bool init(TempAllocator &alloc); |
|
51 |
|
52 public: |
|
53 class Iterator; |
|
54 |
|
55 static BitSet *New(TempAllocator &alloc, unsigned int numBits); |
|
56 |
|
57 unsigned int getNumBits() const { |
|
58 return numBits_; |
|
59 } |
|
60 |
|
61 // O(1): Check if this set contains the given value. |
|
62 bool contains(unsigned int value) const { |
|
63 JS_ASSERT(bits_); |
|
64 JS_ASSERT(value < numBits_); |
|
65 |
|
66 return !!(bits_[wordForValue(value)] & bitForValue(value)); |
|
67 } |
|
68 |
|
69 // O(numBits): Check if this set contains any value. |
|
70 bool empty() const; |
|
71 |
|
72 // O(1): Insert the given value into this set. |
|
73 void insert(unsigned int value) { |
|
74 JS_ASSERT(bits_); |
|
75 JS_ASSERT(value < numBits_); |
|
76 |
|
77 bits_[wordForValue(value)] |= bitForValue(value); |
|
78 } |
|
79 |
|
80 // O(numBits): Insert every element of the given set into this set. |
|
81 void insertAll(const BitSet *other); |
|
82 |
|
83 // O(1): Remove the given value from this set. |
|
84 void remove(unsigned int value) { |
|
85 JS_ASSERT(bits_); |
|
86 JS_ASSERT(value < numBits_); |
|
87 |
|
88 bits_[wordForValue(value)] &= ~bitForValue(value); |
|
89 } |
|
90 |
|
91 // O(numBits): Remove the every element of the given set from this set. |
|
92 void removeAll(const BitSet *other); |
|
93 |
|
94 // O(numBits): Intersect this set with the given set. |
|
95 void intersect(const BitSet *other); |
|
96 |
|
97 // O(numBits): Intersect this set with the given set; return whether the |
|
98 // intersection caused the set to change. |
|
99 bool fixedPointIntersect(const BitSet *other); |
|
100 |
|
101 // O(numBits): Does inplace complement of the set. |
|
102 void complement(); |
|
103 |
|
104 // O(numBits): Clear this set. |
|
105 void clear(); |
|
106 |
|
107 uint32_t *raw() const { |
|
108 return bits_; |
|
109 } |
|
110 size_t rawLength() const { |
|
111 return numWords(); |
|
112 } |
|
113 }; |
|
114 |
|
115 class BitSet::Iterator |
|
116 { |
|
117 private: |
|
118 BitSet &set_; |
|
119 unsigned index_; |
|
120 unsigned word_; |
|
121 uint32_t value_; |
|
122 |
|
123 void skipEmpty() { |
|
124 // Skip words containing only zeros. |
|
125 unsigned numWords = set_.numWords(); |
|
126 const uint32_t *bits = set_.bits_; |
|
127 while (value_ == 0) { |
|
128 word_++; |
|
129 if (word_ == numWords) |
|
130 return; |
|
131 |
|
132 JS_STATIC_ASSERT(sizeof(value_) * 8 == BitSet::BitsPerWord); |
|
133 index_ = word_ * sizeof(value_) * 8; |
|
134 value_ = bits[word_]; |
|
135 } |
|
136 |
|
137 // Be careful: the result of CountTrailingZeroes32 is undefined if the |
|
138 // input is 0. |
|
139 int numZeros = mozilla::CountTrailingZeroes32(value_); |
|
140 index_ += numZeros; |
|
141 value_ >>= numZeros; |
|
142 |
|
143 JS_ASSERT_IF(index_ < set_.numBits_, set_.contains(index_)); |
|
144 } |
|
145 |
|
146 public: |
|
147 Iterator(BitSet &set) : |
|
148 set_(set), |
|
149 index_(0), |
|
150 word_(0), |
|
151 value_(set.bits_[0]) |
|
152 { |
|
153 skipEmpty(); |
|
154 } |
|
155 |
|
156 inline bool more() const { |
|
157 return word_ < set_.numWords(); |
|
158 } |
|
159 inline operator bool() const { |
|
160 return more(); |
|
161 } |
|
162 |
|
163 inline Iterator& operator++(int dummy) { |
|
164 JS_ASSERT(more()); |
|
165 JS_ASSERT(index_ < set_.numBits_); |
|
166 |
|
167 index_++; |
|
168 value_ >>= 1; |
|
169 |
|
170 skipEmpty(); |
|
171 return *this; |
|
172 } |
|
173 |
|
174 unsigned int operator *() { |
|
175 JS_ASSERT(index_ < set_.numBits_); |
|
176 return index_; |
|
177 } |
|
178 }; |
|
179 |
|
180 } |
|
181 } |
|
182 |
|
183 #endif /* jit_BitSet_h */ |