//huffman.h
#ifndef HUFFMAN_HUFFMAN_H
#define HUFFMAN_HUFFMAN_H
#include <sys/param.h>
#include <iostream>
#include <map>
#include <vector>
typedef std::vector<bool> code_t;
typedef std::map<char, code_t> codetable;
class TreeNode{
public:
TreeNode(char c, int cnt, bool l, TreeNode* lc, TreeNode* rc): character(c), count(cnt), is_leaf(l), left(lc), right(rc){};
int getCount() const{
return this -> count;
};
char getChar() const{
return this -> character;
};
TreeNode* getLeftTree() const{
return this -> left;
};
TreeNode* getRightTree() const{
return this -> right;
};
void setLeftTree(TreeNode* n){
if (n != NULL)
this -> left = n;
};
void setRightTree(TreeNode* n){
this -> right = n;
};
void setChar(char c){
this -> character = c;
};
bool isLeaf(){
return is_leaf;
}
void setLeaf(bool num){
this->is_leaf = num;
}
private:
char character;
int count;
bool is_leaf;
TreeNode* left;
TreeNode* right;
};
class HuffmanTree{
public:
HuffmanTree(std::map<char , int>&);
HuffmanTree(){
root = new TreeNode(0, 0, true, NULL, NULL);
};
~HuffmanTree();
TreeNode* getRoot() const{
return this -> root;
};
// void buildTable(TreeNode* n, std::vector<bool> &, std::map<char, std::vector<bool> >&);
std::map<char, std::vector<bool> > buildTable();
std::map<char, std::vector<bool> >& getTable(){
return lookup;
};
std::map<std::vector<bool>, char > buildCodes();
std::map<std::vector<bool>, char>& getCodes(){
return codes;
};
void Print();
class NodeComparator {
public:
bool operator()(const TreeNode *const lhs, const TreeNode *const rhs) {
if (lhs->getCount() == rhs->getCount()) {
return lhs->getChar() > rhs->getChar();
}
return lhs->getCount() > rhs->getCount();
}
};
TreeNode* merge(TreeNode* node1, TreeNode* node2);
void recursiveNodeDelete(TreeNode* node);
// uint32_t check_count(uint32_t count);
private:
TreeNode* root;
std::map<char, std::vector<bool>> lookup;
std::map<std::vector<bool>, char> codes;
};
#endif //HUFFMAN_HUFFMAN_H
__________________________________
//archiver.h
#include "huffman.h"
#include "bitstring.h"
#ifndef HUFFMAN_ARCHIVER_H
#define HUFFMAN_ARCHIVER_H
class Archiver{
private:
std::map<std::vector <bool>, char> codes;
public:
Archiver(){};
void compress(std::ifstream &ifs, std::ofstream &ofs, HuffmanTree *tree);
void decompress(std::ifstream &ifs, std::ofstream &ofs, HuffmanTree *tree);
void encodeTree(BitStringWrite& bw, TreeNode *node);
TreeNode* decodeTree(BitStringRead& br, TreeNode *node);
void buildCodes(TreeNode* n, std::vector<bool> &cur);
std::map<std::vector <bool>, char>& getCodes(){
return codes;
};
};
#endif //HUFFMAN_ARCHIVER_H
__________________________________
//bitstring.h
#ifndef HUFFMAN_BITSTRING_H
#define HUFFMAN_BITSTRING_H
#include <iostream>
#include <vector>
class BitStringWrite {
private:
char _byte;
int _pos;
std::ostream &_out_f;
public:
BitStringWrite(std::ostream &_out_f);
~BitStringWrite();
void writeBit(bool bit);
void writeByte(char b);
void flush();
std::ostream & getStream(){
return _out_f;
}
};
class BitStringRead {
private:
char _byte;
int _pos;
std::istream &_in_f;
public:
BitStringRead(std::istream &_in_f);
char readByte();
bool readBit();
std::istream & getStream(){
return _in_f;
}
};
#endif //HUFFMAN_BITSTRING_H