Reference documentation for deal.II version 9.6.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\}}\)
Loading...
Searching...
No Matches
collection.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2021 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_hp_collection_h
16#define dealii_hp_collection_h
17
18#include <deal.II/base/config.h>
19
22
23#include <iterator>
24#include <memory>
25#include <vector>
26
28
29namespace hp
30{
38 "You are trying to compare iterators into different "
39 "hp::Collection objects.");
40
44 template <typename T>
46 {
47 public:
54 CollectionIterator(const std::vector<std::shared_ptr<const T>> &data,
55 const std::size_t index)
56 : data(&data)
57 , index(index)
58 {}
59
64
69 operator=(const CollectionIterator<T> &other) = default;
70
74 bool
76 {
77 Assert(this->data == other.data, ExcDifferentCollection());
78 return this->index == other.index;
79 }
80
84 bool
86 {
87 Assert(this->data == other.data, ExcDifferentCollection());
88 return this->index != other.index;
89 }
90
94 bool
95 operator<(const CollectionIterator<T> &other) const
96 {
97 Assert(this->data == other.data, ExcDifferentCollection());
98 return this->index < other.index;
99 }
100
104 bool
105 operator<=(const CollectionIterator<T> &other) const
106 {
107 Assert(this->data == other.data, ExcDifferentCollection());
108 return this->index <= other.index;
109 }
110
114 bool
116 {
117 Assert(this->data == other.data, ExcDifferentCollection());
118 return this->index > other.index;
119 }
120
124 bool
126 {
127 Assert(this->data == other.data, ExcDifferentCollection());
128 return this->index >= other.index;
129 }
130
134 const T &
135 operator*() const
136 {
137 AssertIndexRange(index, data->size());
138 return *(*data)[index];
139 }
140
148 {
149 AssertIndexRange(index + 1, data->size() + 1);
150 ++index;
151 return *this;
152 }
153
159 operator+=(const std::size_t offset)
160 {
161 AssertIndexRange(index + offset, data->size() + 1);
162 index += offset;
163 return *this;
164 }
165
173 {
174 Assert(
175 index > 0,
177 "You can't decrement an iterator that is already at the beginning of the range."));
178 --index;
179 return *this;
180 }
181
186 operator+(const std::size_t &offset) const
187 {
188 AssertIndexRange(index + offset, T::size() + 1);
189 return CollectionIterator<T>(*data, index + offset);
190 }
191
195 std::ptrdiff_t
197 {
198 return static_cast<std::ptrdiff_t>(index) -
199 static_cast<ptrdiff_t>(other.index);
200 }
201
202 private:
206 const std::vector<std::shared_ptr<const T>> *data;
207
211 std::size_t index;
212 };
213
223 template <typename T>
224 class Collection : public Subscriptor
225 {
226 public:
231 Collection() = default;
232
236 void
237 push_back(const std::shared_ptr<const T> &new_entry);
238
246 const T &
247 operator[](const unsigned int index) const;
248
252 unsigned int
253 size() const;
254
259 std::size_t
261
267 begin() const;
268
274 end() const;
275
276 private:
280 std::vector<std::shared_ptr<const T>> entries;
281 };
282
283
284 /* --------------- inline functions ------------------- */
285
286
287
288 template <typename T>
289 std::size_t
291 {
292 return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
293 }
294
295
296
297 template <typename T>
298 void
299 Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
300 {
301 entries.push_back(new_entry);
302 }
303
304
305
306 template <typename T>
307 inline unsigned int
309 {
310 return entries.size();
311 }
312
313
314
315 template <typename T>
316 inline const T &
317 Collection<T>::operator[](const unsigned int index) const
318 {
319 AssertIndexRange(index, entries.size());
320 return *entries[index];
321 }
322
323
324
325 template <typename T>
328 {
329 return CollectionIterator<T>(entries, 0);
330 }
331
332
333
334 template <typename T>
337 {
338 return CollectionIterator<T>(entries, entries.size());
339 }
340
341} // namespace hp
342
343
345
346namespace std
347{
351 template <class T>
353 : public iterator_traits<
354 typename std::vector<std::shared_ptr<const T>>::iterator>
355 {};
356} // namespace std
357
358#endif
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition collection.h:196
bool operator<=(const CollectionIterator< T > &other) const
Definition collection.h:105
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition collection.h:186
const T & operator*() const
Definition collection.h:135
CollectionIterator< T > & operator++()
Definition collection.h:147
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition collection.h:159
bool operator<(const CollectionIterator< T > &other) const
Definition collection.h:95
CollectionIterator(const std::vector< std::shared_ptr< const T > > &data, const std::size_t index)
Definition collection.h:54
CollectionIterator(const CollectionIterator< T > &other)=default
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
bool operator==(const CollectionIterator< T > &other) const
Definition collection.h:75
bool operator!=(const CollectionIterator< T > &other) const
Definition collection.h:85
CollectionIterator< T > & operator--()
Definition collection.h:172
bool operator>(const CollectionIterator< T > &other) const
Definition collection.h:115
bool operator>=(const CollectionIterator< T > &other) const
Definition collection.h:125
const std::vector< std::shared_ptr< const T > > * data
Definition collection.h:206
void push_back(const std::shared_ptr< const T > &new_entry)
Definition collection.h:299
std::vector< std::shared_ptr< const T > > entries
Definition collection.h:280
CollectionIterator< T > begin() const
Definition collection.h:327
unsigned int size() const
Definition collection.h:308
std::size_t memory_consumption() const
Definition collection.h:290
Collection()=default
CollectionIterator< T > end() const
Definition collection.h:336
const T & operator[](const unsigned int index) const
Definition collection.h:317
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
static ::ExceptionBase & ExcDifferentCollection()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
Definition exceptions.h:494
static ::ExceptionBase & ExcMessage(std::string arg1)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Definition hp.h:117
STL namespace.