fork download
  1. //huffman.h
  2. #ifndef HUFFMAN_HUFFMAN_H
  3. #define HUFFMAN_HUFFMAN_H
  4.  
  5. #include <sys/param.h>
  6. #include <iostream>
  7. #include <map>
  8. #include <vector>
  9.  
  10. typedef std::vector<bool> code_t;
  11. typedef std::map<char, code_t> codetable;
  12.  
  13. class TreeNode{
  14. public:
  15. TreeNode(char c, int cnt, bool l, TreeNode* lc, TreeNode* rc): character(c), count(cnt), is_leaf(l), left(lc), right(rc){};
  16.  
  17. int getCount() const{
  18. return this -> count;
  19. };
  20.  
  21. char getChar() const{
  22. return this -> character;
  23. };
  24.  
  25. TreeNode* getLeftTree() const{
  26. return this -> left;
  27. };
  28.  
  29. TreeNode* getRightTree() const{
  30. return this -> right;
  31. };
  32.  
  33. void setLeftTree(TreeNode* n){
  34. if (n != NULL)
  35. this -> left = n;
  36. };
  37.  
  38. void setRightTree(TreeNode* n){
  39. this -> right = n;
  40. };
  41.  
  42. void setChar(char c){
  43. this -> character = c;
  44. };
  45.  
  46. bool isLeaf(){
  47. return is_leaf;
  48. }
  49.  
  50. void setLeaf(bool num){
  51. this->is_leaf = num;
  52. }
  53.  
  54. private:
  55. char character;
  56. int count;
  57. bool is_leaf;
  58. TreeNode* left;
  59. TreeNode* right;
  60. };
  61.  
  62. class HuffmanTree{
  63. public:
  64.  
  65. HuffmanTree(std::map<char , int>&);
  66.  
  67. HuffmanTree(){
  68. root = new TreeNode(0, 0, true, NULL, NULL);
  69. };
  70.  
  71. ~HuffmanTree();
  72.  
  73. TreeNode* getRoot() const{
  74. return this -> root;
  75. };
  76.  
  77. // void buildTable(TreeNode* n, std::vector<bool> &, std::map<char, std::vector<bool> >&);
  78.  
  79. std::map<char, std::vector<bool> > buildTable();
  80.  
  81. std::map<char, std::vector<bool> >& getTable(){
  82. return lookup;
  83. };
  84.  
  85. std::map<std::vector<bool>, char > buildCodes();
  86.  
  87. std::map<std::vector<bool>, char>& getCodes(){
  88. return codes;
  89. };
  90.  
  91. void Print();
  92.  
  93.  
  94.  
  95. class NodeComparator {
  96. public:
  97. bool operator()(const TreeNode *const lhs, const TreeNode *const rhs) {
  98. if (lhs->getCount() == rhs->getCount()) {
  99. return lhs->getChar() > rhs->getChar();
  100. }
  101.  
  102. return lhs->getCount() > rhs->getCount();
  103. }
  104. };
  105.  
  106. TreeNode* merge(TreeNode* node1, TreeNode* node2);
  107. void recursiveNodeDelete(TreeNode* node);
  108. // uint32_t check_count(uint32_t count);
  109.  
  110. private:
  111. TreeNode* root;
  112. std::map<char, std::vector<bool>> lookup;
  113. std::map<std::vector<bool>, char> codes;
  114. };
  115.  
  116.  
  117. #endif //HUFFMAN_HUFFMAN_H
  118. __________________________________
  119.  
  120. //archiver.h
  121. #include "huffman.h"
  122. #include "bitstring.h"
  123. #ifndef HUFFMAN_ARCHIVER_H
  124. #define HUFFMAN_ARCHIVER_H
  125.  
  126. class Archiver{
  127. private:
  128. std::map<std::vector <bool>, char> codes;
  129.  
  130. public:
  131. Archiver(){};
  132. void compress(std::ifstream &ifs, std::ofstream &ofs, HuffmanTree *tree);
  133. void decompress(std::ifstream &ifs, std::ofstream &ofs, HuffmanTree *tree);
  134. void encodeTree(BitStringWrite& bw, TreeNode *node);
  135. TreeNode* decodeTree(BitStringRead& br, TreeNode *node);
  136. void buildCodes(TreeNode* n, std::vector<bool> &cur);
  137. std::map<std::vector <bool>, char>& getCodes(){
  138. return codes;
  139. };
  140. };
  141. #endif //HUFFMAN_ARCHIVER_H
  142.  
  143. __________________________________
  144.  
  145. //bitstring.h
  146.  
  147. #ifndef HUFFMAN_BITSTRING_H
  148. #define HUFFMAN_BITSTRING_H
  149. #include <iostream>
  150. #include <vector>
  151.  
  152. class BitStringWrite {
  153.  
  154. private:
  155. char _byte;
  156. int _pos;
  157. std::ostream &_out_f;
  158.  
  159. public:
  160. BitStringWrite(std::ostream &_out_f);
  161.  
  162. ~BitStringWrite();
  163.  
  164. void writeBit(bool bit);
  165.  
  166. void writeByte(char b);
  167.  
  168. void flush();
  169.  
  170. std::ostream & getStream(){
  171. return _out_f;
  172. }
  173. };
  174.  
  175. class BitStringRead {
  176.  
  177. private:
  178. char _byte;
  179. int _pos;
  180. std::istream &_in_f;
  181.  
  182. public:
  183.  
  184. BitStringRead(std::istream &_in_f);
  185.  
  186. char readByte();
  187.  
  188. bool readBit();
  189.  
  190. std::istream & getStream(){
  191. return _in_f;
  192. }
  193.  
  194. };
  195. #endif //HUFFMAN_BITSTRING_H
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:121:21: fatal error: huffman.h: No such file or directory
 #include "huffman.h"
                     ^
compilation terminated.
stdout
Standard output is empty