Advertisement
wurdalak007

for debugging

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