Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-2684-gc61376a70f 2025-02-22 15:30:00+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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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
23
24#include <iterator>
25#include <memory>
26#include <vector>
27
29
30namespace hp
31{
39 "You are trying to compare iterators into different "
40 "hp::Collection objects.");
41
45 template <typename T>
47 {
48 public:
55 CollectionIterator(const std::vector<std::shared_ptr<const T>> &data,
56 const std::size_t index)
57 : data(&data)
58 , index(index)
59 {}
60
65
70 operator=(const CollectionIterator<T> &other) = default;
71
75 bool
77 {
78 Assert(this->data == other.data, ExcDifferentCollection());
79 return this->index == other.index;
80 }
81
85 bool
87 {
88 Assert(this->data == other.data, ExcDifferentCollection());
89 return this->index != other.index;
90 }
91
95 bool
96 operator<(const CollectionIterator<T> &other) const
97 {
98 Assert(this->data == other.data, ExcDifferentCollection());
99 return this->index < other.index;
100 }
101
105 bool
106 operator<=(const CollectionIterator<T> &other) const
107 {
108 Assert(this->data == other.data, ExcDifferentCollection());
109 return this->index <= other.index;
110 }
111
115 bool
117 {
118 Assert(this->data == other.data, ExcDifferentCollection());
119 return this->index > other.index;
120 }
121
125 bool
127 {
128 Assert(this->data == other.data, ExcDifferentCollection());
129 return this->index >= other.index;
130 }
131
135 const T &
136 operator*() const
137 {
138 AssertIndexRange(index, data->size());
139 return *(*data)[index];
140 }
141
149 {
150 AssertIndexRange(index + 1, data->size() + 1);
151 ++index;
152 return *this;
153 }
154
160 operator+=(const std::size_t offset)
161 {
162 AssertIndexRange(index + offset, data->size() + 1);
163 index += offset;
164 return *this;
165 }
166
174 {
175 Assert(
176 index > 0,
178 "You can't decrement an iterator that is already at the beginning of the range."));
179 --index;
180 return *this;
181 }
182
187 operator+(const std::size_t &offset) const
188 {
189 AssertIndexRange(index + offset, T::size() + 1);
190 return CollectionIterator<T>(*data, index + offset);
191 }
192
196 std::ptrdiff_t
198 {
199 return static_cast<std::ptrdiff_t>(index) -
200 static_cast<ptrdiff_t>(other.index);
201 }
202
203 private:
207 const std::vector<std::shared_ptr<const T>> *data;
208
212 std::size_t index;
213 };
214
224 template <typename T>
226 {
227 public:
232 Collection() = default;
233
237 void
238 push_back(const std::shared_ptr<const T> &new_entry);
239
247 const T &
248 operator[](const unsigned int index) const;
249
253 unsigned int
254 size() const;
255
260 bool
261 empty() const;
262
267 std::size_t
269
275 begin() const;
276
282 end() const;
283
284 private:
288 std::vector<std::shared_ptr<const T>> entries;
289 };
290
291
292 /* --------------- inline functions ------------------- */
293
294
295
296 template <typename T>
297 std::size_t
299 {
300 return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
301 }
302
303
304
305 template <typename T>
306 void
307 Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
308 {
309 entries.push_back(new_entry);
310 }
311
312
313
314 template <typename T>
315 inline unsigned int
317 {
318 return entries.size();
319 }
320
321
322
323 template <typename T>
324 inline bool
326 {
327 return this->size() == 0;
328 }
329
330
331
332 template <typename T>
333 inline const T &
334 Collection<T>::operator[](const unsigned int index) const
335 {
336 AssertIndexRange(index, entries.size());
337 return *entries[index];
338 }
339
340
341
342 template <typename T>
345 {
346 return CollectionIterator<T>(entries, 0);
347 }
348
349
350
351 template <typename T>
354 {
355 return CollectionIterator<T>(entries, entries.size());
356 }
357
358} // namespace hp
359
360
362
363namespace std
364{
368 template <class T>
370 : public iterator_traits<
371 typename std::vector<std::shared_ptr<const T>>::iterator>
372 {};
373} // namespace std
374
375#endif
std::ptrdiff_t operator-(const CollectionIterator< T > &other) const
Definition collection.h:197
bool operator<=(const CollectionIterator< T > &other) const
Definition collection.h:106
CollectionIterator< T > operator+(const std::size_t &offset) const
Definition collection.h:187
const T & operator*() const
Definition collection.h:136
CollectionIterator< T > & operator++()
Definition collection.h:148
CollectionIterator< T > & operator+=(const std::size_t offset)
Definition collection.h:160
bool operator<(const CollectionIterator< T > &other) const
Definition collection.h:96
CollectionIterator(const std::vector< std::shared_ptr< const T > > &data, const std::size_t index)
Definition collection.h:55
CollectionIterator(const CollectionIterator< T > &other)=default
CollectionIterator< T > & operator=(const CollectionIterator< T > &other)=default
bool operator==(const CollectionIterator< T > &other) const
Definition collection.h:76
bool operator!=(const CollectionIterator< T > &other) const
Definition collection.h:86
CollectionIterator< T > & operator--()
Definition collection.h:173
bool operator>(const CollectionIterator< T > &other) const
Definition collection.h:116
bool operator>=(const CollectionIterator< T > &other) const
Definition collection.h:126
const std::vector< std::shared_ptr< const T > > * data
Definition collection.h:207
void push_back(const std::shared_ptr< const T > &new_entry)
Definition collection.h:307
std::vector< std::shared_ptr< const T > > entries
Definition collection.h:288
CollectionIterator< T > begin() const
Definition collection.h:344
unsigned int size() const
Definition collection.h:316
std::size_t memory_consumption() const
Definition collection.h:298
Collection()=default
bool empty() const
Definition collection.h:325
CollectionIterator< T > end() const
Definition collection.h:353
const T & operator[](const unsigned int index) const
Definition collection.h:334
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:522
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:523
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:739
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Definition hp.h:117
STL namespace.