-
Notifications
You must be signed in to change notification settings - Fork 0
/
RleData.cpp
176 lines (154 loc) · 4.57 KB
/
RleData.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "RleData.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void RleData::Compress(const char* input, size_t inSize)
{
// reset members
delete[] mData;
mSize = 0;
// dynamically allocate array
mData = new char[2 * inSize];
int count_repeats = 1; // positive run counts
int count_uniques = 1; // negative run counts
size_t max = MaxRunSize()-1; // max count
for (size_t i = 0; i < inSize-1; i++)
{
// two equal values
if (*(input+i) == *(input+i+1))
{
count_repeats++;
i++;
// count length of positive run
for(size_t j = i; j < inSize-1; j++)
{
if (*(input+j) != *(input+j+1) || ((j % max) == 0))
{
break;
}
count_repeats++;
}
// fill in mData with the positive run count/characters
if (count_repeats >= 1)
{
mData[mSize] = count_repeats;
mData[mSize+1] = *(input+i);
mSize += 2;
}
// update counter
i += (count_repeats-2);
count_repeats = 1;
// case of middle single value
if (*(input+i+1) != *(input+i+2) && *(input+i+2) == *(input+i+3))
{
mData[mSize] = 1;
mData[mSize+1] = *(input+i+1);
mSize += 2;
i++;
}
// case of last single value
if (i == inSize-2)
{
mData[mSize] = 1;
mData[mSize+1] = *(input+i+1);
mSize += 2;
}
}
// two unequal values
else if (*(input+i) != *(input+i+1))
{
if (i == 0 && *(input+i+1) == *(input+i+2))
{
mData[mSize] = 1;
mData[mSize+1] = *(input+i);
mSize += 2;
}
else
{
size_t start = i;
count_uniques++;
i++;
// count length of negative run
for(; i < inSize-1; i++)
{
if (*(input+i) == *(input+i+1))
{
count_uniques -= 1;
i--;
break;
}
count_uniques++;
// exceed max count
if (count_uniques % 127 == 0) {
i++;
break;
}
}
if (count_uniques > 1)
{
// fill in mData with negative run count
mData[mSize] = count_uniques*-1;
mSize++;
// fill in mData with characters
for (; start <= i; start++)
{
mData[mSize] = input[start];
mSize++;
}
}
// update counter
count_uniques = 1;
// case of last single value
if (i == inSize-2)
{
mData[mSize] = 1;
mData[mSize+1] = *(input+i+1);
mSize += 2;
}
}
}
}
}
void RleData::Decompress(const char* input, size_t inSize, size_t outSize)
{
// reset members
delete[] mData;
mSize = 0;
// allocate memory
mData = new char[outSize];
for (size_t i = 0; i < inSize; i++)
{
int count = static_cast<int>(*(input+i)); // cast run count to int
// positive count
if (count > 0)
{
for (int j = 0; j < count; j++)
{
mData[mSize] = *(input+i+1);
mSize++;
}
i++;
}
// negative count
else
{
count *= -1; // normalize count
for (int j = 0; j < count; j++)
{
mData[mSize] = *(input+i+j+1);
mSize++;
}
i += count;
}
}
}
std::ostream& operator<< (std::ostream& stream, const RleData& rhs)
{
for (int i = 0; i < rhs.mSize; i++)
{
stream << rhs.mData[i];
}
return stream;
}