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
|
#include<iostream>
#include<fstream>
#include<map>
#include<algorithm>
#include<vector>
#include<string>
#include<time.h>
#include<deque>
#include<sstream>
struct node {
char m_data{};
std::size_t m_frequency{};
node* m_left;
node* m_right;
node() : m_left{ nullptr }, m_right{ nullptr } {}
node(node* left, node* right) :m_left{ left }, m_right{ right } {
m_frequency = m_left->m_frequency + m_right->m_frequency;
}
};
struct functor {
bool operator()(const node* one, const node* two) const { return one->m_frequency > two->m_frequency; }
};
class Huffman {
private:
std::map<char, std::size_t> m_freq_table;
std::map<char, std::vector<bool> > m_key;
std::vector<bool> m_code;
std::deque<node*> m_nodeData;
std::string m_fileName;
std::string m_fileExten;
std::string m_fileContent;
node* m_root{ nullptr };
/*shared functions*/
void readFile(const std::string& filePath);
template<class T>
void release(T& arg) { arg.clear(); arg.shrink_to_fit(); }
/*shared functions*/
/*compressor related functions*/
void storeFreqTable(const std::map<char, std::size_t>& table);
void createBinaryFile(const std::string& filePath);
void createKey(const std::string& filePath) const;
void setNameAndExten(const std::string& filePath);
void encode(const node* const &root);
void deleteTree(node* root);
void UpdateFreqTable();
node* makeTree();
/*compressor related functions*/
/*decompressor related functions*/
void readKey(const std::string& keyPath);
void reCreateThree(const std::map<char,std::vector<bool> >& key);
/*decompressor related functions*/
public:
void compress(const std::string& filePath, const std::string& locToCreateKey, const std::string& locToCompress);
void decompress(const std::string& filePath, const std::string& keyPath, const std::string& locToDecompress);
};
void Huffman::storeFreqTable(const std::map<char, std::size_t>& table) {
for (auto&& index : table) {
node* leaf = new node;
leaf->m_data = index.first;
leaf->m_frequency = index.second;
m_nodeData.emplace_back(leaf);
}//end of for loop
m_freq_table.clear();
}
void Huffman::createBinaryFile(const std::string& filePath) {
char offSet{}; char buffer{};
std::ofstream outFile(filePath + m_fileName + "Compressed.bin", std::ios::binary);
for (const auto& data : m_fileContent) {
buffer = data;
m_code = m_key[buffer];
buffer = 0;
for (const auto& index : m_code) {
buffer |= index << (7 - offSet);
++offSet;
if (offSet == 8) {
offSet = 0;
outFile.put(buffer);
buffer = 0;
}//end of if
}//end of for loop
}//end of for loop
outFile.close();
release<std::vector<bool> >(m_code);
release<std::string>(m_fileContent);
}
void Huffman::createKey(const std::string& filePath) const {
std::ofstream key(filePath + m_fileName + "Key.bin", std::ios::binary);
auto&& index = m_key.begin();
do {
key.put(index->first);
key.put(' ');
for (const auto& itr : index->second) {
if (itr) key.put('1');
else key.put('0');
}//end of for
++index;
if (index != m_key.end()) key.put(' ');
} while (index != m_key.end());
key << m_fileExten;
key.close();
}
void Huffman::setNameAndExten(const std::string& filePath) {
std::size_t foundName = filePath.find_last_of("/\\");
std::size_t foundExten = filePath.find_last_of('.');
m_fileName = filePath.substr(foundName + 1, foundExten - foundName - 1);
m_fileExten = filePath.substr(foundExten);
}
void Huffman::readFile(const std::string& filePath) {
std::ifstream inFile(filePath, std::ios::binary);
auto const start_pos = inFile.tellg();
inFile.ignore(std::numeric_limits<std::streamsize>::max());
std::streamsize char_count = inFile.gcount();
inFile.seekg(start_pos);
m_fileContent = std::string(static_cast<std::size_t>(char_count), '0');
inFile.read(&m_fileContent[0], static_cast<std::streamsize> (m_fileContent.size()));
inFile.close();
}
void Huffman::encode(const node* const &root) {
if (root->m_left != nullptr) {
m_code.emplace_back(false);
encode(root->m_left);
}//end of if
if (root->m_right != nullptr) {
m_code.emplace_back(true);
encode(root->m_right);
}//end of if
if (root->m_data) m_key[root->m_data] = m_code;
if (!m_code.empty()) m_code.pop_back();
}
void Huffman::deleteTree(node* root) {
if (root != nullptr) {
deleteTree(root->m_left);
deleteTree(root->m_right);
delete root;
}//end of if
}
void Huffman::UpdateFreqTable() {
for (const auto& data : m_fileContent) {
m_freq_table[data]++;
}//end of for loop
}
node* Huffman::makeTree() {
while (m_nodeData.size() > 1) {
std::sort(m_nodeData.begin(), m_nodeData.end(), functor());
node* leftSon = m_nodeData.back();
m_nodeData.pop_back();
node* rightSon = m_nodeData.back();
m_nodeData.pop_back();
node* parent = new node(leftSon, rightSon);
m_nodeData.emplace_back(parent);
}//end of while loop
m_nodeData.shrink_to_fit();
return m_nodeData.front();
}
void Huffman::compress(const std::string& filePath,const std::string& locToCreateKey,const std::string& locToCompress) {
setNameAndExten(filePath);
readFile(filePath);
UpdateFreqTable();
storeFreqTable(m_freq_table);
m_root = makeTree();
encode(m_root);
createBinaryFile(locToCompress);
createKey(locToCreateKey);
deleteTree(m_root);
m_root = nullptr;
release<std::string>(m_fileExten);
release<std::string>(m_fileName);
}
void Huffman::readKey(const std::string& keyPath) {
char buffer;
readFile(keyPath);
for (std::size_t index{}; index < m_fileContent.length(); index++) {
buffer = m_fileContent[index];
index += 2;
do {
if (m_fileContent[index] == '1') m_code.emplace_back(true);
else if(m_fileContent[index] == '0') m_code.emplace_back(false);
++index;
} while ((m_fileContent[index] != ' ') && (m_fileContent[index] != '.'));
if (m_fileContent[index] == '.') {
m_fileExten = m_fileContent.substr(index, (m_fileContent.length() - 1));
index = m_fileContent.length();
}//end of if
else {
m_key[buffer] = m_code;
m_code.clear();
}//end of else
}//end of for
release<std::string>(m_fileContent);
release<std::vector<bool> >(m_code);
}
void print(node* root, unsigned k = 0) {
if (root->m_left != nullptr) {
print(root->m_left, k + 3);
for (unsigned i = 0; i < k; i++) {
std::cout << "--";
}
if (root->m_data) std::cout << "(" << root->m_data << ")" << '\n';
print(root->m_right, k + 3);
}
}
void Huffman::reCreateThree(const std::map<char, std::vector<bool> >& key) {
m_root = new node;
node* temp = m_root;
for (auto&& index : key) {
for (const auto& itr : index.second) {
if (itr) {
node* rightSon = new node;
m_root->m_right = rightSon;
m_root = m_root->m_right;
}//end of if
else {
node* leftSon = new node;
m_root->m_left = leftSon;
m_root = m_root->m_left;
}//end of else
}//end of for
m_root->m_data = index.first;
m_root = temp;
}//end of for
if (m_root->m_right == nullptr) { std::cout << "no tree\n"; }
else
print(m_root);
}
void Huffman::decompress(const std::string& filePath, const std::string& keyPath, const std::string& locToDecompress) {
readKey(keyPath);
reCreateThree(m_key);
}
int main() {
clock_t tStart = clock();
Huffman huff;
Huffman dehuff;
//huff.compress("C:/Users/User/Desktop/test.txt", "C:/Users/User/Desktop/", "C:/Users/User/Desktop/");
dehuff.decompress("", "C:/Users/User/Desktop/testKey.bin","");
std::cout << static_cast<double>(clock() - tStart) / CLOCKS_PER_SEC << "(s)" << '\n';
return 0;
}
|