deal.II version GIT relicensing-2165-gc91f007519 2024-11-20 01:40: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\}}\)
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>
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 bool
260 empty() const;
261
266 std::size_t
268
274 begin() const;
275
281 end() const;
282
283 private:
287 std::vector<std::shared_ptr<const T>> entries;
288 };
289
290
291 /* --------------- inline functions ------------------- */
292
293
294
295 template <typename T>
296 std::size_t
298 {
299 return (sizeof(*this) + MemoryConsumption::memory_consumption(entries));
300 }
301
302
303
304 template <typename T>
305 void
306 Collection<T>::push_back(const std::shared_ptr<const T> &new_entry)
307 {
308 entries.push_back(new_entry);
309 }
310
311
312
313 template <typename T>
314 inline unsigned int
316 {
317 return entries.size();
318 }
319
320
321
322 template <typename T>
323 inline bool
325 {
326 return this->size() == 0;
327 }
328
329
330
331 template <typename T>
332 inline const T &
333 Collection<T>::operator[](const unsigned int index) const
334 {
335 AssertIndexRange(index, entries.size());
336 return *entries[index];
337 }
338
339
340
341 template <typename T>
344 {
345 return CollectionIterator<T>(entries, 0);
346 }
347
348
349
350 template <typename T>
353 {
354 return CollectionIterator<T>(entries, entries.size());
355 }
356
357} // namespace hp
358
359
361
362namespace std
363{
367 template <class T>
369 : public iterator_traits<
370 typename std::vector<std::shared_ptr<const T>>::iterator>
371 {};
372} // namespace std
373
374#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:306
std::vector< std::shared_ptr< const T > > entries
Definition collection.h:287
CollectionIterator< T > begin() const
Definition collection.h:343
unsigned int size() const
Definition collection.h:315
std::size_t memory_consumption() const
Definition collection.h:297
Collection()=default
bool empty() const
Definition collection.h:324
CollectionIterator< T > end() const
Definition collection.h:352
const T & operator[](const unsigned int index) const
Definition collection.h:333
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
static ::ExceptionBase & ExcDifferentCollection()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
Definition exceptions.h:489
static ::ExceptionBase & ExcMessage(std::string arg1)
std::size_t size
Definition mpi.cc:734
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Definition hp.h:117
STL namespace.