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

An optimized LIFO (Last In First Out) buffer. More...

#include <LIFOBuffer.h>

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

Public Types

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

Public Member Functions

 LIFOBuffer ()
 Create an empty LIFO buffer. More...
 
 LIFOBuffer (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::LIFOBuffer< T >

An optimized LIFO (Last In First Out) buffer.

Begin()/end() iterators can be used in OpenMP loops Also operator[] works sequentially between 0 and size()

It will also automatically grow larger if capacity is reached

Definition at line 26 of file LIFOBuffer.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

◆ LIFOBuffer() [1/2]

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

Create an empty LIFO buffer.

Definition at line 50 of file LIFOBuffer.h.

50  : Buffer<T>()
51 {
52 }

◆ LIFOBuffer() [2/2]

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

Definition at line 55 of file LIFOBuffer.h.

55  : Buffer<T>(capacity)
56 {
57 }
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::LIFOBuffer< 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 84 of file LIFOBuffer.h.

85 {
86  if (chunk_size > this->size())
87  return this->begin();
88  else
89  return this->end() - chunk_size;
90 }
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

◆ beginChunk() [2/2]

template<typename T >
Buffer< T >::const_iterator MooseUtils::LIFOBuffer< 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 94 of file LIFOBuffer.h.

95 {
96  if (chunk_size > this->size())
97  return this->begin();
98  else
99  return this->end() - chunk_size;
100 }
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

◆ 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::LIFOBuffer< 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 104 of file LIFOBuffer.h.

105 {
106  return this->end();
107 }
iterator end()
Iterator for the last entry in the buffer.
Definition: Buffer.h:122

◆ endChunk() [2/2]

template<typename T >
Buffer< T >::const_iterator MooseUtils::LIFOBuffer< 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 111 of file LIFOBuffer.h.

112 {
113  return this->end();
114 }
iterator end()
Iterator for the last entry in the buffer.
Definition: Buffer.h:122

◆ erase()

template<typename T >
void MooseUtils::LIFOBuffer< 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 61 of file LIFOBuffer.h.

62 {
63  mooseAssert(num <= this->size(), "Cannot erase past the last entry");
64 
65  this->_end_pos -= num;
66 
67  // If there's nothing in the buffer - let's reset the positions
68  if (this->_begin_pos == this->_end_pos)
69  this->clear();
70 }
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::LIFOBuffer< T >::eraseChunk ( const std::size_t  chunk_size)
overridevirtual

Implements MooseUtils::Buffer< T >.

Definition at line 74 of file LIFOBuffer.h.

75 {
76  if (chunk_size > this->size())
77  this->erase(this->size());
78  else
79  this->erase(chunk_size);
80 }
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.
Definition: LIFOBuffer.h:61

◆ 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::LIFOBuffer< 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 118 of file LIFOBuffer.h.

119 {
120  if (new_end > this->_data.size())
121  {
122  auto new_size = new_end - this->_begin_pos;
123 
124  this->_data.resize(std::max(new_size * 2, this->_data.size() * 2));
125  }
126 
127  return new_end;
128 }
auto max(const L &left, const R &right)
std::vector< T > _data
The raw data.
Definition: Buffer.h:205
std::size_t _begin_pos
The beginning position.
Definition: Buffer.h:208

◆ 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: