js/src/jit/BitSet.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial