Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 #ifndef jit_BitSet_h
8 #define jit_BitSet_h
10 #include "mozilla/MathAlgorithms.h"
12 #include "jit/IonAllocPolicy.h"
14 namespace js {
15 namespace jit {
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);
26 static size_t RawLengthForBits(size_t bits) {
27 return (bits + BitsPerWord - 1) / BitsPerWord;
28 }
30 private:
31 BitSet(unsigned int numBits) :
32 bits_(nullptr),
33 numBits_(numBits) {}
35 uint32_t *bits_;
36 const unsigned int numBits_;
38 static inline uint32_t bitForValue(unsigned int value) {
39 return 1l << uint32_t(value % BitsPerWord);
40 }
42 static inline unsigned int wordForValue(unsigned int value) {
43 return value / BitsPerWord;
44 }
46 inline unsigned int numWords() const {
47 return RawLengthForBits(numBits_);
48 }
50 bool init(TempAllocator &alloc);
52 public:
53 class Iterator;
55 static BitSet *New(TempAllocator &alloc, unsigned int numBits);
57 unsigned int getNumBits() const {
58 return numBits_;
59 }
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_);
66 return !!(bits_[wordForValue(value)] & bitForValue(value));
67 }
69 // O(numBits): Check if this set contains any value.
70 bool empty() const;
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_);
77 bits_[wordForValue(value)] |= bitForValue(value);
78 }
80 // O(numBits): Insert every element of the given set into this set.
81 void insertAll(const BitSet *other);
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_);
88 bits_[wordForValue(value)] &= ~bitForValue(value);
89 }
91 // O(numBits): Remove the every element of the given set from this set.
92 void removeAll(const BitSet *other);
94 // O(numBits): Intersect this set with the given set.
95 void intersect(const BitSet *other);
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);
101 // O(numBits): Does inplace complement of the set.
102 void complement();
104 // O(numBits): Clear this set.
105 void clear();
107 uint32_t *raw() const {
108 return bits_;
109 }
110 size_t rawLength() const {
111 return numWords();
112 }
113 };
115 class BitSet::Iterator
116 {
117 private:
118 BitSet &set_;
119 unsigned index_;
120 unsigned word_;
121 uint32_t value_;
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;
132 JS_STATIC_ASSERT(sizeof(value_) * 8 == BitSet::BitsPerWord);
133 index_ = word_ * sizeof(value_) * 8;
134 value_ = bits[word_];
135 }
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;
143 JS_ASSERT_IF(index_ < set_.numBits_, set_.contains(index_));
144 }
146 public:
147 Iterator(BitSet &set) :
148 set_(set),
149 index_(0),
150 word_(0),
151 value_(set.bits_[0])
152 {
153 skipEmpty();
154 }
156 inline bool more() const {
157 return word_ < set_.numWords();
158 }
159 inline operator bool() const {
160 return more();
161 }
163 inline Iterator& operator++(int dummy) {
164 JS_ASSERT(more());
165 JS_ASSERT(index_ < set_.numBits_);
167 index_++;
168 value_ >>= 1;
170 skipEmpty();
171 return *this;
172 }
174 unsigned int operator *() {
175 JS_ASSERT(index_ < set_.numBits_);
176 return index_;
177 }
178 };
180 }
181 }
183 #endif /* jit_BitSet_h */