deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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\}}\)
Loading...
Searching...
No Matches
collection.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2021 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_hp_collection_h
14#define dealii_hp_collection_h
15
16#include <deal.II/base/config.h>
17
21
22#include <iterator>
23#include <memory>
24#include <vector>
25
27
28namespace hp
29{
37 "You are trying to compare iterators into different "
38 "hp::Collection objects.");
39
43 template <typename T>
45 {
46 public:
53 CollectionIterator(const std::vector<std::shared_ptr<const T>> &data,
54 const std::size_t index)
55 : data(&data)
56 , index(index)
57 {}
58
63
68 operator=(const CollectionIterator<T> &other) = default;
69
73 bool
75 {
76 Assert(this->data == other.data, ExcDifferentCollection());
77 return this->index == other.index;
78 }
79
83 bool
85 {
86 Assert(this->data == other.data, ExcDifferentCollection());
87 return this->index != other.index;
88 }
89
93 bool
94 operator<(const CollectionIterator<T> &other) const
95 {
96 Assert(this->data == other.data, ExcDifferentCollection());
97 return this->index < other.index;
98 }
99
103 bool
104 operator<=(const CollectionIterator<T> &other) const
105 {
106 Assert(this->data == other.data, ExcDifferentCollection());
107 return this->index <= other.index;
108 }
109
113 bool
115 {
116 Assert(this->data == other.data, ExcDifferentCollection());
117 return this->index > other.index;
118 }
119
123 bool
125 {
126 Assert(this->data == other.data, ExcDifferentCollection());
127 return this->index >= other.index;
128 }
129
133 const T &
134 operator*() const
135 {
136 AssertIndexRange(index, data->size());
137 return *(*data)[index];
138 }
139
147 {
148 AssertIndexRange(index + 1, data->size() + 1);
149 ++index;
150 return *this;
151 }
152
158 operator+=(const std::size_t offset)
159 {
160 AssertIndexRange(index + offset, data->size() + 1);
161 index += offset;
162 return *this;
163 }
164
172 {
173 Assert(
174 index > 0,
176 "You can't decrement an iterator that is already at the beginning of the range."));
177 --index;
178 return *this;
179 }
180
185 operator+(const std::size_t &offset) const
186 {
187 AssertIndexRange(index + offset, T::size() + 1);
188 return CollectionIterator<T>(*data, index + offset);
189 }
190
194 std::ptrdiff_t
196 {
197 return static_cast<std::ptrdiff_t>(index) -
198 static_cast<std::ptrdiff_t>(other.index);
199 }
200
201 private:
205 const std::vector<std::shared_ptr<const T>> *data;
206
210 std::size_t index;
211 };
212
222 template <typename T>
224 {
225 public:
230 Collection() = default;
231
235 void
236 push_back(const std::shared_ptr<const T> &new_entry);
237
245 const T &
246 operator[](const unsigned int index) const;
247
251 unsigned int
252 size() const;
253
258 bool
259 empty() const;
260
265 std::size_t
267
273 begin() const;
274
280 end() const;
281
282 private:
286 std::vector<std::shared_ptr<const T>> entries;
287 };
288
289
290 /* --------------- inline functions ------------------- */
291
292
293
294 template <typename T>
295 std::size_t
297 {
298 return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
299 }
300
301
302
303 template <typename T>
304 void
305 Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
306 {
307 entries.push_back(new_entry);
308 }
309
310
311
312 template <typename T>
313 inline unsigned int
315 {
316 return entries.size();
317 }
318
319
320
321 template <typename T>
322 inline bool
324 {
325 return this->size() == 0;
326 }
327
328
329
330 template <typename T>
331 inline const T &
332 Collection<T>::operator[](const unsigned int index) const
333 {
334 AssertIndexRange(index, entries.size());
335 return *entries[index];
336 }
337
338
339
340 template <typename T>
343 {
344 return CollectionIterator<T>(entries, 0);
345 }
346
347
348
349 template <typename T>
352 {
353 return CollectionIterator<T>(entries, entries.size());
354 }
355
356} // namespace hp
357
358
360
361namespace std
362{
366 template <class T>
368 : public iterator_traits<
369 typename std::vector<std::shared_ptr<const T>>::iterator>
370 {};
371} // namespace std
372
373#endif
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition collection.h:195
bool operator<=(const CollectionIterator< T > &other) const
Definition collection.h:104
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition collection.h:185
const T & operator*() const
Definition collection.h:134
CollectionIterator< T > & operator++()
Definition collection.h:146
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition collection.h:158
bool operator<(const CollectionIterator< T > &other) const
Definition collection.h:94
CollectionIterator(const std::vector< std::shared_ptr< const T > > &data, const std::size_t index)
Definition collection.h:53
CollectionIterator(const CollectionIterator< T > &other)=default
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
bool operator==(const CollectionIterator< T > &other) const
Definition collection.h:74
bool operator!=(const CollectionIterator< T > &other) const
Definition collection.h:84
CollectionIterator< T > & operator--()
Definition collection.h:171
bool operator>(const CollectionIterator< T > &other) const
Definition collection.h:114
bool operator>=(const CollectionIterator< T > &other) const
Definition collection.h:124
const std::vector< std::shared_ptr< const T > > * data
Definition collection.h:205
void push_back(const std::shared_ptr< const T > &new_entry)
Definition collection.h:305
std::vector< std::shared_ptr< const T > > entries
Definition collection.h:286
CollectionIterator< T > begin() const
Definition collection.h:342
unsigned int size() const
Definition collection.h:314
std::size_t memory_consumption() const
Definition collection.h:296
Collection()=default
bool empty() const
Definition collection.h:323
CollectionIterator< T > end() const
Definition collection.h:351
const T & operator[](const unsigned int index) const
Definition collection.h:332
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcDifferentCollection()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::size_t size
Definition mpi.cc:733
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Definition hp.h:115
STL namespace.