https://mooseframework.inl.gov
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MooseUtils::Buffer< T > Class Template Referenceabstract

Base class for a buffer. More...

#include <Buffer.h>

Inheritance diagram for MooseUtils::Buffer< T >:
[legend]

Public Types

typedef std::vector< T >::iterator iterator
 
typedef std::vector< T >::const_iterator const_iterator
 

Public Member Functions

 Buffer ()
 Create an empty buffer. More...
 
 Buffer (const std::size_t capacity)
 Create a buffer with a specific capacity. More...
 
virtual ~Buffer ()
 
void setCapacity (const std::size_t capacity)
 Resize the capacity. More...
 
std::size_t capacity () const
 Get the capacity. More...
 
void setSize (const std::size_t size)
 Set the size. More...
 
std::size_t size () const
 Get the size. More...
 
bool empty () const
 Whether or not the buffer is empty. More...
 
void push_back (const T &value)
 Add a new entry on the end. More...
 
void move (T &value)
 Moves the object into the buffer (calls std::move()) More...
 
void append (const_iterator in_begin, const_iterator in_end)
 Add new entries to the end. More...
 
void append (const std::vector< T > &vals)
 Add new entries to the end. More...
 
void clear ()
 Remove all entries (does not change the capacity) Note: this does NOT at all free any entries. More...
 
virtual void erase (const std::size_t num)=0
 Remove the first num elements. More...
 
virtual void eraseChunk (const std::size_t chunk_size)=0
 
iterator begin ()
 Iterator for the first entry in the buffer. More...
 
const_iterator begin () const
 Const iterator for the first entry in the buffer. More...
 
iterator end ()
 Iterator for the last entry in the buffer. More...
 
const_iterator end () const
 Const iterator for the last entry in the buffer. More...
 
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 size of the buffer, the full range will be given. More...
 
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 than the size of the buffer, the full range will be given. More...
 
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 the buffer, the full range will be given. More...
 
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 size of the buffer, the full range will be given. More...
 
T & operator[] (const std::size_t index)
 Access an entry at index. More...
 
const T & operator[] (const std::size_t index) const
 Const access an entry at an index. More...
 
void swap (std::vector< T > &in_data)
 Use in_data as our data vector. More...
 
const std::vector< T > & data ()
 Access the raw underlying storage. More...
 
std::size_t dataBeginPos () const
 The current beginning position of the buffer in data(). More...
 
std::size_t dataEndPos () const
 The current end position of the buffer in data(). More...
 

Protected Member Functions

virtual std::size_t newEnd (const std::size_t new_end)=0
 Find out where the new end will be. More...
 

Protected Attributes

std::vector< T > _data
 The raw data. More...
 
std::size_t _begin_pos
 The beginning position. More...
 
std::size_t _end_pos
 The ending position. More...
 

Detailed Description

template<typename T>
class MooseUtils::Buffer< T >

Base class for a buffer.

Enables the controlled access of an underlying raw vector for storage, which can be used to limit memory allocation and copies for various buffer types while providing a useful public API.

Definition at line 28 of file Buffer.h.

Member Typedef Documentation

◆ const_iterator

template<typename T>
typedef std::vector<T>::const_iterator MooseUtils::Buffer< T >::const_iterator

Definition at line 32 of file Buffer.h.

◆ iterator

template<typename T>
typedef std::vector<T>::iterator MooseUtils::Buffer< T >::iterator

Definition at line 31 of file Buffer.h.

Constructor & Destructor Documentation

◆ Buffer() [1/2]

template<typename T >
MooseUtils::Buffer< T >::Buffer ( )

Create an empty buffer.

Definition at line 214 of file Buffer.h.

214  : _begin_pos(0), _end_pos(0)
215 {
216 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ Buffer() [2/2]

template<typename T >
MooseUtils::Buffer< T >::Buffer ( const std::size_t  capacity)

Create a buffer with a specific capacity.

Definition at line 219 of file Buffer.h.

219  : _data(capacity), _begin_pos(0), _end_pos(0)
220 {
221 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::size_t capacity() const
Get the capacity.
Definition: Buffer.h:53
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ ~Buffer()

template<typename T>
virtual MooseUtils::Buffer< T >::~Buffer ( )
inlinevirtual

Definition at line 44 of file Buffer.h.

44 {}

Member Function Documentation

◆ append() [1/2]

template<typename T >
void MooseUtils::Buffer< T >::append ( const_iterator  in_begin,
const_iterator  in_end 
)

Add new entries to the end.

Everything in [in_begin, in_end) is appended

Definition at line 241 of file Buffer.h.

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 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
iterator end()
Iterator for the last entry in the buffer.
Definition: Buffer.h:122
virtual std::size_t newEnd(const std::size_t new_end)=0
Find out where the new end will be.

◆ append() [2/2]

template<typename T >
void MooseUtils::Buffer< T >::append ( const std::vector< T > &  vals)

Add new entries to the end.

Definition at line 253 of file Buffer.h.

254 {
255  this->append(vals.begin(), vals.end());
256 }
void append(const_iterator in_begin, const_iterator in_end)
Add new entries to the end.
Definition: Buffer.h:241

◆ begin() [1/2]

template<typename T>
iterator MooseUtils::Buffer< T >::begin ( )
inline

Iterator for the first entry in the buffer.

Definition at line 113 of file Buffer.h.

113 { return this->_data.begin() + this->_begin_pos; }
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ begin() [2/2]

template<typename T>
const_iterator MooseUtils::Buffer< T >::begin ( ) const
inline

Const iterator for the first entry in the buffer.

Definition at line 117 of file Buffer.h.

117 { return this->_data.begin() + this->_begin_pos; }
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ beginChunk() [1/2]

template<typename T>
virtual iterator MooseUtils::Buffer< T >::beginChunk ( const std::size_t  chunk_size)
pure virtual

Iterator for the first entry with a given chunk size in the buffer If chunk_size is greater than the size of the buffer, the full range will be given.

Implemented in MooseUtils::CircularBuffer< T >, and MooseUtils::LIFOBuffer< T >.

◆ beginChunk() [2/2]

template<typename T>
virtual const_iterator MooseUtils::Buffer< T >::beginChunk ( const std::size_t  chunk_size) const
pure virtual

Const iterator for the first entry with a given chunk size in the buffer If chunk_size is greater than the size of the buffer, the full range will be given.

Implemented in MooseUtils::CircularBuffer< T >, and MooseUtils::LIFOBuffer< T >.

◆ capacity()

template<typename T>
std::size_t MooseUtils::Buffer< T >::capacity ( ) const
inline

Get the capacity.

Definition at line 53 of file Buffer.h.

53 { return this->_data.size(); }
std::vector< T > _data
The raw data.
Definition: Buffer.h:205

◆ clear()

template<typename T >
void MooseUtils::Buffer< T >::clear ( )

Remove all entries (does not change the capacity) Note: this does NOT at all free any entries.

Definition at line 260 of file Buffer.h.

261 {
262  this->_begin_pos = 0;
263  this->_end_pos = 0;
264 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ data()

template<typename T>
const std::vector<T>& MooseUtils::Buffer< T >::data ( )
inline

Access the raw underlying storage.

This is considered an advanced interface. Typically, you should use the begin(), beginChunk(), end(), and endChunk() methods for accessing the data. This should really only be used in unit tests for verifying the underlying storage.

Definition at line 172 of file Buffer.h.

172 { return _data; }
std::vector< T > _data
The raw data.
Definition: Buffer.h:205

◆ dataBeginPos()

template<typename T>
std::size_t MooseUtils::Buffer< T >::dataBeginPos ( ) const
inline

The current beginning position of the buffer in data().

This is considered an advanced interface because data() is the internal storage for the buffer. It should really only be used in unit tests for verifying the underlying storage.

Definition at line 182 of file Buffer.h.

182 { return _begin_pos; }
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ dataEndPos()

template<typename T>
std::size_t MooseUtils::Buffer< T >::dataEndPos ( ) const
inline

The current end position of the buffer in data().

This is considered an advanced interface because data() is the internal storage for the buffer. It should really only be used in unit tests for verifying the underlying storage.

Definition at line 192 of file Buffer.h.

192 { return _end_pos; }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210

◆ empty()

template<typename T>
bool MooseUtils::Buffer< T >::empty ( ) const
inline

Whether or not the buffer is empty.

Definition at line 67 of file Buffer.h.

67 { return !size(); }
std::size_t size() const
Get the size.
Definition: Buffer.h:62

◆ end() [1/2]

template<typename T>
iterator MooseUtils::Buffer< T >::end ( )
inline

Iterator for the last entry in the buffer.

Definition at line 122 of file Buffer.h.

122 { return this->_data.begin() + this->_end_pos; }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205

◆ end() [2/2]

template<typename T>
const_iterator MooseUtils::Buffer< T >::end ( ) const
inline

Const iterator for the last entry in the buffer.

Definition at line 126 of file Buffer.h.

126 { return this->_data.begin() + this->_end_pos; }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205

◆ endChunk() [1/2]

template<typename T>
virtual iterator MooseUtils::Buffer< T >::endChunk ( const std::size_t  chunk_size)
pure virtual

Iterator for the last entry of a chunk size in the buffer If chunk_size is greater than the size of the buffer, the full range will be given.

Implemented in MooseUtils::CircularBuffer< T >, and MooseUtils::LIFOBuffer< T >.

◆ endChunk() [2/2]

template<typename T>
virtual const_iterator MooseUtils::Buffer< T >::endChunk ( const std::size_t  chunk_size) const
pure virtual

Const iterator for the last entry of a chunk size in the buffer If chunk_size is greater than the size of the buffer, the full range will be given.

Implemented in MooseUtils::CircularBuffer< T >, and MooseUtils::LIFOBuffer< T >.

◆ erase()

template<typename T>
virtual void MooseUtils::Buffer< T >::erase ( const std::size_t  num)
pure virtual

Remove the first num elements.

Note that erased items are not guaranteed to be freed immediately

Implemented in MooseUtils::CircularBuffer< T >, and MooseUtils::LIFOBuffer< T >.

◆ eraseChunk()

template<typename T>
virtual void MooseUtils::Buffer< T >::eraseChunk ( const std::size_t  chunk_size)
pure virtual

◆ move()

template<typename T >
void MooseUtils::Buffer< T >::move ( T &  value)

Moves the object into the buffer (calls std::move())

Definition at line 233 of file Buffer.h.

234 {
235  this->_end_pos = newEnd(this->_end_pos + 1);
236  this->_data[this->_end_pos - 1] = std::move(value);
237 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
virtual std::size_t newEnd(const std::size_t new_end)=0
Find out where the new end will be.

◆ newEnd()

template<typename T>
virtual std::size_t MooseUtils::Buffer< T >::newEnd ( const std::size_t  new_end)
protectedpure virtual

Find out where the new end will be.

This will resize/copy data as necessary

Parameters
new_endthe proposed new_end position
Returns
The actual position of the new ending

Implemented in MooseUtils::CircularBuffer< T >, and MooseUtils::LIFOBuffer< T >.

Referenced by MooseUtils::Buffer< T >::setSize().

◆ operator[]() [1/2]

template<typename T >
T & MooseUtils::Buffer< T >::operator[] ( const std::size_t  index)

Access an entry at index.

Definition at line 268 of file Buffer.h.

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 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ operator[]() [2/2]

template<typename T >
const T & MooseUtils::Buffer< T >::operator[] ( const std::size_t  index) const

Const access an entry at an index.

Definition at line 276 of file Buffer.h.

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 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ push_back()

template<typename T >
void MooseUtils::Buffer< T >::push_back ( const T &  value)

Add a new entry on the end.

Definition at line 225 of file Buffer.h.

226 {
227  this->_end_pos = newEnd(this->_end_pos + 1);
228  this->_data[this->_end_pos - 1] = value;
229 }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
virtual std::size_t newEnd(const std::size_t new_end)=0
Find out where the new end will be.

◆ setCapacity()

template<typename T>
void MooseUtils::Buffer< T >::setCapacity ( const std::size_t  capacity)
inline

Resize the capacity.

Definition at line 49 of file Buffer.h.

49 { this->_data.resize(capacity); }
std::size_t capacity() const
Get the capacity.
Definition: Buffer.h:53
std::vector< T > _data
The raw data.
Definition: Buffer.h:205

◆ setSize()

template<typename T>
void MooseUtils::Buffer< T >::setSize ( const std::size_t  size)
inline

Set the size.

Definition at line 58 of file Buffer.h.

58 { this->_end_pos = this->newEnd(this->_begin_pos + size); }
std::size_t size() const
Get the size.
Definition: Buffer.h:62
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208
virtual std::size_t newEnd(const std::size_t new_end)=0
Find out where the new end will be.

◆ size()

template<typename T>
std::size_t MooseUtils::Buffer< T >::size ( ) const
inline

Get the size.

Definition at line 62 of file Buffer.h.

Referenced by MooseUtils::Buffer< T >::empty().

62 { return this->_end_pos - this->_begin_pos; }
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ swap()

template<typename T >
void MooseUtils::Buffer< T >::swap ( std::vector< T > &  in_data)

Use in_data as our data vector.

Definition at line 284 of file Buffer.h.

285 {
286  std::swap(in_data, _data);
287  this->_begin_pos = 0;
288  this->_end_pos = this->_data.size();
289 }
void swap(std::vector< T > &data, const std::size_t idx0, const std::size_t idx1, const libMesh::Parallel::Communicator &comm)
Swap function for serial or distributed vector of data.
Definition: Shuffle.h:494
std::size_t _end_pos
The ending position.
Definition: Buffer.h:210
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

Member Data Documentation

◆ _begin_pos

template<typename T>
std::size_t MooseUtils::Buffer< T >::_begin_pos
protected

◆ _data

template<typename T>
std::vector<T> MooseUtils::Buffer< T >::_data
protected

◆ _end_pos

template<typename T>
std::size_t MooseUtils::Buffer< T >::_end_pos
protected

The documentation for this class was generated from the following file: