Make bitvectors serializable.

This commit is contained in:
Matthias Vallentin 2013-05-28 20:58:01 -07:00
parent 4d275522c7
commit 9e32eaad6d
3 changed files with 67 additions and 5 deletions

View file

@ -2,6 +2,7 @@
#include <cassert> #include <cassert>
#include <limits> #include <limits>
#include "Serializer.h"
BitVector::size_type BitVector::npos = static_cast<BitVector::size_type>(-1); BitVector::size_type BitVector::npos = static_cast<BitVector::size_type>(-1);
BitVector::block_type BitVector::bits_per_block = BitVector::block_type BitVector::bits_per_block =
@ -62,7 +63,7 @@ BitVector::Reference& BitVector::Reference::operator=(Reference const& other)
BitVector::Reference& BitVector::Reference::operator|=(bool x) BitVector::Reference& BitVector::Reference::operator|=(bool x)
{ {
if (x) if (x)
block_ |= mask_; block_ |= mask_;
return *this; return *this;
} }
@ -73,7 +74,7 @@ BitVector::Reference& BitVector::Reference::operator&=(bool x)
block_ &= ~mask_; block_ &= ~mask_;
return *this; return *this;
} }
BitVector::Reference& BitVector::Reference::operator^=(bool x) BitVector::Reference& BitVector::Reference::operator^=(bool x)
{ {
if (x) if (x)
@ -453,3 +454,55 @@ BitVector::size_type BitVector::find_from(size_type i) const
return npos; return npos;
return i * bits_per_block + lowest_bit(bits_[i]); return i * bits_per_block + lowest_bit(bits_[i]);
} }
bool BitVector::Serialize(SerialInfo* info) const
{
return SerialObj::Serialize(info);
}
BitVector* BitVector::Unserialize(UnserialInfo* info)
{
return reinterpret_cast<BitVector*>(
SerialObj::Unserialize(info, SER_BITVECTOR));
}
IMPLEMENT_SERIAL(BitVector, SER_BITVECTOR);
bool BitVector::DoSerialize(SerialInfo* info) const
{
DO_SERIALIZE(SER_BITVECTOR, SerialObj);
if ( ! SERIALIZE(static_cast<uint64>(bits_.size())) )
return false;
for (size_t i = 0; i < bits_.size(); ++i)
if ( ! SERIALIZE(static_cast<uint64>(bits_[i])) )
return false;
return SERIALIZE(static_cast<uint64>(num_bits_));
}
bool BitVector::DoUnserialize(UnserialInfo* info)
{
DO_UNSERIALIZE(SerialObj);
uint64 size;
if ( ! UNSERIALIZE(&size) )
return false;
bits_.resize(static_cast<size_t>(size));
uint64 block;
for ( size_t i = 0; i < bits_.size(); ++i )
{
if ( ! UNSERIALIZE(&block) )
return false;
bits_[i] = static_cast<block_type>(block);
}
uint64 num_bits;
if ( ! UNSERIALIZE(&num_bits) )
return false;
num_bits_ = static_cast<size_type>(num_bits);
return true;
}

View file

@ -3,11 +3,12 @@
#include <iterator> #include <iterator>
#include <vector> #include <vector>
#include "SerialObj.h"
/** /**
* A vector of bits. * A vector of bits.
*/ */
class BitVector { class BitVector : SerialObj {
public: public:
typedef size_t block_type; typedef size_t block_type;
typedef size_t size_type; typedef size_t size_type;
@ -42,7 +43,7 @@ public:
typedef bool const_reference; typedef bool const_reference;
/** /**
* Constructs an empty bit vector. * Default-constructs an empty bit vector.
*/ */
BitVector(); BitVector();
@ -253,6 +254,12 @@ public:
*/ */
size_type find_next(size_type i) const; size_type find_next(size_type i) const;
bool Serialize(SerialInfo* info) const;
static BitVector* Unserialize(UnserialInfo* info);
protected:
DECLARE_SERIAL(BitVector);
private: private:
/** /**
* Computes the block index for a given bit position. * Computes the block index for a given bit position.
@ -286,7 +293,7 @@ private:
*/ */
static size_type bits_to_blocks(size_type bits) static size_type bits_to_blocks(size_type bits)
{ {
return bits / bits_per_block return bits / bits_per_block
+ static_cast<size_type>(bits % bits_per_block != 0); + static_cast<size_type>(bits % bits_per_block != 0);
} }

View file

@ -49,6 +49,7 @@ SERIAL_IS(STATE_ACCESS, 0x1100)
SERIAL_IS_BO(CASE, 0x1200) SERIAL_IS_BO(CASE, 0x1200)
SERIAL_IS(LOCATION, 0x1300) SERIAL_IS(LOCATION, 0x1300)
SERIAL_IS(RE_MATCHER, 0x1400) SERIAL_IS(RE_MATCHER, 0x1400)
SERIAL_IS(BITVECTOR, 0x1500)
// These are the externally visible types. // These are the externally visible types.
const SerialType SER_NONE = 0; const SerialType SER_NONE = 0;
@ -202,5 +203,6 @@ SERIAL_CONST2(STATE_ACCESS)
SERIAL_CONST2(CASE) SERIAL_CONST2(CASE)
SERIAL_CONST2(LOCATION) SERIAL_CONST2(LOCATION)
SERIAL_CONST2(RE_MATCHER) SERIAL_CONST2(RE_MATCHER)
SERIAL_CONST2(BITVECTOR)
#endif #endif