| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2006 - 2025 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_matrix_iterator_h |
| 16 |
#define dealii_matrix_iterator_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/exceptions.h> |
| 22 |
#include <deal.II/base/types.h> |
| 23 |
|
| 24 |
DEAL_II_NAMESPACE_OPEN |
| 25 |
|
| 26 |
/** |
| 27 |
* Iterator for constant and non-constant matrices. |
| 28 |
* |
| 29 |
* This iterator is abstracted from the actual matrix type and can be used for |
| 30 |
* any matrix having the required AccessorType type. |
| 31 |
*/ |
| 32 |
template <typename AccessorType> |
| 33 |
class MatrixIterator |
| 34 |
{ |
| 35 |
public: |
| 36 |
/** |
| 37 |
* Declare type for container size. |
| 38 |
*/ |
| 39 |
using size_type = types::global_dof_index; |
| 40 |
|
| 41 |
/** |
| 42 |
* Typedef for the matrix type (including constness) we are to operate on. |
| 43 |
*/ |
| 44 |
using MatrixType = typename AccessorType::MatrixType; |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor. Create an iterator into the matrix <tt>matrix</tt> for the |
| 48 |
* given <tt>row</tt> and the <tt>index</tt> within it. |
| 49 |
*/ |
| 50 |
MatrixIterator(MatrixType *matrix, |
| 51 |
const size_type row = 0, |
| 52 |
const size_type index = 0); |
| 53 |
|
| 54 |
/** |
| 55 |
* Copy from another matrix iterator. Mostly implemented to allow |
| 56 |
* initialization of a constant iterator from a non constant, this function |
| 57 |
* only requires that a conversion from the other iterator's AccessorType to |
| 58 |
* this AccessorType object is possible. |
| 59 |
*/ |
| 60 |
template <class OtherAccessorType> |
| 61 |
MatrixIterator(const MatrixIterator<OtherAccessorType> &other); |
| 62 |
|
| 63 |
/** |
| 64 |
* Prefix increment. |
| 65 |
*/ |
| 66 |
MatrixIterator & |
| 67 |
operator++(); |
| 68 |
|
| 69 |
/** |
| 70 |
* Postfix increment. |
| 71 |
*/ |
| 72 |
MatrixIterator |
| 73 |
operator++(int); |
| 74 |
|
| 75 |
/** |
| 76 |
* Dereferencing operator. |
| 77 |
*/ |
| 78 |
const AccessorType & |
| 79 |
operator*() const; |
| 80 |
|
| 81 |
/** |
| 82 |
* Dereferencing operator. |
| 83 |
*/ |
| 84 |
const AccessorType * |
| 85 |
operator->() const; |
| 86 |
|
| 87 |
/** |
| 88 |
* Comparison. True, if both accessors are equal. |
| 89 |
*/ |
| 90 |
template <class OtherAccessorType> |
| 91 |
bool |
| 92 |
operator==(const MatrixIterator<OtherAccessorType> &) const; |
| 93 |
|
| 94 |
/** |
| 95 |
* Inverse of <tt>==</tt>. |
| 96 |
*/ |
| 97 |
template <class OtherAccessorType> |
| 98 |
bool |
| 99 |
operator!=(const MatrixIterator<OtherAccessorType> &) const; |
| 100 |
|
| 101 |
/** |
| 102 |
* Comparison operator. Result is true if either the first row number is |
| 103 |
* smaller or if the row numbers are equal and the first index is smaller. |
| 104 |
* |
| 105 |
* This function is only valid if both iterators point into the same matrix. |
| 106 |
*/ |
| 107 |
bool |
| 108 |
operator<(const MatrixIterator &) const; |
| 109 |
|
| 110 |
/** |
| 111 |
* Comparison operator. Works in the same way as above operator, just the |
| 112 |
* other way round. |
| 113 |
*/ |
| 114 |
bool |
| 115 |
operator>(const MatrixIterator &) const; |
| 116 |
|
| 117 |
private: |
| 118 |
/** |
| 119 |
* Store an object of the AccessorType class. |
| 120 |
*/ |
| 121 |
AccessorType accessor; |
| 122 |
|
| 123 |
// Allow other iterators access to private data. |
| 124 |
template <class OtherAccessorType> |
| 125 |
friend class MatrixIterator; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
//----------------------------------------------------------------------// |
| 130 |
|
| 131 |
template <typename AccessorType> |
| 132 |
inline MatrixIterator<AccessorType>::MatrixIterator(MatrixType *matrix, |
| 133 |
const size_type r, |
| 134 |
const size_type i) |
| 135 |
: accessor(matrix, r, i) |
| 136 |
{} |
| 137 |
|
| 138 |
|
| 139 |
template <typename AccessorType> |
| 140 |
template <class OtherAccessorType> |
| 141 |
inline MatrixIterator<AccessorType>::MatrixIterator( |
| 142 |
const MatrixIterator<OtherAccessorType> &other) |
| 143 |
: accessor(other.accessor) |
| 144 |
{} |
| 145 |
|
| 146 |
|
| 147 |
template <typename AccessorType> |
| 148 |
inline MatrixIterator<AccessorType> & |
| 149 |
MatrixIterator<AccessorType>::operator++() |
| 150 |
{ |
| 151 |
accessor.advance(); |
| 152 |
return *this; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
template <typename AccessorType> |
| 157 |
inline MatrixIterator<AccessorType> |
| 158 |
MatrixIterator<AccessorType>::operator++(int) |
| 159 |
{ |
| 160 |
const MatrixIterator iter = *this; |
| 161 |
accessor.advance(); |
| 162 |
return iter; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
template <typename AccessorType> |
| 167 |
inline const AccessorType & |
| 168 |
MatrixIterator<AccessorType>::operator*() const |
| 169 |
{ |
| 170 |
return accessor; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
template <typename AccessorType> |
| 175 |
inline const AccessorType * |
| 176 |
MatrixIterator<AccessorType>::operator->() const |
| 177 |
{ |
| 178 |
return &accessor; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
template <typename AccessorType> |
| 183 |
template <class OtherAccessorType> |
| 184 |
inline bool |
| 185 |
MatrixIterator<AccessorType>::operator==( |
| 186 |
const MatrixIterator<OtherAccessorType> &other) const |
| 187 |
{ |
| 188 |
return (accessor == other.accessor); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
template <typename AccessorType> |
| 193 |
template <class OtherAccessorType> |
| 194 |
inline bool |
| 195 |
MatrixIterator<AccessorType>::operator!=( |
| 196 |
const MatrixIterator<OtherAccessorType> &other) const |
| 197 |
{ |
| 198 |
return !(*this == other); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
template <typename AccessorType> |
| 203 |
inline bool |
| 204 |
MatrixIterator<AccessorType>::operator<(const MatrixIterator &other) const |
| 205 |
{ |
| 206 |
Assert(&accessor.get_matrix() == &other.accessor.get_matrix(), |
| 207 |
ExcInternalError()); |
| 208 |
|
| 209 |
return (accessor < other.accessor); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
template <typename AccessorType> |
| 214 |
inline bool |
| 215 |
MatrixIterator<AccessorType>::operator>(const MatrixIterator &other) const |
| 216 |
{ |
| 217 |
return (other < *this); |
| 218 |
} |
| 219 |
|
| 220 |
DEAL_II_NAMESPACE_CLOSE |
| 221 |
|
| 222 |
#endif |