-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.cpp
More file actions
executable file
·124 lines (113 loc) · 3.12 KB
/
Decoder.cpp
File metadata and controls
executable file
·124 lines (113 loc) · 3.12 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
118
119
120
121
122
123
124
#include "Decoder.h"
using namespace std;
//metodo che restituisce la frequenza dei caratteri
void Decoder::readInfo(string in)
{
const char* inFile = in.c_str();
ifstream ifile(inFile,ios::binary);
if (ifile.fail())
{
cerr<<RED<<"Impossibile aprire il file di input"<<endl;
exit(1);
}
else
{
char ch = 0,c = 0;
//Leggo cumfreq
ch = ifile.get();
cumfreq = (cumfreq<<8)|(ch&255);
int z = cumfreq;
do
{
int val = 0;
//Leggo carattere
ch = ifile.get();
//Leggo occorrenza
c = ifile.get();
val = (val<<8)|(c&255);
occ.insert(pair<char,int>(ch,val));
freq.insert(pair<char,double>(ch,0.0));
lookupTable.insert(pair<char,double>(ch,0.0));
z -= val;
}
while (ifile.good() && z > 0);
//intero a 64 bit
long long iCode = 0;
for (int i = 0;i < 8; ++i)
{
ch = ifile.get();
iCode = (iCode << 8)|(ch&255);
}
code = (double &)iCode;
cout<<YELLOW<<" >> Codice : "<<code<<endl;
}
ifile.close();
}
//calcolo occorrenze totali
void Decoder::model()
{
map<char,double>::iterator itFreq = freq.begin();
map<char,int>::iterator itOcc = occ.begin();
while ( itFreq != freq.end())
{
itFreq->second = static_cast<double>(itOcc->second) / cumfreq;
++itFreq;
++itOcc;
}
}
//aggiornamento mappa
void Decoder::updateMap(double low, double high)
{
map<char,double>::iterator itTable = lookupTable.begin();
map<char,double>::iterator itFreq = freq.begin();
double lowOld = low;
while ( itTable != lookupTable.end())
{
itTable->second = low;
low += (high - lowOld) * itFreq->second;
++itTable;
++itFreq;
}
}
//processo di decodifica
void Decoder::decoding(string in,string out)
{
readInfo(in);
cout<<YELLOW<<" >> Calcolo delle occorrenze..."<<endl;
model();
cout<<YELLOW<<" >> Calcolo delle probabilità..."<<endl;
updateMap(0.0,1.0);
cout<<YELLOW<<" >> Processo di decodifica avviato..."<<endl<<" ";;
const char* outFile = out.c_str();
ofstream ofile(outFile,ios::trunc | ios::binary);
if (ofile.fail())
{
cerr<<RED<<"Impossibile aprire il file"<<endl;
exit(1);
}
double high = 1.0,low = 0.0,lowOld;
char ch = 0;
int i = 0;
map<char,double>::iterator it,itOld;
while ( i < cumfreq)
{
cout<<GREEN<<".";
it = lookupTable.begin();
while ( code >= it->second && it != lookupTable.end() )
{
itOld = it;
++it;
}
ch = itOld->first;
ofile.put((ch&255));
low = itOld->second;
if (it != lookupTable.end())
{
high = it->second;
}
updateMap(low,high);
++i;
}
ofile.close();
cout<<GREEN<<endl<<"\a >> File decodificato"<<endl;
}