Reference documentation for deal.II version 9.4.1
\(\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// Copyright (C) 2018 - 2021 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#ifndef dealii_storage_h
17#define dealii_storage_h
18
19#include <deal.II/base/config.h>
20
22
23#include <deque>
24#include <memory>
25#include <type_traits>
26
28
48template <typename T>
50{
51public:
52 static_assert(
53 std::is_default_constructible<T>::value,
54 "This class requires that the elements of type T are default constructible.");
55
62 FiniteSizeHistory(const std::size_t max_elements = 0);
63
69 void
70 add(const T &element);
71
78 void
79 remove(const std::size_t index);
80
86 T &
87 operator[](const std::size_t index);
88
94 const T &
95 operator[](const std::size_t index) const;
96
100 std::size_t
101 size() const;
102
106 std::size_t
107 max_size() const;
108
112 void
114
115private:
119 std::size_t max_n_elements;
120
124 std::deque<std::unique_ptr<T>> data;
125
130 std::deque<std::unique_ptr<T>> cache;
131};
132
133
134
135// ------------------- inline and template functions ----------------
136#ifndef DOXYGEN
137
138
139
140template <typename T>
141FiniteSizeHistory<T>::FiniteSizeHistory(const std::size_t max_elements)
142 : max_n_elements(max_elements)
143{}
144
145
146
147template <typename T>
148void
149FiniteSizeHistory<T>::remove(const std::size_t ind)
150{
151 AssertIndexRange(ind, data.size());
152 auto el = std::move(data[ind]);
153 data.erase(data.begin() + ind);
154
155 cache.push_back(std::move(el));
156
157 // whatever we do, we shall not store more than the maximum number of
158 // elements
159 Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
160}
161
162
163
164template <typename T>
165void
166FiniteSizeHistory<T>::add(const T &element)
167{
168 std::unique_ptr<T> new_el;
169 if (data.size() < max_n_elements)
170 // have not reached the maximum number of elements yet
171 {
172 if (cache.size() == 0)
173 // nothing is cached, just copy a given element
174 {
175 new_el = std::make_unique<T>(element);
176 }
177 else
178 // something is cached, take one element and copy
179 // the user provided one there.
180 {
181 new_el = std::move(cache.back());
182 (*new_el) = element;
183
184 cache.pop_back(); // removes a pointer that is now a nullptr anyway
185 }
186 }
187 else
188 // we reached the maximum number of elements and
189 // thus have to re-order/cycle elements currently stored
190 {
191 new_el = std::move(data.back());
192 (*new_el) = element;
193
194 data.pop_back(); // removes a pointer that is now a nullptr anyway
195 }
196
197 // finally insert the new one where appropriate
198 data.push_front(std::move(new_el));
199
200 // whatever we do, we shall not store more than the maximum number of
201 // elements
202 Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
203}
204
205
206
207template <typename T>
208T &
209FiniteSizeHistory<T>::operator[](const std::size_t ind)
210{
211 AssertIndexRange(ind, data.size());
212 return *data[ind];
213}
214
215
216
217template <typename T>
218const T &
219FiniteSizeHistory<T>::operator[](const std::size_t ind) const
220{
221 AssertIndexRange(ind, data.size());
222 return *data[ind];
223}
224
225
226
227template <typename T>
228std::size_t
230{
231 return data.size();
232}
233
234
235
236template <typename T>
237std::size_t
239{
240 return max_n_elements;
241}
242
243
244
245template <typename T>
246void
248{
249 data.clear();
250 cache.clear();
251}
252
253#endif // Doxygen
254
256
257#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:130
std::size_t max_n_elements
Definition: history.h:119
void add(const T &element)
std::deque< std::unique_ptr< T > > data
Definition: history.h:124
void remove(const std::size_t index)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define AssertIndexRange(index, range)
Definition: exceptions.h:1732
static ::ExceptionBase & ExcInternalError()