Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBinaryIO.cpp
More file actions
Latest commit
139 lines (114 loc) · 3.92 KB
/
Copy pathBinaryIO.cpp
File metadata and controls
139 lines (114 loc) · 3.92 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
#include<assert.h>
#include"BinaryIO.h"
voiddecompose_string_samples(const std::vector<DataSample<std::string> > &samples,
std::vector<DataSample<uint32> > &indexes,
std::string &text)
{
text = "";
indexes.resize(samples.size());
uint32 index = 0;
for (unsigned i = 0; i < samples.size(); i++) {
indexes[i].time = samples[i].time;
indexes[i].value = index;
indexes[i].weight = samples[i].weight;
indexes[i].stddev = samples[i].stddev;
index += samples[i].value.length();
}
text = "";
text.reserve(index);
for (unsigned i = 0; i < samples.size(); i++) {
text += samples[i].value;
}
};
voidcompose_string_samples(const std::vector<DataSample<uint32> > &indexes,
const std::string &text,
std::vector<DataSample<std::string> > &samples)
{
samples.resize(indexes.size());
// Add another sample at the end to get the index beyond the last char of text
for (unsigned i = 0; i < samples.size(); i++) {
samples[i].time = indexes[i].time;
uint32 begin = indexes[i].value;
uint32 end = i+1 < samples.size() ? indexes[i+1].value : text.length();
samples[i].value = text.substr(begin, end-begin);
samples[i].weight = indexes[i].weight;
samples[i].stddev = indexes[i].stddev;
}
};
BinaryWriter::BinaryWriter(unsignedchar *start, unsignedchar *end) :
m_ptr(start), m_end(end) {}
boolBinaryWriter::eof() const { return m_ptr >= m_end; }
// vector<DataSample<string> >
voidBinaryWriter::write(const std::vector<DataSample<std::string> > &samples) {
std::vector<DataSample<uint32> > indexes;
std::string text;
decompose_string_samples(samples, indexes, text);
write(indexes);
write(text);
}
size_tBinaryWriter::write_length(const std::vector<DataSample<std::string> > &samples) {
// Trading shorter code for less efficiency. If we need to speed
// this up we can calculate the length without computing the value
std::vector<DataSample<uint32> > indexes;
std::string text;
decompose_string_samples(samples, indexes, text);
returnBinaryWriter::write_length(indexes) + BinaryWriter::write_length(text);
}
// string
voidBinaryWriter::write(const std::string &text) {
// align 4-byte
constunsignedchar *endptr = m_ptr + write_string_length(text.length());
write((uint32) text.length());
write_bytes(text.c_str(), text.length());
write_zeros(endptr - m_ptr);
}
size_tBinaryWriter::write_length(const std::string &text) {
returnwrite_string_length(text.length());
}
size_tBinaryWriter::write_string_length(size_t len) {
returnwrite_length((uint32)0) + (len + 3) / 4 * 4; // Pad to nearest 4-byte boundary
}
////
voidBinaryWriter::write_bytes(constvoid *p, size_t len) {
tassert(m_ptr + len <= m_end);
memcpy(m_ptr, p, len);
m_ptr += len;
}
voidBinaryWriter::write_zeros(size_t len) {
assert(m_ptr + len <= m_end);
memset(m_ptr, 0, len);
m_ptr += len;
}
///////////////////////////////
BinaryReader::BinaryReader(constunsignedchar *start, constunsignedchar *end) :
m_ptr(start), m_end(end) {}
boolBinaryReader::eof() const { return m_ptr >= m_end; }
// vector<DataSample<string> >
voidBinaryReader::read(std::vector<DataSample<std::string> > &samples) {
std::vector<DataSample<uint32> > indexes;
std::string text;
read(indexes);
read(text);
compose_string_samples(indexes, text, samples);
}
// string
voidBinaryReader::read(std::string &text) {
// align 4-byte
constunsignedchar *startptr = m_ptr;
uint32 len;
read(len);
constunsignedchar *endptr = startptr + BinaryWriter::write_string_length(len);
text.resize(len);
read_bytes(&text[0], len);
skip_bytes(endptr - m_ptr);
}
////
voidBinaryReader::read_bytes(void *dest, size_t len) {
assert(m_ptr + len <= m_end);
memcpy(dest, m_ptr, len);
m_ptr += len;
}
voidBinaryReader::skip_bytes(size_t len) {
assert(m_ptr + len <= m_end);
m_ptr += len;
}