<div dir="ltr">Sounds like a good idea to me. Maybe we could/should extend the interface then? postfix operator++, the operator--s, logical and stream operators come to my mind.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 16, 2015 at 7:17 PM, Oliver Sander <span dir="ltr"><<a href="mailto:sander@igpm.rwth-aachen.de" target="_blank">sander@igpm.rwth-aachen.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
><br>
>     Move MultiIndex class from StructuredGridFactory to a separate header<br>
><br>
>     The TensorGridFactory needs the same class, so I move it to a new header.<br>
<br>
a smart move.  I have needed (and reimplemented) such a class again and again.<br>
Maybe we should move multiindex.hh into dune-common right away?<br>
--<br>
Oliver<br>
<br>
<br>
><br>
>  dune/grid/utility/CMakeLists.txt           |  1 +<br>
>  dune/grid/utility/Makefile.am              |  1 +<br>
>  dune/grid/utility/multiindex.hh            | 57 ++++++++++++++++++++++++++++++<br>
>  dune/grid/utility/structuredgridfactory.hh | 53 +++------------------------<br>
>  dune/grid/utility/tensorgridfactory.hh     |  2 ++<br>
>  5 files changed, 65 insertions(+), 49 deletions(-)<br>
>  create mode 100644 dune/grid/utility/multiindex.hh<br>
><br>
><br>
><br>
> diff --git a/dune/grid/utility/CMakeLists.txt b/dune/grid/utility/CMakeLists.txt<br>
> index 1c42604..15d2311 100644<br>
> --- a/dune/grid/utility/CMakeLists.txt<br>
> +++ b/dune/grid/utility/CMakeLists.txt<br>
> @@ -8,6 +8,7 @@ set(HEADERS<br>
>    gridtype.hh<br>
>    hierarchicsearch.hh<br>
>    hostgridaccess.hh<br>
> +  multiindex.hh<br>
>    parmetisgridpartitioner.hh<br>
>    persistentcontainer.hh<br>
>    persistentcontainerinterface.hh<br>
> diff --git a/dune/grid/utility/Makefile.am b/dune/grid/utility/Makefile.am<br>
> index e14c152..d81c3e2 100644<br>
> --- a/dune/grid/utility/Makefile.am<br>
> +++ b/dune/grid/utility/Makefile.am<br>
> @@ -10,6 +10,7 @@ gridutility_HEADERS =                               \<br>
>       gridtype.hh                             \<br>
>       hierarchicsearch.hh                     \<br>
>       hostgridaccess.hh                       \<br>
> +     multiindex.hh \<br>
>       parmetisgridpartitioner.hh              \<br>
>       persistentcontainer.hh                  \<br>
>       persistentcontainerinterface.hh         \<br>
> diff --git a/dune/grid/utility/multiindex.hh b/dune/grid/utility/multiindex.hh<br>
> new file mode 100644<br>
> index 0000000..b6b281f<br>
> --- /dev/null<br>
> +++ b/dune/grid/utility/multiindex.hh<br>
> @@ -0,0 +1,57 @@<br>
> +#ifndef DUNE_GRID_UTILITY_MULTIINDEX_HH<br>
> +#define DUNE_GRID_UTILITY_MULTIINDEX_HH<br>
> +<br>
> +/** \file<br>
> + *  \brief Implements a multiindex with arbitrary dimension and fixed index ranges<br>
> + *  This is used by various factory classes.<br>
> + */<br>
> +<br>
> +#include<array><br>
> +<br>
> +namespace Dune<br>
> +{<br>
> + namespace FactoryUtilities<br>
> + {<br>
> +  template<std::size_t dim><br>
> +  class MultiIndex : public std::array<unsigned int,dim><br>
> +  {<br>
> +    // The range of each component<br>
> +    std::array<unsigned int,dim> limits_;<br>
> +<br>
> +  public:<br>
> +    /** \brief Constructor with a given range for each digit */<br>
> +    MultiIndex(const std::array<unsigned int,dim>& limits) : limits_(limits)<br>
> +    {<br>
> +      std::fill(this->begin(), this->end(), 0);<br>
> +    }<br>
> +<br>
> +    /** \brief Increment the MultiIndex */<br>
> +    MultiIndex<dim>& operator++()<br>
> +    {<br>
> +      for (int i=0; i<dim; i++)<br>
> +      {<br>
> +        // Augment digit<br>
> +        (*this)[i]++;<br>
> +<br>
> +        // If there is no carry-over we can stop here<br>
> +        if ((*this)[i]<limits_[i])<br>
> +          break;<br>
> +<br>
> +        (*this)[i] = 0;<br>
> +      }<br>
> +      return *this;<br>
> +    }<br>
> +<br>
> +    /** \brief Compute how many times you can call operator++ before getting to (0,...,0) again */<br>
> +    size_t cycle() const<br>
> +    {<br>
> +      size_t result = 1;<br>
> +      for (int i=0; i<dim; i++)<br>
> +        result *= limits_[i];<br>
> +      return result;<br>
> +    }<br>
> +  };<br>
> + }<br>
> +}<br>
> +<br>
> +#endif<br>
> diff --git a/dune/grid/utility/structuredgridfactory.hh b/dune/grid/utility/structuredgridfactory.hh<br>
> index 8832149..8e2700a 100644<br>
> --- a/dune/grid/utility/structuredgridfactory.hh<br>
> +++ b/dune/grid/utility/structuredgridfactory.hh<br>
> @@ -19,6 +19,7 @@<br>
>  #include <dune/common/shared_ptr.hh><br>
><br>
>  #include <dune/grid/common/gridfactory.hh><br>
> +#include <dune/grid/utility/multiindex.hh><br>
>  #include <dune/grid/yaspgrid.hh><br>
><br>
>  namespace Dune {<br>
> @@ -34,59 +35,13 @@ namespace Dune {<br>
><br>
>      static const int dimworld = GridType::dimensionworld;<br>
><br>
> -    /** \brief dim-dimensional multi-index.  The range for each component can be set individually<br>
> -     */<br>
> -    class MultiIndex<br>
> -      : public array<unsigned int,dim><br>
> -    {<br>
> -<br>
> -      // The range of each component<br>
> -      array<unsigned int,dim> limits_;<br>
> -<br>
> -    public:<br>
> -      /** \brief Constructor with a given range for each digit */<br>
> -      MultiIndex(const array<unsigned int,dim>& limits)<br>
> -        : limits_(limits)<br>
> -      {<br>
> -        std::fill(this->begin(), this->end(), 0);<br>
> -      }<br>
> -<br>
> -      /** \brief Increment the MultiIndex */<br>
> -      MultiIndex& operator++() {<br>
> -<br>
> -        for (int i=0; i<dim; i++) {<br>
> -<br>
> -          // Augment digit<br>
> -          (*this)[i]++;<br>
> -<br>
> -          // If there is no carry-over we can stop here<br>
> -          if ((*this)[i]<limits_[i])<br>
> -            break;<br>
> -<br>
> -          (*this)[i] = 0;<br>
> -<br>
> -        }<br>
> -        return *this;<br>
> -      }<br>
> -<br>
> -      /** \brief Compute how many times you can call operator++ before getting to (0,...,0) again */<br>
> -      size_t cycle() const {<br>
> -        size_t result = 1;<br>
> -        for (int i=0; i<dim; i++)<br>
> -          result *= limits_[i];<br>
> -        return result;<br>
> -      }<br>
> -<br>
> -    };<br>
> -<br>
>      /** \brief Insert a structured set of vertices into the factory */<br>
>      static void insertVertices(GridFactory<GridType>& factory,<br>
>                                 const FieldVector<ctype,dimworld>& lowerLeft,<br>
>                                 const FieldVector<ctype,dimworld>& upperRight,<br>
>                                 const array<unsigned int,dim>& vertices)<br>
>      {<br>
> -<br>
> -      MultiIndex index(vertices);<br>
> +      FactoryUtilities::MultiIndex<dim> index(vertices);<br>
><br>
>        // Compute the total number of vertices to be created<br>
>        int numVertices = index.cycle();<br>
> @@ -166,7 +121,7 @@ namespace Dune {<br>
>                cornersTemplate[i] += unitOffsets[j];<br>
><br>
>          // Insert elements<br>
> -        MultiIndex index(elements);<br>
> +        FactoryUtilities::MultiIndex<dim> index(elements);<br>
><br>
>          // Compute the total number of elementss to be created<br>
>          int numElements = index.cycle();<br>
> @@ -234,7 +189,7 @@ namespace Dune {<br>
><br>
>          // Loop over all "cubes", and split up each cube into dim!<br>
>          // (factorial) simplices<br>
> -        MultiIndex elementsIndex(elements);<br>
> +        FactoryUtilities::MultiIndex<dim> elementsIndex(elements);<br>
>          size_t cycle = elementsIndex.cycle();<br>
><br>
>          for (size_t i=0; i<cycle; ++elementsIndex, i++) {<br>
> diff --git a/dune/grid/utility/tensorgridfactory.hh b/dune/grid/utility/tensorgridfactory.hh<br>
> index 971bc4e..8aa47cf 100644<br>
> --- a/dune/grid/utility/tensorgridfactory.hh<br>
> +++ b/dune/grid/utility/tensorgridfactory.hh<br>
> @@ -19,6 +19,8 @@<br>
>  #include<memory><br>
>  #include<vector><br>
><br>
> +#include<dune/grid/utility/multiindex.hh><br>
> +<br>
>  namespace Dune<br>
>  {<br>
>    // forward declaration of TensorGridFactoryCreator, which is the real factory<br>
><br>
> _______________________________________________<br>
> Dune-Commit mailing list<br>
> <a href="mailto:Dune-Commit@dune-project.org">Dune-Commit@dune-project.org</a><br>
> <a href="http://lists.dune-project.org/mailman/listinfo/dune-commit" target="_blank">http://lists.dune-project.org/mailman/listinfo/dune-commit</a><br>
><br>
<br>
<br>
<br>_______________________________________________<br>
Dune-devel mailing list<br>
<a href="mailto:Dune-devel@dune-project.org">Dune-devel@dune-project.org</a><br>
<a href="http://lists.dune-project.org/mailman/listinfo/dune-devel" target="_blank">http://lists.dune-project.org/mailman/listinfo/dune-devel</a><br>
<br></blockquote></div><br></div>