Reference documentation for deal.II version 9.2.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 
23 
24 #include <deque>
25 #include <type_traits>
26 
28 
50 template <typename T>
52 {
53 public:
54  static_assert(
56  "This class requires that the elements of type T are default constructible.");
57 
64  FiniteSizeHistory(const std::size_t max_elements = 0);
65 
71  void
72  add(const T &element);
73 
80  void
81  remove(const std::size_t index);
82 
88  T &operator[](const std::size_t index);
89 
95  const T &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.size() == 0)
173  // nothing is cached, just copy a given element
174  {
175  new_el = std_cxx14::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 &FiniteSizeHistory<T>::operator[](const std::size_t ind)
209 {
210  AssertIndexRange(ind, data.size());
211  return *data[ind];
212 }
213 
214 
215 
216 template <typename T>
217 const T &FiniteSizeHistory<T>::operator[](const std::size_t ind) const
218 {
219  AssertIndexRange(ind, data.size());
220  return *data[ind];
221 }
222 
223 
224 
225 template <typename T>
226 std::size_t
228 {
229  return data.size();
230 }
231 
232 
233 
234 template <typename T>
235 std::size_t
237 {
238  return max_n_elements;
239 }
240 
241 
242 
243 template <typename T>
244 void
246 {
247  data.clear();
248  cache.clear();
249 }
250 
251 #endif // Doxygen
252 
254 
255 #endif // dealii_storage_h
FiniteSizeHistory::remove
void remove(const std::size_t index)
FiniteSizeHistory
Definition: history.h:51
AssertIndexRange
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
FiniteSizeHistory::data
std::deque< std::unique_ptr< T > > data
Definition: history.h:124
FiniteSizeHistory::cache
std::deque< std::unique_ptr< T > > cache
Definition: history.h:130
FiniteSizeHistory::operator[]
T & operator[](const std::size_t index)
FiniteSizeHistory::clear
void clear()
LAPACKSupport::T
static const char T
Definition: lapack_support.h:163
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
exceptions.h
FiniteSizeHistory::FiniteSizeHistory
FiniteSizeHistory(const std::size_t max_elements=0)
value
static const bool value
Definition: dof_tools_constraints.cc:433
StandardExceptions::ExcInternalError
static ::ExceptionBase & ExcInternalError()
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
FiniteSizeHistory::max_n_elements
std::size_t max_n_elements
Definition: history.h:119
FiniteSizeHistory::add
void add(const T &element)
config.h
memory.h
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
FiniteSizeHistory::max_size
std::size_t max_size() const
FiniteSizeHistory::size
std::size_t size() const