void HuffmanByteDec::build(const HuffmanCode& hcode) { std::vector<CodeInfo> pcode(hcode.size()); for (unsigned int i = 0; i < hcode.size(); ++i) pcode[i] = CodeInfo(hcode[i], i); sort(pcode.begin(), pcode.end()); _size = hcode.size(); int tb = 0; create(pcode, 0, pcode.size(), tb); }
TEST(huffman, test1) { const int n = 5; int arr[n] = {6, 5, 3, 3, 1}; vector<unsigned int> v; for (int i = 0; i < n; i++) v.push_back(arr[i]); HuffmanCode code; int tt = code.build(v); ASSERT_EQ(tt, 6*2 + 5*2 + 3*2 + 3*3 + 1*3); }
TEST(huffman, test2) { const int n = 4; int arr[n] = {1, 2, 1, 4};// vector<unsigned int> v; for (int i = 0; i < n; i++) v.push_back(arr[i]); HuffmanCode code; int tt = code.build(v); ASSERT_EQ(tt, 14); ASSERT_EQ(1, code[3].second); ASSERT_EQ(2, code[1].second); ASSERT_EQ(3, code[0].second); ASSERT_EQ(3, code[2].second); }
int main() { HuffmanCode huffmanCA; huffmanCA.readFromFile(); huffmanCA.getFrequencyTable(); huffmanCA.createPriorityQueue(); huffmanCA.getHuffmanTable(); huffmanCA.encode(); huffmanCA.decode(); huffmanCA.compressToASCII(); huffmanCA.decompressASCII(); system("pause"); return 0; }
void HuffmanTree::build(const HuffmanCode& code) { //int i; tree.resize(1); tree.reserve(code.size() - 1); tree[0].first = 0; tree[0].second = 0; _size = code.size(); for (int i = 1; i <= (int)code.size(); i++) { unsigned int val = code[i-1].first; unsigned int node = 0; for (unsigned int j = 0; j < code[i-1].second - 1; ++j) { bool b = (val & 1) > 0; if (!b) { if (tree[node].first > 0) node = tree[node].first; else { tree.push_back(std::make_pair(-i, -i)); tree[node].first = tree.size() - 1; node = tree.size() - 1; } } else { if (tree[node].second > 0) node = tree[node].second; else { tree.push_back(std::make_pair(-i, -i)); tree[node].second = tree.size() - 1; node = tree.size() - 1; } } val >>= 1; } bool b = (val & 1) > 0; if (!b) { if (tree[node].first > 0) throw std::runtime_error("invalid prefix code"); tree[node].first = -i; } else { if (tree[node].second > 0) throw std::runtime_error("invalid prefix code"); tree[node].second = -i; } } }
void test_huffdec(unsigned int n, unsigned int syncnt) { vector<unsigned int> vals; for (unsigned int i = 0; i < n; ++i) { vals.push_back(rand() % syncnt); } HuffmanCode hc; HuffmanDec dec; hc.build(HuffmanCode::comp_weight(syncnt, vals.begin(), vals.end())); dec.build(hc); for (unsigned int i = 0; i < n; ++i) { auto v = vals[i]; auto x = hc.encode(v); auto y = dec.decode2(x.first); if (v != y.first) { cout << v << endl; cout << i << endl; dec.decode2(x.first); } ASSERT_EQ(v, y.first); ASSERT_EQ(x.second, y.second); } }
// QUESTION 2 void HuffmanTree::dfs(HuffmanCode& hc, TreeNode *tn, string &s) { // if this is a leaf add it to the map if(tn->word != nullptr){ hc.insert( pair<string, string>(*(tn->word), s)); } //if the 0 child is not null, call dfs on it if(tn->children[0] != nullptr) { s = s + "0"; dfs(hc, tn->children[0], s); s = s.substr(0, s.length() - 1); } //if the 0 child is not null, call dfs on it if(tn->children[1] != nullptr) { s = s + "1"; dfs(hc, tn->children[1], s); s = s.substr(0, s.length() - 1); } }
int main() { int total[26] = { 0 }; ifstream infile("TextCount.txt"); if (!infile) { cout << "Error opening input file" << endl; return 0; } string str((istreambuf_iterator<char>(infile)),istreambuf_iterator<char>());//conversion of filename /*************************************************************************************** * Title: parsing ifstream to string * Author: SiCrane * Date: 2005 * Code version: n/a * Availability:http://www.gamedev.net/topic/353162-reading-a-whole-file-into-a-string-with-ifstream/ * ***************************************************************************************/ HuffmanCode* test = new HuffmanCode(str); string b; test->displayTable(); test->displayHuffmanTable(); string code = test->getEncodedString(); cout << "Encoded string: " << code << endl; cout << "Decoded string: " << test->decodeString(code) << endl; b = test->decodeString(code); std::ofstream out("output.txt"); out << b; out.close(); delete test; cin.get(); return 0; }
HuffmanTree::HuffmanTree(const HuffmanCode &hc) { // get all the code string allCode[hc.size()]; string allWord[hc.size()]; int k = 0; for (map<string, string>::const_iterator it = hc.begin(); it != hc.end(); it++){ // check if the code is made of 0 and 1 if(!isZeroOne(it->second)){ throw 2; } else { allCode[k] = it->second; allWord[k] = it->first; k++; } } // check if any of the code is the prefix of another for(int i = 0; i < sizeof(allCode) / sizeof(string); i++){ if(isPrefixOfAnother(allCode[i], allCode)){ throw 1; } } // create root root = new TreeNode(); iter = root; for (int j = 0; j < sizeof(allCode) / sizeof(string); j++) { // create path to the leaf int i; for(i = 0; i < allCode[j].length() - 1; i++){ if(allCode[j].at(i) == '0'){ if(iter->children[0] == nullptr){ iter->children[0] = new TreeNode(); iter = iter->children[0]; } else { iter = iter->children[0]; } } else { if(iter->children[1] == nullptr){ iter->children[1] = new TreeNode(); iter = iter->children[1]; } else { iter = iter->children[1]; } } } if(allCode[j].at(i) == '0'){ if(iter->children[0] == nullptr){ iter->children[0] = new TreeNode(allWord[j]); iter = iter->children[0]; } else { iter = iter->children[0]; } } else { if(iter->children[1] == nullptr){ iter->children[1] = new TreeNode(allWord[j]); iter = iter->children[1]; } else { iter = iter->children[1]; } } resetIterator(); } }
void HuffmanByteDec::build(const std::vector<HuffmanCode::WeightTp>& W) { HuffmanCode code; code.build(W); build(code); }