Reference documentation for deal.II version GIT f6a5d312c9 2023-10-04 08:50: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 - 2023 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 <iterator>
25 #include <memory>
26 #include <vector>
27 
29 
30 namespace hp
31 {
35  template <typename T>
37  {
38  public:
45  CollectionIterator(const std::vector<std::shared_ptr<const T>> &data,
46  const std::size_t index)
47  : data(&data)
48  , index(index)
49  {}
50 
54  CollectionIterator(const CollectionIterator<T> &other) = default;
55 
60  operator=(const CollectionIterator<T> &other) = default;
61 
65  bool
66  operator==(const CollectionIterator<T> &other) const
67  {
68  Assert(
69  this->data == other.data,
70  ExcMessage(
71  "You are trying to compare iterators into different hp::Collection objects."));
72  return this->index == other.index;
73  }
74 
78  bool
79  operator!=(const CollectionIterator<T> &other) const
80  {
81  Assert(
82  this->data == other.data,
83  ExcMessage(
84  "You are trying to compare iterators into different hp::Collection objects."));
85  return this->index != other.index;
86  }
87 
91  const T &
92  operator*() const
93  {
94  AssertIndexRange(index, data->size());
95  return *(*data)[index];
96  }
97 
105  {
106  AssertIndexRange(index + 1, data->size() + 1);
107  index++;
108  return *this;
109  }
110 
116  operator+=(const std::size_t offset)
117  {
118  AssertIndexRange(index + offset, data->size() + 1);
119  index += offset;
120  return *this;
121  }
122 
130  {
131  Assert(
132  index > 0,
133  ExcMessage(
134  "You can't decrement an iterator that is already at the beginning of the range."));
135  --index;
136  return *this;
137  }
138 
143  operator+(const std::size_t &offset) const
144  {
145  AssertIndexRange(index + offset, T::size() + 1);
146  return CollectionIterator<T>(*data, index + offset);
147  }
148 
152  std::ptrdiff_t
153  operator-(const CollectionIterator<T> &other) const
154  {
155  return static_cast<std::ptrdiff_t>(index) -
156  static_cast<ptrdiff_t>(other.index);
157  }
158 
159  private:
163  const std::vector<std::shared_ptr<const T>> *data;
164 
168  std::size_t index;
169  };
170 
180  template <typename T>
181  class Collection : public Subscriptor
182  {
183  public:
188  Collection() = default;
189 
193  void
194  push_back(const std::shared_ptr<const T> &new_entry);
195 
203  const T &
204  operator[](const unsigned int index) const;
205 
209  unsigned int
210  size() const;
211 
216  std::size_t
218 
224  begin() const;
225 
231  end() const;
232 
233  private:
237  std::vector<std::shared_ptr<const T>> entries;
238  };
239 
240 
241  /* --------------- inline functions ------------------- */
242 
243 
244 
245  template <typename T>
246  std::size_t
248  {
249  return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
250  }
251 
252 
253 
254  template <typename T>
255  void
256  Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
257  {
258  entries.push_back(new_entry);
259  }
260 
261 
262 
263  template <typename T>
264  inline unsigned int
266  {
267  return entries.size();
268  }
269 
270 
271 
272  template <typename T>
273  inline const T &
274  Collection<T>::operator[](const unsigned int index) const
275  {
276  AssertIndexRange(index, entries.size());
277  return *entries[index];
278  }
279 
280 
281 
282  template <typename T>
285  {
286  return CollectionIterator<T>(entries, 0);
287  }
288 
289 
290 
291  template <typename T>
294  {
295  return CollectionIterator<T>(entries, entries.size());
296  }
297 
298 } // namespace hp
299 
300 
302 
303 namespace std
304 {
308  template <class T>
310  : public iterator_traits<
311  typename std::vector<std::shared_ptr<const T>>::iterator>
312  {};
313 } // namespace std
314 
315 #endif
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition: collection.h:153
CollectionIterator< T > & operator++()
Definition: collection.h:104
CollectionIterator< T > & operator--()
Definition: collection.h:129
CollectionIterator(const CollectionIterator< T > &other)=default
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition: collection.h:116
bool operator==(const CollectionIterator< T > &other) const
Definition: collection.h:66
CollectionIterator(const std::vector< std::shared_ptr< const T >> &data, const std::size_t index)
Definition: collection.h:45
bool operator!=(const CollectionIterator< T > &other) const
Definition: collection.h:79
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition: collection.h:143
const std::vector< std::shared_ptr< const T > > * data
Definition: collection.h:163
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
const T & operator*() const
Definition: collection.h:92
void push_back(const std::shared_ptr< const T > &new_entry)
Definition: collection.h:256
std::vector< std::shared_ptr< const T > > entries
Definition: collection.h:237
CollectionIterator< T > begin() const
Definition: collection.h:284
unsigned int size() const
Definition: collection.h:265
std::size_t memory_consumption() const
Definition: collection.h:247
Collection()=default
CollectionIterator< T > end() const
Definition: collection.h:293
const T & operator[](const unsigned int index) const
Definition: collection.h:274
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
#define Assert(cond, exc)
Definition: exceptions.h:1616
#define AssertIndexRange(index, range)
Definition: exceptions.h:1857
static ::ExceptionBase & ExcMessage(std::string arg1)
static const char T
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Definition: hp.h:118