https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
 LIFOBuffer (const std::size_t capacity)
 
virtual void erase (const std::size_t num) override
 Remove the first num elements.
 
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.
 
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.
 
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.
 
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.
 
void setCapacity (const std::size_t capacity)
 Resize the capacity.
 
std::size_t capacity () const
 Get the capacity.
 
void setSize (const std::size_t size)
 Set the size.
 
std::size_t size () const
 Get the size.
 
bool empty () const
 Whether or not the buffer is empty.
 
void push_back (const T &value)
 Add a new entry on the end.
 
void move (T &value)
 Moves the object into the buffer (calls std::move())
 
void append (const_iterator in_begin, const_iterator in_end)
 Add new entries to the end.
 
void append (const std::vector< T > &vals)
 Add new entries to the end.
 
void clear ()
 Remove all entries (does not change the capacity) Note: this does NOT at all free any entries.
 
iterator begin ()
 Iterator for the first entry in the buffer.
 
const_iterator begin () const
 Const iterator for the first entry in the buffer.
 
iterator end ()
 Iterator for the last entry in the buffer.
 
const_iterator end () const
 Const iterator for the last entry in the buffer.
 
T & operator[] (const std::size_t index)
 Access an entry at index.
 
const T & operator[] (const std::size_t index) const
 Const access an entry at an index.
 
void swap (std::vector< T > &in_data)
 Use in_data as our data vector.
 
const std::vector< T > & data ()
 Access the raw underlying storage.
 
std::size_t dataBeginPos () const
 The current beginning position of the buffer in data().
 
std::size_t dataEndPos () const
 The current end position of the buffer in data().
 

Protected Member Functions

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

Protected Attributes

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

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

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

◆ 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; }

◆ beginChunk() [1/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 begin()
Iterator for the first entry in the buffer.
Definition Buffer.h:113

◆ beginChunk() [2/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}

◆ 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(); }

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

◆ 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; }

◆ 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; }

◆ 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; }

◆ 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(); }

◆ 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; }

◆ 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; }

◆ endChunk() [1/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}

◆ endChunk() [2/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}

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

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

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

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

◆ 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}
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

◆ 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); }

◆ 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); }

◆ size()

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

Get the size.

Definition at line 62 of file Buffer.h.

62{ return this->_end_pos - this->_begin_pos; }

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

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

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: