Reference documentation for deal.II version GIT b8135fa6eb 2023-03-25 15:55: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\}}\)
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 
179  template <typename T>
180  class Collection : public Subscriptor
181  {
182  public:
187  Collection() = default;
188 
192  void
193  push_back(const std::shared_ptr<const T> &new_entry);
194 
202  const T &
203  operator[](const unsigned int index) const;
204 
208  unsigned int
209  size() const;
210 
215  std::size_t
217 
223  begin() const;
224 
230  end() const;
231 
232  private:
236  std::vector<std::shared_ptr<const T>> entries;
237  };
238 
239 
240  /* --------------- inline functions ------------------- */
241 
242 
243 
244  template <typename T>
245  std::size_t
247  {
248  return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
249  }
250 
251 
252 
253  template <typename T>
254  void
255  Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
256  {
257  entries.push_back(new_entry);
258  }
259 
260 
261 
262  template <typename T>
263  inline unsigned int
265  {
266  return entries.size();
267  }
268 
269 
270 
271  template <typename T>
272  inline const T &
273  Collection<T>::operator[](const unsigned int index) const
274  {
275  AssertIndexRange(index, entries.size());
276  return *entries[index];
277  }
278 
279 
280 
281  template <typename T>
284  {
285  return CollectionIterator<T>(entries, 0);
286  }
287 
288 
289 
290  template <typename T>
293  {
294  return CollectionIterator<T>(entries, entries.size());
295  }
296 
297 } // namespace hp
298 
299 
301 
302 namespace std
303 {
307  template <class T>
308  struct iterator_traits<::hp::CollectionIterator<T>>
309  {
310  using iterator_category = random_access_iterator_tag;
311  using value_type = T;
312  using difference_type = std::ptrdiff_t;
313  };
314 } // namespace std
315 
316 #endif
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition: collection.h:152
CollectionIterator< T > & operator++()
Definition: collection.h:103
CollectionIterator< T > & operator--()
Definition: collection.h:128
CollectionIterator(const CollectionIterator< T > &other)=default
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition: collection.h:115
bool operator==(const CollectionIterator< T > &other) const
Definition: collection.h:65
CollectionIterator(const std::vector< std::shared_ptr< const T >> &data, const std::size_t index)
Definition: collection.h:44
bool operator!=(const CollectionIterator< T > &other) const
Definition: collection.h:78
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition: collection.h:142
const std::vector< std::shared_ptr< const T > > * data
Definition: collection.h:162
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
const T & operator*() const
Definition: collection.h:91
void push_back(const std::shared_ptr< const T > &new_entry)
Definition: collection.h:255
std::vector< std::shared_ptr< const T > > entries
Definition: collection.h:236
CollectionIterator< T > begin() const
Definition: collection.h:283
unsigned int size() const
Definition: collection.h:264
std::size_t memory_consumption() const
Definition: collection.h:246
Collection()=default
CollectionIterator< T > end() const
Definition: collection.h:292
const T & operator[](const unsigned int index) const
Definition: collection.h:273
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:474
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:475
#define Assert(cond, exc)
Definition: exceptions.h:1586
#define AssertIndexRange(index, range)
Definition: exceptions.h:1827
static ::ExceptionBase & ExcMessage(std::string arg1)
static const char T
std::enable_if_t< std::is_fundamental< T >::value, std::size_t > memory_consumption(const T &t)
Definition: hp.h:118