-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunked_vector.hpp
More file actions
223 lines (187 loc) · 5.18 KB
/
chunked_vector.hpp
File metadata and controls
223 lines (187 loc) · 5.18 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
#pragma once
#include <iterator>
#include <tuple>
#include <vector>
/**
* Data structure that behaves both like
* multiple vectors and a single vector
* to allow sharing between threads.
*
* Each chunk can be written to by a single thread.
*
* @tparam T the type to store
*/
template<class T>
class chunked_vector {
template<class DerefType, class VectorRefType>
class iterator_base;
public:
/**
* Iterator that iterates the vector as
* if it was not chunked.
*
* See the implementation below
*/
using iterator = iterator_base<T&, chunked_vector*>;
using const_iterator = iterator_base<const T&, const chunked_vector*>;
/**
* Constructs the chunked vector.
*
* @param chunk_cnt the number of chunks to create
*/
chunked_vector(size_t chunk_cnt) :
chunks_(chunk_cnt) {}
/**
* Find the number of elements.
*
* Thread safe due to not mutating anything.
*
* @return the number of elements in the vector
* in total (across all chunks)
*/
size_t size() const {
size_t res = 0;
for (const auto &chunk : chunks_)
res += chunk.size();
return res;
}
/**
* Remove all elements.
*
* Not thread safe.
*/
void clear() {
for (auto &chunk : chunks_)
chunk.clear();
}
/**
* @return an iterator pointing to the
* first element in the vector
*/
iterator begin() {
return iterator(this, 0);
}
/**
* @return an iterator pointing past the
* last element in the vector
*/
iterator end() {
return iterator(this, size());
}
/**
* Access a chunk.
*
* Modifying the chunk is thread safe
* as long as any single chunk_index
* is used by at most 1 thread.
*
* @param chunk_index the index of the chunk to access
* @return a reference to the chunk
*/
std::vector<T> &chunk(size_t chunk_index) {
return chunks_[chunk_index];
}
private:
std::vector<std::vector<T>> chunks_;
};
/**
* Iterator that iterates the vector as if it was not chunked.
*
* The operators have their standard meaning and can be
* used as though they were iterators in a regular vector.
*
* @tparam DerefType the type the iterator yields
* @tparam VectorRefType the type of the reference to the iterated chunked_vector
*/
template<class T>
template<class DerefType, class VectorRefType>
class chunked_vector<T>::iterator_base
: public std::iterator<std::random_access_iterator_tag, T> {
friend class chunked_vector;
iterator_base(const VectorRefType vector_ref, size_t position) :
vector_ref_(vector_ref),
position_(position),
chunk_id_(0),
chunk_position_(position) { skip(); }
public:
/**
* Default constructs an invalid
* iterator pointing to nothing.
*/
iterator_base() :
vector_ref_(nullptr),
position_(0),
chunk_id_(0),
chunk_position_(0) {}
DerefType operator*() const {
return vector_ref_->chunks_[chunk_id_][chunk_position_];
}
iterator &operator+=(ptrdiff_t offset) {
position_ += offset;
chunk_position_ += offset;
skip();
return *this;
}
iterator &operator-=(ptrdiff_t offset) {
return *this += -offset;
}
iterator &operator++() {
return *this += 1;
}
iterator &operator--() {
return *this -= 1;
}
iterator operator+(ptrdiff_t offset) const {
iterator cpy = *this;
cpy += offset;
return cpy;
}
iterator operator-(ptrdiff_t offset) const {
iterator cpy = *this;
cpy -= offset;
return cpy;
}
friend iterator operator+(ptrdiff_t offset, const iterator &it) {
return it + offset;
}
DerefType operator[](ptrdiff_t offset) const {
return *(*this + offset);
}
iterator operator++(int) {
iterator cpy = *this;
++*this;
return cpy;
}
iterator operator--(int) {
iterator cpy = *this;
--this;
return cpy;
}
ptrdiff_t operator-(const iterator &o) const {
return position_ - o.position_;
}
friend bool operator==(const iterator &l, const iterator &r) { return l.position_ == r.position_; }
friend bool operator!=(const iterator &l, const iterator &r) { return l.position_ != r.position_; }
friend bool operator< (const iterator &l, const iterator &r) { return l.position_ < r.position_; }
friend bool operator<=(const iterator &l, const iterator &r) { return l.position_ <= r.position_; }
friend bool operator>=(const iterator &l, const iterator &r) { return l.position_ >= r.position_; }
friend bool operator> (const iterator &l, const iterator &r) { return l.position_ > r.position_; }
private:
const VectorRefType vector_ref_;
size_t position_;
size_t chunk_id_;
ptrdiff_t chunk_position_;
void skip() {
// change to a further ahead chunk while
// the position within the chunk
// is greater than the size of the chunk
while (chunk_id_ < vector_ref_->chunks_.size()
&& chunk_position_ >= (ptrdiff_t)vector_ref_->chunks_[chunk_id_].size())
chunk_position_ -= vector_ref_->chunks_[chunk_id_++].size();
// change to a further behind chunk while
// the position within the chunk
// is smaller than zero
while (chunk_id_ > 0 && chunk_position_ < 0)
chunk_position_ += vector_ref_->chunks_[--chunk_id_].size();
}
};