https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LIFOBuffer.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 "Buffer.h"
13
14namespace MooseUtils
15{
16
25template <typename T>
26class LIFOBuffer : public Buffer<T>
27{
28public:
32 LIFOBuffer();
33
34 LIFOBuffer(const std::size_t capacity);
35
36 virtual void erase(const std::size_t num) override;
37 virtual void eraseChunk(const std::size_t chunk_size) override;
38
39 virtual typename Buffer<T>::iterator beginChunk(const std::size_t chunk_size) override;
40 virtual typename Buffer<T>::const_iterator
41 beginChunk(const std::size_t chunk_size) const override;
42 virtual typename Buffer<T>::iterator endChunk(const std::size_t chunk_size) override;
43 virtual typename Buffer<T>::const_iterator endChunk(const std::size_t chunk_size) const override;
44
45protected:
46 virtual std::size_t newEnd(const std::size_t new_end) override;
47};
48
49template <typename T>
53
54template <typename T>
55LIFOBuffer<T>::LIFOBuffer(const std::size_t capacity) : Buffer<T>(capacity)
56{
57}
58
59template <typename T>
60void
61LIFOBuffer<T>::erase(const std::size_t num)
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}
71
72template <typename T>
73void
74LIFOBuffer<T>::eraseChunk(const std::size_t chunk_size)
75{
76 if (chunk_size > this->size())
77 this->erase(this->size());
78 else
79 this->erase(chunk_size);
80}
81
82template <typename T>
84LIFOBuffer<T>::beginChunk(const std::size_t chunk_size)
85{
86 if (chunk_size > this->size())
87 return this->begin();
88 else
89 return this->end() - chunk_size;
90}
91
92template <typename T>
94LIFOBuffer<T>::beginChunk(const std::size_t chunk_size) const
95{
96 if (chunk_size > this->size())
97 return this->begin();
98 else
99 return this->end() - chunk_size;
100}
101
102template <typename T>
103typename Buffer<T>::iterator
104LIFOBuffer<T>::endChunk(const std::size_t /* chunk_size */)
105{
106 return this->end();
107}
108
109template <typename T>
111LIFOBuffer<T>::endChunk(const std::size_t /* chunk_size */) const
112{
113 return this->end();
114}
115
116template <typename T>
117std::size_t
118LIFOBuffer<T>::newEnd(const std::size_t new_end)
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}
129
130}
sideset clear()
Base class for a buffer.
Definition Buffer.h:29
std::vector< T >::iterator iterator
Definition Buffer.h:31
std::vector< T >::const_iterator const_iterator
Definition Buffer.h:32
std::size_t capacity() const
Get the capacity.
Definition Buffer.h:53
An optimized LIFO (Last In First Out) buffer.
Definition LIFOBuffer.h:27
LIFOBuffer()
Create an empty LIFO buffer.
Definition LIFOBuffer.h:50
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 t...
Definition LIFOBuffer.h:104
virtual void eraseChunk(const std::size_t chunk_size) override
Definition LIFOBuffer.h:74
virtual std::size_t newEnd(const std::size_t new_end) override
Find out where the new end will be.
Definition LIFOBuffer.h:118
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 ...
Definition LIFOBuffer.h:84
virtual void erase(const std::size_t num) override
Remove the first num elements.
Definition LIFOBuffer.h:61