Vince's CSV Parser
Loading...
Searching...
No Matches
field_scalar_list.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <atomic>
9#include <cassert>
10#include <memory>
11#include <utility>
12#include <vector>
13
14#include "../common.hpp"
15#include "../data_type.hpp"
16
17namespace csv {
18 namespace internals {
19 namespace memory {
29 public:
30 CSVFieldScalarList(size_t single_buffer_capacity = (size_t)(internals::PAGE_SIZE / sizeof(CSVFieldScalar))) :
31 block_capacity_(single_buffer_capacity == 0 ? 1 : single_buffer_capacity) {
32 const size_t max_fields = internals::CSV_CHUNK_SIZE_DEFAULT + 1;
33 const size_t block_count = (max_fields + this->block_capacity_ - 1) / this->block_capacity_;
34 this->blocks_.reserve(block_count);
35 this->blocks_.resize(block_count);
36 }
37
39
41 : block_capacity_(other.block_capacity_),
42 blocks_(std::move(other.blocks_)) {
43 this->size_.store(other.size_.load(std::memory_order_acquire), std::memory_order_release);
44 this->block_count_.store(other.block_count_.load(std::memory_order_acquire), std::memory_order_release);
45 other.size_.store(0, std::memory_order_release);
46 other.block_count_.store(0, std::memory_order_release);
47 }
48
49 void emplace_back(const CSVFieldScalar& scalar) {
50 const size_t offset = this->size_.load(std::memory_order_acquire);
51 const size_t page_no = offset / this->block_capacity_;
52 const size_t buffer_idx = offset % this->block_capacity_;
53
54 if (page_no >= this->block_count_.load(std::memory_order_acquire)) {
55 this->allocate_block(page_no);
56 }
57
58 this->blocks_[page_no].get()[buffer_idx] = scalar;
59 this->size_.store(offset + 1, std::memory_order_release);
60 }
61
62 size_t size() const noexcept {
63 return this->size_.load(std::memory_order_acquire);
64 }
65
66 bool empty() const noexcept {
67 return this->size() == 0;
68 }
69
70 void reserve_for_source_size(size_t source_size) {
71 const size_t max_fields = source_size + 1;
72 const size_t block_count = (max_fields + this->block_capacity_ - 1) / this->block_capacity_;
73 if (block_count > this->blocks_.size()) {
74 this->blocks_.resize(block_count);
75 }
76 }
77
78 inline const CSVFieldScalar& operator[](size_t n) const {
79 assert(n < this->size_.load(std::memory_order_acquire));
80 const size_t page_no = n / this->block_capacity_;
81 const size_t buffer_idx = n % this->block_capacity_;
82 assert(page_no < this->block_count_.load(std::memory_order_acquire));
83 return this->blocks_[page_no].get()[buffer_idx];
84 }
85
86 private:
87 size_t block_capacity_;
88 std::vector<std::unique_ptr<CSVFieldScalar[]>> blocks_;
89 std::atomic<size_t> size_{ 0 };
90 std::atomic<size_t> block_count_{ 0 };
91
92 void allocate_block(size_t page_no) {
93 if (page_no >= this->blocks_.size()) {
94 this->blocks_.resize((std::max)(page_no + 1, this->blocks_.size() * 2));
95 }
96
97 if (!this->blocks_[page_no]) {
98 this->blocks_[page_no] = std::unique_ptr<CSVFieldScalar[]>(new CSVFieldScalar[this->block_capacity_]);
99 }
100
101 this->block_count_.store(page_no + 1, std::memory_order_release);
102 }
103 };
104 }
105 }
106}
Borrow a row and error state for synchronous, single-pass field access.
Stable sidecar storage for parser-time CSVFieldScalar values.
The all encompassing namespace.
Cached scalar classification and parsed value for one CSV field.
Definition data_type.hpp:35