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
| #include <bitset> #include <cstddef> #include <cstdlib> #include <fstream> #include <iostream> #include <queue> #include <string> #include <vector> using std::size_t; typedef unsigned char byte; struct Huff_Node { byte data; unsigned long weight; std::string str; Huff_Node *lchild; Huff_Node *rchild; Huff_Node(unsigned long w = 0, byte d = '\000', Huff_Node *l = nullptr, Huff_Node *r = nullptr) :data(d), weight(w), lchild(l), rchild(r) { } };
struct MyCmp { bool operator()(Huff_Node *L, Huff_Node *R) { return L && R ? L->weight > R->weight:L && !R; } };
struct GenerateTraverseString { void operator()(Huff_Node *p, std::string const &s = "") { if (p->lchild == nullptr && p->rchild == nullptr) { p->str = s; return; } this->operator()(p->lchild, s + '0'); this->operator()(p->rchild, s + '1'); } };
struct DeconstructTree { void operator()(Huff_Node *p) { if (p->lchild) this->operator()(p->lchild); if (p->rchild) this->operator()(p->rchild); delete p; } };
template <typename _VST> void traverse(Huff_Node *p, _VST visit) { visit(p); } bool encode(std::string const &FileName, std::string const &DestName) { std::ifstream fin; std::ofstream fout; fin.open(FileName, std::ios::binary); if (!fin) { std::cerr << "文件" << FileName << "打开失败" << std::endl; return false; } fout.open(DestName, std::ios::binary); if (!fout) { std::cerr << "文件" << DestName << "打开或创建失败" << std::endl; return false; } if (fin.peek() == EOF) { fout.close(); return true; } std::vector<Huff_Node *> Vec(256, nullptr); int c; byte count = 0; while ((c = fin.get()) != EOF) { if (Vec[c]) ++Vec[c]->weight; else { Vec[c] = new Huff_Node(1, c); ++count; } } Huff_Node *root = nullptr; if (count == 1) { for (size_t i = 0; i < 256; ++i) if (Vec[i] != nullptr) root = new Huff_Node(Vec[i]->weight, 0, new Huff_Node(), Vec[i]); } else { std::priority_queue<Huff_Node *, std::vector<Huff_Node *>, MyCmp> Q(Vec.begin(), Vec.end()); while (Q.size() > 1) { if (Q.top() == nullptr) { Q.pop(); continue; } Huff_Node *l = Q.top(); Q.pop(); Huff_Node *r = Q.top(); Q.pop(); Q.push(new Huff_Node(l->weight + r->weight, '\000', l, r)); } root = Q.top(); } traverse(root, GenerateTraverseString()); fin.clear(); fin.seekg(0); std::ofstream temp_out; char add_zero_length = 0; temp_out.open("temp.tmp", std::ios::binary); if (!temp_out) { std::cerr << "文件temp.tmp打开或创建失败" << std::endl; return false; } while ((c = fin.get()) != EOF) temp_out << Vec[c]->str; if (temp_out.tellp() % 8) { do { ++add_zero_length; temp_out << '0'; } while (temp_out.tellp() % 8); } temp_out.close(); fout.write((char*)&count, 1); for (size_t i = 0; i < 256; ++i) { Huff_Node *p = Vec[i]; if (p) { fout.write((char*)&p->data, 1); fout.write((char*)&p->weight, sizeof(unsigned long)); } } fout.write((char*)&add_zero_length, sizeof(add_zero_length)); std::ifstream temp_in; temp_in.open("temp.tmp", std::ios::binary); temp_in.seekg(0); char str[9] = {}; while (temp_in.peek() != EOF) { temp_in.read(str, 8); char _str = char(std::strtol(str, nullptr, 2)); fout.write(&_str, 1); } temp_in.close(); fout.close(); traverse(root, DeconstructTree()); return std::remove("temp.tmp") != EOF; } bool decode(std::string const &FileName, std::string const &DestName) { std::ifstream fin; std::ofstream fout; fin.open(FileName, std::ios::binary); if (!fin) { std::cerr << "文件" << FileName << "打开失败" << std::endl; return false; } fout.open(DestName, std::ios::binary); if (!fout) { std::cerr << "文件" << DestName << "打开或创建失败" << std::endl; return false; } if (fin.peek() == EOF) { fout.close(); return true; } std::vector<Huff_Node *> Vec(256, nullptr); int count = [](int c) { return c == 0 ? 256 : c; }(fin.get()); for (int i = 0; i < count; ++i) { byte c = fin.get(); Vec[c] = new Huff_Node(0, c); fin.read((char*)&Vec[c]->weight, sizeof(unsigned long)); } Huff_Node *root = nullptr; if (count == 1) { for (size_t i = 0; i < 256; ++i) if (Vec[i] != nullptr) root = new Huff_Node(Vec[i]->weight, 0, new Huff_Node(), Vec[i]); } else { std::priority_queue<Huff_Node *, std::vector<Huff_Node *>, MyCmp> Q(Vec.begin(), Vec.end()); while (Q.size() > 1) { if (Q.top() == nullptr) { Q.pop(); continue; } Huff_Node *l = Q.top(); Q.pop(); Huff_Node *r = Q.top(); Q.pop(); Q.push(new Huff_Node(l->weight + r->weight, '\000', l, r)); } root = Q.top(); } Huff_Node *p = root; traverse(p, GenerateTraverseString()); count = fin.get(); std::ofstream temp_out; temp_out.open("temp.tmp", std::ios::binary); if (!temp_out) { std::cerr << "文件temp.tmp打开或创建失败" << std::endl; return false; } int c; char str[9] = {}; while ((c = fin.get()) != EOF) temp_out << (std::bitset<8>(c).to_string()); std::streampos size = temp_out.tellp() - (std::streampos)count; temp_out.close(); std::ifstream tmp_in("temp.tmp", std::ios::binary); while (tmp_in.tellg() != size) { if (tmp_in.get() == '0') p = p->lchild; else p = p->rchild; if (p->lchild == NULL && p->rchild == NULL) { fout.write((char*)&p->data, 1); p = root; } } tmp_in.close(); fout.close(); traverse(root, DeconstructTree()); return remove("temp.tmp") != EOF; } int main() { bool flag1 = encode("test", "test.encode"); bool flag2 = decode("test.encode", "decode.test"); }
|