1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#include <iostream.h>
#include <stddef.h>
#include "HuffmanCoder.H"
#include "EncodeBuffer.H"
#include "DecodeBuffer.H"
class EncodeNode
{
public:
EncodeNode(unsigned int v, unsigned int f):
value_(v), frequency_(f), encoding_(NULL), codeLength_(0), left_(NULL),
right_(NULL)
{
}
EncodeNode(EncodeNode * left, EncodeNode * right, unsigned int f):
value_(0), frequency_(f), encoding_(NULL), codeLength_(0), left_(left),
right_(right)
{
}
~EncodeNode()
{
delete[]encoding_;
delete left_;
delete right_;
}
unsigned int getValue() const
{
return value_;
}
unsigned int getFrequency() const
{
return frequency_;
}
void setCode(unsigned char *code, unsigned int length);
unsigned int getCodeLength() const
{
return codeLength_;
}
const unsigned char *getCode() const
{
return encoding_;
}
EncodeNode *addCode(unsigned int v, const char *code);
unsigned int decode(DecodeBuffer &);
private:
unsigned int value_;
unsigned int frequency_;
unsigned char *encoding_;
unsigned int codeLength_; // in bits
EncodeNode *left_;
EncodeNode *right_;
};
void
EncodeNode::setCode(unsigned char *code, unsigned int length)
{
if (encoding_)
delete[]encoding_;
encoding_ = code;
codeLength_ = length;
unsigned int nextCodeLength = codeLength_ + 1;
if (left_ != NULL)
{
unsigned char *leftCode = new unsigned char[nextCodeLength];
unsigned int i = 0;
for (; i < codeLength_; i++)
leftCode[i] = encoding_[i];
leftCode[i] = 0;
left_->setCode(leftCode, nextCodeLength);
}
if (right_ != NULL)
{
unsigned char *rightCode = new unsigned char[nextCodeLength];
unsigned int i = 0;
for (; i < codeLength_; i++)
rightCode[i] = encoding_[i];
rightCode[i] = 1;
right_->setCode(rightCode, nextCodeLength);
}
}
unsigned int
EncodeNode::decode(DecodeBuffer & decodeBuffer)
{
if ((left_ == NULL) && (right_ == NULL))
return value_;
unsigned int nextBit;
decodeBuffer.decodeValue(nextBit, 1);
if (nextBit == 0)
return left_->decode(decodeBuffer);
else
return right_->decode(decodeBuffer);
}
EncodeNode *
EncodeNode::addCode(unsigned int value, const char *code)
{
if (*code == 0)
{
value_ = value;
return this;
}
else if (*code == '0')
{
if (left_ == NULL)
left_ = new EncodeNode(NULL, NULL, 0);
return left_->addCode(value, code + 1);
}
else
{
if (right_ == NULL)
right_ = new EncodeNode(NULL, NULL, 0);
return right_->addCode(value, code + 1);
}
}
class Heap
{
public:
Heap(unsigned int maxSize);
~Heap();
void insert(EncodeNode *);
EncodeNode *pop();
unsigned int size() const
{
return numElements_;
}
private:
unsigned int numElements_;
unsigned int size_;
EncodeNode **heap_;
};
Heap::Heap(unsigned int maxSize):
numElements_(0), size_(maxSize), heap_(new EncodeNode *[maxSize])
{
}
Heap::~Heap()
{
delete[]heap_;
}
void
Heap::insert(EncodeNode * node)
{
unsigned int location = numElements_++;
unsigned int parent = ((location - 1) >> 1);
while (location && (heap_[parent]->getFrequency() > node->getFrequency()))
{
heap_[location] = heap_[parent];
location = parent;
parent = ((location - 1) >> 1);
}
heap_[location] = node;
}
EncodeNode*
Heap::pop()
{
if (numElements_ == 0)
return NULL;
EncodeNode *result = heap_[0];
numElements_--;
EncodeNode *node = heap_[numElements_];
unsigned int location = 0;
do
{
unsigned int left = (location << 1) + 1;
if (left >= numElements_)
break;
unsigned int nodeToSwap = left;
unsigned int right = left + 1;
if (right < numElements_)
if (heap_[right]->getFrequency() < heap_[left]->getFrequency())
nodeToSwap = right;
if (heap_[nodeToSwap]->getFrequency() < node->getFrequency())
{
heap_[location] = heap_[nodeToSwap];
location = nodeToSwap;
}
else
break;
}
while (location < numElements_);
heap_[location] = node;
return result;
}
HuffmanCoder::HuffmanCoder(unsigned int histogramLength,
const unsigned int *histogram,
unsigned int overflowCount):
numTokens_(histogramLength), tokens_(new EncodeNode *[histogramLength + 1])
{
Heap heap(histogramLength + 1);
for (unsigned int i = 0; i < histogramLength; i++)
heap.insert(tokens_[i] = new EncodeNode(i, histogram[i]));
heap.insert(tokens_[histogramLength] =
new EncodeNode(histogramLength, overflowCount));
while (heap.size() > 1)
{
EncodeNode *node1 = heap.pop();
EncodeNode *node2 = heap.pop();
EncodeNode *newNode = new EncodeNode(node1, node2, node1->getFrequency() +
node2->getFrequency());
heap.insert(newNode);
}
root_ = heap.pop();
root_->setCode(NULL, 0);
}
HuffmanCoder::HuffmanCoder(unsigned int numCodes, const char **codes):
numTokens_(numCodes), tokens_(new EncodeNode *[numCodes])
{
root_ = new EncodeNode(NULL, NULL, 0);
for (unsigned int i = 0; i < numCodes; i++)
tokens_[i] = root_->addCode(i, codes[i]);
root_->setCode(NULL, 0);
}
HuffmanCoder::~HuffmanCoder()
{
delete root_;
delete[]tokens_;
}
void
HuffmanCoder::encode(unsigned int value, EncodeBuffer & encodeBuffer)
{
unsigned int index = value;
if (index >= numTokens_)
index = numTokens_;
const EncodeNode *node = tokens_[index];
const unsigned char *code = node->getCode();
unsigned int codeLength = node->getCodeLength();
for (unsigned int i = 0; i < codeLength; i++)
encodeBuffer.encodeValue(code[i], 1);
if (value >= numTokens_)
encodeBuffer.encodeValue(value, 16, 8);
}
unsigned int
HuffmanCoder::decode(DecodeBuffer & decodeBuffer)
{
unsigned int result = root_->decode(decodeBuffer);
if (result >= numTokens_)
decodeBuffer.decodeValue(result, 16, 8);
return result;
}
|