/* * (C) Crown Copyright 2021 Met Office * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. */ #pragma once #include "atlas/array.h" //------------------------------------------------------------------------------ namespace atlas { namespace array { namespace helpers { //------------------------------------------------------------------------------ /// Helper to copy from source array to target array. template struct array_copier; //------------------------------------------------------------------------------ // Recursive function to copy array elements across all dimensions. template struct array_copier_impl { template static void apply(const SourceView& sourceArr, TargetView& targetArr, const std::array& shape, DimIndex... idxs) { for (idx_t i = 0; i < shape[Dim]; ++i) { array_copier_impl::apply(sourceArr, targetArr, shape, idxs..., i); } } }; // End of recursion when Dim == Rank template struct array_copier_impl { template static void apply(const SourceView& sourceArr, TargetView& targetArr, const std::array& shape, DimIndex... idxs) { targetArr(idxs...) = sourceArr(idxs...); } }; //------------------------------------------------------------------------------ template struct array_copier { // Copy from source array to target array. static void apply(const ArrayView& sourceArr, ArrayView& targetArr) { array_copier_impl::apply(sourceArr, targetArr, shape(sourceArr, targetArr)); } // Copy from const source array to target array. static void apply(const ArrayView& sourceArr, ArrayView& targetArr) { array_copier_impl::apply(sourceArr, targetArr, shape(sourceArr, targetArr)); } template static void apply(const SourceView& sourceArr, TargetView& targetArr) { array_copier_impl::apply(sourceArr, targetArr, shape(sourceArr, targetArr)); } private: // Make an array shape that is compatible with sourceArr and targetArr. // This is useful for arrays from fields with different sized halos. template static std::array shape(const SourceView& sourceArr, const TargetView& targetArr) { auto arrShape = std::array{}; for (unsigned int Dim = 0; Dim < Rank; ++Dim) { arrShape[Dim] = std::min(sourceArr.shape(Dim), targetArr.shape(Dim)); } return arrShape; } }; //------------------------------------------------------------------------------ } // namespace helpers } // namespace array } // namespace atlas