Implementation of a fixed-size bit vector.

This is a basic implementation bit vector containing an fixed 
arbitrary number of bits.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3991 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 16b34b8c
......@@ -28,12 +28,73 @@
#ifndef V8_DATAFLOW_H_
#define V8_DATAFLOW_H_
#include "v8.h"
#include "ast.h"
#include "compiler.h"
namespace v8 {
namespace internal {
class BitVector {
public:
explicit BitVector(int length) : length_(length) {
ASSERT(length > 0);
bits_ = Vector<uint32_t>::New(1 + length / 32);
for (int i = 0; i < bits_.length(); i++) {
bits_[i] = 0;
}
}
~BitVector() { bits_.Dispose(); }
void CopyFrom(const BitVector& other) {
ASSERT(other.length() == length());
for (int i = 0; i < bits_.length(); i++) {
bits_[i] = other.bits_[i];
}
}
bool Contains(int i) {
ASSERT(i >= 0 && i < length());
uint32_t block = bits_[i / 32];
return (block & (1U << (i % 32))) != 0;
}
void Add(int i) {
ASSERT(i >= 0 && i < length());
bits_[i / 32] |= (1U << (i % 32));
}
void Remove(int i) {
ASSERT(i >= 0 && i < length());
bits_[i / 32] &= ~(1U << (i % 32));
}
void Union(const BitVector& other) {
ASSERT(other.length() == length());
for (int i = 0; i < bits_.length(); i++) {
bits_[i] |= other.bits_[i];
}
}
void Intersect(const BitVector& other) {
ASSERT(other.length() == length());
for (int i = 0; i < bits_.length(); i++) {
bits_[i] &= other.bits_[i];
}
}
int length() const { return length_; }
private:
int length_;
Vector<uint32_t> bits_;
DISALLOW_COPY_AND_ASSIGN(BitVector);
};
// This class is used to number all expressions in the AST according to
// their evaluation order (post-order left-to-right traversal).
class AstLabeler: public AstVisitor {
......
......@@ -40,6 +40,7 @@ SOURCES = {
'test-ast.cc',
'test-compiler.cc',
'test-conversions.cc',
'test-dataflow.cc',
'test-debug.cc',
'test-decls.cc',
'test-flags.cc',
......
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdlib.h>
#include "v8.h"
#include "data-flow.h"
#include "cctest.h"
using namespace v8::internal;
TEST(BitVector) {
{
BitVector v(15);
v.Add(1);
CHECK(v.Contains(1));
v.Remove(0);
CHECK(!v.Contains(0));
v.Add(0);
v.Add(1);
BitVector w(15);
w.Add(1);
v.Intersect(w);
CHECK(!v.Contains(0));
CHECK(v.Contains(1));
}
{
BitVector v(15);
v.Add(0);
BitVector w(15);
w.Add(1);
v.Union(w);
CHECK(v.Contains(0));
CHECK(v.Contains(1));
}
{
BitVector v(35);
v.Add(0);
BitVector w(35);
w.Add(33);
v.Union(w);
CHECK(v.Contains(0));
CHECK(v.Contains(33));
}
{
BitVector v(35);
v.Add(32);
v.Add(33);
BitVector w(35);
w.Add(33);
v.Intersect(w);
CHECK(!v.Contains(32));
CHECK(v.Contains(33));
BitVector r(35);
r.CopyFrom(v);
CHECK(!r.Contains(32));
CHECK(r.Contains(33));
}
}
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