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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
| #pragma once
#include <iostream> #include <fstream> #include <algorithm> #include <queue> #include <string> #include <cmath> #include <map>
#define SHOW_DETAIL
#define COMPRESS 0x1 #define DECOMPRESS 0x2 #define EXIT 0x3
#define BITSIZE 0x8
const int maxN = 1e4 + 1; const int initC = 2; int timestamp;
inline unsigned char dbToCh(double); inline double chToDb(unsigned char);
struct VarAndFrec { char var; int frec; bool operator < (const VarAndFrec &obj) const { return this->frec < obj.frec; } VarAndFrec(char v = '\0', int f = 0) : var(v), frec(f) { } ~VarAndFrec() { } };
struct Node { int frec; bool isLeaf; char var; Node *left; Node *right;
Node(int frec = 0, char var = '\0') : frec(frec), var(var), isLeaf(true), left(NULL), right(NULL) { } ~Node() { } };
class HuffMan { public: HuffMan() { root = new Node; } ~HuffMan() { removeAll(root); }
void Operate();
private: Node *root; std::map<char, int> varToFrec; std::map<char, std::string> codeList; std::ifstream input; std::ofstream output; std::string texts;
void removeAll(Node *); void coding(Node *, std::string);
bool getFilestream(); void buildTree(bool); void compress(); void decompress(); };
void HuffMan::removeAll(Node *root) { if (root == NULL) return;
removeAll(root->left); removeAll(root->right);
delete root; }
void HuffMan::coding(Node *root, std::string code) { if (root->isLeaf) { codeList[root->var] = code; return; } coding(root->left, code + "0"); coding(root->right, code + "1"); }
bool HuffMan::getFilestream() { std::string getin = "", getout = "";
printf("Please input source file name(size less than 4GB)\n>> "); getline(std::cin, getin); printf("Please input code file name\n>> "); getline(std::cin, getout); printf("\n");
input.open(getin.c_str(), std::ios::in | std::ios::binary); output.open(getout.c_str(), std::ios::out | std::ios::binary);
return input.good(); }
void HuffMan::buildTree(bool sign = true) { if (sign) { char alpha = '\0'; varToFrec.clear(); while (input.get(alpha)) { texts += alpha; varToFrec.find(alpha) != varToFrec.end() ? varToFrec[alpha] ++ : varToFrec[alpha] = 1; } if (varToFrec.size() < 3) { printf("File no need to be compressed!\n"); return; } } #ifdef SHOW_DETAIL std::map<char, int>::iterator mpiter = varToFrec.begin(); printf("char frec\n"); for (; mpiter != varToFrec.end(); mpiter++) { printf(" %c %d\n", mpiter->first, mpiter->second); } #endif
removeAll(root); root = new Node; std::map<std::pair<int, int>, Node *> priQ; std::map<char, int>::iterator iter = varToFrec.begin(); for (; iter != varToFrec.end(); iter++) { priQ[std::pair<int, int>(iter->second, timestamp++)] = new Node(iter->second, iter->first); } while (priQ.size() > 1) { Node *fir = priQ.begin()->second; priQ.erase(priQ.begin()); Node *sec = priQ.begin()->second; priQ.erase(priQ.begin());
Node *inter = new Node(fir->frec + sec->frec); inter->isLeaf = false; inter->left = fir->frec < sec->frec ? fir : sec; inter->right = fir->frec < sec->frec ? sec : fir;
priQ[std::pair<int, int>(inter->frec, timestamp++)] = inter; }
root = priQ.begin()->second; priQ.clear(); }
void HuffMan::compress() { codeList.clear(); coding(root, "");
#ifdef SHOW_DETAIL std::map<char, std::string>::iterator iter = codeList.begin(); for (; iter != codeList.end(); iter++) { printf(" %c %s\n", iter->first, iter->second.c_str()); } #endif
std::string LastCode = ""; for (std::string::iterator iter = texts.begin(); iter != texts.end(); iter++) { LastCode += codeList[*iter]; }
int recLen = LastCode.length(); int PairNum = varToFrec.size(); VarAndFrec FTV[maxN]; std::map<char, int>::iterator iterC = varToFrec.begin(); for (int inc = 0; iterC != varToFrec.end(); iterC++, inc++) { FTV[inc] = VarAndFrec(iterC->first, iterC->second); } std::sort(FTV, FTV + PairNum);
for (int idx = 0; idx < PairNum; idx++) { unsigned char K_i = FTV[idx].var, V_i = '\0'; if (idx == PairNum - 1) { K_i |= (recLen % BITSIZE != 0) << (BITSIZE - 1); } else { K_i |= (idx == PairNum - 2) ? 0x80 : 0x00; }
output << K_i; if (idx) { V_i = dbToCh(1.0 * FTV[idx - 1].frec / FTV[idx].frec); output << V_i; } }
int len = recLen / BITSIZE; for (int inc = 0; inc < len; inc++) { unsigned char Tm = '\0'; for (int snc = 0; snc < BITSIZE; snc++) { Tm |= (int(*(LastCode.begin() + inc * BITSIZE + snc) - '0') << snc); } output << Tm; }
int Modlen = LastCode.length() % BITSIZE; unsigned char Ch = '\0'; for (int inc = 0; inc < BITSIZE; inc++) { if (inc < Modlen) Ch |= (int(*(LastCode.begin() + len * BITSIZE + inc) - '0') << inc); else if (inc > Modlen) Ch |= (0x1 << inc); }
if (Modlen) output << Ch;
printf("Compress succeed!\n");
input.close(); output.close(); }
void HuffMan::decompress() { std::string line; std::string text; bool needEnter = false; while (std::getline(input, line)) { if (needEnter) line = "\n" + line; text += line; needEnter = true; }
int KVpairNum = 0, loc = 0; int nextStop = 0; varToFrec.clear(); for (std::string::iterator iter = text.begin(); iter != text.end(); iter++) { if (iter != text.begin()) { varToFrec[(*iter) & 0x7F] = std::floor(varToFrec[(*(iter - 1 - (varToFrec.size() >= 2))) & 0x7F] / chToDb(*(iter + 1))); if (nextStop) { loc = ((*iter) >> (BITSIZE - 1)) & 0x01; break; } else { nextStop = ((*iter) >> (BITSIZE - 1)) & 0x01; } iter++; } else { nextStop = ((*iter) >> (BITSIZE - 1)) & 0x01; varToFrec[(*iter) & 0x7F] = initC; } } buildTree(false); unsigned char HFLast = *text.rbegin(); if (loc) { loc = BITSIZE; while (HFLast & (1 << (--loc))); } else { loc = BITSIZE; }
int PairSize = 2 * varToFrec.size() - 1; Node *curr = root; for (std::string::iterator iter = text.begin() + PairSize; iter != text.end(); iter++) { unsigned char Tm = *iter; int Range = (iter + 1 == text.end() ? loc : BITSIZE); for (int inc = 0; inc < Range; inc++) { curr = ((Tm >> inc) & 0x1) ? curr->right : curr->left; if (curr->isLeaf) { output << curr->var; curr = root; } } }
printf("Decompress succeed!\n");
input.close(); output.close(); }
void HuffMan::Operate() { bool quit = false; int opeNum = 0; while (!quit) { printf("-------------------\n"); printf("1. Compress \n"); printf("2. Decompress \n"); printf("3. Exit \n"); printf("-------------------\n"); printf(">> "); scanf("%d", &opeNum); getchar();
if (opeNum == COMPRESS) { if (getFilestream()) { buildTree(); compress(); } } else if (opeNum == DECOMPRESS) { if (getFilestream()) { decompress(); } } else if (opeNum == EXIT) { quit = true; } } }
inline unsigned char dbToCh(double db) { unsigned char Ch = '\0'; for (int mv = 0; mv < 8; mv++) { Ch |= (unsigned char)((db * 2 >= 1) << (7 - mv)); db = 2 * db >= 1 ? 2 * db - 1 : 2 * db; } return Ch; }
inline double chToDb(unsigned char Ch) { double db = 0; for (int mv = 0; mv < 8; mv++) { db += pow(2, mv - 8) * ((Ch >> mv) & 0x01); } return db; }
|