aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/qtools/bitvector.h
blob: d3f5cf1ea60423261a299af59e6ccd54ec669412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2006 The Android Open Source Project

#ifndef BITVECTOR_H
#define BITVECTOR_H

#include <inttypes.h>
#include <assert.h>

class Bitvector {
 public:
  explicit Bitvector(int num_bits) {
    num_bits_ = num_bits;

    // Round up to a multiple of 32
    num_bits = (num_bits + 31) & ~31;
    vector_ = new uint32_t[num_bits >> 5];
  }
  ~Bitvector() {
    delete[] vector_;
  }

  void        SetBit(int bitnum) {
    assert(bitnum < num_bits_);
    vector_[bitnum >> 5] |= 1 << (bitnum & 31);
  }
  void        ClearBit(int bitnum) {
    assert(bitnum < num_bits_);
    vector_[bitnum >> 5] &= ~(1 << (bitnum & 31));
  }
  bool        GetBit(int bitnum) {
    assert(bitnum < num_bits_);
    return (vector_[bitnum >> 5] >> (bitnum & 31)) & 1;
  }

 private:
  int         num_bits_;
  uint32_t    *vector_;
};

#endif  // BITVECTOR_H