https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "MooseError.h"
13
14#include <vector>
15
16namespace MooseUtils
17{
18
27template <typename T>
28class Buffer
29{
30public:
31 typedef typename std::vector<T>::iterator iterator;
32 typedef typename std::vector<T>::const_iterator const_iterator;
33
37 Buffer();
38
42 Buffer(const std::size_t capacity);
43
44 virtual ~Buffer() {}
45
49 void setCapacity(const std::size_t capacity) { this->_data.resize(capacity); }
53 std::size_t capacity() const { return this->_data.size(); }
54
58 void setSize(const std::size_t size) { this->_end_pos = this->newEnd(this->_begin_pos + size); }
62 std::size_t size() const { return this->_end_pos - this->_begin_pos; }
63
67 bool empty() const { return !size(); }
68
72 void push_back(const T & value);
73
77 void move(T & value);
78
84 void append(const_iterator in_begin, const_iterator in_end);
88 void append(const std::vector<T> & vals);
89
94 void clear();
95
101 virtual void erase(const std::size_t num) = 0;
102 /*
103 * Similar to erase(), but if the chunk size is larger than the current size,
104 * no error (all elements are erased gracefully).
105 *
106 * Note that erased items are not guaranteed to be freed immediately
107 */
108 virtual void eraseChunk(const std::size_t chunk_size) = 0;
109
113 iterator begin() { return this->_data.begin() + this->_begin_pos; }
117 const_iterator begin() const { return this->_data.begin() + this->_begin_pos; }
118
122 iterator end() { return this->_data.begin() + this->_end_pos; }
126 const_iterator end() const { return this->_data.begin() + this->_end_pos; }
127
132 virtual iterator beginChunk(const std::size_t chunk_size) = 0;
137 virtual const_iterator beginChunk(const std::size_t chunk_size) const = 0;
138
143 virtual iterator endChunk(const std::size_t chunk_size) = 0;
148 virtual const_iterator endChunk(const std::size_t chunk_size) const = 0;
149
153 T & operator[](const std::size_t index);
157 const T & operator[](const std::size_t index) const;
158
162 void swap(std::vector<T> & in_data);
163
172 const std::vector<T> & data() { return _data; }
173
182 std::size_t dataBeginPos() const { return _begin_pos; }
183
192 std::size_t dataEndPos() const { return _end_pos; }
193
194protected:
202 virtual std::size_t newEnd(const std::size_t new_end) = 0;
203
205 std::vector<T> _data;
206
208 std::size_t _begin_pos;
210 std::size_t _end_pos;
211};
212
213template <typename T>
214Buffer<T>::Buffer() : _begin_pos(0), _end_pos(0)
215{
216}
217
218template <typename T>
219Buffer<T>::Buffer(const std::size_t capacity) : _data(capacity), _begin_pos(0), _end_pos(0)
220{
221}
222
223template <typename T>
224void
225Buffer<T>::push_back(const T & value)
226{
227 this->_end_pos = newEnd(this->_end_pos + 1);
228 this->_data[this->_end_pos - 1] = value;
229}
230
231template <typename T>
232void
234{
235 this->_end_pos = newEnd(this->_end_pos + 1);
236 this->_data[this->_end_pos - 1] = std::move(value);
237}
238
239template <typename T>
240void
242{
243 const auto additional_size = std::distance(in_begin, in_end);
244 if (additional_size == 0)
245 return;
246
247 this->_end_pos = this->newEnd(this->_end_pos + additional_size);
248 std::copy(in_begin, in_end, this->end() - additional_size);
249}
250
251template <typename T>
252void
253Buffer<T>::append(const std::vector<T> & vals)
254{
255 this->append(vals.begin(), vals.end());
256}
257
258template <typename T>
259void
261{
262 this->_begin_pos = 0;
263 this->_end_pos = 0;
264}
265
266template <typename T>
267T &
268Buffer<T>::operator[](const std::size_t index)
269{
270 mooseAssert(this->_begin_pos + index < this->_end_pos, "Attempt to access off end of Buffer!");
271 return this->_data[this->_begin_pos + index];
272}
273
274template <typename T>
275const T &
276Buffer<T>::operator[](const std::size_t index) const
277{
278 mooseAssert(this->_begin_pos + index < this->_end_pos, "Attempt to access off end of Buffer!");
279 return this->_data[this->_begin_pos + index];
280}
281
282template <typename T>
283void
284Buffer<T>::swap(std::vector<T> & in_data)
285{
286 std::swap(in_data, _data);
287 this->_begin_pos = 0;
288 this->_end_pos = this->_data.size();
289}
290
291}
Base class for a buffer.
Definition Buffer.h:29
std::size_t dataEndPos() const
The current end position of the buffer in data().
Definition Buffer.h:192
std::vector< T >::iterator iterator
Definition Buffer.h:31
virtual std::size_t newEnd(const std::size_t new_end)=0
Find out where the new end will be.
virtual void erase(const std::size_t num)=0
Remove the first num elements.
virtual iterator endChunk(const std::size_t chunk_size)=0
Iterator for the last entry of a chunk size in the buffer If chunk_size is greater than the size of t...
virtual const_iterator beginChunk(const std::size_t chunk_size) const =0
Const iterator for the first entry with a given chunk size in the buffer If chunk_size is greater tha...
iterator end()
Iterator for the last entry in the buffer.
Definition Buffer.h:122
void setSize(const std::size_t size)
Set the size.
Definition Buffer.h:58
void push_back(const T &value)
Add a new entry on the end.
Definition Buffer.h:225
std::size_t size() const
Get the size.
Definition Buffer.h:62
std::vector< T >::const_iterator const_iterator
Definition Buffer.h:32
std::size_t _end_pos
The ending position.
Definition Buffer.h:210
std::vector< T > _data
The raw data.
Definition Buffer.h:205
bool empty() const
Whether or not the buffer is empty.
Definition Buffer.h:67
virtual void eraseChunk(const std::size_t chunk_size)=0
const_iterator end() const
Const iterator for the last entry in the buffer.
Definition Buffer.h:126
std::size_t dataBeginPos() const
The current beginning position of the buffer in data().
Definition Buffer.h:182
void swap(std::vector< T > &in_data)
Use in_data as our data vector.
Definition Buffer.h:284
void setCapacity(const std::size_t capacity)
Resize the capacity.
Definition Buffer.h:49
virtual const_iterator endChunk(const std::size_t chunk_size) const =0
Const iterator for the last entry of a chunk size in the buffer If chunk_size is greater than the siz...
std::size_t _begin_pos
The beginning position.
Definition Buffer.h:208
T & operator[](const std::size_t index)
Access an entry at index.
Definition Buffer.h:268
virtual iterator beginChunk(const std::size_t chunk_size)=0
Iterator for the first entry with a given chunk size in the buffer If chunk_size is greater than the ...
virtual ~Buffer()
Definition Buffer.h:44
const std::vector< T > & data()
Access the raw underlying storage.
Definition Buffer.h:172
const_iterator begin() const
Const iterator for the first entry in the buffer.
Definition Buffer.h:117
void append(const_iterator in_begin, const_iterator in_end)
Add new entries to the end.
Definition Buffer.h:241
Buffer()
Create an empty buffer.
Definition Buffer.h:214
iterator begin()
Iterator for the first entry in the buffer.
Definition Buffer.h:113
void move(T &value)
Moves the object into the buffer (calls std::move())
Definition Buffer.h:233
void clear()
Remove all entries (does not change the capacity) Note: this does NOT at all free any entries.
Definition Buffer.h:260
std::size_t capacity() const
Get the capacity.
Definition Buffer.h:53