]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use iterators in Tensor::unroll_recursion().
authorDavid Wells <drwells@email.unc.edu>
Tue, 24 Aug 2021 18:27:24 +0000 (14:27 -0400)
committerDavid Wells <drwells@email.unc.edu>
Tue, 7 Sep 2021 21:27:33 +0000 (17:27 -0400)
This makes the code applicable for more linear data structures than just Vector.

include/deal.II/base/tensor.h

index d4fd088aa5a37bb54619401cbf0dec26be3ac96c..2f687d5d0bb404ee7d081246e9af38eb7f2573f0 100644 (file)
@@ -407,10 +407,9 @@ private:
   /**
    * Internal helper function for unroll.
    */
-  template <typename OtherNumber>
-  void
-  unroll_recursion(Vector<OtherNumber> &result,
-                   unsigned int &       start_index) const;
+  template <typename Iterator>
+  Iterator
+  unroll_recursion(const Iterator current, const Iterator end) const;
 
   // Allow an arbitrary Tensor to access the underlying values.
   template <int, int, typename>
@@ -846,10 +845,9 @@ private:
   /**
    * Internal helper function for unroll.
    */
-  template <typename OtherNumber>
-  void
-  unroll_recursion(Vector<OtherNumber> &result,
-                   unsigned int &       start_index) const;
+  template <typename Iterator>
+  Iterator
+  unroll_recursion(const Iterator current, const Iterator end) const;
 
   /**
    * This constructor is for internal use. It provides a way
@@ -1213,15 +1211,16 @@ constexpr DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE
 
 
 template <int dim, typename Number>
-template <typename OtherNumber>
-inline void
-Tensor<0, dim, Number>::unroll_recursion(Vector<OtherNumber> &result,
-                                         unsigned int &       index) const
+template <typename Iterator>
+Iterator
+Tensor<0, dim, Number>::unroll_recursion(const Iterator current,
+                                         const Iterator end) const
 {
   Assert(dim != 0,
          ExcMessage("Cannot unroll an object of type Tensor<0,0,Number>"));
-  result[index] = value;
-  ++index;
+  Assert(current != end, ExcMessage("Cannot put value in end iterator"));
+  *current = value;
+  return current + 1;
 }
 
 
@@ -1712,19 +1711,20 @@ Tensor<rank_, dim, Number>::unroll(Vector<OtherNumber> &result) const
   AssertDimension(result.size(),
                   (Utilities::fixed_power<rank_, unsigned int>(dim)));
 
-  unsigned int index = 0;
-  unroll_recursion(result, index);
+  unroll_recursion(result.begin(), result.end());
 }
 
 
 template <int rank_, int dim, typename Number>
-template <typename OtherNumber>
-inline void
-Tensor<rank_, dim, Number>::unroll_recursion(Vector<OtherNumber> &result,
-                                             unsigned int &       index) const
+template <typename Iterator>
+Iterator
+Tensor<rank_, dim, Number>::unroll_recursion(const Iterator current,
+                                             const Iterator end) const
 {
+  auto next = current;
   for (unsigned int i = 0; i < dim; ++i)
-    values[i].unroll_recursion(result, index);
+    next = values[i].unroll_recursion(next, end);
+  return next;
 }
 
 

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.