From 0f646a8933c8388a7e7ee964b9c1fa8f9fd420a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Thu, 30 Sep 2021 12:36:44 +0200
Subject: [PATCH] Displace ArrayUtils functions in Array.hpp

---
 src/mesh/DiamondDualConnectivityBuilder.cpp |   1 -
 src/mesh/GmshReader.cpp                     |   1 -
 src/utils/Array.hpp                         | 190 +++++++++++++++++-
 src/utils/ArrayUtils.hpp                    | 211 --------------------
 src/utils/Messenger.hpp                     |   1 -
 tests/CMakeLists.txt                        |   1 -
 tests/test_Array.cpp                        |  68 +++++++
 tests/test_ArrayUtils.cpp                   |  83 --------
 tests/test_CastArray.cpp                    |   1 -
 9 files changed, 254 insertions(+), 303 deletions(-)
 delete mode 100644 src/utils/ArrayUtils.hpp
 delete mode 100644 tests/test_ArrayUtils.cpp

diff --git a/src/mesh/DiamondDualConnectivityBuilder.cpp b/src/mesh/DiamondDualConnectivityBuilder.cpp
index 31d54be89..40e7a0dfc 100644
--- a/src/mesh/DiamondDualConnectivityBuilder.cpp
+++ b/src/mesh/DiamondDualConnectivityBuilder.cpp
@@ -8,7 +8,6 @@
 #include <mesh/Mesh.hpp>
 #include <mesh/RefId.hpp>
 #include <utils/Array.hpp>
-#include <utils/ArrayUtils.hpp>
 #include <utils/Messenger.hpp>
 
 #include <vector>
diff --git a/src/mesh/GmshReader.cpp b/src/mesh/GmshReader.cpp
index 3a83ef5c3..5ecd752dc 100644
--- a/src/mesh/GmshReader.cpp
+++ b/src/mesh/GmshReader.cpp
@@ -9,7 +9,6 @@
 #include <mesh/ItemValueUtils.hpp>
 #include <mesh/Mesh.hpp>
 #include <mesh/RefItemList.hpp>
-#include <utils/ArrayUtils.hpp>
 #include <utils/Exceptions.hpp>
 
 #include <rang.hpp>
diff --git a/src/utils/Array.hpp b/src/utils/Array.hpp
index f459821bb..f78f54308 100644
--- a/src/utils/Array.hpp
+++ b/src/utils/Array.hpp
@@ -6,6 +6,7 @@
 #include <utils/PugsAssert.hpp>
 #include <utils/PugsMacros.hpp>
 #include <utils/PugsUtils.hpp>
+#include <utils/Types.hpp>
 
 #include <Kokkos_CopyViews.hpp>
 #include <algorithm>
@@ -141,14 +142,14 @@ class [[nodiscard]] Array
 };
 
 template <typename DataType>
-PUGS_INLINE size_t
+[[nodiscard]] PUGS_INLINE size_t
 size(const Array<DataType>& array)
 {
   return array.size();
 }
 
 template <typename DataType, typename... RT>
-PUGS_INLINE Array<DataType>
+[[nodiscard]] PUGS_INLINE Array<DataType>
 encapsulate(const Kokkos::View<DataType*, RT...>& values)
 {
   Array<DataType> array;
@@ -157,7 +158,7 @@ encapsulate(const Kokkos::View<DataType*, RT...>& values)
 }
 
 template <typename DataType>
-PUGS_INLINE Array<DataType>
+[[nodiscard]] PUGS_INLINE Array<DataType>
 subArray(const Array<DataType>& array,
          typename Array<DataType>::index_type begin,
          typename Array<DataType>::index_type size)
@@ -169,7 +170,7 @@ subArray(const Array<DataType>& array,
 
 // map, multimap, unordered_map and stack cannot be copied this way
 template <typename Container>
-PUGS_INLINE Array<std::remove_const_t<typename Container::value_type>>
+[[nodiscard]] PUGS_INLINE Array<std::remove_const_t<typename Container::value_type>>
 convert_to_array(const Container& given_container)
 {
   using DataType = typename Container::value_type;
@@ -180,4 +181,185 @@ convert_to_array(const Container& given_container)
   return array;
 }
 
+template <typename DataType>
+[[nodiscard]] std::remove_const_t<DataType>
+min(const Array<DataType>& array)
+{
+  using ArrayType  = Array<DataType>;
+  using data_type  = std::remove_const_t<typename ArrayType::data_type>;
+  using index_type = typename ArrayType::index_type;
+
+  class ArrayMin
+  {
+   private:
+    const ArrayType m_array;
+
+   public:
+    PUGS_INLINE
+    operator data_type()
+    {
+      data_type reduced_value;
+      parallel_reduce(m_array.size(), *this, reduced_value);
+      return reduced_value;
+    }
+
+    PUGS_INLINE
+    void
+    operator()(const index_type& i, data_type& data) const
+    {
+      if (m_array[i] < data) {
+        data = m_array[i];
+      }
+    }
+
+    PUGS_INLINE
+    void
+    join(volatile data_type& dst, const volatile data_type& src) const
+    {
+      if (src < dst) {
+        dst = src;
+      }
+    }
+
+    PUGS_INLINE
+    void
+    init(data_type& value) const
+    {
+      value = std::numeric_limits<data_type>::max();
+    }
+
+    PUGS_INLINE
+    ArrayMin(const ArrayType& array) : m_array(array)
+    {
+      ;
+    }
+
+    PUGS_INLINE
+    ~ArrayMin() = default;
+  };
+
+  return ArrayMin(array);
+}
+
+template <typename DataType>
+[[nodiscard]] std::remove_const_t<DataType>
+max(const Array<DataType>& array)
+{
+  using ArrayType  = Array<DataType>;
+  using data_type  = std::remove_const_t<typename ArrayType::data_type>;
+  using index_type = typename ArrayType::index_type;
+
+  class ArrayMax
+  {
+   private:
+    const ArrayType m_array;
+
+   public:
+    PUGS_INLINE
+    operator data_type()
+    {
+      data_type reduced_value;
+      parallel_reduce(m_array.size(), *this, reduced_value);
+      return reduced_value;
+    }
+
+    PUGS_INLINE
+    void
+    operator()(const index_type& i, data_type& data) const
+    {
+      if (m_array[i] > data) {
+        data = m_array[i];
+      }
+    }
+
+    PUGS_INLINE
+    void
+    join(volatile data_type& dst, const volatile data_type& src) const
+    {
+      if (src > dst) {
+        dst = src;
+      }
+    }
+
+    PUGS_INLINE
+    void
+    init(data_type& value) const
+    {
+      value = std::numeric_limits<data_type>::min();
+    }
+
+    PUGS_INLINE
+    ArrayMax(const ArrayType& array) : m_array(array)
+    {
+      ;
+    }
+
+    PUGS_INLINE
+    ~ArrayMax() = default;
+  };
+
+  return ArrayMax(array);
+}
+
+template <typename DataType>
+[[nodiscard]] std::remove_const_t<DataType>
+sum(const Array<DataType>& array)
+{
+  using ArrayType  = Array<DataType>;
+  using data_type  = std::remove_const_t<DataType>;
+  using index_type = typename ArrayType::index_type;
+
+  class ArraySum
+  {
+   private:
+    const ArrayType m_array;
+
+   public:
+    PUGS_INLINE
+    operator data_type()
+    {
+      data_type reduced_value;
+      parallel_reduce(m_array.size(), *this, reduced_value);
+      return reduced_value;
+    }
+
+    PUGS_INLINE
+    void
+    operator()(const index_type& i, data_type& data) const
+    {
+      data += m_array[i];
+    }
+
+    PUGS_INLINE
+    void
+    join(volatile data_type& dst, const volatile data_type& src) const
+    {
+      dst += src;
+    }
+
+    PUGS_INLINE
+    void
+    init(data_type& value) const
+    {
+      if constexpr (std::is_arithmetic_v<data_type>) {
+        value = 0;
+      } else {
+        static_assert(is_tiny_vector_v<data_type> or is_tiny_matrix_v<data_type>, "invalid data type");
+        value = zero;
+      }
+    }
+
+    PUGS_INLINE
+    ArraySum(const ArrayType& array) : m_array(array)
+    {
+      ;
+    }
+
+    PUGS_INLINE
+    ~ArraySum() = default;
+  };
+
+  return ArraySum(array);
+}
+
 #endif   // ARRAY_HPP
diff --git a/src/utils/ArrayUtils.hpp b/src/utils/ArrayUtils.hpp
deleted file mode 100644
index 3b3438294..000000000
--- a/src/utils/ArrayUtils.hpp
+++ /dev/null
@@ -1,211 +0,0 @@
-#ifndef ARRAY_UTILS_HPP
-#define ARRAY_UTILS_HPP
-
-#include <utils/PugsMacros.hpp>
-#include <utils/PugsTraits.hpp>
-#include <utils/PugsUtils.hpp>
-
-#include <utils/Types.hpp>
-
-template <typename DataType, template <typename> typename ArrayT>
-std::remove_const_t<DataType>
-min(const ArrayT<DataType>& array)
-{
-  using ArrayType  = ArrayT<DataType>;
-  using data_type  = std::remove_const_t<typename ArrayType::data_type>;
-  using index_type = typename ArrayType::index_type;
-
-  class ArrayMin
-  {
-   private:
-    const ArrayType m_array;
-
-   public:
-    PUGS_INLINE
-    operator data_type()
-    {
-      data_type reduced_value;
-      parallel_reduce(m_array.size(), *this, reduced_value);
-      return reduced_value;
-    }
-
-    PUGS_INLINE
-    void
-    operator()(const index_type& i, data_type& data) const
-    {
-      if (m_array[i] < data) {
-        data = m_array[i];
-      }
-    }
-
-    PUGS_INLINE
-    void
-    join(volatile data_type& dst, const volatile data_type& src) const
-    {
-      if (src < dst) {
-        dst = src;
-      }
-    }
-
-    PUGS_INLINE
-    void
-    init(data_type& value) const
-    {
-      value = std::numeric_limits<data_type>::max();
-    }
-
-    PUGS_INLINE
-    ArrayMin(const ArrayType& array) : m_array(array)
-    {
-      ;
-    }
-
-    PUGS_INLINE
-    ~ArrayMin() = default;
-  };
-
-  return ArrayMin(array);
-}
-
-template <typename DataType, template <typename> typename ArrayT>
-std::remove_const_t<DataType>
-max(const ArrayT<DataType>& array)
-{
-  using ArrayType  = ArrayT<DataType>;
-  using data_type  = std::remove_const_t<typename ArrayType::data_type>;
-  using index_type = typename ArrayType::index_type;
-
-  class ArrayMax
-  {
-   private:
-    const ArrayType m_array;
-
-   public:
-    PUGS_INLINE
-    operator data_type()
-    {
-      data_type reduced_value;
-      parallel_reduce(m_array.size(), *this, reduced_value);
-      return reduced_value;
-    }
-
-    PUGS_INLINE
-    void
-    operator()(const index_type& i, data_type& data) const
-    {
-      if (m_array[i] > data) {
-        data = m_array[i];
-      }
-    }
-
-    PUGS_INLINE
-    void
-    join(volatile data_type& dst, const volatile data_type& src) const
-    {
-      if (src > dst) {
-        dst = src;
-      }
-    }
-
-    PUGS_INLINE
-    void
-    init(data_type& value) const
-    {
-      value = std::numeric_limits<data_type>::min();
-    }
-
-    PUGS_INLINE
-    ArrayMax(const ArrayType& array) : m_array(array)
-    {
-      ;
-    }
-
-    PUGS_INLINE
-    ~ArrayMax() = default;
-  };
-
-  return ArrayMax(array);
-}
-
-template <typename DataType, template <typename> typename ArrayT>
-std::remove_const_t<DataType>
-sum(const ArrayT<DataType>& array)
-{
-  using ArrayType  = ArrayT<DataType>;
-  using data_type  = std::remove_const_t<DataType>;
-  using index_type = typename ArrayType::index_type;
-
-  class ArraySum
-  {
-   private:
-    const ArrayType m_array;
-
-   public:
-    PUGS_INLINE
-    operator data_type()
-    {
-      data_type reduced_value;
-      parallel_reduce(m_array.size(), *this, reduced_value);
-      return reduced_value;
-    }
-
-    PUGS_INLINE
-    void
-    operator()(const index_type& i, data_type& data) const
-    {
-      data += m_array[i];
-    }
-
-    PUGS_INLINE
-    void
-    join(volatile data_type& dst, const volatile data_type& src) const
-    {
-      dst += src;
-    }
-
-    PUGS_INLINE
-    void
-    init(data_type& value) const
-    {
-      if constexpr (std::is_arithmetic_v<data_type>) {
-        value = 0;
-      } else {
-        static_assert(is_tiny_vector_v<data_type> or is_tiny_matrix_v<data_type>, "invalid data type");
-        value = zero;
-      }
-    }
-
-    PUGS_INLINE
-    ArraySum(const ArrayType& array) : m_array(array)
-    {
-      ;
-    }
-
-    PUGS_INLINE
-    ~ArraySum() = default;
-  };
-
-  return ArraySum(array);
-}
-
-template <template <typename... SourceT> typename SourceArray,
-          template <typename... ImageT>
-          typename ImageArray,
-          typename... SourceT,
-          typename... ImageT>
-void
-value_copy(const SourceArray<SourceT...>& source_array, ImageArray<ImageT...>& image_array)
-{
-  using SourceDataType = typename SourceArray<SourceT...>::data_type;
-  using ImageDataType  = typename ImageArray<ImageT...>::data_type;
-
-  static_assert(std::is_same_v<std::remove_const_t<SourceDataType>, ImageDataType>);
-  static_assert(not std::is_const_v<ImageDataType>);
-
-  Assert(source_array.size() == image_array.size());
-
-  parallel_for(
-    source_array.size(), PUGS_LAMBDA(size_t i) { image_array[i] = source_array[i]; });
-}
-
-#endif   // ARRAY_UTILS_HPP
diff --git a/src/utils/Messenger.hpp b/src/utils/Messenger.hpp
index 7e8b30c1a..69b144224 100644
--- a/src/utils/Messenger.hpp
+++ b/src/utils/Messenger.hpp
@@ -5,7 +5,6 @@
 #include <utils/PugsMacros.hpp>
 
 #include <utils/Array.hpp>
-#include <utils/ArrayUtils.hpp>
 #include <utils/CastArray.hpp>
 
 #include <type_traits>
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index e0d6921f0..5cc4fee50 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -11,7 +11,6 @@ add_executable (unit_tests
   test_AffectationToTupleProcessor.cpp
   test_Array.cpp
   test_ArraySubscriptProcessor.cpp
-  test_ArrayUtils.cpp
   test_ASTBuilder.cpp
   test_ASTDotPrinter.cpp
   test_ASTModulesImporter.cpp
diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp
index 5b9889f39..d6382d606 100644
--- a/tests/test_Array.cpp
+++ b/tests/test_Array.cpp
@@ -1,6 +1,8 @@
 #include <catch2/catch_test_macros.hpp>
 #include <catch2/matchers/catch_matchers_all.hpp>
 
+#include <algebra/TinyMatrix.hpp>
+#include <algebra/TinyVector.hpp>
 #include <utils/Array.hpp>
 #include <utils/PugsAssert.hpp>
 #include <utils/Types.hpp>
@@ -251,6 +253,72 @@ TEST_CASE("Array", "[utils]")
     REQUIRE(array_ost.str() == ref_ost.str());
   }
 
+  SECTION("checking for Array reductions")
+  {
+    Array<int> a(10);
+    a[0] = 13;
+    a[1] = 1;
+    a[2] = 8;
+    a[3] = -3;
+    a[4] = 23;
+    a[5] = -1;
+    a[6] = 13;
+    a[7] = 0;
+    a[8] = 12;
+    a[9] = 9;
+
+    SECTION("Min")
+    {
+      REQUIRE(min(a) == -3);
+    }
+
+    SECTION("Max")
+    {
+      REQUIRE(max(a) == 23);
+    }
+
+    SECTION("Sum")
+    {
+      REQUIRE((sum(a) == 75));
+    }
+
+    SECTION("TinyVector Sum")
+    {
+      using N2 = TinyVector<2, int>;
+      Array<N2> b(10);
+      b[0] = {13, 2};
+      b[1] = {1, 3};
+      b[2] = {8, -2};
+      b[3] = {-3, 2};
+      b[4] = {23, 4};
+      b[5] = {-1, -3};
+      b[6] = {13, 17};
+      b[7] = {0, 9};
+      b[8] = {12, 13};
+      b[9] = {9, -17};
+
+      REQUIRE((sum(b) == N2{75, 28}));
+    }
+
+    SECTION("TinyMatrix Sum")
+    {
+      using N22 = TinyMatrix<2, 2, int>;
+      Array<N22> b(10);
+      b[0] = {13, 2, 0, 1};
+      b[1] = {1, 3, 6, 3};
+      b[2] = {8, -2, -1, 21};
+      b[3] = {-3, 2, 5, 12};
+      b[4] = {23, 4, 7, 1};
+      b[5] = {-1, -3, 33, 11};
+      b[6] = {13, 17, 12, 13};
+      b[7] = {0, 9, 1, 14};
+      b[8] = {12, 13, -3, -71};
+      b[9] = {9, -17, 0, 16};
+
+      REQUIRE((sum(b) == N22{75, 28, 60, 21}));
+    }
+  }
+
 #ifndef NDEBUG
 
   SECTION("output with signaling NaN")
diff --git a/tests/test_ArrayUtils.cpp b/tests/test_ArrayUtils.cpp
deleted file mode 100644
index eb976b40d..000000000
--- a/tests/test_ArrayUtils.cpp
+++ /dev/null
@@ -1,83 +0,0 @@
-#include <catch2/catch_test_macros.hpp>
-#include <catch2/matchers/catch_matchers_all.hpp>
-
-#include <utils/Array.hpp>
-#include <utils/ArrayUtils.hpp>
-#include <utils/PugsAssert.hpp>
-
-#include <algebra/TinyMatrix.hpp>
-#include <algebra/TinyVector.hpp>
-
-// clazy:excludeall=non-pod-global-static
-
-// Instantiate to ensure full coverage is performed
-template class Array<int>;
-
-TEST_CASE("ArrayUtils", "[utils]")
-{
-  SECTION("checking for Array reductions")
-  {
-    Array<int> a(10);
-    a[0] = 13;
-    a[1] = 1;
-    a[2] = 8;
-    a[3] = -3;
-    a[4] = 23;
-    a[5] = -1;
-    a[6] = 13;
-    a[7] = 0;
-    a[8] = 12;
-    a[9] = 9;
-
-    SECTION("Min")
-    {
-      REQUIRE((min(a) == -3));
-    }
-
-    SECTION("Max")
-    {
-      REQUIRE((max(a) == 23));
-    }
-
-    SECTION("Sum")
-    {
-      REQUIRE((sum(a) == 75));
-    }
-
-    SECTION("TinyVector Sum")
-    {
-      using N2 = TinyVector<2, int>;
-      Array<N2> b(10);
-      b[0] = {13, 2};
-      b[1] = {1, 3};
-      b[2] = {8, -2};
-      b[3] = {-3, 2};
-      b[4] = {23, 4};
-      b[5] = {-1, -3};
-      b[6] = {13, 17};
-      b[7] = {0, 9};
-      b[8] = {12, 13};
-      b[9] = {9, -17};
-
-      REQUIRE((sum(b) == N2{75, 28}));
-    }
-
-    SECTION("TinyMatrix Sum")
-    {
-      using N22 = TinyMatrix<2, 2, int>;
-      Array<N22> b(10);
-      b[0] = {13, 2, 0, 1};
-      b[1] = {1, 3, 6, 3};
-      b[2] = {8, -2, -1, 21};
-      b[3] = {-3, 2, 5, 12};
-      b[4] = {23, 4, 7, 1};
-      b[5] = {-1, -3, 33, 11};
-      b[6] = {13, 17, 12, 13};
-      b[7] = {0, 9, 1, 14};
-      b[8] = {12, 13, -3, -71};
-      b[9] = {9, -17, 0, 16};
-
-      REQUIRE((sum(b) == N22{75, 28, 60, 21}));
-    }
-  }
-}
diff --git a/tests/test_CastArray.cpp b/tests/test_CastArray.cpp
index d1c9cad2c..0f4a841f1 100644
--- a/tests/test_CastArray.cpp
+++ b/tests/test_CastArray.cpp
@@ -1,7 +1,6 @@
 #include <catch2/catch_test_macros.hpp>
 #include <catch2/matchers/catch_matchers_all.hpp>
 
-#include <utils/ArrayUtils.hpp>
 #include <utils/CastArray.hpp>
 
 // clazy:excludeall=non-pod-global-static
-- 
GitLab