Reference documentation for deal.II version 9.3.0
\(\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 - 2020 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<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 &operator[](const std::size_t index);
87 
93  const T &operator[](const std::size_t index) const;
94 
98  std::size_t
99  size() const;
100 
104  std::size_t
105  max_size() const;
106 
110  void
111  clear();
112 
113 private:
117  std::size_t max_n_elements;
118 
122  std::deque<std::unique_ptr<T>> data;
123 
128  std::deque<std::unique_ptr<T>> cache;
129 };
130 
131 
132 
133 // ------------------- inline and template functions ----------------
134 #ifndef DOXYGEN
135 
136 
137 
138 template <typename T>
139 FiniteSizeHistory<T>::FiniteSizeHistory(const std::size_t max_elements)
140  : max_n_elements(max_elements)
141 {}
142 
143 
144 
145 template <typename T>
146 void
147 FiniteSizeHistory<T>::remove(const std::size_t ind)
148 {
149  AssertIndexRange(ind, data.size());
150  auto el = std::move(data[ind]);
151  data.erase(data.begin() + ind);
152 
153  cache.push_back(std::move(el));
154 
155  // whatever we do, we shall not store more than the maximum number of
156  // elements
157  Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
158 }
159 
160 
161 
162 template <typename T>
163 void
164 FiniteSizeHistory<T>::add(const T &element)
165 {
166  std::unique_ptr<T> new_el;
167  if (data.size() < max_n_elements)
168  // have not reached the maximum number of elements yet
169  {
170  if (cache.size() == 0)
171  // nothing is cached, just copy a given element
172  {
173  new_el = std::make_unique<T>(element);
174  }
175  else
176  // something is cached, take one element and copy
177  // the user provided one there.
178  {
179  new_el = std::move(cache.back());
180  (*new_el) = element;
181 
182  cache.pop_back(); // removes a pointer that is now a nullptr anyway
183  }
184  }
185  else
186  // we reached the maximum number of elements and
187  // thus have to re-order/cycle elements currently stored
188  {
189  new_el = std::move(data.back());
190  (*new_el) = element;
191 
192  data.pop_back(); // removes a pointer that is now a nullptr anyway
193  }
194 
195  // finally insert the new one where appropriate
196  data.push_front(std::move(new_el));
197 
198  // whatever we do, we shall not store more than the maximum number of
199  // elements
200  Assert(data.size() + cache.size() <= max_n_elements, ExcInternalError());
201 }
202 
203 
204 
205 template <typename T>
206 T &FiniteSizeHistory<T>::operator[](const std::size_t ind)
207 {
208  AssertIndexRange(ind, data.size());
209  return *data[ind];
210 }
211 
212 
213 
214 template <typename T>
215 const T &FiniteSizeHistory<T>::operator[](const std::size_t ind) const
216 {
217  AssertIndexRange(ind, data.size());
218  return *data[ind];
219 }
220 
221 
222 
223 template <typename T>
224 std::size_t
226 {
227  return data.size();
228 }
229 
230 
231 
232 template <typename T>
233 std::size_t
235 {
236  return max_n_elements;
237 }
238 
239 
240 
241 template <typename T>
242 void
244 {
245  data.clear();
246  cache.clear();
247 }
248 
249 #endif // Doxygen
250 
252 
253 #endif // dealii_storage_h
std::size_t size() const
void remove(const std::size_t index)
#define AssertIndexRange(index, range)
Definition: exceptions.h:1690
T & operator[](const std::size_t index)
std::deque< std::unique_ptr< T > > data
Definition: history.h:122
static const char T
#define Assert(cond, exc)
Definition: exceptions.h:1465
std::deque< std::unique_ptr< T > > cache
Definition: history.h:128
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:395
void add(const T &element)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:394
std::size_t max_size() const
FiniteSizeHistory(const std::size_t max_elements=0)
std::size_t max_n_elements
Definition: history.h:117
static ::ExceptionBase & ExcInternalError()