From a97fdc7e10df29d768f0c21a5f17bb9794320817 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Fri, 18 Jan 2019 12:56:10 +0100 Subject: [PATCH] add default constructor for DynamicSparsityPatternIterators::Accessor and DynamicSparsityPatternIterators::Iterator --- doc/news/changes/minor/20190118DenisDavydov | 5 ++ .../deal.II/lac/dynamic_sparsity_pattern.h | 42 ++++++++++++++++ .../dynamic_sparsity_pattern_iterator_09.cc | 50 +++++++++++++++++++ ...ynamic_sparsity_pattern_iterator_09.output | 2 + 4 files changed, 99 insertions(+) create mode 100644 doc/news/changes/minor/20190118DenisDavydov create mode 100644 tests/sparsity/dynamic_sparsity_pattern_iterator_09.cc create mode 100644 tests/sparsity/dynamic_sparsity_pattern_iterator_09.output diff --git a/doc/news/changes/minor/20190118DenisDavydov b/doc/news/changes/minor/20190118DenisDavydov new file mode 100644 index 0000000000..5492238df4 --- /dev/null +++ b/doc/news/changes/minor/20190118DenisDavydov @@ -0,0 +1,5 @@ +New: Add default constructor to DynamicSparsityPatternIterators::Accessor() and +DynamicSparsityPatternIterators::Iterator() to be able to store such objects +in STL containers. +
+(Denis Davydov, 2019/01/18) diff --git a/include/deal.II/lac/dynamic_sparsity_pattern.h b/include/deal.II/lac/dynamic_sparsity_pattern.h index b05f029241..2feb05a52d 100644 --- a/include/deal.II/lac/dynamic_sparsity_pattern.h +++ b/include/deal.II/lac/dynamic_sparsity_pattern.h @@ -77,6 +77,13 @@ namespace DynamicSparsityPatternIterators */ Accessor(const DynamicSparsityPattern *sparsity_pattern); + /** + * Default constructor creating a dummy accessor. This constructor is here + * only to be able to store accessors in STL containers such as + * `std::vector`. + */ + Accessor(); + /** * Row number of the element represented by this object. */ @@ -112,6 +119,12 @@ namespace DynamicSparsityPatternIterators operator<(const Accessor &) const; protected: + DeclExceptionMsg(DummyAccessor, + "The instance of this class was initialized" + " without DynamicSparsityPattern object, which" + " means that it is a dummy accessor that can" + " not do any operations."); + /** * The sparsity pattern we operate on accessed. */ @@ -192,6 +205,13 @@ namespace DynamicSparsityPatternIterators */ Iterator(const DynamicSparsityPattern *sp); + /** + * Default constructor creating an invalid iterator. This constructor is + * here only to be able to store iterators in STL containers such as + * `std::vector`. + */ + Iterator(); + /** * Prefix increment. */ @@ -736,9 +756,18 @@ namespace DynamicSparsityPatternIterators + inline Accessor::Accessor() + : sparsity_pattern(nullptr) + , current_row(numbers::invalid_size_type) + , current_entry() + , end_of_row() + {} + + inline size_type Accessor::row() const { + Assert(sparsity_pattern != nullptr, DummyAccessor()); Assert(current_row < sparsity_pattern->n_rows(), ExcInternalError()); return current_row; @@ -748,6 +777,7 @@ namespace DynamicSparsityPatternIterators inline size_type Accessor::column() const { + Assert(sparsity_pattern != nullptr, DummyAccessor()); Assert(current_row < sparsity_pattern->n_rows(), ExcInternalError()); return *current_entry; @@ -757,6 +787,7 @@ namespace DynamicSparsityPatternIterators inline size_type Accessor::index() const { + Assert(sparsity_pattern != nullptr, DummyAccessor()); Assert(current_row < sparsity_pattern->n_rows(), ExcInternalError()); return (current_entry - @@ -772,6 +803,8 @@ namespace DynamicSparsityPatternIterators inline bool Accessor::operator==(const Accessor &other) const { + Assert(sparsity_pattern != nullptr, DummyAccessor()); + Assert(other.sparsity_pattern != nullptr, DummyAccessor()); // compare the sparsity pattern the iterator points into, the // current row, and the location within this row. ignore the // latter if the row is past-the-end because in that case the @@ -787,6 +820,8 @@ namespace DynamicSparsityPatternIterators inline bool Accessor::operator<(const Accessor &other) const { + Assert(sparsity_pattern != nullptr, DummyAccessor()); + Assert(other.sparsity_pattern != nullptr, DummyAccessor()); Assert(sparsity_pattern == other.sparsity_pattern, ExcInternalError()); // if *this is past-the-end, then it is less than no one @@ -811,6 +846,7 @@ namespace DynamicSparsityPatternIterators inline void Accessor::advance() { + Assert(sparsity_pattern != nullptr, DummyAccessor()); Assert(current_row < sparsity_pattern->n_rows(), ExcInternalError()); // move to the next element in this row @@ -850,6 +886,12 @@ namespace DynamicSparsityPatternIterators + inline Iterator::Iterator() + : accessor() + {} + + + inline Iterator & Iterator::operator++() { diff --git a/tests/sparsity/dynamic_sparsity_pattern_iterator_09.cc b/tests/sparsity/dynamic_sparsity_pattern_iterator_09.cc new file mode 100644 index 0000000000..285f962bd8 --- /dev/null +++ b/tests/sparsity/dynamic_sparsity_pattern_iterator_09.cc @@ -0,0 +1,50 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// test that DynamicSparsityPattern::iterator can be used +// inside STL containers (i.e. check default constructor) + +#include + +#include "../tests.h" + + +void +test() +{ + DynamicSparsityPattern sp; + // put nothing into the sparsity pattern + sp.compress(); + + std::vector> + iterators(1); + iterators[0].first = sp.begin(); + iterators[0].second = sp.end(); + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + + test(); +} diff --git a/tests/sparsity/dynamic_sparsity_pattern_iterator_09.output b/tests/sparsity/dynamic_sparsity_pattern_iterator_09.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/sparsity/dynamic_sparsity_pattern_iterator_09.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5