Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
linear_index_iterator.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 - 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_linear_index_iterator_h
17 #define dealii_linear_index_iterator_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/exceptions.h>
22 
23 
24 DEAL_II_NAMESPACE_OPEN
140 template <class DerivedIterator, class AccessorType>
142 {
143 public:
147  using iterator_category = std::random_access_iterator_tag;
148 
153  using value_type = AccessorType;
154 
158  using difference_type = std::ptrdiff_t;
159 
163  using reference = const value_type &;
164 
168  using pointer = const value_type *;
169 
173  using size_type = typename value_type::size_type;
174 
178  DerivedIterator &
179  operator=(const DerivedIterator &it);
180 
184  DerivedIterator &
185  operator++();
186 
190  DerivedIterator
191  operator++(int);
192 
196  DerivedIterator &
197  operator--();
198 
202  DerivedIterator
203  operator--(int);
204 
208  DerivedIterator
209  operator+(const difference_type n) const;
210 
214  DerivedIterator
215  operator-(const difference_type n) const;
216 
220  DerivedIterator &
221  operator+=(const difference_type n);
222 
226  DerivedIterator &
227  operator-=(const difference_type n);
228 
236  operator-(const DerivedIterator &p) const;
237 
241  reference operator*() const;
242 
246  pointer operator->() const;
247 
252  bool
253  operator==(const DerivedIterator &) const;
254 
258  bool
259  operator!=(const DerivedIterator &) const;
260 
268  bool
269  operator<=(const DerivedIterator &) const;
270 
278  bool
279  operator>=(const DerivedIterator &) const;
280 
288  bool
289  operator<(const DerivedIterator &) const;
290 
295  bool
296  operator>(const DerivedIterator &) const;
297 
298 protected:
299  /*
300  * The inheriting class should have a default constructor.
301  */
302  LinearIndexIterator() = default; // NOLINT
303 
307  LinearIndexIterator(const AccessorType accessor);
308 
309 protected:
313  AccessorType accessor;
314 };
315 
316 
317 
318 template <class DerivedIterator, class AccessorType>
319 inline DerivedIterator &
321 operator=(const DerivedIterator &it)
322 {
323  accessor.container = it.container;
324  accessor.linear_index = it.linear_index;
325  return static_cast<DerivedIterator &>(*this);
326 }
327 
328 
329 
330 template <class DerivedIterator, class AccessorType>
331 inline DerivedIterator &
333 {
334  return operator+=(1);
335 }
336 
337 
338 
339 template <class DerivedIterator, class AccessorType>
340 inline DerivedIterator
342 {
343  const DerivedIterator copy(this->accessor);
344  operator+=(1);
345  return copy;
346 }
347 
348 
349 
350 template <class DerivedIterator, class AccessorType>
351 inline DerivedIterator &
353 {
354  return operator+=(-1);
355 }
356 
357 
358 
359 template <class DerivedIterator, class AccessorType>
360 inline DerivedIterator
362 {
363  const DerivedIterator copy(this->accessor);
364  operator+=(-1);
365  return copy;
366 }
367 
368 
369 
370 template <class DerivedIterator, class AccessorType>
371 inline DerivedIterator
374 {
375  DerivedIterator copy(this->accessor);
376  copy += n;
377  return copy;
378 }
379 
380 
381 
382 template <class DerivedIterator, class AccessorType>
383 inline DerivedIterator
386 {
387  DerivedIterator copy(this->accessor);
388  copy += -n;
389  return copy;
390 }
391 
392 
393 
394 template <class DerivedIterator, class AccessorType>
395 inline DerivedIterator &
398 {
399  accessor.linear_index += n;
400  return static_cast<DerivedIterator &>(*this);
401 }
402 
403 
404 
405 template <class DerivedIterator, class AccessorType>
406 inline DerivedIterator &
409 {
410  return operator+=(-n);
411 }
412 
413 
414 
415 template <class DerivedIterator, class AccessorType>
416 inline
419  operator-(const DerivedIterator &other) const
420 {
421  Assert(this->accessor.container == other.accessor.container,
422  ExcMessage(
423  "Only iterators pointing to the same container can be compared."));
424  return this->accessor.linear_index - other.accessor.linear_index;
425 }
426 
427 
428 
429 template <class DerivedIterator, class AccessorType>
432 {
433  return accessor;
434 }
435 
436 
437 
438 template <class DerivedIterator, class AccessorType>
441 {
442  return &accessor;
443 }
444 
445 
446 
447 template <class DerivedIterator, class AccessorType>
448 inline bool
450 operator==(const DerivedIterator &other) const
451 {
452  const auto &other_2 = static_cast<decltype(*this) &>(other);
453  return accessor.container == other_2.accessor.container &&
454  accessor.linear_index == other_2.accessor.linear_index;
455 }
456 
457 
458 
459 template <class DerivedIterator, class AccessorType>
460 inline bool
462 operator!=(const DerivedIterator &other) const
463 {
464  return !(*this == other);
465 }
466 
467 
468 
469 template <class DerivedIterator, class AccessorType>
470 inline bool
472 operator<=(const DerivedIterator &other) const
473 {
474  return (*this == other) || (*this < other);
475 }
476 
477 
478 
479 template <class DerivedIterator, class AccessorType>
480 inline bool
482 operator>=(const DerivedIterator &other) const
483 {
484  return !(*this < other);
485 }
486 
487 
488 
489 template <class DerivedIterator, class AccessorType>
490 inline bool
492 operator<(const DerivedIterator &other) const
493 {
494  Assert(this->accessor.container == other.accessor.container,
495  ExcMessage(
496  "Only iterators pointing to the same container can be compared."));
497  return this->accessor.linear_index < other.accessor.linear_index;
498 }
499 
500 
501 
502 template <class DerivedIterator, class AccessorType>
503 inline bool
505 operator>(const DerivedIterator &other) const
506 {
507  return other < static_cast<const DerivedIterator &>(*this);
508 }
509 
510 
511 
512 template <class DerivedIterator, class AccessorType>
514  const AccessorType accessor)
515  : accessor(accessor)
516 {}
517 
518 
519 DEAL_II_NAMESPACE_CLOSE
520 
521 #endif
DerivedIterator & operator+=(const difference_type n)
bool operator<(const DerivedIterator &) const
DerivedIterator operator-(const difference_type n) const
DerivedIterator & operator=(const DerivedIterator &it)
bool operator!=(const DerivedIterator &) const
static ::ExceptionBase & ExcMessage(std::string arg1)
DerivedIterator operator+(const difference_type n) const
bool operator>(const DerivedIterator &) const
bool operator<=(const DerivedIterator &) const
#define Assert(cond, exc)
Definition: exceptions.h:1407
bool operator==(const DerivedIterator &) const
std::random_access_iterator_tag iterator_category
DerivedIterator & operator++()
bool operator>=(const DerivedIterator &) const
DerivedIterator & operator-=(const difference_type n)
DerivedIterator & operator--()
reference operator*() const