-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.hxx
321 lines (268 loc) · 8.44 KB
/
index.hxx
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#ifndef HYPER_UTIL_BUFFER_H
#define HYPER_UTIL_BUFFER_H
#include <algorithm>
#include <vector>
#include <initializer_list>
#include <string>
#include <iostream>
#include <iomanip>
#include <exception>
///
/// namespace Hyper
///
namespace Hyper {
///
/// namespace Util
///
namespace Util {
///
/// class Buffer<T>
///
template<typename T>
class Buffer {
public:
///
/// property vector<T> value
/// comment Contains a vector of the type the constructor was specialized with.
///
std::vector<T> value;
class OutOfRange : public std::exception {
virtual const char* what() const throw() {
return "Out Of Range Exception";
}
} EX_OUT_OF_RANGE;
// bool equals(Buffer b);
// int indexOf(T value, int byteOffset);
///
/// operator =
/// comment Assign one buffer to another.
///
Buffer& operator= (const Buffer& buf);
///
/// operator ==
/// comment Determine if two buffers values are equal.
///
bool operator== (const Buffer<T>& rhs) {
return std::equal(
std::begin(this->value),
std::end(this->value),
std::begin(rhs.value)
);
}
///
/// operator !=
/// comment Determine if two buffers values are not equal.
///
bool operator!= (const Buffer<T>& rhs) {
return !std::equal(
std::begin(this->value),
std::end(this->value),
std::begin(rhs.value)
);
}
///
/// operator <<
/// comment Allow buffers to interface with streams. The value of the
/// comment buffer will be represented as hex in a angle-bracket delimited
/// comment string.
///
friend std::ostream& operator<< (std::ostream &os, const Buffer<T>& buf) {
if (buf.value.empty()) {
os << "";
return os;
}
std::ios_base::fmtflags f(std::cout.flags());
os << "<Buffer" << std::hex << std::setfill('0');
for (auto const& c : buf.value) {
os << " " << std::setw(2) << (int) c;
}
std::cout.flags(f);
os << ">";
return os;
};
///
/// operator []
/// comment Provides access of the buffer via index.
///
T operator[] (int i) {
return this->value[i];
}
///
/// constructor Buffer(initializer_list<T> list)
/// comment Constructs a buffer from an initializer list.
///
Buffer(std::initializer_list<T> l) : value(l) {}
///
/// constructor Buffer()
/// comment Constructs an empty buffer.
///
Buffer() {
this->value = std::vector<T>(0);
};
///
/// constructor Buffer(size_t size)
/// comment Constructs a buffer with a value of `size`.
///
/// param size The size initially allocated for the buffer value.
///
Buffer(size_t size) {
this->value = std::vector<T>(size);
};
///
/// constructor Buffer(const string&)
/// comment Constructs a buffer from a string.
///
Buffer(const std::string& str) {
this->value = std::vector<uint8_t>(str.begin(), str.end());
};
///
/// constructor Buffer(const Buffer&)
/// comment Constructs a buffer from another buffer.
///
Buffer(const Buffer& buf) {
this->value = buf.value;
};
///
/// method toString()
/// return std::string
/// comment Get the value of of the buffer as a string.
///
std::string toString() {
std::string str(this->value.begin(), this->value.end());
return str;
};
///
/// method length()
/// return int
/// comment Get the size of the buffer in by bytes.
///
int length() {
return this->value.size();
};
///
/// method concat(initializer_list<T>& list)
/// comment Concatinate this buffer with a list of buffers.
///
/// param list A list of buffers to be joined.
///
/// return Buffer
/// comment Returns a new combined buffer.
///
template<typename LT>
Buffer concat(const std::initializer_list<LT>& list) {
using std::end;
using std::begin;
unsigned size = 0;
Buffer cpy(size);
for (auto& buf : list) {
size += this->value.size() + buf.value.size();
this->value.reserve(size);
this->value.insert(end(this->value), begin(buf.value), end(buf.value));
}
cpy.value = this->value;
return cpy;
};
///
/// method copy(Buffer& target, size_t targetStart, size_t sourceStart, size_t sourceEnd)
/// comment Copies this buffer (or part of it) into another buffer.
///
/// param target A Buffer to copy into.
/// param targetStart The offset within target at which to begin writing.
/// param sourceStart The offset within buf from which to begin copying.
/// param sourceEnd The offset within buf at which to stop. copying (not inclusive).
///
/// return size_t
/// comment Returns how many bytes were written to this buffer.
///
size_t copy (Buffer& buf, size_t targetStart, size_t sourceStart, size_t sourceEnd) {
using std::end;
using std::begin;
auto& source = this->value;
auto& target = buf.value;
if (targetStart >= target.size() || sourceStart >= sourceEnd) {
return 0;
}
if (sourceStart > target.size()) {
throw EX_OUT_OF_RANGE;
}
if (sourceEnd - sourceStart > target.size() - targetStart) {
sourceEnd = sourceStart + target.size() - targetStart;
}
unsigned size = std::min(
std::min(sourceEnd - sourceStart, target.size() - targetStart),
target.size() - targetStart
);
if (buf.value.size() < size) {
buf.value.resize(size);
}
std::copy(
begin(source) + sourceStart,
begin(source) + sourceEnd,
begin(target) + targetStart
);
return size;
};
///
/// overload copy(Buffer& target)
///
/// return size_t
///
size_t copy (Buffer& buf) {
if (buf.value.size() == 0) {
return 0;
}
buf.value = std::vector<T> (this->value);
return buf.value.size();
};
///
/// overload copy(Buffer& target, size_t targetStart)
///
/// return size_t
///
size_t copy (Buffer& buf, size_t targetStart) {
return copy(buf, targetStart, 0, buf.value.size());
};
///
/// overload copy(Buffer& target, size_t targetStart, size_t sourceStart)
///
/// return size_t
///
size_t copy (Buffer& buf, size_t targetStart, size_t sourceStart) {
return copy(buf, targetStart, sourceStart, buf.value.size());
};
///
/// method slice(size_t from, size_t to)
/// comment provides a new buffer that represents a slice of this one.
///
/// param from Where the new Buffer will start. Default: 0.
/// param to Where the new Buffer will end (not inclusive). Default: buf.length.
///
/// return Buffer
///
Buffer slice(size_t from, size_t to) {
const auto size = this->value.size();
if (from > size || to > size) {
Buffer<T> buf;
return buf;
}
auto _from = std::begin(this->value) + from;
auto _to = std::begin(this->value) + to;
std::vector<T> value(_from, _to);
Buffer<T> buf(value.size());
buf.value = value;
return buf;
};
///
/// overload slice(size_t)
/// return Buffer
///
Buffer slice(size_t from) {
return slice(from, this->value.size());
};
~Buffer () {
this->value.clear();
};
};
} // namespace Util
} // namespace Hyper
#endif