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

An optimized circular buffer. More...

#include <CircularBuffer.h>

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

Public Types

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

Public Member Functions

 CircularBuffer ()
 
 CircularBuffer (const std::size_t capacity)
 
virtual void erase (const std::size_t num) override
 Remove the first num elements. More...
 
virtual void eraseChunk (const std::size_t chunk_size) override
 
virtual Buffer< T >::iterator beginChunk (const std::size_t chunk_size) override
 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 Buffer< T >::const_iterator beginChunk (const std::size_t chunk_size) const override
 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 Buffer< T >::iterator endChunk (const std::size_t chunk_size) override
 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 Buffer< T >::const_iterator endChunk (const std::size_t chunk_size) const override
 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...
 
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...
 
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...
 
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) override
 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::CircularBuffer< T >

An optimized circular buffer.

This also always ensures that begin() < end(). That means that if the end of the buffer capacity is reached, an O(N) operation will be used to move data from the end of the capacity to the beginning

This is done so that begin()/end() iterators can be used in OpenMP loops Also means that operator[] works sequentially between 0 and size()

It will also automatically grow larger if capacity is reached

NOTE: This buffer will not properly wrap around as a standard circular buffer does. Once the end of the internal storage has been reached, the data will be moved to the beginning of the internal storage via a copy. This is needed to ensure that the access through the begin()/end() iterators is contiguous.

Definition at line 36 of file CircularBuffer.h.

Member Typedef Documentation

◆ const_iterator

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

Definition at line 32 of file Buffer.h.

◆ iterator

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

Definition at line 31 of file Buffer.h.

Constructor & Destructor Documentation

◆ CircularBuffer() [1/2]

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

Definition at line 57 of file CircularBuffer.h.

57  : Buffer<T>()
58 {
59 }

◆ CircularBuffer() [2/2]

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

Definition at line 62 of file CircularBuffer.h.

62  : Buffer<T>(capacity)
63 {
64 }
std::size_t capacity() const
Get the capacity.
Definition: Buffer.h:53

Member Function Documentation

◆ append() [1/2]

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

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)
inherited

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 ( )
inlineinherited

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
inlineinherited

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 >
Buffer< T >::iterator MooseUtils::CircularBuffer< T >::beginChunk ( const std::size_t  chunk_size)
overridevirtual

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.

Implements MooseUtils::Buffer< T >.

Definition at line 91 of file CircularBuffer.h.

92 {
93  return this->begin();
94 }
iterator begin()
Iterator for the first entry in the buffer.
Definition: Buffer.h:113

◆ beginChunk() [2/2]

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

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.

Implements MooseUtils::Buffer< T >.

Definition at line 98 of file CircularBuffer.h.

99 {
100  return this->begin();
101 }
iterator begin()
Iterator for the first entry in the buffer.
Definition: Buffer.h:113

◆ capacity()

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

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 ( )
inherited

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 ( )
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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 ( )
inlineinherited

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
inlineinherited

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 >
Buffer< T >::iterator MooseUtils::CircularBuffer< T >::endChunk ( const std::size_t  chunk_size)
overridevirtual

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.

Implements MooseUtils::Buffer< T >.

Definition at line 105 of file CircularBuffer.h.

106 {
107  if (chunk_size > this->size())
108  return this->end();
109  else
110  return this->begin() + chunk_size;
111 }
std::size_t size() const
Get the size.
Definition: Buffer.h:62
iterator end()
Iterator for the last entry in the buffer.
Definition: Buffer.h:122
iterator begin()
Iterator for the first entry in the buffer.
Definition: Buffer.h:113

◆ endChunk() [2/2]

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

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.

Implements MooseUtils::Buffer< T >.

Definition at line 115 of file CircularBuffer.h.

116 {
117  if (chunk_size > this->size())
118  return this->end();
119  else
120  return this->begin() + chunk_size;
121 }
std::size_t size() const
Get the size.
Definition: Buffer.h:62
iterator end()
Iterator for the last entry in the buffer.
Definition: Buffer.h:122
iterator begin()
Iterator for the first entry in the buffer.
Definition: Buffer.h:113

◆ erase()

template<typename T >
void MooseUtils::CircularBuffer< T >::erase ( const std::size_t  num)
overridevirtual

Remove the first num elements.

Note that erased items are not guaranteed to be freed immediately

Implements MooseUtils::Buffer< T >.

Definition at line 68 of file CircularBuffer.h.

69 {
70  mooseAssert(num <= this->size(), "Cannot erase past the last entry");
71 
72  this->_begin_pos += num;
73 
74  // If there's nothing in the buffer - let's reset the positions
75  if (this->_begin_pos == this->_end_pos)
76  this->clear();
77 }
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
void clear()
Remove all entries (does not change the capacity) Note: this does NOT at all free any entries...
Definition: Buffer.h:260

◆ eraseChunk()

template<typename T >
void MooseUtils::CircularBuffer< T >::eraseChunk ( const std::size_t  chunk_size)
overridevirtual

Implements MooseUtils::Buffer< T >.

Definition at line 81 of file CircularBuffer.h.

82 {
83  if (chunk_size > this->size())
84  this->erase(this->size());
85  else
86  this->erase(chunk_size);
87 }
std::size_t size() const
Get the size.
Definition: Buffer.h:62
virtual void erase(const std::size_t num) override
Remove the first num elements.

◆ move()

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

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 >
std::size_t MooseUtils::CircularBuffer< T >::newEnd ( const std::size_t  new_end)
overrideprotectedvirtual

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

Implements MooseUtils::Buffer< T >.

Definition at line 125 of file CircularBuffer.h.

126 {
127  auto actual_new_end = new_end;
128 
129  if (new_end > this->_data.size())
130  {
131  const auto new_size = new_end - this->_begin_pos;
132 
133  // See if we need to grow our capacity
134  if (this->_begin_pos == 0) // If we're already using the beginning - just resize
135  this->_data.resize(2 * new_size);
136  else
137  {
138  // Move everything to the beginning
139  auto to_it = this->_data.begin();
140  for (auto from_it = this->begin(); from_it < this->end(); ++from_it)
141  *to_it++ = std::move(*from_it);
142 
143  this->_begin_pos = 0;
144  actual_new_end = new_size;
145 
146  // If there still isn't room... add space
147  if (actual_new_end > this->_data.size())
148  this->_data.resize(2 * new_size);
149  }
150  }
151 
152  return actual_new_end;
153 }
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
iterator end()
Iterator for the last entry in the buffer.
Definition: Buffer.h:122
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208
iterator begin()
Iterator for the first entry in the buffer.
Definition: Buffer.h:113

◆ operator[]() [1/2]

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

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
inherited

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)
inherited

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)
inlineinherited

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)
inlineinherited

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
inlineinherited

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)
inherited

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
protectedinherited

◆ _data

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

◆ _end_pos

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

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