Reference documentation for deal.II version GIT 35969cdc9b 2023-12-09 01:10: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\}}\)
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 
48 template <typename T>
50 {
51 public:
52  static_assert(
53  std::is_default_constructible_v<T>,
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
113  clear();
114 
115 private:
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 
140 template <typename T>
141 FiniteSizeHistory<T>::FiniteSizeHistory(const std::size_t max_elements)
142  : max_n_elements(max_elements)
143 {}
144 
145 
146 
147 template <typename T>
148 void
149 FiniteSizeHistory<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 
164 template <typename T>
165 void
166 FiniteSizeHistory<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.empty())
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 
207 template <typename T>
208 T &
209 FiniteSizeHistory<T>::operator[](const std::size_t ind)
210 {
211  AssertIndexRange(ind, data.size());
212  return *data[ind];
213 }
214 
215 
216 
217 template <typename T>
218 const T &
219 FiniteSizeHistory<T>::operator[](const std::size_t ind) const
220 {
221  AssertIndexRange(ind, data.size());
222  return *data[ind];
223 }
224 
225 
226 
227 template <typename T>
228 std::size_t
230 {
231  return data.size();
232 }
233 
234 
235 
236 template <typename T>
237 std::size_t
239 {
240  return max_n_elements;
241 }
242 
243 
244 
245 template <typename T>
246 void
248 {
249  data.clear();
250  cache.clear();
251 }
252 
253 #endif // Doxygen
254 
256 
257 #endif // dealii_storage_h
std::size_t size() const
T & operator[](const std::size_t index)
std::size_t max_size() const
const T & operator[](const std::size_t index) 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:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
Definition: exceptions.h:1631
#define AssertIndexRange(index, range)
Definition: exceptions.h:1888
static const char T