-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBWTdec.cpp
More file actions
89 lines (75 loc) · 2.19 KB
/
BWTdec.cpp
File metadata and controls
89 lines (75 loc) · 2.19 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
// Inverse BWT Transform
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <list>
// Parameters
std::string EncInputString;
std::string decodedout;
//Read file
void DecReadFile(std::string inputFileName)
{
// read file and put it to string
std::ifstream inputStream(inputFileName);
std::stringstream sstream;
sstream << inputStream.rdbuf();
EncInputString = sstream.str();
size_t dataSize = EncInputString.size();
}
void DecWriteFile(std::string outputFileName)
{
// write the vector output to file
std::ofstream outStream(outputFileName, std::ofstream::out | std::ofstream::binary);
long outSize = decodedout.size();
for (int i = 0; i < outSize; i++)
{
outStream.write((&decodedout[i]), sizeof(char));
}
}
void invert(std::string bwt_arr)
{
int i, len_bwt = bwt_arr.size();
std::vector<uint8_t> sorted_bwt(len_bwt);
std::copy(bwt_arr.begin(),bwt_arr.end(),sorted_bwt.begin());
int *l_shift = new int[len_bwt];
// Index at which original string appears
int x = bwt_arr.find((char)2);
if (x == std::string::npos)
std::cout << "ERROR: char not found";
// Sorts the characters of bwt_arr[] alphabetically
std::sort(sorted_bwt.begin(), sorted_bwt.end());
// Vector of Lists for every Character
std::vector<std::list<int>> arr(256);
// Adds the indices of every character to its list in arr ascendingly
for (i = 0; i < len_bwt; i++)
{
arr[ (uint8_t)bwt_arr[i] ].push_back(i);
}
// takes the shifted version of each character
for (i = 0; i < len_bwt; i++)
{
l_shift[i] = arr[(uint8_t)sorted_bwt[i] ].front();
arr[(uint8_t)sorted_bwt[i] ].pop_front();
}
// set size of string
decodedout.reserve(len_bwt);
// Decodes the bwt
for (i = 0; i < len_bwt; i++)
{
x = l_shift[x];
decodedout += bwt_arr[x];
}
// remove last character
decodedout.pop_back();
}
int BWTdec(std::string inputFileName, std::string outputFileName)
{
DecReadFile(inputFileName);
invert(&EncInputString[0]);
DecWriteFile(outputFileName);
return 0;
}