view BitArray.cpp @ 65:bc55cbd827bf

Compile fix.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Sun, 23 Dec 2012 22:04:55 +0100
parents b9515dc35fe4
children
line wrap: on
line source

#include "BitArray.hpp"

#include <cstring>

BitArray::~BitArray()
{
  delete bits;
}

BitArray::BitArray(const BitArray& other)
  : size_(other.size()), bits(new uchar[NUMCHARS(size_)])
{
  std::memcpy(bits, other.bits, NUMCHARS(size_));
}

BitArray& BitArray::operator=(const BitArray& other)
{
  if (NUMCHARS(size_) != NUMCHARS(other.size_)) {
    delete bits;
    bits = new uchar[NUMCHARS(other.size_)];
  }
  size_ = other.size_;
  memcpy(bits, other.bits, NUMCHARS(size_));
  return *this;
}

bool BitArray::operator==(const BitArray& rhs) const
{
  if (size() != rhs.size())
    return false;
  for(size_t i = 0; i < size(); ++i) {
    if (testBit(i) != rhs.testBit(i))
      return false;
  }
  return true;
}

std::ostream& operator<<(std::ostream& out, const BitArray& rhs)
{
  for (size_t i = 0; i < rhs.size(); ++i) {
    out << (rhs.testBit(i) ? "1" : "0");
  }
  return out;
}