forked from redinator2000/signed_RLE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigned_RLE.cpp
More file actions
185 lines (179 loc) · 5.16 KB
/
signed_RLE.cpp
File metadata and controls
185 lines (179 loc) · 5.16 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
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
177
178
179
180
181
182
183
184
185
#include "signed_RLE.hpp"
#include <limits>
#include <string.h>
#include <stdlib.h>
namespace signed_RLE
{
template<class T>
size_t push_end_chars(std::vector<int8_t> * v, const T data)
{
size_t start = v->size();
v->insert(v->end(), (int8_t*)&data, (int8_t*)&data + sizeof(T));
return start;
}
std::vector<int8_t> uncompress(const std::vector<int8_t> & compressed, int limit)
{
std::vector<int8_t> out = {};
enum class Uncomp_State
{
start,
big_number,
repeat_data,
unique_data
};
Uncomp_State state = Uncomp_State::start;
int count;
int8_t bigcount[sizeof(int32_t)];
int bigcount_builder;
for(auto c : compressed)
{
switch(state)
{
default:
case Uncomp_State::start:
if(c == 0)
{
state = Uncomp_State::big_number;
bigcount_builder = 0;
}
else if(c > 0)
{
count = c;
state = Uncomp_State::repeat_data;
}
else//(c < 0)
{
count = abs(c);
state = Uncomp_State::unique_data;
}
break;
case Uncomp_State::big_number:
//printf("building big number '%d'\n", c);
bigcount[bigcount_builder] = c;
bigcount_builder++;
if(bigcount_builder >= sizeof(int32_t))
{
int32_t copycount;
memcpy(©count, bigcount, sizeof(int32_t));
//printf("built big number %d\n", copycount);
if(copycount == 0)
{
return out;
}
else if(copycount > 0)
{
count = copycount;
state = Uncomp_State::repeat_data;
}
else//(copycount < 0)
{
count = abs(copycount);
state = Uncomp_State::unique_data;
}
break;
}
break;
case Uncomp_State::repeat_data:
//printf("putting in repeat %d x '%d'\n", count, c);
out.insert(out.end(), count, c);
if(limit && out.size() > limit)
return out;
state = Uncomp_State::start;
break;
case Uncomp_State::unique_data:
//printf("putting in unique '%d', %d remaining\n", c, count);
out.push_back(c);
if(limit && out.size() > limit)
return out;
count--;
if(count <= 0)
state = Uncomp_State::start;
break;
}
}
return out;
}
std::vector<int8_t> compress(const std::vector<int8_t> & raw)
{
std::vector<int8_t> out = {};
if(raw.empty())
return out;
struct Segment
{
int32_t count = 0;
int8_t similar;
std::vector<int8_t> uniques = {};
};
std::vector<Segment> segments = {};
{
Segment builder_segments;
builder_segments.similar = raw[0] + 1;
for(auto r : raw)
{
if(r == builder_segments.similar)
builder_segments.count++;//todo: split segments if goes above int32_t count
else
{
if(builder_segments.count)
segments.push_back(builder_segments);
builder_segments.count = 1;
builder_segments.similar = r;
builder_segments.uniques.clear();
}
}
if(builder_segments.count)
segments.push_back(builder_segments);
}
std::vector<Segment> uniqued_segments = {};
{
Segment builder_uniqued;
builder_uniqued.count = 0;
for(auto s : segments)
{
if(s.count > 3)
{
if(builder_uniqued.count)
{
uniqued_segments.push_back(builder_uniqued);
builder_uniqued.count = 0;
builder_uniqued.uniques.clear();
}
uniqued_segments.push_back(s);
}
else
{
builder_uniqued.count -= s.count;
builder_uniqued.uniques.insert(builder_uniqued.uniques.end(), s.count, s.similar);
}
}
if(builder_uniqued.count)
uniqued_segments.push_back(builder_uniqued);
}
for(auto s : uniqued_segments)
{
if(s.count > 0)
{
if(s.count >= std::numeric_limits<int8_t>::max())
{
out.push_back(0);
push_end_chars<int32_t>(&out, s.count);
}
else
out.push_back(static_cast<int8_t>(s.count));
out.push_back(s.similar);
}
else
{
if(s.count <= std::numeric_limits<int8_t>::min())
{
out.push_back(0);
push_end_chars<int32_t>(&out, s.count);
}
else
out.push_back(static_cast<int8_t>(s.count));
out.insert(out.end(), s.uniques.begin(), s.uniques.end());
}
}
return out;
}
};