/* * (C) Copyright 2013 ECMWF. * * 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. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ #pragma once #include #include "atlas/array.h" #include "atlas/runtime/Exception.h" //------------------------------------------------------------------------------ namespace atlas { namespace array { namespace helpers { //------------------------------------------------------------------------------ /// Helper to assign a value to an array or arrayview template struct array_assigner; //------------------------------------------------------------------------------ // Recursive function to apply value to every index template struct array_assigner_impl { template static void apply(View& arr, Value value, DimIndex... idxs) { for (idx_t i = 0; i < arr.shape(Dim); ++i) { array_assigner_impl::apply(arr, value, idxs..., i); } } template static void apply(View& arr, Iterator& it, DimIndex... idxs) { for (idx_t i = 0; i < arr.shape(Dim); ++i) { array_assigner_impl::apply(arr, it, idxs..., i); } } }; // End of recursion when Dim == Rank template struct array_assigner_impl { template static void apply(View& arr, Value value, DimIndex... idxs) { arr(idxs...) = value; } template static void apply(View& arr, Iterator& it, DimIndex... idxs) { arr(idxs...) = *it; ++it; } }; //------------------------------------------------------------------------------ template struct array_assigner { template static void apply(Array& arr, Value value) { return apply(make_host_view(arr), value); } static void apply(ArrayView& arr, Value value) { array_assigner_impl::apply(arr, value); // Note: no need to apply variadic pack (idxs...) } template static void apply(ArrayView& arr, const Iterable& iterable) { typename Iterable::const_iterator it = iterable.begin(); array_assigner_impl::apply(arr, it); ATLAS_ASSERT(it = iterable.end()); } template static void apply(IndexView& arr, const Iterable& iterable) { typename Iterable::const_iterator it = iterable.begin(); array_assigner_impl::apply(arr, it); ATLAS_ASSERT(it = iterable.end()); } static void apply(LocalView& arr, Value value) { array_assigner_impl::apply(arr, value); // Note: no need to apply variadic pack (idxs...) } }; //------------------------------------------------------------------------------ } // namespace helpers } // namespace array } // namespace atlas