view FastBitDecoder.cpp @ 62:247adcbbaf8b

Remove unnecessary includes in .cpp files.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Fri, 14 Sep 2012 20:57:44 +0200
parents c8111de2e0bb
children
line wrap: on
line source

#include "FastBitDecoder.hpp"

#include "BitDecoder.hpp"

unsigned char FastBitDecoder::getPaddedChar(const BitArray& bits, uint offset)
{
  unsigned char retVal = 0;
  size_t n = std::min(8u, bits.size() - offset);
  for (size_t i = 0; i < n; ++i) {
    retVal |= (bits.testBit(i + offset) ? 1 : 0) << (7 - i) ;
  }
  return retVal;
}

BitArray FastBitDecoder::removeFirst(const BitArray& bits)
{
  size_t N = bits.size();
  assert(N - 8 > 0);
  size_t n = N - 8;
  BitArray retVal(n);
  for (size_t i = 0; i < n; ++i) {
    retVal.setBit(i, bits.testBit(i + 8));
  }
  return retVal;
}


FastBitDecoder::~FastBitDecoder()
{
  for (size_t i = 0; i < N; ++i) {
    delete decoder[i];
    if (i == 0 || (data[i] != data[i - 1]))
      delete data[i];
  }
}

void FastBitDecoder::fill()
{
  for (size_t i = 0; i < N; ++i) {
    if (decoder[i] != 0) {
      decoder[i]->fill();
    }
    else if (data[i] == 0) {
      assert(data[i - 1]);
      data[i] = data[i - 1];
      numBits[i] = numBits[i - 1];
    }
  }
}

void FastBitDecoder::blank()
{
  for (size_t i = 0; i < N; ++i) {
    decoder[i] = 0;
    data[i] = 0;
    numBits[i] = 0;
  }
}

FastBitDecoder::FastBitDecoder()
{
  blank();
}


FastBitDecoder::FastBitDecoder(const QMap<QString, BitArray>& encoder)
{
  blank();
  for(QMap<QString, BitArray>::const_iterator it =  encoder.begin();
      it != encoder.end(); ++it) {
    insert(it.value(), it.key());
  }
  fill();
}


void FastBitDecoder::insert(const BitArray& key, const QString& value)
{
  unsigned char l = key.getPaddedChar();
  if (key.size() <= 8) {
    data[l] = new QString(value);
    numBits[l] = key.size();
  }
  else {
    if (!decoder[l])
      decoder[l] = new FastBitDecoder();
    decoder[l]->insert(removeFirst(key), value);
    numBits[l] = 8;
  }
}


uint FastBitDecoder::decode(QString& resString,
			    const BitArray& bits,
			    uint offset) const
{
  unsigned char l = bits.getPaddedChar(offset);
  if (data[l]) {
    resString.append(*data[l]);
    return numBits[l];
  }
  else {
    return decoder[l]->decode(resString, bits, offset + 8) + 8;
  }
}

QString FastBitDecoder::decode(const QString& bits) const
{
  return decode(BitDecoder::bitsFromString(bits));
}