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) {
25 (void)notify_size;
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 size_t drain_front(std::vector<T>& out, size_t max_items) {
73 const size_t drain_count = this->records_.size() < max_items ? this->records_.size() : max_items;
74 out.reserve(out.size() + drain_count);
75
76 for (size_t i = 0; i < drain_count; ++i) {
77 out.push_back(std::move(this->records_.front()));
78 this->records_.pop_front();
79 }
80
81 if (this->records_.empty()) {
82 this->_is_empty = true;
83 }
84
85 return drain_count;
86 }
87
88 bool is_waitable() const noexcept {
89 return this->_is_waitable;
90 }
91
92 void wait() {
93 // No-op in single-thread mode.
94 }
95
96 size_t size() const noexcept {
97 return this->records_.size();
98 }
99
100 void notify_all() {
101 this->_is_waitable = true;
102 }
103
104 void kill_all() {
105 this->_is_waitable = false;
106 }
107
108 private:
109 bool _is_empty = true;
110 bool _is_waitable = false;
111 std::deque<T> records_;
112 };
113 }
114}
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.
The all encompassing namespace.