-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunLengthEncoding.h
60 lines (56 loc) · 1.37 KB
/
RunLengthEncoding.h
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
#include <bits/stdc++.h>
#include "BinaryStdOut.h"
#include "BinaryStdIn.h"
using namespace std;
#ifndef COMPRESSION_LIBRARY_RUNLENGTHENCODING_H
#define COMPRESSION_LIBRARY_RUNLENGTHENCODING_H
class RunLengthEncoding {
private:
BinaryStdIn in;
BinaryStdOut out;
public:
explicit RunLengthEncoding(std::string && source, std::string && destination)
: in(BinaryStdIn(std::move(source))), out(BinaryStdOut(std::move(destination))) {}
void expand()
{
bool b = false;
while (!in.isEmpty())
{
unsigned char cnt = in.readChar();
for (int i = 0; i < (int)cnt; i++)
{
out.write(b);
}
b = !b;
}
out.close();
}
void compress()
{
unsigned char cnt = 0;
bool b, old = false;
while (!in.isEmpty())
{
b = in.readBoolean();
if (b != old)
{
out.write(cnt);
cnt = 0;
old = !old;
}
else
{
if (cnt == 255)
{
out.write(cnt);
cnt = 0;
out.write(cnt);
}
}
cnt++;
}
out.write(cnt);
out.close();
}
};
#endif //COMPRESSION_LIBRARY_RUNLENGTHENCODING_H