1#ifndef BYTEME_PERBYTE_HPP
2#define BYTEME_PERBYTE_HPP
10#include <condition_variable>
24template<
class Po
inter_>
25void skip_zero_buffers(Pointer_& reader,
size_t& available) {
27 while (reader->load()) {
28 available = reader->available();
50template<
typename Type_ =
char,
class Po
inter_ = Reader*>
53 const Type_* my_ptr =
nullptr;
54 size_t my_available = 0;
55 size_t my_current = 0;
56 size_t my_overall = 0;
61 my_overall += my_available;
62 skip_zero_buffers(my_reader, my_available);
63 my_ptr =
reinterpret_cast<const Type_*
>(my_reader->buffer());
71 PerByte(Pointer_ reader) : my_reader(std::move(reader)) {
79 return my_current < my_available;
90 if (my_current < my_available) {
95 return my_available > 0;
104 return my_ptr[my_current];
111 return my_overall + my_current;
128 std::pair<size_t, bool>
extract(
size_t number, Type_* output) {
129 size_t original = number;
133 auto start = my_ptr + my_current;
134 auto leftover = my_available - my_current;
136 if (leftover > number) {
137 my_current += number;
139 std::copy(start, my_ptr + my_current, output);
144 std::copy(start, my_ptr + my_available, output);
147 okay = (my_available > 0);
148 if (number == 0 || !okay) {
155 return std::make_pair(original - number, okay);
168template<
typename Type_ =
char,
class Po
inter_ = Reader*>
171 size_t my_current = 0;
172 size_t my_available = 0;
173 size_t my_overall = 0;
177 std::vector<Type_> my_buffer;
178 size_t my_next_available = 0;
179 bool my_finished =
false;
182 std::thread my_thread;
183 std::exception_ptr my_thread_err =
nullptr;
185 std::condition_variable my_cv;
186 bool my_ready_input, my_ready_output;
189 while (!my_finished) {
190 std::unique_lock lck(my_mut);
191 my_cv.wait(lck, [&]() {
return my_ready_input; });
192 my_ready_input =
false;
199 skip_zero_buffers(my_reader, my_next_available);
200 my_finished = my_next_available == 0;
202 my_thread_err = std::current_exception();
206 my_ready_output =
true;
214 std::unique_lock lck(my_mut);
215 my_cv.wait(lck, [&]() {
return my_ready_output; });
216 my_ready_output =
false;
219 std::rethrow_exception(my_thread_err);
222 my_overall += my_available;
224 auto ptr =
reinterpret_cast<const Type_*
>(my_reader->buffer());
225 my_available = my_next_available;
226 my_buffer.resize(my_available);
229 std::copy(ptr, ptr + my_available, my_buffer.begin());
230 my_ready_input =
true;
243 my_ready_input =
false;
244 my_thread = std::thread([&]() { thread_loop(); });
246 skip_zero_buffers(my_reader, my_next_available);
247 my_ready_output =
true;
256 std::unique_lock lck(my_mut);
258 my_ready_input =
true;
272 return my_current < my_available;
280 if (my_current < my_available) {
289 return my_available > 0;
296 return my_buffer[my_current];
303 return my_overall + my_current;
320 std::pair<size_t, bool>
extract(
size_t number, Type_* output) {
321 size_t original = number;
325 auto start = my_buffer.data() + my_current;
326 auto leftover = my_available - my_current;
328 if (leftover > number) {
329 my_current += number;
331 std::copy(start, my_buffer.data() + my_current, output);
336 std::copy(start, my_buffer.data() + my_available, output);
339 my_current += leftover;
345 okay = (my_available > 0);
346 if (number == 0 || !okay) {
353 return std::make_pair(original - number, okay);
Perform parallelized byte-by-byte extraction from a Reader source.
Definition PerByte.hpp:169
Type_ get() const
Definition PerByte.hpp:295
bool advance()
Definition PerByte.hpp:278
size_t position() const
Definition PerByte.hpp:302
std::pair< size_t, bool > extract(size_t number, Type_ *output)
Definition PerByte.hpp:320
bool valid() const
Definition PerByte.hpp:271
PerByteParallel(Pointer_ reader)
Definition PerByte.hpp:242
Perform byte-by-byte extraction from a Reader source.
Definition PerByte.hpp:51
size_t position() const
Definition PerByte.hpp:110
PerByte(Pointer_ reader)
Definition PerByte.hpp:71
Type_ get() const
Definition PerByte.hpp:103
bool valid() const
Definition PerByte.hpp:78
std::pair< size_t, bool > extract(size_t number, Type_ *output)
Definition PerByte.hpp:128
bool advance()
Definition PerByte.hpp:88
Simple byte readers and writers.