Reference documentation for deal.II version GIT relicensing-464-g14f7274e4d 2024-04-22 15:40:02+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: 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// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_storage_h
16#define dealii_storage_h
17
18#include <deal.II/base/config.h>
19
21
22#include <deque>
23#include <memory>
24#include <type_traits>
25
27
47template <typename T>
49{
50public:
51 static_assert(
52 std::is_default_constructible_v<T>,
53 "This class requires that the elements of type T are default constructible.");
54
61 FiniteSizeHistory(const std::size_t max_elements = 0);
62
68 void
69 add(const T &element);
70
77 void
78 remove(const std::size_t index);
79
85 T &
86 operator[](const std::size_t index);
87
93 const T &
94 operator[](const std::size_t index) const;
95
99 std::size_t
100 size() const;
101
105 std::size_t
106 max_size() const;
107
111 void
113
114private:
118 std::size_t max_n_elements;
119
123 std::deque<std::unique_ptr<T>> data;
124
129 std::deque<std::unique_ptr<T>> cache;
130};
131
132
133
134// ------------------- inline and template functions ----------------
135#ifndef DOXYGEN
136
137
138
139template <typename T>
140FiniteSizeHistory<T>::FiniteSizeHistory(const std::size_t max_elements)
141 : max_n_elements(max_elements)
142{}
143
144
145
146template <typename T>
147void
148FiniteSizeHistory<T>::remove(const std::size_t ind)
149{
150 AssertIndexRange(ind, data.size());
151 auto el = std::move(data[ind]);
152 data.erase(data.begin() + ind);
153
154 cache.push_back(std::move(el));
155
156 // whatever we do, we shall not store more than the maximum number of
157 // elements
158 Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
159}
160
161
162
163template <typename T>
164void
165FiniteSizeHistory<T>::add(const T &element)
166{
167 std::unique_ptr<T> new_el;
168 if (data.size() < max_n_elements)
169 // have not reached the maximum number of elements yet
170 {
171 if (cache.empty())
172 // nothing is cached, just copy a given element
173 {
174 new_el = std::make_unique<T>(element);
175 }
176 else
177 // something is cached, take one element and copy
178 // the user provided one there.
179 {
180 new_el = std::move(cache.back());
181 (*new_el) = element;
182
183 cache.pop_back(); // removes a pointer that is now a nullptr anyway
184 }
185 }
186 else
187 // we reached the maximum number of elements and
188 // thus have to re-order/cycle elements currently stored
189 {
190 new_el = std::move(data.back());
191 (*new_el) = element;
192
193 data.pop_back(); // removes a pointer that is now a nullptr anyway
194 }
195
196 // finally insert the new one where appropriate
197 data.push_front(std::move(new_el));
198
199 // whatever we do, we shall not store more than the maximum number of
200 // elements
201 Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
202}
203
204
205
206template <typename T>
207T &
208FiniteSizeHistory<T>::operator[](const std::size_t ind)
209{
210 AssertIndexRange(ind, data.size());
211 return *data[ind];
212}
213
214
215
216template <typename T>
217const T &
218FiniteSizeHistory<T>::operator[](const std::size_t ind) const
219{
220 AssertIndexRange(ind, data.size());
221 return *data[ind];
222}
223
224
225
226template <typename T>
227std::size_t
229{
230 return data.size();
231}
232
233
234
235template <typename T>
236std::size_t
238{
239 return max_n_elements;
240}
241
242
243
244template <typename T>
245void
247{
248 data.clear();
249 cache.clear();
250}
251
252#endif // Doxygen
253
255
256#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:129
std::size_t max_n_elements
Definition history.h:118
void add(const T &element)
std::deque< std::unique_ptr< T > > data
Definition history.h:123
void remove(const std::size_t index)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()