Repository navigation
Expand file tree
/
Copy pathBibleCount.cpp
More file actions
271 lines (255 loc) · 5.35 KB
/
Copy pathBibleCount.cpp
File metadata and controls
271 lines (255 loc) · 5.35 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <array>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
// Download text from
// https://raw.githubusercontent.com/mxw/grmr/master/src/finaltests/bible.txt
static std::string slurp(std::istream& in)
{
// Read entire stream into one contiguous string.
std::stringstream sstr;
sstr << in.rdbuf();
return sstr.str();
}
template <typename ByteSink>
struct BitCache {
// Packs variable-sized bit chunks into a 64-bit cache and emits bytes to sink.
explicit BitCache(ByteSink& fcn)
: sink(fcn)
{
cache = 0;
counter = 0;
}
void commit()
{
// Cache is full (64 bits): emit 8 bytes, least-significant byte first.
for (uint32_t j = 0; j < 8; ++j) {
sink.push(uint8_t(cache));
cache >>= 8U;
}
}
void push(uint64_t bits, uint32_t count)
{
// Append 'count' bits from 'bits' into cache, committing as needed.
if (counter + count > 64) {
cache |= bits << counter;
commit();
uint32_t remaining = 64 - counter;
cache = bits >> remaining;
counter = count - remaining;
} else if (counter + count == 64) {
cache |= bits << counter;
commit();
cache = 0;
counter = 0;
} else {
cache |= bits << counter;
counter += count;
}
}
void flush()
{
// Emit any partially filled bytes at end of stream.
while (counter > 0) {
sink.push(uint8_t(cache));
cache >>= 8U;
if (counter >= 8) {
counter -= 8;
} else {
counter = 0;
}
}
cache = 0;
}
uint64_t cache;
uint32_t counter;
ByteSink& sink;
};
template <typename Store, uint32_t MAXBITS>
struct VarCodeFilter {
// Encodes upper/lower case runs into variable-length codewords.
explicit VarCodeFilter(Store& s)
: store(s)
{
counter = 0;
}
void push(bool isupper)
{
// Uppercase emits full-length marker; lowercase extends the run.
if (isupper) {
pack();
store.push(1U << (MAXBITS - 1), MAXBITS);
} else {
counter++;
if (counter == MAXBITS - 1) {
pack();
}
}
}
void pack()
{
// Flush pending lowercase run as a shorter marker.
if (counter > 0) {
uint32_t nbits = MAXBITS - counter;
store.push(1U << (nbits - 1), nbits);
counter = 0;
}
}
void flush()
{
pack();
store.flush();
}
uint32_t counter;
Store& store;
};
struct ByteStorage
{
// Minimal growable byte buffer used by the bit writer.
ByteStorage()
{
count = 0;
capacity = 4096;
ptr = static_cast<uint8_t*>(::malloc(capacity));
}
ByteStorage(const ByteStorage& rhs)
{
*this = rhs;
}
ByteStorage& operator=(const ByteStorage& rhs)
{
if (this == &rhs) {
return *this;
}
::free(ptr);
count = rhs.count;
capacity = rhs.capacity;
ptr = static_cast<uint8_t*>(::malloc(rhs.capacity));
std::memcpy(ptr, rhs.ptr, count);
return *this;
}
ByteStorage(const ByteStorage&& rhs) = delete;
ByteStorage& operator=(const ByteStorage&& rhs) = delete;
~ByteStorage()
{
::free(ptr);
count = 0;
capacity = 0;
ptr = nullptr;
}
void push(uint8_t byte)
{
// Double capacity on demand.
if (count == capacity) {
capacity = 2 * capacity;
ptr = static_cast<uint8_t*>(::realloc(ptr, capacity));
}
ptr[count++] = byte;
}
uint32_t size() const
{
return count;
}
const uint8_t* begin() const
{
return ptr;
}
const uint8_t* end() const
{
return ptr + count;
}
uint8_t operator[](uint32_t index) const
{
return ptr[index];
}
uint8_t& operator[](uint32_t index)
{
return ptr[index];
}
uint8_t* ptr;
uint32_t count;
uint32_t capacity;
};
template <unsigned NBITS>
struct CompressedStream {
// Pipeline: VarCodeFilter -> BitCache -> ByteStorage.
CompressedStream()
: cache(store)
, varicode(cache)
{
}
void push(bool bit)
{
varicode.push(bit);
}
void flush()
{
varicode.flush();
}
using Store = ByteStorage;
using Cache = BitCache<Store>;
using Encoder = VarCodeFilter<Cache, NBITS>;
Store store;
Cache cache;
Encoder varicode;
};
static std::string to_binary_str(uint8_t v)
{
// Show byte as bit string (least-significant bit first).
char str[8];
for (uint32_t k = 0; k < 8; ++k) {
str[k] = (((v & 1) == 0) ? '0' : '1');
v >>= 1;
}
return std::string(str, 8);
}
void process_file(std::istream& ifs)
{
// Build one compressed stream per uppercase-normalized character.
using Stream = CompressedStream<3>;
std::array<Stream, 256> bstream;
std::string text = slurp(ifs);
uint32_t totalbytes = 0;
uint32_t origbytes = 0;
for (char ch : text) {
unsigned char uch = static_cast<unsigned char>(ch);
if (std::isprint(uch) != 0) {
// Normalize by uppercase character, store whether original was uppercase.
int upch = std::toupper(uch);
bool isupper = (upch == static_cast<int>(uch));
bstream[upch].push(isupper);
origbytes += 1;
}
}
for (uint32_t j = 0; j < 256; ++j) {
Stream& cs(bstream[j]);
cs.flush();
if (cs.store.size() != 0) {
totalbytes += cs.store.size();
std::cout << "Array " << j << " "
<< "[" << char(j) << "] "
<< cs.store.size() << " bytes " << std::endl;
for (uint8_t v : cs.store) {
std::cout << to_binary_str(v) << ' ';
}
std::cout << '\n';
}
}
std::cout << "Total original bytes: " << origbytes << "\n";
std::cout << "Total processed bytes: " << totalbytes << "\n";
}
int main(int argc, char* argv[])
{
if (argc < 2) {
process_file(std::cin);
} else {
std::ifstream ifs(argv[1]);
if (!ifs.good()) {
return 1;
}
process_file(ifs);
}
return 0;
}