Reference documentation for deal.II version GIT relicensing-437-g81ec864850 2024-04-19 07:30: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\}}\)
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{
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
54
59 operator=(const CollectionIterator<T> &other) = default;
60
64 bool
66 {
67 Assert(
68 this->data == other.data,
70 "You are trying to compare iterators into different hp::Collection objects."));
71 return this->index == other.index;
72 }
73
77 bool
79 {
80 Assert(
81 this->data == other.data,
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,
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
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
302namespace std
303{
307 template <class T>
309 : public iterator_traits<
310 typename std::vector<std::shared_ptr<const T>>::iterator>
311 {};
312} // namespace std
313
314#endif
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition collection.h:152
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition collection.h:142
const T & operator*() const
Definition collection.h:91
CollectionIterator< T > & operator++()
Definition collection.h:103
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition collection.h:115
CollectionIterator(const std::vector< std::shared_ptr< const T > > &data, const std::size_t index)
Definition collection.h:44
CollectionIterator(const CollectionIterator< T > &other)=default
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
bool operator==(const CollectionIterator< T > &other) const
Definition collection.h:65
bool operator!=(const CollectionIterator< T > &other) const
Definition collection.h:78
CollectionIterator< T > & operator--()
Definition collection.h:128
const std::vector< std::shared_ptr< const T > > * data
Definition collection.h:162
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:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
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.