Vince's CSV Parser
Loading...
Searching...
No Matches
raw_csv_field_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 "raw_csv_field.hpp"
16
17namespace csv {
18 namespace internals {
19 namespace memory {
37 public:
39 RawCSVFieldList(size_t single_buffer_capacity = (size_t)(internals::PAGE_SIZE / sizeof(RawCSVField))) :
40 block_capacity_(single_buffer_capacity == 0 ? 1 : single_buffer_capacity) {
41 const size_t max_fields = internals::CSV_CHUNK_SIZE_DEFAULT + 1;
42 const size_t block_count = (max_fields + this->block_capacity_ - 1) / this->block_capacity_;
43 this->blocks_.reserve(block_count);
44 this->blocks_.resize(block_count);
45 }
46
47 // No copy constructor
48 RawCSVFieldList(const RawCSVFieldList& other) = delete;
49
50 // RawCSVFieldList may be moved with its backing blocks intact.
52 : block_capacity_(other.block_capacity_),
53 blocks_(std::move(other.blocks_)) {
54 this->size_.store(other.size_.load(std::memory_order_acquire), std::memory_order_release);
55 this->block_count_.store(other.block_count_.load(std::memory_order_acquire), std::memory_order_release);
56 other.size_.store(0, std::memory_order_release);
57 other.block_count_.store(0, std::memory_order_release);
58 }
59
60 template <class... Args>
61 void emplace_back(Args&&... args) {
62 const size_t offset = this->size_.load(std::memory_order_acquire);
63 const size_t page_no = offset / this->block_capacity_;
64 const size_t buffer_idx = offset % this->block_capacity_;
65
66 if (page_no >= this->block_count_.load(std::memory_order_acquire)) {
67 this->allocate_block(page_no);
68 }
69
70 this->blocks_[page_no].get()[buffer_idx] = RawCSVField(std::forward<Args>(args)...);
71 this->size_.store(offset + 1, std::memory_order_release);
72 }
73
74 size_t size() const noexcept {
75 return this->size_.load(std::memory_order_acquire);
76 }
77
78 void reserve_for_source_size(size_t source_size) {
79 const size_t max_fields = source_size + 1;
80 const size_t block_count = (max_fields + this->block_capacity_ - 1) / this->block_capacity_;
81 if (block_count > this->blocks_.size()) {
82 this->blocks_.resize(block_count);
83 }
84 }
85
89 inline RawCSVField& operator[](size_t n) const {
90 assert(n < this->size_.load(std::memory_order_acquire));
91 const size_t page_no = n / this->block_capacity_;
92 const size_t buffer_idx = n % this->block_capacity_;
93 assert(page_no < this->block_count_.load(std::memory_order_acquire));
94 return this->blocks_[page_no].get()[buffer_idx];
95 }
96
97 private:
98 size_t block_capacity_;
99 std::vector<std::unique_ptr<RawCSVField[]>> blocks_;
100 std::atomic<size_t> size_{ 0 };
101 std::atomic<size_t> block_count_{ 0 };
102
103 void allocate_block(size_t page_no) {
104 if (page_no >= this->blocks_.size()) {
105 this->blocks_.resize((std::max)(page_no + 1, this->blocks_.size() * 2));
106 }
107
108 if (!this->blocks_[page_no]) {
109 this->blocks_[page_no] = std::unique_ptr<RawCSVField[]>(new RawCSVField[this->block_capacity_]);
110 }
111
112 this->block_count_.store(page_no + 1, std::memory_order_release);
113 }
114 };
115 }
116 }
117}
Borrow a row and error state for synchronous, single-pass field access.
A class used for efficiently storing RawCSVField objects and expanding as necessary.
RawCSVFieldList(size_t single_buffer_capacity=(size_t)(internals::PAGE_SIZE/sizeof(RawCSVField)))
Construct a RawCSVFieldList which allocates blocks of a certain size.
RawCSVField & operator[](size_t n) const
Access a field by its index.
The all encompassing namespace.
Compact parser metadata for a single CSV field.
A barebones class used for describing CSV fields.