/*
Copyright (c) Microsoft Corporation
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License
at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF
TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions and
limitations under the License.
*/
using System;
using Microsoft.Research.DryadLinq;
namespace Microsoft.Research.DryadLinq.Internal
{
///
/// A simple BitVector implementation.
///
public struct BitVector
{
private byte[] m_array;
///
/// Initializes a new instance of the BitVector class.
///
/// The number of bits for the bit vector.
public BitVector(int length)
{
this.m_array = new byte[(length + 7) / 8];
}
///
/// Initializes a new instance of the BitVector class from an array of boolean values.
///
/// An array of boolean values representing a bit vector.
public BitVector(bool[] values)
{
this.m_array = new byte[(values.Length + 7) / 8];
for (int i = 0; i < values.Length; i++)
{
if (values[i])
{
m_array[i / 8] |= (byte)(1 << (i % 8));
}
}
}
private BitVector(byte[] values)
{
this.m_array = values;
}
///
/// Gets the bit at the specified index.
///
/// An index into the bit vector.
/// The value of the bit as a boolean.
public bool this[int index]
{
get { return this.Get(index); }
}
///
/// Gets the bit at the specified index.
///
/// An index into the bit vector.
/// The value of the bit as a boolean.
public bool Get(int index)
{
int idx = index / 8;
return ((idx < this.m_array.Length) &&
(this.m_array[idx] & (1 << (index % 8))) != 0);
}
///
/// Sets the bit at the specified index.
///
/// An index into the bit vector.
public void Set(int index)
{
this.m_array[index / 8] |= (byte)(1 << (index % 8));
}
///
/// Sets all the bits to the specified value.
///
/// The value to be set for all bits.
public void SetAll(bool value)
{
byte fillValue = 0;
if (value) fillValue = 0xff;
for (int i = 0; i < this.m_array.Length; i++)
{
this.m_array[i] = fillValue;
}
}
private void WriteInner(DryadLinqBinaryWriter writer)
{
int len;
for (len = this.m_array.Length - 1; len >= 0; len--)
{
if (this.m_array[len] != 0) break;
}
len++;
writer.WriteCompact(len);
for (int i = 0; i < len; i++)
{
writer.Write(this.m_array[i]);
}
}
///
/// Reads a BitVector from the specified DryadLinqBinaryReader.
///
/// The DryadLinqBinaryReader to read from.
/// A BitVector
public static BitVector Read(DryadLinqBinaryReader reader)
{
Int32 len = reader.ReadCompactInt32();
byte[] values = new byte[len];
for (int i = 0; i < len; i++)
{
values[i] = reader.ReadUByte();
}
return new BitVector(values);
}
///
/// Writes a BitVector to the specified DryadLinqBinaryWriter.
///
/// The DryadLinqBinaryWriter to write to.
/// The BitVector to write
public static void Write(DryadLinqBinaryWriter writer, BitVector bv)
{
bv.WriteInner(writer);
}
}
}