Repository navigation
Expand file tree
/
Copy pathcold_vector.h
More file actions
372 lines (315 loc) · 9.35 KB
/
Copy pathcold_vector.h
File metadata and controls
372 lines (315 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* File: cold_vector.h
* Author: Daniel Hannon
*
* Copyright: 2024 Daniel Hannon
*
* Brief: ColdVector is a storage backed vector type.
*/
#include "util.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <initializer_list>
#include <stdexcept>
#include <string>
#include <utility>
#include <iostream>
#include <iterator>
#ifndef COLD_VECTOR_H_6C4A5FE951FC4491A740E7D8F71E2E6B
#define COLD_VECTOR_H_6C4A5FE951FC4491A740E7D8F71E2E6B 1
/*
* ColdVector is a storage-backed vector with an internal
* buffer to reduce the need for read/write operations.
*/
template<typename T, std::size_t BUFF_SIZE = 100>
struct ColdVector {
ColdVector(std::initializer_list<T> inp): m_buffer{},
m_buffIndex{0},
m_buffSize{0},
m_vectorSize{0},
m_fileName{get_uuid()},
m_fileOwner{true},
m_fileStream{m_fileName, std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc}
{
static_assert(BUFF_SIZE != 0, "Cannot have 0 buffer");
for(auto const& v : inp) {
emplace_back(v);
}
}
ColdVector(ColdVector const& other): m_buffer{other.m_buffer},
m_buffIndex{other.m_buffIndex},
m_buffSize{other.m_buffSize},
m_vectorSize{other.m_vectorSize},
m_fileName{get_uuid()},
m_fileOwner{true},
m_fileStream{m_fileName, std::ios::in | std::ios::out | std::ios::binary}
{
std::ifstream temp{other.m_fileName};
if(m_fileStream.is_open() && temp.is_open()) {
while(!temp.eof()) {
m_fileStream.put(temp.get());
}
m_fileStream.seekp(0, std::ios::beg);
m_fileStream.seekg(0, std::ios::beg);
}
temp.close();
}
ColdVector(ColdVector&& a): m_buffer{std::move(a.m_buffer)},
m_buffIndex{a.m_buffIndex},
m_buffSize{a.m_buffSize},
m_vectorSize{a.m_vectorSize},
m_fileName{std::move(a.m_fileName)},
m_fileOwner{true},
m_fileStream{std::move(a.m_fileStream)}
{
a.m_fileOwner = false;
}
~ColdVector() {
m_fileStream.close();
// Cleanup
if(m_fileOwner) {
std::remove(m_fileName.c_str());
}
}
ColdVector& operator=(ColdVector const& other) {
if(this != &other) {
auto val = ColdVector{other};
swap(val);
}
return *this;
}
// Amount of elements currently stored in the vector
std::size_t size() const noexcept {
return m_vectorSize;
}
// Maximum allowed number of elements
std::size_t max_size() const noexcept {
constexpr std::size_t maxSize = UINT64_MAX / sizeof(T);
return maxSize;
}
// Returns true if ColdVector is empty
bool empty() const {
return (m_buffSize == 0 && m_vectorSize == 0);
}
T& at(std::size_t idx) {
if(idx >= m_vectorSize) {
throw std::out_of_range("Element is out of bounds!");
}
if((idx < m_buffIndex)
|| (idx >= (m_buffIndex + m_buffSize))) {
dump_buffer();
load_buffer_at(idx);
}
return m_buffer[idx - m_buffIndex];
}
template<class... Args>
void emplace_back(Args&&... args) {
if((m_vectorSize < m_buffIndex)
|| (m_vectorSize >= (m_buffIndex + BUFF_SIZE))) {
dump_buffer();
load_buffer_at(m_vectorSize);
}
m_buffer[m_vectorSize - m_buffIndex] = T(std::forward<Args>(args)...);
m_buffSize++;
m_vectorSize++;
}
void pop_back() {
if(m_vectorSize == 0) return;
m_vectorSize--;
}
T& operator[](std::size_t idx) {
if((idx < m_buffIndex) || (idx >= (m_buffIndex + m_buffSize))) {
dump_buffer();
load_buffer_at(idx);
}
return m_buffer[idx - m_buffIndex];
}
T& operator[](std::size_t idx) const {
// I can't make this truly const :DDDDD
return const_cast<ColdVector *>(this)->operator[](idx);
}
void swap(ColdVector & other) noexcept {
std::swap(m_buffer,other.m_buffer);
std::swap(m_buffIndex, other.m_buffIndex);
std::swap(m_buffSize,other.m_buffSize);
std::swap(m_vectorSize, other.m_vectorSize);
std::swap(m_fileName, other.m_fileName);
std::swap(m_fileOwner, other.m_fileOwner);
std::swap(m_fileStream, other.m_fileStream);
}
class iterator {
public:
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;
iterator(ColdVector const* pVec): m_internal{pVec},
m_idx{0}
{}
iterator(iterator const& itr): m_internal{itr.m_internal},
m_idx{itr.m_idx}
{}
iterator& operator++(int) {
m_idx++;
return *this;
}
iterator& operator++() {
m_idx++;
return *this;
}
iterator operator+(std::size_t val) const {
auto tmp{*this};
tmp.m_idx += val;
return tmp;
}
iterator operator+(std::integral auto x) const {
if(std::signbit(x)) {
return this->operator-((std::size_t)(x * -1));
}
return this->operator+((std::size_t)x);
}
iterator operator-(std::size_t val) const {
auto tmp{*this};
tmp.m_idx -= val;
return tmp;
}
iterator& operator--(int) {
m_idx--;
return *this;
}
iterator& operator--() {
m_idx--;
return *this;
}
T& operator*() const {
return const_cast<ColdVector *>(m_internal)->operator[](m_idx);
}
pointer operator->() {
return &m_internal->operator[](m_idx);
}
iterator operator+(difference_type v) const {
iterator temp{*this};
temp.m_idx += v;
return temp;
}
bool operator==(iterator const& rhs) const {return (m_idx == rhs.m_idx) && (m_internal == rhs.m_internal);}
bool operator!=(iterator const& rhs) const {return !(*this == rhs);}
friend std::weak_ordering operator<=>(iterator const& lhs, iterator const& rhs) {
if(lhs.m_internal != rhs.m_internal) return std::weak_ordering::greater;
return lhs.m_idx <=> rhs.m_idx;
}
std::int64_t get_index() const {
return m_idx;
}
private:
ColdVector const* m_internal;
std::int64_t m_idx;
};
class reverse_iterator: public iterator {
public:
reverse_iterator(ColdVector const* pVec): iterator{pVec}
{}
reverse_iterator(iterator const& itr): iterator{itr}
{}
reverse_iterator& operator++(int) {
iterator::operator--();
return *this;
}
reverse_iterator& operator++() {
iterator::operator--();
return *this;
}
reverse_iterator operator+(iterator::difference_type v) const {
return iterator::operator-(v);
}
reverse_iterator operator-(iterator::difference_type v) const {
return iterator::operator+(v);
}
reverse_iterator& operator+=(iterator::difference_type v) {
return iterator::operator-=(v);
}
reverse_iterator& operator-=(iterator::difference_type v) {
return iterator::operator+=(v);
}
friend std::weak_ordering operator<=>(reverse_iterator const& lhs, reverse_iterator const& rhs) {
return *(static_cast<iterator const*>(&rhs)) <=> *(static_cast<iterator const *>(&lhs));
}
};
iterator begin() const { return iterator{this}; }
iterator end() const {
auto tmp = iterator{this};
tmp = tmp + m_vectorSize;
return tmp;
}
reverse_iterator rbegin() const {
return end() - 1;
}
reverse_iterator rend() const {
return begin() -1;
}
ColdVector(iterator const& begin,
iterator const& end) : m_buffer{},
m_buffIndex{0},
m_buffSize{0},
m_vectorSize{0},
m_fileName{get_uuid()},
m_fileOwner{true},
m_fileStream{m_fileName, std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc}
{
for(auto it = begin; it < end; it++) {
emplace_back(*it);
}
}
void insert(iterator const& at, iterator const& begin, iterator const& end) {
auto insertedData = begin;
auto atPosition = at.get_index();
while(insertedData != end) {
if(atPosition == m_vectorSize) {
emplace_back(*insertedData);
} else {
this->operator[](atPosition) = *insertedData;
}
atPosition++;
insertedData++;
}
}
private:
// dump_buffer is an internal thing to help deal with random file access.
void dump_buffer() {
if(m_buffSize == 0) return;
m_fileStream.seekp(m_buffIndex * sizeof(T), std::ios::beg);
m_fileStream.write(reinterpret_cast<const char *>(&m_buffer), m_buffSize * sizeof(T));
}
// Another helper for dealing with random file access.
// Internally this loads the boundary of BUFF_SIZE so as to
// Reduce read/write operations from (N-BUFF_SIZE) to floor(N/BUFF_SIZE)
void load_buffer_at(std::size_t idx) {
// if the index is the same as the size of the file, load in nothing.
if(idx >= m_vectorSize) {
m_buffSize = 0;
m_buffIndex = m_vectorSize;
return;
}
std::size_t new_idx = (idx / BUFF_SIZE) * BUFF_SIZE;
m_fileStream.seekg(new_idx * sizeof(T), std::ios::beg);
m_fileStream.read(reinterpret_cast<char*>(&m_buffer), std::min(BUFF_SIZE, (m_vectorSize - new_idx)) * sizeof(T));
m_buffIndex = new_idx;
m_buffSize = std::min((m_vectorSize - new_idx),BUFF_SIZE);
}
private:
std::array<T, BUFF_SIZE> m_buffer; // The Data buffer to reduce read/writes
std::size_t m_buffIndex; // Index of m_buffer[0]
std::size_t m_buffSize; // The amount of indexed data currently stored in the vector
std::size_t m_vectorSize; // The size of the vector
std::string m_fileName; // The name of the file in which the data is stored
bool m_fileOwner; // Check if this instance owns the file or not.
std::fstream m_fileStream; // The filestream associated with the vector
};
#endif // COLD_VECTOR_H_6C4A5FE951FC4491A740E7D8F71E2E6B