]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add default constructor for DynamicSparsityPatternIterators::Accessor and DynamicSpar... 7610/head
authorDenis Davydov <davydden@gmail.com>
Fri, 18 Jan 2019 11:56:10 +0000 (12:56 +0100)
committerDenis Davydov <davydden@gmail.com>
Fri, 18 Jan 2019 18:00:44 +0000 (19:00 +0100)
doc/news/changes/minor/20190118DenisDavydov [new file with mode: 0644]
include/deal.II/lac/dynamic_sparsity_pattern.h
tests/sparsity/dynamic_sparsity_pattern_iterator_09.cc [new file with mode: 0644]
tests/sparsity/dynamic_sparsity_pattern_iterator_09.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190118DenisDavydov b/doc/news/changes/minor/20190118DenisDavydov
new file mode 100644 (file)
index 0000000..5492238
--- /dev/null
@@ -0,0 +1,5 @@
+New: Add default constructor to DynamicSparsityPatternIterators::Accessor() and
+DynamicSparsityPatternIterators::Iterator() to be able to store such objects
+in STL containers.
+<br>
+(Denis Davydov, 2019/01/18)
index b05f02924167e70739090f706a60e646a5cf988c..2feb05a52d16eeb90f7bd627550bebf48ef3b641 100644 (file)
@@ -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 (file)
index 0000000..285f962
--- /dev/null
@@ -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 <deal.II/lac/dynamic_sparsity_pattern.h>
+
+#include "../tests.h"
+
+
+void
+test()
+{
+  DynamicSparsityPattern sp;
+  // put nothing into the sparsity pattern
+  sp.compress();
+
+  std::vector<std::pair<DynamicSparsityPattern::const_iterator,
+                        DynamicSparsityPattern::const_iterator>>
+    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 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.