https://mooseframework.inl.gov
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 
14 namespace MooseUtils
15 {
16 
25 template <typename T>
26 class LIFOBuffer : public Buffer<T>
27 {
28 public:
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 
45 protected:
46  virtual std::size_t newEnd(const std::size_t new_end) override;
47 };
48 
49 template <typename T>
51 {
52 }
53 
54 template <typename T>
55 LIFOBuffer<T>::LIFOBuffer(const std::size_t capacity) : Buffer<T>(capacity)
56 {
57 }
58 
59 template <typename T>
60 void
61 LIFOBuffer<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 
72 template <typename T>
73 void
74 LIFOBuffer<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 
82 template <typename T>
83 typename Buffer<T>::iterator
84 LIFOBuffer<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 
92 template <typename T>
94 LIFOBuffer<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 
102 template <typename T>
103 typename Buffer<T>::iterator
104 LIFOBuffer<T>::endChunk(const std::size_t /* chunk_size */)
105 {
106  return this->end();
107 }
108 
109 template <typename T>
111 LIFOBuffer<T>::endChunk(const std::size_t /* chunk_size */) const
112 {
113  return this->end();
114 }
115 
116 template <typename T>
117 std::size_t
118 LIFOBuffer<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 }
std::vector< T >::const_iterator const_iterator
Definition: Buffer.h:32
LIFOBuffer()
Create an empty LIFO buffer.
Definition: LIFOBuffer.h:50
Base class for a buffer.
Definition: Buffer.h:28
std::vector< T >::iterator iterator
Definition: Buffer.h:31
auto max(const L &left, const R &right)
std::size_t capacity() const
Get the capacity.
Definition: Buffer.h:53
virtual void eraseChunk(const std::size_t chunk_size) override
Definition: LIFOBuffer.h:74
sideset clear()
virtual void erase(const std::size_t num) override
Remove the first num elements.
Definition: LIFOBuffer.h:61
An optimized LIFO (Last In First Out) buffer.
Definition: LIFOBuffer.h:26
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 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 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