-
Notifications
You must be signed in to change notification settings - Fork 2
/
decompress.cpp
111 lines (96 loc) · 3.14 KB
/
decompress.cpp
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
#include <string>
#include <chrono>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
struct keyComparator {
bool operator()(const int a, const int b) const {
return a > b;
}
};
void decompress(std::string decompressed_file_path, std::string compressed_file_path)
{
std::string line; int num ;
std::map<int, std::set<int>, keyComparator> conversion_dictionary;
std::map<int, std::set<int>, keyComparator> new_conversion_dictionary;
std::ifstream compressed_file(compressed_file_path);
if (!compressed_file.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
return ;
}
std::ofstream outFile;
outFile.open(decompressed_file_path, std::ofstream::out | std::ofstream::trunc);
if (!outFile.is_open()) {
std::cerr << "Error opening file." << std::endl;
return ; // Return an error code
}
bool dictionary_ended = false;
while(std::getline(compressed_file, line))
{
if(!dictionary_ended)
{
// store dictionary
int key;
std::istringstream iss(line);
iss >> key ;;
while(iss >> num)
{
conversion_dictionary[key].insert(num);
}
}else{
// process compressed transactions
std::istringstream iss(line);
while(iss>>num)
{
if(num > 0)
{
outFile << num << " ";
}else{
for(auto ele : new_conversion_dictionary[num])
outFile << ele << " ";
}
}
outFile << "\n" ;
}
if(!dictionary_ended and line.length() == 0)
{
dictionary_ended = true;
// decompress dictionary
for(auto const &pr : conversion_dictionary)
{
for(auto ele : pr.second)
{
if (ele < 0)
{
for(auto element : new_conversion_dictionary[ele])
{
new_conversion_dictionary[pr.first].insert(element);
}
}else{
new_conversion_dictionary[pr.first].insert(ele);
}
}
}
conversion_dictionary.clear();
}
}
compressed_file.close();
outFile.close();
}
int main(int argc, const char *argv[])
{
// Record the start time
auto startTime = std::chrono::high_resolution_clock::now();
std::string compressed_file_path = argv[1];
std::string decompressed_file_path = argv[2];
decompress(decompressed_file_path, compressed_file_path);
// Record the end time
auto endTime = std::chrono::high_resolution_clock::now();
// Calculate the duration
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
// Print the duration in seconds
std::cout << "Time taken: " << duration.count() / 1000.0 << " seconds" << std::endl;
return EXIT_SUCCESS;
}