Add copy constructor and assignment operator to the BitVector class.

Review URL: http://codereview.chromium.org/668259

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4063 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 97f74843
......@@ -36,18 +36,29 @@
namespace v8 {
namespace internal {
class BitVector {
class BitVector BASE_EMBEDDED {
public:
explicit BitVector(int length) : length_(length) {
explicit BitVector(int length)
: length_(length), bits_(Vector<uint32_t>::New(1 + length / 32)) {
ASSERT(length > 0);
bits_ = Vector<uint32_t>::New(1 + length / 32);
for (int i = 0; i < bits_.length(); i++) {
bits_[i] = 0;
}
}
BitVector(const BitVector& other)
: length_(other.length()),
bits_(Vector<uint32_t>::New(1 + other.length() / 32)) {
CopyFrom(other);
}
~BitVector() { bits_.Dispose(); }
BitVector& operator=(const BitVector& rhs) {
if (this != &rhs) CopyFrom(rhs);
return *this;
}
void CopyFrom(const BitVector& other) {
ASSERT(other.length() == length());
for (int i = 0; i < bits_.length(); i++) {
......@@ -85,13 +96,24 @@ class BitVector {
}
}
void Clear() {
for (int i = 0; i < bits_.length(); i++) {
bits_[i] = 0;
}
}
bool IsEmpty() {
for (int i = 0; i < bits_.length(); i++) {
if (bits_[i] != 0) return false;
}
return true;
}
int length() const { return length_; }
private:
int length_;
Vector<uint32_t> bits_;
DISALLOW_COPY_AND_ASSIGN(BitVector);
};
......
......@@ -60,6 +60,21 @@ TEST(BitVector) {
CHECK(v.Contains(1));
}
{
BitVector v(15);
v.Add(0);
BitVector w(15);
w = v;
CHECK(w.Contains(0));
w.Add(1);
BitVector u(w);
CHECK(u.Contains(0));
CHECK(u.Contains(1));
v.Union(w);
CHECK(v.Contains(0));
CHECK(v.Contains(1));
}
{
BitVector v(35);
v.Add(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment