]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use generic container for write_dataset
authorDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Sun, 26 Aug 2018 14:07:28 +0000 (16:07 +0200)
committerDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Wed, 9 Jan 2019 18:08:22 +0000 (19:08 +0100)
include/deal.II/base/hdf5.h
source/base/hdf5.cc
source/base/hdf5.inst.in

index 484065eb5c695632f585ecf26f8a91eff1656a1f..6a36845cfb7d0e8a67da6818df193c6e8f22e69c 100644 (file)
@@ -651,24 +651,9 @@ namespace HDF5
      * Transfer: Datatype Conversion and Selection</a>  section in the HDF5
      * User's Guide.
      */
-    template <typename number>
-    void
-    write_dataset(const std::string          name,
-                  const std::vector<number> &data) const;
-
-    /**
-     * Creates and writes data to a dataset. Number can be double, int, unsigned
-     * int, bool or std::complex<double>.
-     *
-     * Datatype conversion takes place at the time of a read or write and is
-     * automatic. See the <a
-     * href="https://support.hdfgroup.org/HDF5/doc/UG/HDF5_Users_Guide-Responsive%20HTML5/index.html#t=HDF5_Users_Guide%2FDatatypes%2FHDF5_Datatypes.htm%23TOC_6_10_Data_Transferbc-26&rhtocid=6.5_2">Data
-     * Transfer: Datatype Conversion and Selection</a>  section in the HDF5
-     * User's Guide.
-     */
-    template <typename number>
+    template <template <class...> class Container, typename number>
     void
-    write_dataset(const std::string name, const FullMatrix<number> &data) const;
+    write_dataset(const std::string name, const Container<number> &data) const;
   };
 
   /**
index e10b0771c7b79aced18bc1cb756576da06eb8a86..b7774d0974ba7bbbb52f8b2506200073c7cb90b9 100644 (file)
@@ -100,6 +100,35 @@ namespace HDF5
       return t_type;
     }
 
+    // This function returns the pointer to the raw data of a container
+    template <template <class...> class Container, typename number>
+    typename std::enable_if<
+      std::is_same<Container<number>, std::vector<number>>::value,
+      std::vector<hsize_t>>::type
+    get_container_dimensions(const Container<number> &data)
+    {
+      return std::vector<hsize_t>{data.size()};
+    }
+
+    template <template <class...> class Container, typename number>
+    typename std::enable_if<
+      std::is_same<Container<number>, Vector<number>>::value,
+      std::vector<hsize_t>>::type
+    get_container_dimensions(const Container<number> &data)
+    {
+      return std::vector<hsize_t>{data.size()};
+    }
+
+    template <template <class...> class Container, typename number>
+    typename std::enable_if<
+      std::is_same<Container<number>, FullMatrix<number>>::value,
+      std::vector<hsize_t>>::type
+    get_container_dimensions(const Container<number> &data)
+    {
+      return std::vector<hsize_t>{data.m(), data.n()};
+    }
+
+
     // This function returns the pointer to the raw data of a container
     template <template <class...> class Container, typename number>
     typename std::enable_if<
@@ -107,8 +136,6 @@ namespace HDF5
       unsigned int>::type
     get_container_size(const Container<number> &data)
     {
-      // It is very important to pass the variable "data" by reference otherwise
-      // the pointer will be wrong
       return static_cast<unsigned int>(data.size());
     }
 
@@ -118,8 +145,6 @@ namespace HDF5
       unsigned int>::type
     get_container_size(const Container<number> &data)
     {
-      // It is very important to pass the variable "data" by reference otherwise
-      // the pointer will be wrong
       return static_cast<unsigned int>(data.size());
     }
 
@@ -129,9 +154,6 @@ namespace HDF5
       unsigned int>::type
     get_container_size(const Container<number> &data)
     {
-      // It is very important to pass the variable "data" by reference otherwise
-      // the pointer will be wrong.
-      // Use the first element of FullMatrix to get the pointer to the raw data
       return static_cast<unsigned int>(data.m() * data.n());
     }
 
@@ -1264,27 +1286,16 @@ namespace HDF5
     return DataSet(name, *hdf5_reference, dimensions, t_type, mpi);
   }
 
-  template <typename number>
-  void
-  Group::write_dataset(const std::string          name,
-                       const std::vector<number> &data) const
-  {
-    std::vector<hsize_t> dimensions = {data.size()};
-    auto                 dataset    = create_dataset<number>(name, dimensions);
-    dataset.write(data);
-  }
-
-  template <typename number>
+  template <template <class...> class Container, typename number>
   void
-  Group::write_dataset(const std::string         name,
-                       const FullMatrix<number> &data) const
+  Group::write_dataset(const std::string        name,
+                       const Container<number> &data) const
   {
-    std::vector<hsize_t> dimensions = {data.m(), data.n()};
+    std::vector<hsize_t> dimensions = internal::get_container_dimensions(data);
     auto                 dataset    = create_dataset<number>(name, dimensions);
     dataset.write(data);
   }
 
-
   File::File(const std::string name,
              const bool        mpi,
              const MPI_Comm    mpi_communicator,
@@ -1449,11 +1460,12 @@ namespace HDF5
     const std::vector<hsize_t> dimensions) const;
 
   template void
-  Group::write_dataset(const std::string       name,
-                       const std::vector<int> &data) const;
+  Group::write_dataset<std::vector, int>(const std::string       name,
+                                         const std::vector<int> &data) const;
   template void
-  Group::write_dataset(const std::string                name,
-                       const std::vector<unsigned int> &data) const;
+  Group::write_dataset<std::vector, unsigned int>(
+    const std::string                name,
+    const std::vector<unsigned int> &data) const;
 
 #  endif // DOXYGEN
 
index 9b1830ebfcf450d964f364a705b8120891c53ec3..481b82391f9b3ea10be37fc291d28da130db487e 100644 (file)
@@ -85,9 +85,12 @@ for (number : REAL_AND_COMPLEX_SCALARS)
     template DataSet Group::create_dataset<number>(
       const std::string name, const std::vector<hsize_t> dimensions) const;
 
-    template void Group::write_dataset(const std::string          name,
-                                       const std::vector<number> &data) const;
+    template void Group::write_dataset<std::vector, number>(
+      const std::string name, const std::vector<number> &data) const;
 
-    template void Group::write_dataset(const std::string         name,
-                                       const FullMatrix<number> &data) const;
+    template void Group::write_dataset<Vector, number>(
+      const std::string name, const Vector<number> &data) const;
+
+    template void Group::write_dataset<FullMatrix, number>(
+      const std::string name, const FullMatrix<number> &data) const;
   }
\ No newline at end of file

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.