Vince's CSV Parser
Loading...
Searching...
No Matches
single_thread_deque.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstddef>
8#include <deque>
9#include <iterator>
10#include <utility>
11#include <vector>
12
13namespace csv {
14 namespace internals {
21 template<typename T>
23 public:
24 SingleThreadDeque(size_t notify_size = 100) {
26 }
27
29 this->records_ = other.records_;
30 this->_is_empty = other._is_empty;
31 this->_is_waitable = other._is_waitable;
32 }
33
34 SingleThreadDeque(const std::deque<T>& source)
35 : _is_empty(source.empty()),
36 records_(source) {}
37
38 bool empty() const noexcept {
39 return this->_is_empty;
40 }
41
42 void push_back(T&& item) {
43 this->records_.push_back(std::move(item));
44 this->_is_empty = false;
45 }
46
47 void append_rows(std::vector<T>&& rows) {
48 if (rows.empty()) {
49 return;
50 }
51
52 this->records_.insert(
53 this->records_.end(),
54 std::make_move_iterator(rows.begin()),
55 std::make_move_iterator(rows.end())
56 );
57 this->_is_empty = false;
58 }
59
60 T pop_front() noexcept {
61 T item = std::move(this->records_.front());
62 this->records_.pop_front();
63
64 if (this->records_.empty()) {
65 this->_is_empty = true;
66 }
67
68 return item;
69 }
70
72 bool try_copy_front(T& item) const {
73 if (this->records_.empty()) {
74 return false;
75 }
76
77 item = this->records_.front();
78 return true;
79 }
80
82 size_t drain_front(std::vector<T>& out, size_t max_items) {
83 const size_t drain_count = this->records_.size() < max_items ? this->records_.size() : max_items;
84 out.reserve(out.size() + drain_count);
85
86 for (size_t i = 0; i < drain_count; ++i) {
87 out.push_back(std::move(this->records_.front()));
88 this->records_.pop_front();
89 }
90
91 if (this->records_.empty()) {
92 this->_is_empty = true;
93 }
94
95 return drain_count;
96 }
97
98 bool is_waitable() const noexcept {
99 return this->_is_waitable;
100 }
101
102 void wait() {
103 // No-op in single-thread mode.
104 }
105
106 size_t size() const noexcept {
107 return this->records_.size();
108 }
109
110 void notify_all() {
111 this->_is_waitable = true;
112 }
113
114 void kill_all() {
115 this->_is_waitable = false;
116 }
117
118 private:
119 bool _is_empty = true;
120 bool _is_waitable = false;
121 std::deque<T> records_;
122 };
123 }
124}
Borrow a row and error state for synchronous, single-pass field access.
Minimal row queue used when parser threading is disabled.
size_t drain_front(std::vector< T > &out, size_t max_items)
Move up to max_items rows into a caller-owned batch buffer.
bool try_copy_front(T &item) const
Copy the front item without removing it.
The all encompassing namespace.