tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

BitSet.cpp (2627B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      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 #include "jit/BitSet.h"
      8 
      9 #include <string.h>
     10 
     11 #include "jit/JitAllocPolicy.h"
     12 
     13 using namespace js;
     14 using namespace js::jit;
     15 
     16 bool BitSet::init(TempAllocator& alloc) {
     17  size_t sizeRequired = numWords() * sizeof(*bits_);
     18 
     19  bits_ = (uint32_t*)alloc.allocate(sizeRequired);
     20  if (!bits_) {
     21    return false;
     22  }
     23 
     24  memset(bits_, 0, sizeRequired);
     25 
     26  return true;
     27 }
     28 
     29 bool BitSet::empty() const {
     30  MOZ_ASSERT(bits_);
     31  const uint32_t* bits = bits_;
     32  for (unsigned int i = 0, e = numWords(); i < e; i++) {
     33    if (bits[i]) {
     34      return false;
     35    }
     36  }
     37  return true;
     38 }
     39 
     40 void BitSet::insertAll(const BitSet& other) {
     41  MOZ_ASSERT(bits_);
     42  MOZ_ASSERT(other.numBits_ == numBits_);
     43  MOZ_ASSERT(other.bits_);
     44 
     45  uint32_t* bits = bits_;
     46  const uint32_t* otherBits = other.bits_;
     47  for (unsigned int i = 0, e = numWords(); i < e; i++) {
     48    bits[i] |= otherBits[i];
     49  }
     50 }
     51 
     52 void BitSet::removeAll(const BitSet& other) {
     53  MOZ_ASSERT(bits_);
     54  MOZ_ASSERT(other.numBits_ == numBits_);
     55  MOZ_ASSERT(other.bits_);
     56 
     57  uint32_t* bits = bits_;
     58  const uint32_t* otherBits = other.bits_;
     59  for (unsigned int i = 0, e = numWords(); i < e; i++) {
     60    bits[i] &= ~otherBits[i];
     61  }
     62 }
     63 
     64 void BitSet::intersect(const BitSet& other) {
     65  MOZ_ASSERT(bits_);
     66  MOZ_ASSERT(other.numBits_ == numBits_);
     67  MOZ_ASSERT(other.bits_);
     68 
     69  uint32_t* bits = bits_;
     70  const uint32_t* otherBits = other.bits_;
     71  for (unsigned int i = 0, e = numWords(); i < e; i++) {
     72    bits[i] &= otherBits[i];
     73  }
     74 }
     75 
     76 // returns true if the intersection caused the contents of the set to change.
     77 bool BitSet::fixedPointIntersect(const BitSet& other) {
     78  MOZ_ASSERT(bits_);
     79  MOZ_ASSERT(other.numBits_ == numBits_);
     80  MOZ_ASSERT(other.bits_);
     81 
     82  bool changed = false;
     83 
     84  uint32_t* bits = bits_;
     85  const uint32_t* otherBits = other.bits_;
     86  for (unsigned int i = 0, e = numWords(); i < e; i++) {
     87    uint32_t old = bits[i];
     88    bits[i] &= otherBits[i];
     89 
     90    if (!changed && old != bits[i]) {
     91      changed = true;
     92    }
     93  }
     94  return changed;
     95 }
     96 
     97 void BitSet::complement() {
     98  MOZ_ASSERT(bits_);
     99  uint32_t* bits = bits_;
    100  for (unsigned int i = 0, e = numWords(); i < e; i++) {
    101    bits[i] = ~bits[i];
    102  }
    103 }
    104 
    105 void BitSet::clear() {
    106  MOZ_ASSERT(bits_);
    107  uint32_t* bits = bits_;
    108  for (unsigned int i = 0, e = numWords(); i < e; i++) {
    109    bits[i] = 0;
    110  }
    111 }