-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.h
More file actions
109 lines (99 loc) · 2.73 KB
/
header.h
File metadata and controls
109 lines (99 loc) · 2.73 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
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <sys/stat.h>
template <typename Iterator>
Iterator compress(const std::string &uncompressed, Iterator result) {
// Build the dictionary.
int dictSize = 256;
std::map<std::string,int> dictionary;
for (int i = 0; i < 256; i++)
dictionary[std::string(1, i)] = i;
std::string w;
for (std::string::const_iterator it = uncompressed.begin();
it != uncompressed.end(); ++it) {
char c = *it;
std::string wc = w + c;
if (dictionary.count(wc))
w = wc;
else {
*result++ = dictionary[w];
// Add wc to the dictionary. Assuming the size is 4096!!!
if (dictionary.size()<4096)
dictionary[wc] = dictSize++;
w = std::string(1, c);
}
}
// Output the code for w.
if (!w.empty())
*result++ = dictionary[w];
return result;
}
// Decompress a list of output ks to a string.
// "begin" and "end" must form a valid range of ints
template <typename Iterator>
std::string decompress(Iterator begin, Iterator end) {
// Build the dictionary.
int dictSize = 256;
std::map<int,std::string> dictionary;
for (int i = 0; i < 256; i++)
dictionary[i] = std::string(1, i);
std::string w(1, *begin++);
std::string result = w;
std::cout << result<<"???:::\n";
std::string entry;
for ( ; begin != end; begin++) {
int k = *begin;
if (dictionary.count(k))
entry = dictionary[k];
else if (k == dictSize)
entry = w + w[0];
else
throw "Bad compressed k";
result += entry;
// Add w+entry[0] to the dictionary.
if (dictionary.size()<4096)
dictionary[dictSize++] = w + entry[0];
w = entry;
}
return result;
}
std::string int2BinaryString(int c, int cl) {
std::string p = ""; //a binary code string with code length = cl
int code = c;
while (c>0) {
if (c%2==0)
p="0"+p;
else
p="1"+p;
c=c>>1;
}
int zeros = cl-p.size();
if (zeros<0) {
std::cout << "\nWarning: Overflow. code " << code <<" is too big to be coded by " << cl <<" bits!\n";
p = p.substr(p.size()-cl);
}
else {
for (int i=0; i<zeros; i++) //pad 0s to left of the binary code if needed
p = "0" + p;
}
return p;
}
int binaryString2Int(std::string p) {
int code = 0;
if (p.size()>0) {
if (p.at(0)=='1')
code = 1;
p = p.substr(1);
while (p.size()>0) {
code = code << 1;
if (p.at(0)=='1')
code++;
p = p.substr(1);
}
}
return code;
}