// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2006 - 2025 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_matrix_iterator_h #define dealii_matrix_iterator_h #include #include #include DEAL_II_NAMESPACE_OPEN /** * Iterator for constant and non-constant matrices. * * This iterator is abstracted from the actual matrix type and can be used for * any matrix having the required AccessorType type. */ template class MatrixIterator { public: /** * Declare type for container size. */ using size_type = types::global_dof_index; /** * Typedef for the matrix type (including constness) we are to operate on. */ using MatrixType = typename AccessorType::MatrixType; /** * Constructor. Create an iterator into the matrix matrix for the * given row and the index within it. */ MatrixIterator(MatrixType *matrix, const size_type row = 0, const size_type index = 0); /** * Copy from another matrix iterator. Mostly implemented to allow * initialization of a constant iterator from a non constant, this function * only requires that a conversion from the other iterator's AccessorType to * this AccessorType object is possible. */ template MatrixIterator(const MatrixIterator &other); /** * Prefix increment. */ MatrixIterator & operator++(); /** * Postfix increment. */ MatrixIterator operator++(int); /** * Dereferencing operator. */ const AccessorType & operator*() const; /** * Dereferencing operator. */ const AccessorType * operator->() const; /** * Comparison. True, if both accessors are equal. */ template bool operator==(const MatrixIterator &) const; /** * Inverse of ==. */ template bool operator!=(const MatrixIterator &) const; /** * Comparison operator. Result is true if either the first row number is * smaller or if the row numbers are equal and the first index is smaller. * * This function is only valid if both iterators point into the same matrix. */ bool operator<(const MatrixIterator &) const; /** * Comparison operator. Works in the same way as above operator, just the * other way round. */ bool operator>(const MatrixIterator &) const; private: /** * Store an object of the AccessorType class. */ AccessorType accessor; // Allow other iterators access to private data. template friend class MatrixIterator; }; //----------------------------------------------------------------------// template inline MatrixIterator::MatrixIterator(MatrixType *matrix, const size_type r, const size_type i) : accessor(matrix, r, i) {} template template inline MatrixIterator::MatrixIterator( const MatrixIterator &other) : accessor(other.accessor) {} template inline MatrixIterator & MatrixIterator::operator++() { accessor.advance(); return *this; } template inline MatrixIterator MatrixIterator::operator++(int) { const MatrixIterator iter = *this; accessor.advance(); return iter; } template inline const AccessorType & MatrixIterator::operator*() const { return accessor; } template inline const AccessorType * MatrixIterator::operator->() const { return &accessor; } template template inline bool MatrixIterator::operator==( const MatrixIterator &other) const { return (accessor == other.accessor); } template template inline bool MatrixIterator::operator!=( const MatrixIterator &other) const { return !(*this == other); } template inline bool MatrixIterator::operator<(const MatrixIterator &other) const { Assert(&accessor.get_matrix() == &other.accessor.get_matrix(), ExcInternalError()); return (accessor < other.accessor); } template inline bool MatrixIterator::operator>(const MatrixIterator &other) const { return (other < *this); } DEAL_II_NAMESPACE_CLOSE #endif