Reference documentation for deal.II version Git 4e8af90f34 2022-01-22 18:32:44 +0100
\(\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\}}\)
collection.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 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_hp_collection_h
17 #define dealii_hp_collection_h
18 
19 #include <deal.II/base/config.h>
20 
23 
24 #include <memory>
25 #include <vector>
26 
28 
29 namespace hp
30 {
34  template <typename T>
36  {
37  public:
44  CollectionIterator(const std::vector<std::shared_ptr<const T>> &data,
45  const std::size_t index)
46  : data(&data)
47  , index(index)
48  {}
49 
53  CollectionIterator(const CollectionIterator<T> &other) = default;
54 
59  operator=(const CollectionIterator<T> &other) = default;
60 
64  bool
65  operator==(const CollectionIterator<T> &other) const
66  {
67  Assert(
68  this->data == other.data,
69  ExcMessage(
70  "You are trying to compare iterators into different hp::Collection objects."));
71  return this->index == other.index;
72  }
73 
77  bool
78  operator!=(const CollectionIterator<T> &other) const
79  {
80  Assert(
81  this->data == other.data,
82  ExcMessage(
83  "You are trying to compare iterators into different hp::Collection objects."));
84  return this->index != other.index;
85  }
86 
90  const T &
91  operator*() const
92  {
93  AssertIndexRange(index, data->size());
94  return *(*data)[index];
95  }
96 
104  {
105  AssertIndexRange(index + 1, data->size() + 1);
106  index++;
107  return *this;
108  }
109 
115  operator+=(const std::size_t offset)
116  {
117  AssertIndexRange(index + offset, data->size() + 1);
118  index += offset;
119  return *this;
120  }
121 
129  {
130  Assert(
131  index > 0,
132  ExcMessage(
133  "You can't decrement an iterator that is already at the beginning of the range."));
134  --index;
135  return *this;
136  }
137 
142  operator+(const std::size_t &offset) const
143  {
144  AssertIndexRange(index + offset, T::size() + 1);
145  return CollectionIterator<T>(*data, index + offset);
146  }
147 
151  std::ptrdiff_t
152  operator-(const CollectionIterator<T> &other) const
153  {
154  return static_cast<std::ptrdiff_t>(index) -
155  static_cast<ptrdiff_t>(other.index);
156  }
157 
158  private:
162  const std::vector<std::shared_ptr<const T>> *data;
163 
167  std::size_t index;
168  };
169 
178  template <typename T>
179  class Collection : public Subscriptor
180  {
181  public:
186  Collection() = default;
187 
191  void
192  push_back(const std::shared_ptr<const T> &new_entry);
193 
201  const T &
202  operator[](const unsigned int index) const;
203 
207  unsigned int
208  size() const;
209 
214  std::size_t
215  memory_consumption() const;
216 
222  begin() const;
223 
229  end() const;
230 
231  private:
235  std::vector<std::shared_ptr<const T>> entries;
236  };
237 
238 
239  /* --------------- inline functions ------------------- */
240 
241 
242 
243  template <typename T>
244  std::size_t
246  {
247  return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
248  }
249 
250 
251 
252  template <typename T>
253  void
254  Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
255  {
256  entries.push_back(new_entry);
257  }
258 
259 
260 
261  template <typename T>
262  inline unsigned int
264  {
265  return entries.size();
266  }
267 
268 
269 
270  template <typename T>
271  inline const T &
272  Collection<T>::operator[](const unsigned int index) const
273  {
274  AssertIndexRange(index, entries.size());
275  return *entries[index];
276  }
277 
278 
279 
280  template <typename T>
283  {
284  return CollectionIterator<T>(entries, 0);
285  }
286 
287 
288 
289  template <typename T>
292  {
293  return CollectionIterator<T>(entries, entries.size());
294  }
295 
296 } // namespace hp
297 
298 
300 
301 namespace std
302 {
306  template <class T>
307  struct iterator_traits<::hp::CollectionIterator<T>>
308  {
309  using iterator_category = random_access_iterator_tag;
310  using value_type = T;
311  using difference_type = std::ptrdiff_t;
312  };
313 } // namespace std
314 
315 #endif
bool operator==(const CollectionIterator< T > &other) const
Definition: collection.h:65
unsigned int size() const
Definition: collection.h:263
void push_back(const std::shared_ptr< const T > &new_entry)
Definition: collection.h:254
CollectionIterator< T > begin() const
Definition: collection.h:282
#define AssertIndexRange(index, range)
Definition: exceptions.h:1720
const T & operator[](const unsigned int index) const
Definition: collection.h:272
STL namespace.
std::vector< std::shared_ptr< const T > > entries
Definition: collection.h:235
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
const T & operator*() const
Definition: collection.h:91
CollectionIterator< T > & operator--()
Definition: collection.h:128
static ::ExceptionBase & ExcMessage(std::string arg1)
std::size_t memory_consumption() const
Definition: collection.h:245
static const char T
const std::vector< std::shared_ptr< const T > > * data
Definition: collection.h:162
#define Assert(cond, exc)
Definition: exceptions.h:1461
CollectionIterator(const std::vector< std::shared_ptr< const T >> &data, const std::size_t index)
Definition: collection.h:44
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:407
CollectionIterator< T > & operator++()
Definition: collection.h:103
VectorType::value_type * end(VectorType &V)
Definition: hp.h:117
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition: collection.h:152
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:406
VectorType::value_type * begin(VectorType &V)
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition: collection.h:142
bool operator!=(const CollectionIterator< T > &other) const
Definition: collection.h:78
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition: collection.h:115
CollectionIterator< T > end() const
Definition: collection.h:291
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)