-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
117 lines (78 loc) · 2.56 KB
/
Copy pathMain.cpp
File metadata and controls
117 lines (78 loc) · 2.56 KB
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
#include "Shannon.h"
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <unordered_map>
#include <fstream>
#include <chrono>
#include <string>
using namespace std;
using vi = vector<int>;
#define ARCHIEVE
#ifdef ARCHIEVE
int main() {
//------------------------------------------------READ---------------------------------------------//
auto _begin = std::chrono::steady_clock::now();
string path = "D:\\Programming\\C++\\Archiver\\allchars.txt";
std::ifstream file(path, std::ios::binary);
// Get the size of the file
file.seekg(0, std::ios::end);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// Create a vector to store the byte sequence
std::vector<char> buffer(fileSize);
// Read the file into the vector
file.read(buffer.data(), fileSize);
// Close the file
file.close();
auto _end = std::chrono::steady_clock::now();
auto _elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(_end - _begin);
std::cout << "The time: " << _elapsed_ms.count() << " ms\n";
//---------------------------------------------BUILD_CODE------------------------------------------//
auto begin = std::chrono::steady_clock::now();
//cout << "Message to code:\n";
//for (auto x : buffer)
// cout << x << ' ';
//cout << "\n\n";
auto [code, table] = ShannonCompress(buffer);
auto end = std::chrono::steady_clock::now();
auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
std::cout << "The time: " << elapsed_ms.count() << " ms\n";
cout << "msg:\t" << buffer.size() << "\ncode:\t" << code.size() / 8 << "\n\n\n"
<< "table.size(): " << table.size();
//cout << "encoded msg: ";
//for (auto x : code)
// cout << x;
cout << "\ntable for encoding:\n";
for (auto [key, val] : table) {
cout << key << ' ';
for (auto x : val)
cout << x;
cout << '\n';
}
//-----------------------------------------------WRITE---------------------------------------------//
std::ofstream outFile("compressed.bin", std::ios::binary);
int numBits = code.size();
int numBytes = (numBits + 7) / 8; // round up to nearest byte
for (int i = 0; i < numBytes; i++) {
char byte = 0;
for (int j = 0; j < 8; j++) {
int bitIndex = i * 8 + j;
if (bitIndex < numBits) {
if (code[bitIndex]) {
byte |= (1 << j);
}
}
}
outFile.write(&byte, sizeof(byte));
}
outFile.close();
//-----------------------------------------------DECODE-------------------------------------------//
}
#endif
// Ryan.jpg : 53 56 56
#ifndef ARCHIEVE
int main() {
}
#endif