]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Simplify a function that returns an array.
authorWolfgang Bangerth <bangerth@colostate.edu>
Wed, 9 May 2018 01:46:19 +0000 (09:46 +0800)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 9 May 2018 01:46:40 +0000 (09:46 +0800)
Previously, it used a reference to an array as an output argument. Now, we can just
return a std::array<...,dim>.

include/deal.II/base/polynomial_space.h
include/deal.II/base/polynomials_p.h
source/base/polynomial_space.cc
source/fe/fe_dgp_monomial.cc

index 929470015b55d935be2c783c06dbeb05d2e955c5..47842506201ca76692cfdd59fa4736c806c38d7c 100644 (file)
@@ -213,12 +213,14 @@ protected:
 
   /**
    * Compute numbers in x, y and z direction. Given an index <tt>n</tt> in the
-   * d-dimensional polynomial space, compute the indices i,j,k such that
+   * d-dimensional polynomial space, return the indices i,j,k such that
    * <i>p<sub>n</sub>(x,y,z) =
    * p<sub>i</sub>(x)p<sub>j</sub>(y)p<sub>k</sub>(z)</i>.
+   *
+   * In 1d and 2d, obviously only i and i,j are returned.
    */
-  void compute_index (const unsigned int n,
-                      unsigned int      (&index)[dim>0?dim:1]) const;
+  std::array<unsigned int,dim>
+  compute_index (const unsigned int n) const;
 
 private:
   /**
@@ -246,14 +248,11 @@ private:
 /* -------------- declaration of explicit specializations --- */
 
 template <>
-void PolynomialSpace<1>::compute_index(const unsigned int n,
-                                       unsigned int      (&index)[1]) const;
+std::array<unsigned int,1> PolynomialSpace<1>::compute_index(const unsigned int n) const;
 template <>
-void PolynomialSpace<2>::compute_index(const unsigned int n,
-                                       unsigned int      (&index)[2]) const;
+std::array<unsigned int,2> PolynomialSpace<2>::compute_index(const unsigned int n) const;
 template <>
-void PolynomialSpace<3>::compute_index(const unsigned int n,
-                                       unsigned int      (&index)[3]) const;
+std::array<unsigned int,3> PolynomialSpace<3>::compute_index(const unsigned int n) const;
 
 
 
@@ -304,10 +303,9 @@ template <class StreamType>
 void
 PolynomialSpace<dim>::output_indices(StreamType &out) const
 {
-  unsigned int ix[dim];
   for (unsigned int i=0; i<n_pols; ++i)
     {
-      compute_index(i,ix);
+      const std::array<unsigned int,dim> ix = compute_index(i);
       out << i << "\t";
       for (unsigned int d=0; d<dim; ++d)
         out << ix[d] << " ";
@@ -321,8 +319,7 @@ Tensor<order,dim>
 PolynomialSpace<dim>::compute_derivative (const unsigned int i,
                                           const Point<dim> &p) const
 {
-  unsigned int indices[dim];
-  compute_index (i, indices);
+  const std::array<unsigned int,dim> indices = compute_index (i);
 
   double v [dim][order+1];
   {
index c89b0f47da163451c3ed729eb1833a68a604ca92..a1750700d29028078bb83818d434fdfd07fcb564 100644 (file)
@@ -71,9 +71,11 @@ public:
   /**
    * For the <tt>n</tt>th polynomial $p_n(x,y,z)=x^i y^j z^k$ this function
    * gives the degrees i,j,k in the x,y,z directions.
+   *
+   * In 1d and 2d, obviously only i and i,j are returned.
    */
-  void directional_degrees(unsigned int n,
-                           unsigned int (&degrees)[dim]) const;
+  std::array<unsigned int,dim>
+  directional_degrees(unsigned int n) const;
 
 private:
 
@@ -100,11 +102,11 @@ PolynomialsP<dim>::degree() const
 
 
 template <int dim>
-inline void
-PolynomialsP<dim>::directional_degrees(unsigned int n,
-                                       unsigned int (&degrees)[dim]) const
+inline
+std::array<unsigned int,dim>
+PolynomialsP<dim>::directional_degrees(unsigned int n) const
 {
-  this->compute_index(n,degrees);
+  return this->compute_index(n);
 }
 
 DEAL_II_NAMESPACE_CLOSE
index 664d29ef7978ba823626faddd7a1b92cb4c78fc8..ddb9ef9835a2921b440d98d3506bac34cad99c05 100644 (file)
@@ -43,24 +43,21 @@ PolynomialSpace<0>::compute_n_pols (const unsigned int)
 
 
 template <>
-void
+std::array<unsigned int,1>
 PolynomialSpace<1>::
-compute_index (const unsigned int i,
-               unsigned int      (&index)[1]) const
+compute_index (const unsigned int i) const
 {
   Assert(i<index_map.size(),
          ExcIndexRange(i,0,index_map.size()));
-  const unsigned int n=index_map[i];
-  index[0] = n;
+  return {index_map[i]};
 }
 
 
 
 template <>
-void
+std::array<unsigned int,2>
 PolynomialSpace<2>::
-compute_index (const unsigned int i,
-               unsigned int      (&index)[2]) const
+compute_index (const unsigned int i) const
 {
   Assert(i<index_map.size(),
          ExcIndexRange(i,0,index_map.size()));
@@ -73,22 +70,20 @@ compute_index (const unsigned int i,
   unsigned int k=0;
   for (unsigned int iy=0; iy<n_1d; ++iy)
     if (n < k+n_1d-iy)
-      {
-        index[0] = n-k;
-        index[1] = iy;
-        return;
-      }
+      return {n-k, iy};
     else
       k+=n_1d-iy;
+
+  Assert (false, ExcInternalError());
+  return {numbers::invalid_unsigned_int, numbers::invalid_unsigned_int};
 }
 
 
 
 template <>
-void
+std::array<unsigned int,3>
 PolynomialSpace<3>::
-compute_index (const unsigned int i,
-               unsigned int      (&index)[3]) const
+compute_index (const unsigned int i) const
 {
   Assert(i<index_map.size(),
          ExcIndexRange(i,0,index_map.size()));
@@ -105,14 +100,12 @@ compute_index (const unsigned int i,
   for (unsigned int iz=0; iz<n_1d; ++iz)
     for (unsigned int iy=0; iy<n_1d-iz; ++iy)
       if (n < k+n_1d-iy-iz)
-        {
-          index[0] = n-k;
-          index[1] = iy;
-          index[2] = iz;
-          return;
-        }
+        return {n-k, iy, iz};
       else
         k += n_1d-iy-iz;
+
+  Assert (false, ExcInternalError());
+  return {numbers::invalid_unsigned_int, numbers::invalid_unsigned_int};
 }
 
 
@@ -136,8 +129,7 @@ double
 PolynomialSpace<dim>::compute_value (const unsigned int i,
                                      const Point<dim>  &p) const
 {
-  unsigned int ix[dim];
-  compute_index(i,ix);
+  const auto ix = compute_index(i);
   // take the product of the
   // polynomials in the various space
   // directions
@@ -154,8 +146,7 @@ Tensor<1,dim>
 PolynomialSpace<dim>::compute_grad (const unsigned int i,
                                     const Point<dim>  &p) const
 {
-  unsigned int ix[dim];
-  compute_index(i,ix);
+  const auto ix = compute_index(i);
 
   Tensor<1,dim> result;
   for (unsigned int d=0; d<dim; ++d)
@@ -180,8 +171,7 @@ Tensor<2,dim>
 PolynomialSpace<dim>::compute_grad_grad (const unsigned int i,
                                          const Point<dim>  &p) const
 {
-  unsigned int ix[dim];
-  compute_index(i,ix);
+  const auto ix = compute_index(i);
 
   Tensor<2,dim> result;
   for (unsigned int d=0; d<dim; ++d)
index 94c642ff4e446c9ccd434d4a015e0a94b062a275..1a7745ca3057ea645b8dd49b4e857884d7951291 100644 (file)
@@ -415,8 +415,9 @@ FE_DGPMonomial<2>::has_support_on_face (const unsigned int shape_index,
     support_on_face=true;
   else
     {
-      unsigned int degrees[2];
-      this->poly_space.directional_degrees(shape_index, degrees);
+      const std::array<unsigned int,2> degrees
+        = this->poly_space.directional_degrees(shape_index);
+
       if ((face_index==0 && degrees[1]==0) ||
           (face_index==3 && degrees[0]==0))
         support_on_face=true;
@@ -436,8 +437,9 @@ FE_DGPMonomial<3>::has_support_on_face (const unsigned int shape_index,
     support_on_face=true;
   else
     {
-      unsigned int degrees[3];
-      this->poly_space.directional_degrees(shape_index, degrees);
+      const std::array<unsigned int,3> degrees
+        = this->poly_space.directional_degrees(shape_index);
+
       if ((face_index==0 && degrees[1]==0) ||
           (face_index==2 && degrees[2]==0) ||
           (face_index==5 && degrees[0]==0))

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.