Advertisement
wurdalak007

debughuf67

Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <queue>
  5. #include "Huffman.h"
  6. #include <assert.h>
  7.  
  8. using namespace std;
  9.  
  10. typedef char byte;
  11.  
  12. struct list {
  13.  
  14. int repeat;
  15. char symb;
  16. string code;
  17. list* left;
  18. list* right;
  19.  
  20. list() :
  21. repeat( 0 ),
  22. symb( 0 ),
  23. code( "" ),
  24. left( nullptr ),
  25. right( nullptr )
  26. {}
  27.  
  28. list( const list& root );
  29.  
  30. };
  31.  
  32. list::list( const list &root ) :
  33. repeat( root.repeat ),
  34. symb( 0 ),
  35. code( "" ),
  36. left( root.left ),
  37. right( root.right)
  38. {}
  39.  
  40.  
  41. bool operator <( const list& first, const list& second ) {
  42. return first.repeat > second.repeat;
  43. }
  44.  
  45. void init_arr( vector<list*>& ASCII ) {
  46. for( int i = 0; i < 256; i++ ) {
  47. ASCII[i] = new list();
  48. ASCII[i]->symb = (unsigned char)(i);
  49. ASCII[i]->repeat = 0;
  50. }
  51. }
  52.  
  53. void generateCode( list*& root ) {
  54. if( root == nullptr ) {
  55. return;
  56. }
  57. if( root->left != nullptr ) {
  58. root->left->code = root->code + "0";
  59. }
  60. if( root->right != nullptr ) {
  61. root->right->code = root->code + "1";
  62. }
  63.  
  64. generateCode(root->left);
  65. generateCode(root->right);
  66. }
  67.  
  68. void translate( list* root, vector<list*>& shedule ) {
  69.  
  70. if( root->right == nullptr && root->left == nullptr ) {
  71. shedule.push_back(root);
  72. return;
  73. } else {
  74. translate(root->left, shedule);
  75. translate(root->right, shedule);
  76. }
  77.  
  78. }
  79.  
  80. string find_code( vector<list*> shed, char p ) {
  81. int i = 0;
  82. while( shed[i]->symb != p ) {
  83. i++;
  84. }
  85. return shed[i]->code;
  86. }
  87.  
  88. string into_bin ( char p ) {
  89. string binary;
  90. int p_num = (unsigned int)p;
  91. int i = 0;
  92.  
  93. while( p_num > 0 ) {
  94. p_num /= 2;
  95. i++;
  96. }
  97. p_num = (int)p;
  98. i--;
  99. for( ; i >= 0; --i) {
  100. if( 0 == ((p_num>>i)&(0x1)) ) {
  101. binary += "0";
  102. } else {
  103. binary += "1";
  104. }
  105. }
  106.  
  107. return binary;
  108.  
  109. }
  110.  
  111. void crypt_tree( vector<list*>& shedule, string& crypt ) {
  112.  
  113. for( int i = 0; i < shedule.size(); i++ ) {
  114. crypt += into_bin(shedule[i]->symb);
  115. crypt += " ";
  116. crypt += shedule[i]->code;
  117. crypt += " ";
  118. }
  119.  
  120. }
  121.  
  122. void deletetree( list* tree ) {
  123. if( tree == nullptr ) {
  124. return;
  125. }
  126. deletetree(tree->left);
  127. deletetree(tree->right);
  128. delete tree;
  129. }
  130.  
  131.  
  132.  
  133. void Encode( IInputStream& original, IOutputStream& compressed ) {
  134. byte value = 0;
  135. string input = "";
  136. string output = "";
  137. vector<list*> ASCII(256);
  138. vector<list*> shedule;
  139. priority_queue<list*> queue;
  140.  
  141. init_arr( ASCII );
  142.  
  143. while( original.Read(value) ) {
  144. int uval = value + 128;
  145. input += uval;
  146. ASCII[uval]->repeat++;
  147. }
  148.  
  149. for( int i = 0; i < 256; i++ ) {
  150. if( ASCII[i]->repeat != 0 ) {
  151. queue.push(ASCII[i]);
  152. }
  153. }
  154.  
  155. while( queue.size() >= 2) {
  156. list* tmp1 = queue.top();
  157. queue.pop();
  158. list* tmp2 = queue.top();
  159. queue.pop();
  160.  
  161. list* newtmp = new list();
  162. newtmp->left = tmp1;
  163. newtmp->right = tmp2;
  164. newtmp->repeat = tmp1->repeat + tmp2->repeat;
  165. queue.push(newtmp);
  166. }
  167.  
  168. list* root = queue.top();
  169. queue.pop();
  170.  
  171. root->left->code = "0";
  172. root->right->code = "1";
  173. generateCode(root->right);
  174. generateCode(root->left);
  175. translate( root, shedule );
  176.  
  177. crypt_tree(shedule, output);
  178.  
  179. for( int i = 0; i < input.length(); i++ ) {
  180. output += find_code(shedule, input[i]);
  181. }
  182.  
  183. for( int i = 0; i < output.length(); i++ ) {
  184. compressed.Write(output[i] - 128);
  185. }
  186.  
  187. deletetree(root);
  188. }
  189.  
  190. list* decode_tree ( string output, int& position ) {
  191. list* root = new list();
  192. list* tmp = root;
  193. int symb_num = 0;
  194. int j = 0;
  195. int i = 0;
  196.  
  197. while( i < output.length() ) {
  198. for(; output[i] != ' '; i++ ) {
  199. j++;
  200. symb_num = symb_num*2 + (output[i] - '0' )*2;
  201. if( symb_num >= 512 ) {
  202. position = i - j + 1;
  203. return root;
  204. }
  205. }
  206. symb_num /= 2;
  207. j = 0;
  208. i++;
  209. while( output[i] != ' ') {
  210. if( output[i] == '1' ) {
  211. if( tmp->right == nullptr) {
  212. tmp->right = new list();
  213. tmp->right->code = tmp->code + "1";
  214. tmp = tmp->right;
  215. i++;
  216. } else {
  217. tmp = tmp->right;
  218. i++;
  219. }
  220. } else {
  221. if( tmp->left == nullptr ) {
  222. tmp->left = new list();
  223. tmp->left->code = tmp->code + "0";
  224. tmp = tmp->left;
  225. i++;
  226. } else {
  227. tmp = tmp->left;
  228. i++;
  229. }
  230. }
  231. }
  232. tmp->symb = (char)symb_num;
  233. tmp = root;
  234. i++;
  235. symb_num = 0;
  236. }
  237. return root;
  238. }
  239.  
  240. void Decode( IInputStream& compressed, IOutputStream& original ) {
  241. string output= "";
  242. int position = 0;
  243. byte value = 0;
  244.  
  245. while( compressed.Read(value) ) {
  246. int uval = value + 128;
  247. output += uval;
  248. }
  249.  
  250.  
  251. list* root = decode_tree( output, position );
  252. list* tmp = root;
  253.  
  254. for( int i = position; i < output.length(); i++ ) {
  255. if( output[i] == '0' ) {
  256. if( tmp->left == nullptr ) {
  257. original.Write(tmp->symb - 128);
  258. tmp = root;
  259. }
  260. tmp = tmp->left;
  261. } else {
  262. if( tmp->right == nullptr ) {
  263. original.Write(tmp->symb - 128);
  264. tmp = root;
  265. }
  266. tmp = tmp->right;
  267. }
  268.  
  269. }
  270.  
  271. deletetree(root);
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement