deal.II version GIT relicensing-6842-g793a97d2aa 2026-10-02 14:00:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
history.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2018 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_storage_h
14#define dealii_storage_h
15
16#include <deal.II/base/config.h>
17
19
20#include <deque>
21#include <memory>
22#include <type_traits>
23
25
45template <typename T>
47{
48public:
49 static_assert(
50 std::is_default_constructible_v<T>,
51 "This class requires that the elements of type T are default constructible.");
52
59 FiniteSizeHistory(const std::size_t max_elements = 0);
60
66 void
67 add(const T &element);
68
75 void
76 remove(const std::size_t index);
77
83 T &
84 operator[](const std::size_t index);
85
91 const T &
92 operator[](const std::size_t index) const;
93
97 std::size_t
98 size() const;
99
103 std::size_t
104 max_size() const;
105
109 void
111
112private:
116 std::size_t max_n_elements;
117
121 std::deque<std::unique_ptr<T>> data;
122
127 std::deque<std::unique_ptr<T>> cache;
128};
129
130
131
132// ------------------- inline and template functions ----------------
133#ifndef DOXYGEN
134
135
136
137template <typename T>
138FiniteSizeHistory<T>::FiniteSizeHistory(const std::size_t max_elements)
139 : max_n_elements(max_elements)
140{}
141
142
143
144template <typename T>
145void
146FiniteSizeHistory<T>::remove(const std::size_t ind)
147{
148 AssertIndexRange(ind, data.size());
149 auto el = std::move(data[ind]);
150 data.erase(data.begin() + ind);
151
152 cache.push_back(std::move(el));
153
154 // whatever we do, we shall not store more than the maximum number of
155 // elements
156 Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
157}
158
159
160
161template <typename T>
162void
163FiniteSizeHistory<T>::add(const T &element)
164{
165 std::unique_ptr<T> new_el;
166 if (data.size() < max_n_elements)
167 // have not reached the maximum number of elements yet
168 {
169 if (cache.empty())
170 // nothing is cached, just copy a given element
171 {
172 new_el = std::make_unique<T>(element);
173 }
174 else
175 // something is cached, take one element and copy
176 // the user provided one there.
177 {
178 new_el = std::move(cache.back());
179 (*new_el) = element;
180
181 cache.pop_back(); // removes a pointer that is now a nullptr anyway
182 }
183 }
184 else
185 // we reached the maximum number of elements and
186 // thus have to re-order/cycle elements currently stored
187 {
188 new_el = std::move(data.back());
189 (*new_el) = element;
190
191 data.pop_back(); // removes a pointer that is now a nullptr anyway
192 }
193
194 // finally insert the new one where appropriate
195 data.push_front(std::move(new_el));
196
197 // whatever we do, we shall not store more than the maximum number of
198 // elements
199 Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
200}
201
202
203
204template <typename T>
205T &
206FiniteSizeHistory<T>::operator[](const std::size_t ind)
207{
208 AssertIndexRange(ind, data.size());
209 return *data[ind];
210}
211
212
213
214template <typename T>
215const T &
216FiniteSizeHistory<T>::operator[](const std::size_t ind) const
217{
218 AssertIndexRange(ind, data.size());
219 return *data[ind];
220}
221
222
223
224template <typename T>
225std::size_t
227{
228 return data.size();
229}
230
231
232
233template <typename T>
234std::size_t
236{
237 return max_n_elements;
238}
239
240
241
242template <typename T>
243void
245{
246 data.clear();
247 cache.clear();
248}
249
250#endif // Doxygen
251
253
254#endif // dealii_storage_h
T & operator[](const std::size_t index)
std::size_t size() const
const T & operator[](const std::size_t index) const
std::size_t max_size() const
FiniteSizeHistory(const std::size_t max_elements=0)
std::deque< std::unique_ptr< T > > cache
Definition history.h:127
std::size_t max_n_elements
Definition history.h:116
void add(const T &element)
std::deque< std::unique_ptr< T > > data
Definition history.h:121
void remove(const std::size_t index)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
std::vector< index_type > data
Definition mpi.cc:734
constexpr char T